1 /* 2 Copyright (C) 2010-2014 Kristian Duske 3 4 This file is part of TrenchBroom. 5 6 TrenchBroom is free software: you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 TrenchBroom is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #include "SmartChoiceEditor.h" 21 #include "Assets/AttributeDefinition.h" 22 #include "Model/AttributableNode.h" 23 #include "View/MapDocument.h" 24 #include "View/ViewConstants.h" 25 26 #include <wx/combobox.h> 27 #include <wx/panel.h> 28 #include <wx/sizer.h> 29 #include <wx/stattext.h> 30 31 #include <cassert> 32 33 namespace TrenchBroom { 34 namespace View { SmartChoiceEditor(View::MapDocumentWPtr document)35 SmartChoiceEditor::SmartChoiceEditor(View::MapDocumentWPtr document) : 36 SmartAttributeEditor(document), 37 m_panel(NULL), 38 m_comboBox(NULL) {} 39 OnComboBox(wxCommandEvent & event)40 void SmartChoiceEditor::OnComboBox(wxCommandEvent& event) { 41 if (m_panel->IsBeingDeleted()) return; 42 43 const String valueDescStr = m_comboBox->GetValue().ToStdString(); 44 const String valueStr = valueDescStr.substr(0, valueDescStr.find_first_of(':') - 1); 45 document()->setAttribute(name(), valueStr); 46 } 47 OnTextEnter(wxCommandEvent & event)48 void SmartChoiceEditor::OnTextEnter(wxCommandEvent& event) { 49 if (m_panel->IsBeingDeleted()) return; 50 51 document()->setAttribute(name(), m_comboBox->GetValue().ToStdString()); 52 } 53 doCreateVisual(wxWindow * parent)54 wxWindow* SmartChoiceEditor::doCreateVisual(wxWindow* parent) { 55 assert(m_panel == NULL); 56 assert(m_comboBox == NULL); 57 58 m_panel = new wxPanel(parent); 59 wxStaticText* infoText = new wxStaticText(m_panel, wxID_ANY, "Select a choice option:"); 60 #if defined __APPLE__ 61 infoText->SetFont(*wxSMALL_FONT); 62 #endif 63 m_comboBox = new wxComboBox(m_panel, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, NULL, wxTE_PROCESS_ENTER); 64 m_comboBox->Bind(wxEVT_COMBOBOX, &SmartChoiceEditor::OnComboBox, this); 65 m_comboBox->Bind(wxEVT_TEXT_ENTER, &SmartChoiceEditor::OnTextEnter, this); 66 67 wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL); 68 sizer->AddSpacer(LayoutConstants::WideVMargin); 69 sizer->Add(infoText, 0, wxLEFT | wxRIGHT, LayoutConstants::WideHMargin); 70 sizer->AddSpacer(LayoutConstants::WideVMargin); 71 sizer->Add(m_comboBox, 0, wxEXPAND | wxLEFT | wxRIGHT, LayoutConstants::WideHMargin); 72 sizer->AddStretchSpacer(); 73 74 m_panel->SetSizer(sizer); 75 return m_panel; 76 } 77 doDestroyVisual()78 void SmartChoiceEditor::doDestroyVisual() { 79 assert(m_panel != NULL); 80 assert(m_comboBox != NULL); 81 82 m_panel->Destroy(); 83 m_panel = NULL; 84 m_comboBox= NULL; 85 } 86 doUpdateVisual(const Model::AttributableNodeList & attributables)87 void SmartChoiceEditor::doUpdateVisual(const Model::AttributableNodeList& attributables) { 88 assert(m_panel != NULL); 89 assert(m_comboBox != NULL); 90 91 m_comboBox->Clear(); 92 93 const Assets::AttributeDefinition* attrDef = Model::AttributableNode::selectAttributeDefinition(name(), attributables); 94 if (attrDef == NULL || attrDef->type() != Assets::AttributeDefinition::Type_ChoiceAttribute) { 95 m_comboBox->Disable(); 96 } else { 97 const Assets::ChoiceAttributeDefinition* choiceDef = static_cast<const Assets::ChoiceAttributeDefinition*>(attrDef); 98 const Assets::ChoiceAttributeOption::List& options = choiceDef->options(); 99 100 Assets::ChoiceAttributeOption::List::const_iterator it, end; 101 for (it = options.begin(), end = options.end(); it != end; ++it) { 102 const Assets::ChoiceAttributeOption& option = *it; 103 m_comboBox->Append(option.value() + " : " + option.description()); 104 } 105 106 const Model::AttributeValue value = Model::AttributableNode::selectAttributeValue(name(), attributables); 107 m_comboBox->SetValue(value); 108 } 109 } 110 } 111 } 112