19#include <wx/fontenum.h>
21#include <wx/stdpaths.h>
23#include <wx/tokenzr.h>
24#include <wx/imaglist.h>
25#include <wx/dcmemory.h>
27#include "../utils/PropertiesData.h"
30static bool ParseVoltageDouble(
const wxString& str,
double& value)
34 if (s.IsEmpty())
return false;
37 if (s.ToDouble(&value))
return true;
41 sDot.Replace(wxT(
","), wxT(
"."));
42 if (sDot.ToCDouble(&value))
return true;
46 sComma.Replace(wxT(
"."), wxT(
","));
47 if (sComma.ToDouble(&value))
return true;
52GeneralPropertiesForm::GeneralPropertiesForm(wxWindow* parent,
PropertiesData* properties)
53 : GeneralPropertiesFormBase(parent)
55 m_properties = properties;
56 auto data = m_properties->GetGeneralPropertiesData();
59 m_choiceLanguage->Clear();
60 m_choiceLanguage->Insert(_(
"English"), 0);
61 m_choiceLanguage->Insert(_(
"Portuguese"), 1);
62 m_choiceTheme->Clear();
63 m_choiceTheme->Insert(_(
"Light"), 0);
64 m_choiceTheme->Insert(_(
"Dark"), 1);
68 m_choicePlotLib->Clear();
69 m_choicePlotLib->Insert(_(
"Chart Director"), 0);
70 m_choicePlotLib->Insert(_(
"wxMathPlot"), 1);
72 switch (data.language) {
73 case wxLANGUAGE_ENGLISH: {
74 m_choiceLanguage->SetSelection(0);
76 case wxLANGUAGE_PORTUGUESE_BRAZILIAN: {
77 m_choiceLanguage->SetSelection(1);
80 m_choiceLanguage->SetSelection(wxNOT_FOUND);
83 switch (data.plotLib) {
84 case PlotLib::wxCHART_DIR: {
85 m_choicePlotLib->SetSelection(0);
87 case PlotLib::wxMATH_PLOT: {
88 m_choicePlotLib->SetSelection(1);
93 m_choiceTheme->SetSelection(0);
96 m_choiceTheme->SetSelection(1);
101 m_filePickerATPFolder->SetPath(data.atpPath.GetFullPath());
102 m_choiceElementToolbar->SetSelection(data.elementsToolbar);
104 wxFont currentFont(data.labelFontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL,
false, data.labelFont);
105 m_fontPickerText->SetSelectedFont(currentFont);
108 m_checkBoxUseBusColours->SetValue(data.useBusVoltageColours);
109 EnableBusColoursControls(data.useBusVoltageColours);
110 m_voltageLevels = data.voltageLevels;
111 if (m_voltageLevels.empty()) {
112 m_voltageLevels = PropertiesData::GetDefaultVoltageLevels();
115 m_listCtrlVoltages->InsertColumn(0, wxEmptyString, wxLIST_FORMAT_LEFT, 110);
116 m_listCtrlVoltages->InsertColumn(1, wxEmptyString, wxLIST_FORMAT_LEFT, 110);
118 PopulateVoltageList();
121 GetSizer()->Fit(
this);
124 SetMinSize(wxSize(480, 380));
129GeneralPropertiesForm::~GeneralPropertiesForm()
132void GeneralPropertiesForm::OnButtonOKClick(wxCommandEvent& event)
134 if (ValidateData()) EndModal(wxID_OK);
137bool GeneralPropertiesForm::ValidateData()
139 auto data = m_properties->GetGeneralPropertiesData();
140 auto checkData = m_properties->GetGeneralPropertiesData();
141 bool needRestart =
false;
142 data.atpPath = wxFileName(m_filePickerATPFolder->GetPath());
143 wxFont slectedFont = m_fontPickerText->GetSelectedFont();
144 data.labelFont = slectedFont.GetFaceName();
145 data.labelFontSize = slectedFont.GetPointSize();
147 switch (m_choiceLanguage->GetSelection()) {
149 data.language = wxLANGUAGE_ENGLISH;
152 data.language = wxLANGUAGE_PORTUGUESE_BRAZILIAN;
155 if (data.language != checkData.language) needRestart =
true;
157 switch (m_choicePlotLib->GetSelection()) {
159 data.plotLib = PlotLib::wxCHART_DIR;
162 data.plotLib = PlotLib::wxMATH_PLOT;
166 switch (m_choiceTheme->GetSelection()) {
168 data.theme = THEME_LIGHT;
169 wxTheApp->SetAppearance(wxApp::Appearance::Light);
172 data.theme = THEME_DARK;
173 wxTheApp->SetAppearance(wxApp::Appearance::Dark);
176 if (data.theme != checkData.theme) needRestart =
true;
178 data.elementsToolbar =
static_cast<ElementsToolbar
>(m_choiceElementToolbar->GetSelection());
180 data.useBusVoltageColours = m_checkBoxUseBusColours->GetValue();
181 data.voltageLevels = m_voltageLevels;
183 if (!PropertiesData::SaveConfigFile(data)) {
184 wxMessageDialog msgDialog(
this,
185 _(
"It was not possible to access the init file.\nThe settings won't be applied."),
186 _(
"Error"), wxOK | wxCENTRE | wxICON_ERROR);
187 msgDialog.ShowModal();
191 wxMessageDialog msgDialog(
this, _(
"The application must be restarted to settings changes be applied."),
192 _(
"Info"), wxOK | wxCENTRE | wxICON_INFORMATION);
193 msgDialog.ShowModal();
195 m_properties->SetGeneralPropertiesData(data);
199void GeneralPropertiesForm::OnThemeSelected(wxCommandEvent& event)
201 switch (event.GetSelection())
204 wxTheApp->SetAppearance(wxApp::Appearance::Light);
207 wxTheApp->SetAppearance(wxApp::Appearance::Dark);
212void GeneralPropertiesForm::PopulateVoltageList()
214 m_listCtrlVoltages->DeleteAllItems();
216 wxImageList* imgList =
new wxImageList(16, 16,
true);
218 for (
size_t i = 0; i < m_voltageLevels.size(); ++i) {
219 const auto& lvl = m_voltageLevels[i];
221 wxBitmap bmp(16, 16);
222 wxMemoryDC memDC(bmp);
223 memDC.SetPen(*wxTRANSPARENT_PEN);
224 memDC.SetBrush(wxBrush(lvl.colour));
225 memDC.DrawRectangle(0, 0, 16, 16);
227 memDC.SetPen(wxPen(wxColour(120, 120, 120), 1));
228 memDC.SetBrush(*wxTRANSPARENT_BRUSH);
229 memDC.DrawRectangle(0, 0, 16, 16);
230 memDC.SelectObject(wxNullBitmap);
232 int imgIdx = imgList->Add(bmp);
234 long itemIndex = m_listCtrlVoltages->InsertItem(
static_cast<long>(i), wxString::Format(wxT(
"%.2f"), lvl.voltage));
235 m_listCtrlVoltages->SetItem(itemIndex, 1, lvl.colour.GetAsString(wxC2S_HTML_SYNTAX), imgIdx);
238 m_listCtrlVoltages->AssignImageList(imgList, wxIMAGE_LIST_SMALL);
241void GeneralPropertiesForm::EnableBusColoursControls(
bool enable)
243 m_listCtrlVoltages->Enable(enable);
244 m_textCtrlVoltage->Enable(enable);
245 m_colourPickerBus->Enable(enable);
246 m_buttonAddVoltage->Enable(enable);
247 m_buttonRemoveVoltage->Enable(enable);
248 m_buttonDefaultVoltages->Enable(enable);
251void GeneralPropertiesForm::OnVoltageItemSelected(wxListEvent& event)
253 long itemIndex =
event.GetIndex();
254 if (itemIndex >= 0 &&
static_cast<size_t>(itemIndex) < m_voltageLevels.size()) {
255 const auto& lvl = m_voltageLevels[itemIndex];
256 m_textCtrlVoltage->SetValue(wxString::Format(wxT(
"%.2f"), lvl.voltage));
257 m_colourPickerBus->SetColour(lvl.colour);
261void GeneralPropertiesForm::OnButtonAddVoltage(wxCommandEvent& event)
263 wxString vStr = m_textCtrlVoltage->GetValue();
266 if (!ParseVoltageDouble(vStr, v) || v <= 0.0) {
267 wxMessageDialog msgDialog(
this, _(
"Please enter a valid voltage value in kV."), _(
"Warning"), wxOK | wxICON_WARNING);
268 msgDialog.ShowModal();
272 wxColour col = m_colourPickerBus->GetColour();
275 for (
auto& lvl : m_voltageLevels) {
276 if (std::abs(lvl.voltage - v) < 0.001) {
284 m_voltageLevels.push_back({ v, col });
288 return a.voltage > b.voltage;
291 PopulateVoltageList();
293 for (
size_t i = 0; i < m_voltageLevels.size(); ++i) {
294 if (std::abs(m_voltageLevels[i].voltage - v) < 0.001) {
295 m_listCtrlVoltages->SetItemState(
static_cast<long>(i), wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED | wxLIST_STATE_FOCUSED);
296 m_listCtrlVoltages->EnsureVisible(
static_cast<long>(i));
302void GeneralPropertiesForm::OnButtonRemoveVoltage(wxCommandEvent& event)
304 long itemIndex = m_listCtrlVoltages->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
305 if (itemIndex >= 0 &&
static_cast<size_t>(itemIndex) < m_voltageLevels.size()) {
306 m_voltageLevels.erase(m_voltageLevels.begin() + itemIndex);
307 PopulateVoltageList();
308 m_textCtrlVoltage->Clear();
311 wxMessageDialog msgDialog(
this, _(
"Select a voltage level from the list to remove."), _(
"Warning"), wxOK | wxICON_WARNING);
312 msgDialog.ShowModal();
316void GeneralPropertiesForm::OnButtonDefaultVoltages(wxCommandEvent& event)
318 m_voltageLevels = PropertiesData::GetDefaultVoltageLevels();
319 PopulateVoltageList();
320 m_textCtrlVoltage->Clear();
323void GeneralPropertiesForm::OnUseBusColoursClick(wxCommandEvent& event)
325 EnableBusColoursControls(event.GetSelection());
General and simulation data manager.