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 
41 {
42  public:
43 
44  PROGRESS_REPORTER( int aNumPhases );
45  PROGRESS_REPORTER( const PROGRESS_REPORTER& ) = delete;
46 
48  {
49  }
50 
54  void BeginPhase( int aPhase );
55 
59  void AdvancePhase();
60 
64  void Report( const wxString& aMessage );
65 
69  void SetCurrentProgress( double aProgress );
70 
75  void SetMaxProgress( int aMaxProgress );
76 
80  void AdvanceProgress();
81 
86  bool KeepRefreshing( bool aWait = false );
87 
93  virtual void SetTitle( const wxString& aTitle ) {}
94 
95 
96  protected:
97 
98  int currentProgress() const;
99 
100  virtual bool updateUI() = 0;
101 
102  wxString m_rptMessage;
103  mutable std::mutex m_mutex;
104  std::atomic_int m_phase;
105  std::atomic_int m_numPhases;
106  std::atomic_int m_progress;
107  std::atomic_int m_maxProgress;
108 };
109 
110 
111 class WX_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxProgressDialog
112 {
113 public:
125  WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle, int aNumPhases,
126  bool aCanAbort = true );
128 
131  virtual void SetTitle( const wxString& aTitle ) override
132  {
133  wxProgressDialog::SetTitle( aTitle );
134  }
135 
136 private:
137 
138  virtual bool updateUI() override;
139 };
140 
141 
142 class GAUGE_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxGauge
143 {
144 public:
152  GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases );
153 
154 private:
155 
156  bool updateUI() override;
157 };
158 
159 #endif
void AdvancePhase()
Uses the next vailable virtual zone of the dialog progress bar.
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
void Report(const wxString &aMessage)
Display aMessage in the progress bar dialog.
GAUGE_PROGRESS_REPORTER(wxWindow *aParent, int aNumPhases)
PROGRESS_REPORTER(int aNumPhases)
void SetCurrentProgress(double aProgress)
Set the progress value to aProgress (0..1)
std::atomic_int m_numPhases
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 AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)