/
PropertyEditor.cpp
101 lines (80 loc) · 2.29 KB
/
PropertyEditor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "PropertyEditor.h"
#include "ientity.h"
#include "iundo.h"
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/button.h>
namespace ui
{
PropertyEditor::PropertyEditor() :
_mainWidget(nullptr),
_entity(nullptr)
{}
PropertyEditor::PropertyEditor(Entity* entity) :
_mainWidget(nullptr),
_entity(entity)
{}
PropertyEditor::~PropertyEditor()
{
// Destroy the widget
if (_mainWidget != nullptr)
{
_mainWidget->Destroy();
_mainWidget = nullptr;
}
}
void PropertyEditor::setMainWidget(wxPanel* widget)
{
_mainWidget = widget;
// Get notified upon main widget destruction, we need
// to forget about our reference to avoid double deletions
_mainWidget->Bind(wxEVT_DESTROY, [&] (wxWindowDestroyEvent&)
{
_mainWidget = nullptr;
});
}
wxPanel* PropertyEditor::getWidget()
{
assert(_mainWidget); // should be set by the subclass at this point
return _mainWidget;
}
void PropertyEditor::setEntity(Entity* entity)
{
if (entity == nullptr) throw std::logic_error("No nullptrs allowed as entity argument");
if (_entity != entity)
{
_entity = entity;
// Let any subclasses update themselves now that the entity got changed
updateFromEntity();
}
}
std::string PropertyEditor::getKeyValue(const std::string& key)
{
return _entity != nullptr ? _entity->getKeyValue(key) : std::string();
}
void PropertyEditor::setKeyValue(const std::string& key, const std::string& value)
{
if (_entity == nullptr) return;
UndoableCommand cmd("setProperty");
_entity->setKeyValue(key, value);
}
void PropertyEditor::constructBrowseButtonPanel(wxWindow* parent, const std::string& label,
const wxBitmap& bitmap)
{
// Construct the main widget (will be managed by the base class)
wxPanel* mainVBox = new wxPanel(parent, wxID_ANY);
mainVBox->SetSizer(new wxBoxSizer(wxHORIZONTAL));
// Register the main widget in the base class
setMainWidget(mainVBox);
// Button with image
wxButton* button = new wxButton(mainVBox, wxID_ANY, label);
button->SetBitmap(bitmap);
button->Bind(wxEVT_BUTTON, &PropertyEditor::_onBrowseButtonClick, this);
mainVBox->GetSizer()->Add(button, 0, wxALIGN_CENTER_VERTICAL);
}
void PropertyEditor::_onBrowseButtonClick(wxCommandEvent& ev)
{
// Redirect the event to the method overridden by subclasses
onBrowseButtonClick();
}
} // namespace ui