Power System Platform  2026w23a-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
14#include <wx/cmdline.h>
15#include <wx/msgout.h>
16
17#ifndef WX_PRECOMP
18#include <wx/wx.h>
19#endif
20
21#include "MainFrame.h"
23#include "utils/Path.h"
24
69 // Define the MainApp
70class MainApp : public wxApp
71{
72public:
73
74 MainApp() {}
75 virtual ~MainApp() {}
76 bool LoadInitFile(PropertiesData* propertiesData)
77 {
78#ifdef _DEBUG
79 wxSizerFlags::DisableConsistencyChecks();
80#endif
81
82 // Load configuration file, if don't exists create it.
83 wxFileName fn(Paths::GetDocumentsPath() + "/PSP-UFU/config.ini");
84 if (!fn.DirExists()) {
85 fn.Mkdir();
86 }
87
88 wxTextFile file(fn.GetFullPath());
89 auto data = propertiesData->GetGeneralPropertiesData();
90
91 if (!file.Create()) {
92 if (!file.Open()) return false;
93
94 wxString line;
95 for (line = file.GetFirstLine(); !file.Eof(); line = file.GetNextLine()) {
96 wxString tag = "";
97 wxString tagValue = "";
98 bool parseValue = false;
99 for (unsigned int i = 0; i < line.Len(); ++i) {
100 if (line[i] == '=') {
101 parseValue = true;
102 }
103 else {
104 if (parseValue)
105 tagValue += line[i];
106 else
107 tag += line[i];
108 }
109 }
110 // Language
111 if (tag == "lang") {
112 if (tagValue == "pt-br" || tagValue == "pt") {
113 data.language = wxLANGUAGE_PORTUGUESE_BRAZILIAN;
114 }
115 else if (tagValue == "en" || tagValue == "en-us" || tagValue == "en-uk") {
116 data.language = wxLANGUAGE_ENGLISH;
117 }
118 }
119 if (tag == "theme") {
120 if (tagValue == "light") {
121 data.theme = THEME_LIGHT;
122 }
123 else if (tagValue == "dark") {
124 data.theme = THEME_DARK;
125 }
126 }
127 if (tag == "plotlib") {
128 if (tagValue == "chartdir") {
129 data.plotLib = PlotLib::wxCHART_DIR;
130 }
131 else if (tagValue == "mathplot") {
132 data.plotLib = PlotLib::wxMATH_PLOT;
133 }
134 }
135 //if (tag == "useOpenGL") {
136 // if (tagValue == "yes") {
137 // data.useOpenGL = true;
138 // }
139 // else if (tagValue == "no") {
140 // data.useOpenGL = false;
141 // }
142 //}
143 if (tag == "labelfont") {
144 data.labelFont = tagValue;
145 }
146 if (tag == "labelfontsize") {
147 long size = 10;
148 tagValue.ToLong(&size);
149 data.labelFontSize = static_cast<int>(size);
150 }
151 if (tag == "atpfile") {
152 data.atpPath = wxFileName(tagValue);
153 }
154 }
155 file.Close();
156 }
157 else { // Create default init file.
158 if (!file.Open()) return false;
159
160 wxString plotLibProp = "plotlib=chartdir";
161#ifdef __WXGTK__
162 plotLibProp = "plotlib=mathplot";
163#endif
164
165 // Default parameters.
166 file.AddLine("lang=en");
167 file.AddLine(plotLibProp);
168 file.AddLine("theme=light");
169 file.AddLine("labelfont=Arial");
170 file.AddLine("labelfontsize=10");
171 file.AddLine("atpfile=");
172 //file.AddLine("useOpenGL=yes");
173
174 file.Write();
175 file.Close();
176
177 data.language = wxLANGUAGE_ENGLISH;
178 data.theme = THEME_LIGHT;
179 data.plotLib = PlotLib::wxCHART_DIR;
180#ifdef __WXGTK__
181 data.plotLib = PlotLib::wxMATH_PLOT;
182#endif
183 //data.useOpenGL = true;
184 propertiesData->SetGeneralPropertiesData(data);
185 }
186
187 // Check if the atp folder exists, if not create it.
188 wxString atpFolder = Paths::GetDocumentsPath() + "/PSP-UFU/atp";
189 if (!wxDir::Exists(atpFolder)) wxDir::Make(atpFolder);
190 data.atpWorkFolder = atpFolder;
191
192 propertiesData->SetGeneralPropertiesData(data);
193 return true;
194 }
195
196 void LoadCatalogs(wxLocale* locale, PropertiesData* propertiesData)
197 {
198 // Load language catalogs according the propertiesData attribute.
199 if (!locale->Init(propertiesData->GetGeneralPropertiesData().language, wxLOCALE_DONT_LOAD_DEFAULT)) {
200 wxMessageDialog msgDialog(nullptr, _("This language is not supported by the system."), _("Error"),
201 wxOK | wxCENTRE | wxICON_ERROR);
202 msgDialog.ShowModal();
203 }
204
205 //wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
206 wxString langPath = Paths::GetDataPath() + "/lang";
207 locale->AddCatalogLookupPathPrefix(langPath);
208 // pt_BR
209 if (propertiesData->GetGeneralPropertiesData().language == wxLANGUAGE_PORTUGUESE_BRAZILIAN) {
210 if (!locale->AddCatalog(wxT("pt_BR"))) {
211 wxMessageDialog msgDialog(nullptr, _("Fail to load brazilian portuguese language catalog."), _("Error"),
212 wxOK | wxCENTRE | wxICON_ERROR);
213 msgDialog.ShowModal();
214 }
215 }
216 }
217
218 virtual bool OnInit()
219 {
220 // Add image handlers
221 wxImage::AddHandler(new wxPNGHandler);
222 wxImage::AddHandler(new wxJPEGHandler);
223
224 // Load fonts
225#ifdef __WXMSW__
226 //wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
227 wxString fontsPath = Paths::GetDataPath() + "/fonts";
228 bool loadFont = wxFont::AddPrivateFont(fontsPath + "/cmunrm.ttf"); // regular
229 if (loadFont) loadFont = wxFont::AddPrivateFont(fontsPath + "/cmunbx.ttf"); // bold
230 if (!loadFont) {
231 wxMessageDialog msgDialog(nullptr, _("Failed to load local font."), _("Error"),
232 wxOK | wxCENTRE | wxICON_ERROR);
233 msgDialog.ShowModal();
234 }
235 //MSWEnableDarkMode();
236#endif // __WXMSW__
237
238 PropertiesData* propertiesData = new PropertiesData();
239 LoadInitFile(propertiesData);
240
241 if (propertiesData->GetGeneralPropertiesData().theme == THEME_LIGHT)
242 SetAppearance(Appearance::Light);
243 else if (propertiesData->GetGeneralPropertiesData().theme == THEME_DARK)
244 SetAppearance(Appearance::Dark);
245 else
246 SetAppearance(Appearance::System);
247
248 wxLocale* locale = new wxLocale();
249 LoadCatalogs(locale, propertiesData);
250
251 wxString openFilePath = "";
252
253 // Load command line attributes. This is used to directly open saved files (.psp).
254 wxCmdLineParser cmdLineParser(wxApp::argc, wxApp::argv);
255 //cmdLineParser.SetDesc(cmdLineDesc);
256 cmdLineParser.AddParam("", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
257 cmdLineParser.AddSwitch("t", "test", "Run PSP-UFU tests");
258
259 bool test = false;
260 if (cmdLineParser.Parse() == 0) {
261 wxCmdLineArgs args = cmdLineParser.GetArguments();
262 for (auto it = args.begin(), itEnd = args.end(); it != itEnd; ++it) {
263 if (it->GetKind() == wxCMD_LINE_PARAM) { openFilePath = it->GetStrVal(); }
264 else if (it->GetShortName() == "t") {
265 test = true;
266 }
267 }
268 }
269 MainFrame* mainFrame = new MainFrame(nullptr, locale, propertiesData, openFilePath);
270#ifdef __WXMSW__
271 // Set application icon for windows
272 mainFrame->SetIcon(wxICON(aaaaprogicon));
273#endif
274 SetTopWindow(mainFrame);
275 if (test) {
276 GetTopWindow()->Show();
277 exit(mainFrame->RunPSPTest());
278 }
279 return GetTopWindow()->Show();
280 }
281
282 virtual int OnExit()
283 {
284 // clean up
285 return 0;
286 }
287};
288
289DECLARE_APP(MainApp)
290IMPLEMENT_APP(MainApp)
Main frame of the program. This class manage the ribbon menu and the notebook behavior.
Definition MainFrame.h:69
General and simulation data manager.