Power System Platform  2026w23a-beta
Loading...
Searching...
No Matches
GeneralPropertiesForm.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
19#include <wx/fontenum.h>
20#include <wx/font.h>
21#include <wx/stdpaths.h>
22#include <wx/app.h>
23#include "../utils/PropertiesData.h"
24
25
26GeneralPropertiesForm::GeneralPropertiesForm(wxWindow* parent, PropertiesData* properties)
27 : GeneralPropertiesFormBase(parent)
28{
29 m_properties = properties;
30 auto data = m_properties->GetGeneralPropertiesData();
31
32 // Clear the choices and rebuild to set the correct translations.
33 m_choiceLanguage->Clear();
34 m_choiceLanguage->Insert(_("English"), 0);
35 m_choiceLanguage->Insert(_("Portuguese"), 1);
36 m_choiceTheme->Clear();
37 m_choiceTheme->Insert(_("Light"), 0);
38 m_choiceTheme->Insert(_("Dark"), 1);
39 //m_choiceRender->Clear();
40 //m_choiceRender->Insert(_("OpenGL"), 0);
41 //m_choiceRender->Insert(_("Device Context"), 0);
42 m_choicePlotLib->Clear();
43 m_choicePlotLib->Insert(_("Chart Director"), 0);
44 m_choicePlotLib->Insert(_("wxMathPlot"), 1);
45
46 switch (data.language) {
47 case wxLANGUAGE_ENGLISH: {
48 m_choiceLanguage->SetSelection(0);
49 } break;
50 case wxLANGUAGE_PORTUGUESE_BRAZILIAN: {
51 m_choiceLanguage->SetSelection(1);
52 } break;
53 default: {
54 m_choiceLanguage->SetSelection(wxNOT_FOUND);
55 } break;
56 }
57 switch (data.plotLib) {
58 case PlotLib::wxCHART_DIR: {
59 m_choicePlotLib->SetSelection(0);
60 } break;
61 case PlotLib::wxMATH_PLOT: {
62 m_choicePlotLib->SetSelection(1);
63 } break;
64 }
65 switch (data.theme) {
66 case THEME_LIGHT: {
67 m_choiceTheme->SetSelection(0);
68 } break;
69 case THEME_DARK: {
70 m_choiceTheme->SetSelection(1);
71 } break;
72 }
73 //if (data.useOpenGL) m_choiceRender->SetSelection(0);
74 //else m_choiceRender->SetSelection(1);
75 m_filePickerATPFolder->SetPath(data.atpPath.GetFullPath());
76
77
78 wxFont currentFont(data.labelFontSize, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL, false, data.labelFont);
79 m_fontPickerText->SetSelectedFont(currentFont);
80}
81
82GeneralPropertiesForm::~GeneralPropertiesForm() {}
83void GeneralPropertiesForm::OnButtonOKClick(wxCommandEvent& event)
84{
85 if (ValidateData()) EndModal(wxID_OK);
86}
87
88bool GeneralPropertiesForm::ValidateData()
89{
90 auto data = m_properties->GetGeneralPropertiesData();
91 auto checkData = m_properties->GetGeneralPropertiesData();
92 bool needRestart = false;
93 data.atpPath = wxFileName(m_filePickerATPFolder->GetPath());
94 wxFont slectedFont = m_fontPickerText->GetSelectedFont();
95 data.labelFont = slectedFont.GetFaceName();
96 data.labelFontSize = slectedFont.GetPointSize();
97
98 //wxTextFile file("config.ini");
99 wxFileName fn(wxStandardPaths::Get().GetDocumentsDir() + wxFileName::GetPathSeparator() + "PSP-UFU" + wxFileName::GetPathSeparator() + "config.ini");
100 wxTextFile file(fn.GetFullPath());
101 if (!file.Create()) {
102 if (!file.Open()) {
103 // Fail to access the file.
104 wxMessageDialog msgDialog(this,
105 _("It was not possible to access the init file.\nThe settings won't be applied."),
106 _("Error"), wxOK | wxCENTRE | wxICON_ERROR);
107 msgDialog.ShowModal();
108 }
109 file.Clear();
110 }
111
112 wxString line = "lang=";
113 switch (m_choiceLanguage->GetSelection()) {
114 case 0: {
115 line += "en";
116 data.language = wxLANGUAGE_ENGLISH;
117 } break;
118 case 1: {
119 line += "pt-br";
120 data.language = wxLANGUAGE_PORTUGUESE_BRAZILIAN;
121 } break;
122 }
123 file.AddLine(line);
124 if (data.language != checkData.language) needRestart = true;
125
126 line = "plotlib=";
127 switch (m_choicePlotLib->GetSelection()) {
128 case 0: {
129 line += "chartdir";
130 data.plotLib = PlotLib::wxCHART_DIR;
131 } break;
132 case 1: {
133 line += "mathplot";
134 data.plotLib = PlotLib::wxMATH_PLOT;
135 } break;
136 }
137 file.AddLine(line);
138 //if (data.plotLib != checkData.plotLib) hasChanges = true;
139
140 line = "theme=";
141 switch (m_choiceTheme->GetSelection()) {
142 case 0: {
143 line += "light";
144 data.theme = THEME_LIGHT;
145 wxTheApp->SetAppearance(wxApp::Appearance::Light);
146 } break;
147 case 1: {
148 line += "dark";
149 data.theme = THEME_DARK;
150 wxTheApp->SetAppearance(wxApp::Appearance::Dark);
151 } break;
152 }
153 file.AddLine(line);
154 if (data.theme != checkData.theme) needRestart = true;
155
156 line = "labelfont=";
157 line += data.labelFont;
158 file.AddLine(line);
159
160 line = "labelfontsize=";
161 line += wxString::Format("%d", data.labelFontSize);
162 file.AddLine(line);
163
164
165 line = "atpfile=";
166 line += data.atpPath.GetFullPath();
167 file.AddLine(line);
168
169 file.Write();
170 file.Close();
171
172 if (needRestart) {
173 wxMessageDialog msgDialog(this, _("The application must be restarted to settings changes be applied."),
174 _("Info"), wxOK | wxCENTRE | wxICON_INFORMATION);
175 msgDialog.ShowModal();
176 }
177 m_properties->SetGeneralPropertiesData(data);
178 return true;
179}
180
181void GeneralPropertiesForm::OnThemeSelected(wxCommandEvent& event)
182{
183 switch (event.GetSelection())
184 {
185 case 0:
186 wxTheApp->SetAppearance(wxApp::Appearance::Light);
187 break;
188 case 1:
189 wxTheApp->SetAppearance(wxApp::Appearance::Dark);
190 break;
191 }
192}
General and simulation data manager.