Power System Platform  2026w11a-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 // Default parameters.
161 file.AddLine("lang=en");
162 file.AddLine("plotlib=chartdir");
163 file.AddLine("theme=light");
164 file.AddLine("labelfont=Arial");
165 file.AddLine("labelfontsize=10");
166 file.AddLine("atpfile=");
167 //file.AddLine("useOpenGL=yes");
168
169 file.Write();
170 file.Close();
171
172 data.language = wxLANGUAGE_ENGLISH;
173 data.theme = THEME_LIGHT;
174 data.plotLib = PlotLib::wxCHART_DIR;
175 //data.useOpenGL = true;
176 propertiesData->SetGeneralPropertiesData(data);
177 }
178
179 // Check if the atp folder exists, if not create it.
180 wxString atpFolder = Paths::GetDocumentsPath() + "/PSP-UFU/atp";
181 if (!wxDir::Exists(atpFolder)) wxDir::Make(atpFolder);
182 data.atpWorkFolder = atpFolder;
183
184 propertiesData->SetGeneralPropertiesData(data);
185 return true;
186 }
187
188 void LoadCatalogs(wxLocale* locale, PropertiesData* propertiesData)
189 {
190 // Load language catalogs according the propertiesData attribute.
191 if (!locale->Init(propertiesData->GetGeneralPropertiesData().language, wxLOCALE_DONT_LOAD_DEFAULT)) {
192 wxMessageDialog msgDialog(nullptr, _("This language is not supported by the system."), _("Error"),
193 wxOK | wxCENTRE | wxICON_ERROR);
194 msgDialog.ShowModal();
195 }
196
197 //wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
198 wxString langPath = Paths::GetDataPath() + "/lang";
199 locale->AddCatalogLookupPathPrefix(langPath);
200 // pt_BR
201 if (propertiesData->GetGeneralPropertiesData().language == wxLANGUAGE_PORTUGUESE_BRAZILIAN) {
202 if (!locale->AddCatalog(wxT("pt_BR"))) {
203 wxMessageDialog msgDialog(nullptr, _("Fail to load brazilian portuguese language catalog."), _("Error"),
204 wxOK | wxCENTRE | wxICON_ERROR);
205 msgDialog.ShowModal();
206 }
207 }
208 }
209
210 virtual bool OnInit()
211 {
212 // Add image handlers
213 wxImage::AddHandler(new wxPNGHandler);
214 wxImage::AddHandler(new wxJPEGHandler);
215
216 // Load fonts
217#ifdef __WXMSW__
218 //wxFileName fn(wxStandardPaths::Get().GetExecutablePath());
219 wxString fontsPath = Paths::GetDataPath() + "/fonts";
220 bool loadFont = wxFont::AddPrivateFont(fontsPath + "/cmunrm.ttf"); // regular
221 if (loadFont) loadFont = wxFont::AddPrivateFont(fontsPath + "/cmunbx.ttf"); // bold
222 if (!loadFont) {
223 wxMessageDialog msgDialog(nullptr, _("Failed to load local font."), _("Error"),
224 wxOK | wxCENTRE | wxICON_ERROR);
225 msgDialog.ShowModal();
226 }
227#endif // __WXMSW__
228
229 PropertiesData* propertiesData = new PropertiesData();
230 LoadInitFile(propertiesData);
231
232 wxLocale* locale = new wxLocale();
233 LoadCatalogs(locale, propertiesData);
234
235 wxString openFilePath = "";
236
237 // Load command line attributes. This is used to directly open saved files (.psp).
238 wxCmdLineParser cmdLineParser(wxApp::argc, wxApp::argv);
239 //cmdLineParser.SetDesc(cmdLineDesc);
240 cmdLineParser.AddParam("", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL);
241 cmdLineParser.AddSwitch("t", "test", "Run PSP-UFU tests");
242
243 bool test = false;
244 if (cmdLineParser.Parse() == 0) {
245 wxCmdLineArgs args = cmdLineParser.GetArguments();
246 for (auto it = args.begin(), itEnd = args.end(); it != itEnd; ++it) {
247 if (it->GetKind() == wxCMD_LINE_PARAM) { openFilePath = it->GetStrVal(); }
248 else if (it->GetShortName() == "t") {
249 test = true;
250 }
251 }
252 }
253 MainFrame* mainFrame = new MainFrame(nullptr, locale, propertiesData, openFilePath);
254#ifdef __WXMSW__
255 // Set application icon for windows
256 mainFrame->SetIcon(wxICON(aaaaprogicon));
257#endif
258 SetTopWindow(mainFrame);
259 if (test) {
260 GetTopWindow()->Show();
261 exit(mainFrame->RunPSPTest());
262 }
263 return GetTopWindow()->Show();
264 }
265
266 virtual int OnExit()
267 {
268 // clean up
269 return 0;
270 }
271
272 /*virtual int OnRun()
273 {
274 int exitcode = wxApp::OnRun();
275 // wxTheClipboard->Flush();
276 if(exitcode != 0) return exitcode;
277 }
278
279 virtual void OnInitCmdLine(wxCmdLineParser& parser)
280 {
281 parser.SetDesc(g_cmdLineDesc);
282 // must refuse '/' as parameter starter or cannot use "/path" style paths
283 parser.SetSwitchChars(wxT("-"));
284 }
285
286 virtual bool OnCmdLineParsed(wxCmdLineParser& parser)
287 {
288 //silent_mode = parser.Found(wxT("s"));
289
290 // to get at your unnamed parameters use
291 wxArrayString files;
292 for(unsigned int i = 0; i < parser.GetParamCount(); i++) { files.Add(parser.GetParam(i));
293wxMessageBox(parser.GetParam(i)); }
294
295 // and other command line parameters
296
297 // then do what you need with them.
298
299 return true;
300 }*/
301};
302
303DECLARE_APP(MainApp)
304IMPLEMENT_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.