KiCad PCB EDA Suite
progress_reporter.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2017 CERN
5  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6 *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef __PROGRESS_REPORTER
26 #define __PROGRESS_REPORTER
27 
28 #include <mutex>
29 #include <atomic>
30 
31 #include <wx/progdlg.h>
32 #include <wx/gauge.h>
33 #if wxCHECK_VERSION(3, 1, 0)
34 #include <wx/appprogress.h>
35 #endif
36 
44 {
45 public:
46 
47  PROGRESS_REPORTER( int aNumPhases );
48  PROGRESS_REPORTER( const PROGRESS_REPORTER& ) = delete;
49 
51  {
52  }
53 
57  void SetNumPhases( int aNumPhases );
58  void AddPhases( int aNumPhases );
59 
63  virtual void BeginPhase( int aPhase );
64 
68  virtual void AdvancePhase();
69 
74  virtual void AdvancePhase( const wxString& aMessage );
75 
79  virtual void Report( const wxString& aMessage );
80 
84  virtual void SetCurrentProgress( double aProgress );
85 
90  void SetMaxProgress( int aMaxProgress );
91 
95  void AdvanceProgress();
96 
101  bool KeepRefreshing( bool aWait = false );
102 
108  virtual void SetTitle( const wxString& aTitle ) {}
109 
110  bool IsCancelled() const { return m_cancelled.load(); }
111 
112 protected:
113 
114  int currentProgress() const;
115 
116  virtual bool updateUI() = 0;
117 
118  wxString m_rptMessage;
119  mutable std::mutex m_mutex;
120  std::atomic_int m_phase;
121  std::atomic_int m_numPhases;
122  std::atomic_int m_progress;
123  std::atomic_int m_maxProgress;
124  std::atomic_bool m_cancelled;
125 };
126 
131 class WX_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxProgressDialog
132 {
133 public:
145  WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle, int aNumPhases,
146  bool aCanAbort = true );
148 
151  virtual void SetTitle( const wxString& aTitle ) override
152  {
153  wxProgressDialog::SetTitle( aTitle );
154  }
155 
156 private:
157 #if wxCHECK_VERSION(3, 1, 0)
158  wxAppProgressIndicator m_appProgressIndicator;
159 #endif
160 
161  virtual bool updateUI() override;
162 };
163 
164 
165 class GAUGE_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxGauge
166 {
167 public:
175  GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases );
176 
177 private:
178 
179  bool updateUI() override;
180 };
181 
182 #endif
virtual void AdvancePhase()
Uses the next vailable virtual zone of the dialog progress bar.
virtual void BeginPhase(int aPhase)
initialize the aPhase virtual zone of the dialog progress bar
std::atomic_int m_phase
A progress reporter for use in multi-threaded environments.
std::atomic_int m_progress
virtual void Report(const wxString &aMessage)
Display aMessage in the progress bar dialog.
GAUGE_PROGRESS_REPORTER(wxWindow *aParent, int aNumPhases)
PROGRESS_REPORTER(int aNumPhases)
virtual void SetCurrentProgress(double aProgress)
Set the progress value to aProgress (0..1)
Multi-thread safe progress reporter dialog, intended for use of tasks that paralleize reporting back ...
bool IsCancelled() const
void SetNumPhases(int aNumPhases)
sets the number of phases
std::atomic_int m_numPhases
std::atomic_bool m_cancelled
virtual void SetTitle(const wxString &aTitle) override
change the title displayed on the window caption
virtual bool updateUI()=0
virtual bool updateUI() override
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
WX_PROGRESS_REPORTER(wxWindow *aParent, const wxString &aTitle, int aNumPhases, bool aCanAbort=true)
Ctor: the PROGRESS_REPORTER will stay on top of aParent.
int currentProgress() const
virtual ~PROGRESS_REPORTER()
std::atomic_int m_maxProgress
void SetMaxProgress(int aMaxProgress)
Fix the value thar gives the 100 precent progress bar length (inside the current virtual zone)
virtual void SetTitle(const wxString &aTitle)
change the title displayed on the window caption MUST only be called from the main thread.
void AddPhases(int aNumPhases)
void AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)