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 
70  void SetMaxProgress( int aMaxProgress );
71 
75  void AdvanceProgress();
76 
81  bool KeepRefreshing( bool aWait = false );
82 
88  virtual void SetTitle( const wxString& aTitle ) {}
89 
90 
91  protected:
92 
93  int currentProgress() const;
94 
95  virtual bool updateUI() = 0;
96 
97  wxString m_rptMessage;
98  mutable std::mutex m_mutex;
99  std::atomic_int m_phase;
100  std::atomic_int m_numPhases;
101  std::atomic_int m_progress;
102  std::atomic_int m_maxProgress;
103 };
104 
105 
106 class WX_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxProgressDialog
107 {
108 public:
120  WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle, int aNumPhases,
121  bool aCanAbort = true );
123 
126  virtual void SetTitle( const wxString& aTitle ) override
127  {
128  wxProgressDialog::SetTitle( aTitle );
129  }
130 
131 private:
132 
133  virtual bool updateUI() override;
134 };
135 
136 
137 class GAUGE_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxGauge
138 {
139 public:
147  GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases );
148 
149 private:
150 
151  bool updateUI() override;
152 };
153 
154 #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)
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)