Antares Simulator
Power System Simulator
job.h
1 /*
2  * Copyright 2007-2025, RTE (https://www.rte-france.com)
3  * See AUTHORS.txt
4  * SPDX-License-Identifier: MPL-2.0
5  * This file is part of Antares-Simulator,
6  * Adequacy and Performance assessment for interconnected energy networks.
7  *
8  * Antares_Simulator is free software: you can redistribute it and/or modify
9  * it under the terms of the Mozilla Public Licence 2.0 as published by
10  * the Mozilla Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Antares_Simulator is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * Mozilla Public Licence 2.0 for more details.
17  *
18  * You should have received a copy of the Mozilla Public Licence 2.0
19  * along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20  */
21 #ifndef __ANTARES_TOOLBOX_JOB_H__
22 #define __ANTARES_TOOLBOX_JOB_H__
23 
24 #include <antares/logs/logs.h>
25 #include <yuni/core/slist/slist.h>
26 #include <yuni/thread/thread.h>
27 #include <yuni/thread/timer.h>
28 
29 #include <wx/dialog.h>
30 #include <wx/frame.h>
31 #include <wx/animate.h>
32 #include <wx/stattext.h>
33 #include <wx/timer.h>
34 #include <wx/stattext.h>
35 #include <wx/regex.h>
36 #include "../components/progressbar.h"
37 #include "../create.h"
38 #include <ui/common/component/panel.h>
39 
40 #include <mutex>
41 #include <atomic>
42 #include <list>
43 
44 namespace Antares::Toolbox::Jobs
45 {
46 // Forward declarations
47 class JobThread;
48 class TimerElapsedTime;
49 class TimerRemainingTime;
50 class PartList;
51 
52 class Job: public wxDialog, public Yuni::IEventObserver<Job>
53 {
54 public:
56 
57 
60  Job(const wxString& title, const wxString& subTitle, const char* icon);
64  virtual ~Job();
65 
67 
69 
70  bool displayProgression() const
71  {
72  return pDisplayProgression;
73  }
74 
75  void displayProgression(const bool v)
76  {
77  pDisplayProgression = v;
78  }
79 
81 
83 
84  bool canCancel() const
85  {
86  return pCanCancel;
87  }
88 
89  void canCancel(const bool v)
90  {
91  pCanCancel = v;
92  }
93 
95 
97 
98  bool result() const
99  {
100  return pResult;
101  }
102 
103  void result(const bool v)
104  {
105  pResult = v;
106  }
107 
109 
111 
112 
116  bool isRunning() const
117  {
118  return !(!pJobIsRunning);
119  }
120 
124  bool run();
125 
129  void stop();
131 
132 protected:
136  virtual bool executeTask() = 0; // Please override
137 
143  void updateTheProgressValue(double progress);
144 
151  void updateTheMessage(const wxString& msg, bool mustBeInterpreted = true);
152 
160  virtual void onBeforeRunning()
161  {
162  }
163 
171  virtual bool onRunQuery()
172  {
173  return true;
174  }
175 
179  virtual void onCancel()
180  { /* do nothing */
181  }
182 
187 
191  void manageLogLevelMessage(enum LogLevel lvl, const wxString& msg);
192 
193 protected:
197  void onCancel(void*);
198 
199  void evtOnClose(wxCloseEvent& evt);
200 
201  void onLogMessage(int, const std::string& message);
202 
203 protected:
207  wxSizer* createMainPnl(wxWindow* parent);
208 
212  wxSizer* createPnlButtons(wxWindow* parent);
213 
214  wxSizer* createPnlLoading(wxWindow* parent);
215 
219  void displayGauge(const bool visible = true);
220 
221  void displayMessage(const wxString& line);
222 
223  void onUIUpdateProgression(uint value);
224  void onUIUpdateMessage();
225  void onUIUpdateLabelErrors();
226 
227  void onUIUpdateElapsedTime(const Yuni::String& text);
228  void onUIUpdateRemainingTime(const Yuni::String& text);
229 
230  void evtOnInit(wxInitDialogEvent& event);
231 
232  void deleteAllThreads();
233 
234  void recomputeWindowSize();
235 
236  void disableAllComponents();
237 
238 private:
240  const wxString& pTitle;
241  const wxString& pSubTitle;
242  const char* pIcon;
243  bool pDisplayProgression;
244  bool pCanCancel;
245  bool pResult;
246  long pMemoryFootprint;
247 
249  wxSizer* pGaugeSizer;
250  wxSizer* pGaugeParentSizer;
251  wxSizer* pRemainingSizer;
252  Component::ProgressBar* pGauge;
253  wxSizer* pCancelSizer;
255  wxStaticText* pProgrText;
256  wxStaticText* pProgrSubText;
257  wxStaticText* pElapsedTimeText;
258  wxStaticText* pRemainingTimeText;
259  wxStaticText* pReadWriteStats;
261  Yuni::String::ListPtr pWarningList;
262  Yuni::String::ListPtr pErrorList;
263 
264  std::atomic<int> pJobIsRunning;
265  std::atomic<int> pGUICanUpdate;
266 
267  // Event table
268  DECLARE_EVENT_TABLE()
269 
270 private:
272  TimerElapsedTime* pTimerElapsedTime;
274  // (only if a progress is available)
275  TimerRemainingTime* pTimerRemainingTime;
276 
277  mutable Yuni::Thread::IThread::Ptr pThread;
278  wxAnimationCtrl* pAnim;
279  wxStaticText* pLblErrors;
280  wxString pLastNoticeMessage;
281  wxButton* pBtnCancel;
282 
283  wxRegEx pLogRegex;
284  wxString pLogEntryLevelTmp;
285  wxString pLogEntryTmp;
286 
287  bool pCatchLogEvents;
288 
289  wxString pMessage;
290  wxString pSubMessage;
291 
292  std::mutex pMutex;
293  std::mutex pErrorMutex;
294 
295  PartList* pPartList;
296 
297  wxColour pDefaultBGColor;
298  Component::Panel* pPanelButtons;
299  Component::Panel* pPanelHeader;
300 
301  wxWindow* pWndCancelOperation;
302 
304  wxTimer* pTimerMessageUpdater;
305  wxTimer* pTimerReadWriteStats;
306 
307  // Our friends !
308  friend class JobThread;
309  friend class TimerElapsedTime;
310  friend class TimerRemainingTime;
311 
312 }; // class Job
313 
314 } // namespace Antares::Toolbox::Jobs
315 
316 #endif // __ANTARES_TOOLBOX_JOB_H__
A simple panel with a caption.
Definition: progressbar.h:32
Definition: job.cpp:267
Definition: job.h:53
void updateTheMessage(const wxString &msg, bool mustBeInterpreted=true)
Update the message displayed for the user.
virtual void onBeforeRunning()
Event: Before the thread is launched.
Definition: job.h:160
virtual bool executeTask()=0
Execute the real task.
void displayGauge(const bool visible=true)
Display (or not) the gauge.
wxSizer * createPnlButtons(wxWindow *parent)
Create the bottom panel, which contains buttons to cancel and to continue.
void stop()
Stop the job gracefully.
virtual bool onRunQuery()
Event: Ask if the job is allow to run.
Definition: job.h:171
bool isRunning() const
Get if the job is running.
Definition: job.h:116
virtual void onCancel()
Event: The user ask to cancel the operation.
Definition: job.h:179
void stopAllGuiComponents()
Gracefully Stop all Gui components.
wxSizer * createMainPnl(wxWindow *parent)
Create the main panel, which contains all important controls.
Job(const wxString &title, const wxString &subTitle, const char *icon)
Constructor.
void onCancel(void *)
Event: The user asks to cancel the operation.
void manageLogLevelMessage(enum LogLevel lvl, const wxString &msg)
Dispatch a log entry, grabbed from from the standard output.
bool run()
Run the job.
void updateTheProgressValue(double progress)
Update the progress value.
virtual ~Job()
Destructor.
Definition: job.cpp:112