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