Debian Codegraph @

plugins/dm.objectives/ce/ComponentEditorBase.h

raw
1 #pragma once
2
3 #include "ComponentEditor.h"
4 #include <wx/panel.h>
5 #include <wx/sizer.h>
6 #include <stdexcept>
7 #include <functional>
8
9 namespace objectives
10 {
11
12 namespace ce
13 {
14
15 /**
16 * greebo: Common base class for all component editor implementations.
17 * This base class implements the required getWidget() method, returning a panel.
18 */
19 class ComponentEditorBase :
20 public ComponentEditor
21 {
22 protected:
23 wxPanel* _panel;
24 bool _active; // whether writeToComponent is active
25
26 // Constructors may only be used by subclasses
27 ComponentEditorBase() :
28 _panel(nullptr),
29 _active(false)
30 {}
31
32 ComponentEditorBase(wxWindow* parent) :
33 _panel(new wxPanel(parent, wxID_ANY)),
34 _active(false)
35 {
36 _panel->SetSizer(new wxBoxSizer(wxVERTICAL));
37 }
38
39 public:
40 virtual ~ComponentEditorBase()
41 {
42 // When destroyed, remove the panel from its parent
43 delete _panel;
44 _panel = nullptr;
45 }
46
47 virtual wxWindow* getWidget() override
48 {
49 if (_panel == nullptr)
50 {
51 throw std::runtime_error("Cannot pack a ComponentEditor created by its default constructor!");
52 }
53
54 return _panel;
55 }
56
57 virtual void setActive(bool active) override
58 {
59 _active = active;
60 }
61
62 protected:
63 // Shortcut used by subclasses to acquire a bind to to the onChange() method
64 std::function<void()> getChangeCallback()
65 {
66 return std::bind(&ComponentEditorBase::onChange, this);
67 }
68
69 // When anything changes, just trigger the writeToComponent callback,
70 // the Component will be updated and in turn fires its changed signal.
71 void onChange()
72 {
73 if (_active)
74 {
75 this->writeToComponent();
76 }
77 }
78 };
79
80 }
81
82 }