| 1 |
#include "SpecifierEditCombo.h" |
| 2 |
#include "specpanel/SpecifierPanelFactory.h" |
| 3 |
|
| 4 |
#include <wx/sizer.h> |
| 5 |
#include <wx/choice.h> |
| 6 |
#include "string/convert.h" |
| 7 |
#include "wxutil/ChoiceHelper.h" |
| 8 |
|
| 9 |
namespace objectives |
| 10 |
{ |
| 11 |
|
| 12 |
namespace ce |
| 13 |
{ |
| 14 |
|
| 15 |
// Constructor |
| 16 |
SpecifierEditCombo::SpecifierEditCombo(wxWindow* parent, |
| 17 |
const std::function<void()>& valueChanged, |
| 18 |
const SpecifierTypeSet& set) : |
| 19 |
wxPanel(parent, wxID_ANY), |
| 20 |
_valueChanged(valueChanged) |
| 21 |
{ |
| 22 |
SetSizer(new wxBoxSizer(wxHORIZONTAL)); |
| 23 |
|
| 24 |
// Create the dropdown containing specifier types |
| 25 |
_specifierCombo = new wxChoice(this, wxID_ANY); |
| 26 |
|
| 27 |
for (SpecifierTypeSet::const_iterator i = set.begin(); |
| 28 |
i != set.end(); ++i) |
| 29 |
{ |
| 30 |
_specifierCombo->Append( |
| 31 |
i->getDisplayName(), |
| 32 |
new wxStringClientData(string::to_string(i->getId()))); |
| 33 |
} |
| 34 |
|
| 35 |
_specifierCombo->Connect(wxEVT_CHOICE, |
| 36 |
wxCommandEventHandler(SpecifierEditCombo::_onChange), NULL, this); |
| 37 |
|
| 38 |
GetSizer()->Add(_specifierCombo, 1, wxRIGHT | wxEXPAND, 6); |
| 39 |
} |
| 40 |
|
| 41 |
// Get the selected Specifier |
| 42 |
SpecifierPtr SpecifierEditCombo::getSpecifier() const |
| 43 |
{ |
| 44 |
return SpecifierPtr(new Specifier( |
| 45 |
SpecifierType::getSpecifierType(getSpecName()), |
| 46 |
_specPanel ? _specPanel->getValue() : "" |
| 47 |
)); |
| 48 |
} |
| 49 |
|
| 50 |
// Set the selected Specifier |
| 51 |
void SpecifierEditCombo::setSpecifier(SpecifierPtr spec) |
| 52 |
{ |
| 53 |
// If the SpecifierPtr is null (because the Component object does not have a |
| 54 |
// specifier for this slot), create a default None specifier. |
| 55 |
if (!spec) |
| 56 |
{ |
| 57 |
spec = std::make_shared<Specifier>(); |
| 58 |
} |
| 59 |
|
| 60 |
// I copied and pasted this from the StimResponseEditor, the SelectionFinder |
| 61 |
// could be cleaned up a bit. |
| 62 |
wxutil::ChoiceHelper::SelectItemByStoredId(_specifierCombo, spec->getType().getId()); |
| 63 |
|
| 64 |
// Create the necessary SpecifierPanel, and set it to display the current |
| 65 |
// value |
| 66 |
createSpecifierPanel(spec->getType().getName()); |
| 67 |
|
| 68 |
if (_specPanel) |
| 69 |
{ |
| 70 |
_specPanel->setValue(spec->getValue()); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Get the selected SpecifierType string |
| 75 |
std::string SpecifierEditCombo::getSpecName() const |
| 76 |
{ |
| 77 |
// Get the current selection |
| 78 |
int selectedId = wxutil::ChoiceHelper::GetSelectionId(_specifierCombo); |
| 79 |
|
| 80 |
if (selectedId != -1) |
| 81 |
{ |
| 82 |
SpecifierType specType = SpecifierType::getSpecifierType(selectedId); |
| 83 |
|
| 84 |
return specType.getName(); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
return ""; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
// Create the required SpecifierPanel |
| 93 |
void SpecifierEditCombo::createSpecifierPanel(const std::string& type) |
| 94 |
{ |
| 95 |
// Get a panel from the factory |
| 96 |
_specPanel = SpecifierPanelFactory::create(this, type); |
| 97 |
|
| 98 |
// If the panel is valid, get its widget and pack into the hbox |
| 99 |
if (_specPanel) |
| 100 |
{ |
| 101 |
// Wire up the changed signal, we want to call ComponentEditor::writeToComponent |
| 102 |
// when anything changes |
| 103 |
_specPanel->setChangedCallback(_valueChanged); |
| 104 |
|
| 105 |
GetSizer()->Add(_specPanel->getWidget(), 1, wxEXPAND); |
| 106 |
} |
| 107 |
|
| 108 |
// As first measure, we fire the callback itself, as the specifier type changed |
| 109 |
_valueChanged(); |
| 110 |
|
| 111 |
Layout(); |
| 112 |
} |
| 113 |
|
| 114 |
void SpecifierEditCombo::_onChange(wxCommandEvent& ev) |
| 115 |
{ |
| 116 |
// Change the SpecifierPanel |
| 117 |
createSpecifierPanel(getSpecName()); |
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
} |