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 
50  void BeginPhase( int aPhase );
51 
55  void AdvancePhase();
56 
60  void Report( const wxString& aMessage );
61 
66  void SetMaxProgress( int aMaxProgress );
67 
71  void AdvanceProgress();
72 
77  bool KeepRefreshing( bool aWait = false );
78 
84  virtual void SetTitle( const wxString& aTitle ) {}
85 
86 
87  protected:
88 
89  int currentProgress() const;
90 
91  virtual bool updateUI() = 0;
92 
93  wxString m_rptMessage;
94  mutable std::mutex m_mutex;
95  std::atomic_int m_phase;
96  std::atomic_int m_numPhases;
97  std::atomic_int m_progress;
98  std::atomic_int m_maxProgress;
99 };
100 
101 
102 class WX_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxProgressDialog
103 {
104 public:
116  WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle, int aNumPhases,
117  bool aCanAbort = true );
119 
122  virtual void SetTitle( const wxString& aTitle ) override
123  {
124  wxProgressDialog::SetTitle( aTitle );
125  }
126 
127 private:
128 
129  virtual bool updateUI() override;
130 };
131 
132 
133 class GAUGE_PROGRESS_REPORTER : public PROGRESS_REPORTER, public wxGauge
134 {
135 public:
143  GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases );
144 
145 private:
146 
147  bool updateUI() override;
148 };
149 
150 #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.
PROGRESS_REPORTER(int aNumPhases)
std::atomic_int m_numPhases
virtual void SetTitle(const wxString &aTitle) override
change the title displayed on the window caption
int currentProgress() const
virtual bool updateUI()=0
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
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)