KiCad PCB EDA Suite
progress_reporter.cpp
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 
26 #include <wx/evtloop.h>
27 #include <thread>
28 
30  m_phase( 0 ),
31  m_numPhases( aNumPhases ),
32  m_progress( 0 ),
33  m_maxProgress( 1000 ),
34  m_cancelled( false )
35 {
36 }
37 
38 
40 {
41  m_phase.store( aPhase );
42  m_progress.store( 0 );
43 }
44 
45 
47 {
48  m_phase.fetch_add( 1 );
49  m_progress.store( 0 );
50 }
51 
52 
53 void PROGRESS_REPORTER::AdvancePhase( const wxString& aMessage )
54 {
55  AdvancePhase();
56  Report( aMessage );
57 }
58 
59 
60 void PROGRESS_REPORTER::Report( const wxString& aMessage )
61 {
62  std::lock_guard<std::mutex> guard( m_mutex );
63  m_rptMessage = aMessage;
64 }
65 
66 
67 void PROGRESS_REPORTER::SetMaxProgress( int aMaxProgress )
68 {
69  m_maxProgress.store( aMaxProgress );
70 }
71 
72 void PROGRESS_REPORTER::SetCurrentProgress( double aProgress )
73 {
74  m_maxProgress.store( 1000 );
75  m_progress.store( (int) (aProgress * 1000.0) );
76 }
77 
78 
80 {
81  m_progress.fetch_add( 1 );
82 }
83 
84 
85 void PROGRESS_REPORTER::SetNumPhases( int aNumPhases )
86 {
87  m_numPhases = aNumPhases;
88 }
89 
90 
91 void PROGRESS_REPORTER::AddPhases( int aNumPhases )
92 {
93  m_numPhases += aNumPhases;
94 }
95 
96 
98 {
99  double current = ( 1.0 / (double) m_numPhases ) *
100  ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
101 
102  return (int)( current * 1000 );
103 }
104 
105 
107 {
108  if( aWait )
109  {
110  while( m_progress.load() < m_maxProgress && m_maxProgress > 0 )
111  {
112  if( !updateUI() )
113  {
114  m_cancelled.store( true );
115  return false;
116  }
117 
118  wxMilliSleep( 20 );
119  }
120  return true;
121  }
122  else
123  {
124  if( !updateUI() )
125  {
126  m_cancelled.store( true );
127  return false;
128  }
129 
130  return true;
131  }
132 }
133 
134 
135 WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle,
136  int aNumPhases, bool aCanAbort,
137  bool aReserveSpaceForMessage ) :
138  PROGRESS_REPORTER( aNumPhases ),
139  wxProgressDialog( aTitle, ( aReserveSpaceForMessage ? wxT( " " ) : wxT( "" ) ), 1, aParent,
140  // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
141  // quasi-modal dialog
142  wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
143  // up another event loop on completion which
144  // causes all sorts of grief
145  ( aCanAbort ? wxPD_CAN_ABORT : 0 ) |
146  wxPD_ELAPSED_TIME )
147 #if wxCHECK_VERSION( 3, 1, 0 )
148  ,
149  m_appProgressIndicator( aParent )
150 #endif
151 {
152 #if wxCHECK_VERSION( 3, 1, 0 )
153  // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
154  // with multithreaded setting of those values making a mess
155  // the cop out is just to set the progress to "indeterminate"
156  m_appProgressIndicator.Pulse();
157 #endif
158 }
159 
160 
162 {
163 }
164 
165 
167 {
168  int cur = currentProgress();
169 
170  if( cur < 0 || cur > 1000 )
171  cur = 0;
172 
173  wxString message;
174  {
175  std::lock_guard<std::mutex> guard( m_mutex );
176  message = m_rptMessage;
177  }
178 
179  SetRange( 1000 );
180  return wxProgressDialog::Update( cur, message );
181 }
182 
183 
184 GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
185  PROGRESS_REPORTER( aNumPhases ),
186  wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL,
187  wxDefaultValidator, wxGaugeNameStr )
188 {
189 }
190 
191 
193 {
194  int cur = currentProgress();
195 
196  if( cur < 0 || cur > 1000 )
197  cur = 0;
198 
199  wxGauge::SetValue( cur );
200  wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
201 
202  return true; // No cancel button on a wxGauge
203 }
204 
205 
206 
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
WX_PROGRESS_REPORTER(wxWindow *aParent, const wxString &aTitle, int aNumPhases, bool aCanAbort=true, bool aReserveSpaceForMessage=true)
Ctor: the PROGRESS_REPORTER will stay on top of aParent.
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)
void SetNumPhases(int aNumPhases)
sets the number of phases
std::atomic_int m_numPhases
std::atomic_bool m_cancelled
virtual bool updateUI()=0
virtual bool updateUI() override
bool KeepRefreshing(bool aWait=false)
Update the UI dialog.
int currentProgress() const
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)
void AddPhases(int aNumPhases)
void AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)