Power System Platform  2026w34a-beta
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#ifdef __INTELLISENSE__
2#pragma diag_suppress 102
3#endif
4
5#include <wx/app.h>
6#include <wx/cmdline.h>
7#include <wx/event.h>
8#include <wx/image.h>
9#include <wx/msgdlg.h>
10#include <wx/stdpaths.h>
11#include <wx/dir.h>
12#include <wx/textfile.h>
13#include <wx/tokenzr.h>
14
15#include <wx/cmdline.h>
16#include <wx/msgout.h>
17
18#ifndef WX_PRECOMP
19#include <wx/wx.h>
20#endif
21
22#include "MainFrame.h"
24#include "utils/Path.h"
25
71 // Define the MainApp
72class MainApp : public wxApp
73{
74public:
75
76 MainApp() {}
77 virtual ~MainApp() {}
78 bool LoadInitFile(PropertiesData* propertiesData)
79 {
80#ifdef _DEBUG
81 wxSizerFlags::DisableConsistencyChecks();
82#endif
83
84 // Load configuration file, if don't exists create it.
85 wxFileName fn(Paths::GetDocumentsPath() + "/PSP-UFU/config.ini");
86 if (!fn.DirExists()) {
87 fn.Mkdir();
88 }
89
90 wxTextFile file(fn.GetFullPath());
91 auto data = propertiesData->GetGeneralPropertiesData();
92
93 if (!file.Create()) {
94 if (!file.Open()) return false;
95
96 wxString line;
97 for (line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine()) {
98 wxString tag = "";
99 wxString tagValue = "";
100 bool parseValue = false;
101 for (unsigned int i = 0; i < line.Len(); ++i) {
102 if (line[i] == '=') {
103 parseValue = true;
104 }
105 else {
106 if (parseValue)
107 tagValue += line[i];
108 else
109 tag += line[i];
110 }
111 }
112 // Language
113 if (tag == "lang") {
114 if (tagValue == "pt-br" || tagValue == "pt") {
115 data.language = wxLANGUAGE_PORTUGUESE_BRAZILIAN;
116 }
117 else if (tagValue == "en" || tagValue == "en-us" || tagValue == "en-uk") {
118 data.language = wxLANGUAGE_ENGLISH;
119 }
120 }
121 if (tag == "theme") {
122 if (tagValue == "light") {
123 data.theme = THEME_LIGHT;
124 }
125 else if (tagValue == "dark") {
126 data.theme = THEME_DARK;
127 }
128 }
129 if (tag == "plotlib") {
130 if (tagValue == "chartdir") {
131 data.plotLib = PlotLib::wxCHART_DIR;
132 }
133 else if (tagValue == "mathplot") {
134 data.plotLib = PlotLib::wxMATH_PLOT;
135 }
136 }
137 //if (tag == "useOpenGL") {
138 // if (tagValue == "yes") {
139 // data.useOpenGL = true;
140 // }
141 // else if (tagValue == "no") {
142 // data.useOpenGL = false;
143 // }
144 //}
145 if (tag == "labelfont") {
146 data.labelFont = tagValue;
147 }
148 if (tag == "labelfontsize") {
149 long size = 10;
150 tagValue.ToLong(&size);
151 data.labelFontSize = static_cast<int>(size);
152 }
153 if (tag == "atpfile") {
154 data.atpPath = wxFileName(tagValue);
155 }
156 if (tag == "elementstoolbar") {
157 if (tagValue == "none") {
158 data.elementsToolbar = NONE;
159 }
160 else if (tagValue == "vertical") {
161 data.elementsToolbar = VERTICAL;
162 }
163 else if (tagValue == "horizontal") {
164 data.elementsToolbar = HORIZONTAL;
165 }
166 else
167 {
168 data.elementsToolbar = VERTICAL;
169 }
170 }
171 if (tag == "usevoltagebuscolours") {
172 if (tagValue == "yes" || tagValue == "true" || tagValue == "1") {
173 data.useBusVoltageColours = true;
174 }
175 else if (tagValue == "no" || tagValue == "false" || tagValue == "0") {
176 data.useBusVoltageColours = false;
177 }
178 }
179 if (tag == "voltage_levels") {
180 data.voltageLevels.clear();
181 wxStringTokenizer tkz(tagValue, ";");
182 while (tkz.HasMoreTokens()) {
183 wxString token = tkz.GetNextToken().Trim().Trim(false);
184 if (token.IsEmpty()) continue;
185 wxString vStr = token.BeforeFirst(':');
186 wxString cStr = token.AfterFirst(':');
187 double v = 0.0;
188 if (vStr.ToCDouble(&v) || vStr.ToDouble(&v)) {
189 wxColour col(cStr);
190 if (!col.IsOk()) col = *wxBLACK;
191 data.voltageLevels.push_back({ v, col });
192 }
193 }
194 }
195 }
196 file.Close();
197 }
198 else { // Create default init file.
199 if (!file.Open()) return false;
200
201 wxString plotLibProp = "plotlib=chartdir";
202#ifdef __WXGTK__
203 plotLibProp = "plotlib=mathplot";
204#endif
205
206 // Default parameters.
207 file.AddLine("lang=en");
208 file.AddLine(plotLibProp);
209 file.AddLine("theme=light");
210 file.AddLine("labelfont=Arial");
211 file.AddLine("labelfontsize=10");
212 file.AddLine("atpfile=");
213 file.AddLine("elementstoolbar=no");
214 file.AddLine("usevoltagebuscolours=no");
215 //file.AddLine("useOpenGL=yes");
216
217 wxString vlProp = "voltage_levels=";
218 for (size_t i = 0; i < data.voltageLevels.size(); ++i) {
219 if (i > 0) vlProp += ";";
220 vlProp += wxString::Format("%s:%s", wxString::FromCDouble(data.voltageLevels[i].voltage), data.voltageLevels[i].colour.GetAsString(wxC2S_HTML_SYNTAX));
221 }
222 file.AddLine(vlProp);
223
224 file.Write();
225 file.Close();
226
227 data.language = wxLANGUAGE_ENGLISH;
228 data.theme = THEME_LIGHT;
229 data.plotLib = PlotLib::wxCHART_DIR;
230#ifdef __WXGTK__
231 data.plotLib = PlotLib::wxMATH_PLOT;
232#endif
233 data.elementsToolbar = VERTICAL;
234 data.useBusVoltageColours = true;
235 //data.useOpenGL = true;
236 propertiesData->SetGeneralPropertiesData(data);
237 }
238
239 // Check if the atp folder exists, if not create it.
240 wxString atpFolder = Paths::GetDocumentsPath() + "/PSP-UFU/atp";
241 if (!wxDir::Exists(atpFolder)) wxDir::Make(atpFolder);
242 data.atpWorkFolder = atpFolder;
243
244 propertiesData->SetGeneralPropertiesData(data);
245 return true;
246 }
247
248 void LoadCatalogs(wxLocale* locale, PropertiesData* propertiesData)
249 {
250 // Load language catalogs according the propertiesData attribute.
251 if (!locale->Init(propertiesData->GetGeneralPropertiesData().language, wxLOCALE_DONT_LOAD_DEFAULT)) {
252 wxMessageDialog msgDialog(nullptr, _("This language is not supported by the system."), _("Error"),
253 wxOK | wxCENTRE | wxICON_ERROR);
254 msgDialog.ShowModal();
255 }
256
257 //wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
258 wxString langPath = Paths::GetDataPath() + "/lang";
259 locale->AddCatalogLookupPathPrefix(langPath);
260 // pt_BR
261 if (propertiesData->GetGeneralPropertiesData().language == wxLANGUAGE_PORTUGUESE_BRAZILIAN) {
262 if (!locale->AddCatalog(wxT("pt_BR"))) {
263 wxMessageDialog msgDialog(nullptr, _("Fail to load brazilian portuguese language catalog."), _("Error"),
264 wxOK | wxCENTRE | wxICON_ERROR);
265 msgDialog.ShowModal();
266 }
267 }
268 }
269
270 virtual bool OnInit()
271 {
272 // Add image handlers
273 wxImage::AddHandler(new wxPNGHandler);
274 wxImage::AddHandler(new wxJPEGHandler);
275
276 // Load fonts
277#ifdef __WXMSW__
278 //wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
279 wxString fontsPath = Paths::GetDataPath() + "/fonts";
280 bool loadFont = wxFont::AddPrivateFont(fontsPath + "/cmunrm.ttf"); // regular
281 if (loadFont) loadFont = wxFont::AddPrivateFont(fontsPath + "/cmunbx.ttf"); // bold
282 if (!loadFont) {
283 wxMessageDialog msgDialog(nullptr, _("Failed to load local font."), _("Error"),
284 wxOK | wxCENTRE | wxICON_ERROR);
285 msgDialog.ShowModal();
286 }
287 //MSWEnableDarkMode();
288#endif // __WXMSW__
289
290 PropertiesData* propertiesData = new PropertiesData();
291 LoadInitFile(propertiesData);
292
293 if (propertiesData->GetGeneralPropertiesData().theme == THEME_LIGHT)
294 SetAppearance(Appearance::Light);
295 else if (propertiesData->GetGeneralPropertiesData().theme == THEME_DARK)
296 SetAppearance(Appearance::Dark);
297 else
298 SetAppearance(Appearance::System);
299
300 wxLocale* locale = new wxLocale();
301 LoadCatalogs(locale, propertiesData);
302
303 wxString openFilePath = "";
304
305 // Load command line attributes. This is used to directly open saved files (.psp).
306 wxCmdLineParser cmdLineParser(wxApp::argc, wxApp::argv);
307 //cmdLineParser.SetDesc(cmdLineDesc);
308 cmdLineParser.AddParam("", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
309 cmdLineParser.AddSwitch("t", "test", "Run PSP-UFU tests");
310
311 bool test = false;
312 if (cmdLineParser.Parse() == 0) {
313 wxCmdLineArgs args = cmdLineParser.GetArguments();
314 for (auto it = args.begin(), itEnd = args.end(); it != itEnd; ++it) {
315 if (it->GetKind() == wxCMD_LINE_PARAM) { openFilePath = it->GetStrVal(); }
316 else if (it->GetShortName() == "t") {
317 test = true;
318 }
319 }
320 }
321 MainFrame* mainFrame = new MainFrame(nullptr, locale, propertiesData, openFilePath);
322#ifdef __WXMSW__
323 // Set application icon for windows
324 mainFrame->SetIcon(wxICON(aaaaprogicon));
325#endif
326 SetTopWindow(mainFrame);
327 if (test) {
328 GetTopWindow()->Show();
329 exit(mainFrame->RunPSPTest());
330 }
331 return GetTopWindow()->Show();
332 }
333
334 virtual int OnExit()
335 {
336 // clean up
337 return 0;
338 }
339};
340
341DECLARE_APP(MainApp)
342IMPLEMENT_APP(MainApp)
Main frame of the program. This class manage the ribbon menu and the notebook behavior.
Definition MainFrame.h:71
General and simulation data manager.