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  PROGRESS_REPORTER( aNumPhases ),
138  wxProgressDialog( aTitle, wxT( "" ), 1, aParent,
139  // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
140  // quasi-modal dialog
141  wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
142  // up another event loop on completion which
143  // causes all sorts of grief
144  ( aCanAbort ? wxPD_CAN_ABORT : 0 ) |
145  wxPD_ELAPSED_TIME )
146 #if wxCHECK_VERSION( 3, 1, 0 )
147  ,
148  m_appProgressIndicator( aParent )
149 #endif
150 {
151 #if wxCHECK_VERSION( 3, 1, 0 )
152  // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
153  // with multithreaded setting of those values making a mess
154  // the cop out is just to set the progress to "indeterminate"
155  m_appProgressIndicator.Pulse();
156 #endif
157 }
158 
159 
161 {
162 }
163 
164 
166 {
167  int cur = currentProgress();
168 
169  if( cur < 0 || cur > 1000 )
170  cur = 0;
171 
172  wxString message;
173  {
174  std::lock_guard<std::mutex> guard( m_mutex );
175  message = m_rptMessage;
176  }
177 
178  SetRange( 1000 );
179  return wxProgressDialog::Update( cur, message );
180 }
181 
182 
183 GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
184  PROGRESS_REPORTER( aNumPhases ),
185  wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL,
186  wxDefaultValidator, wxGaugeNameStr )
187 {
188 }
189 
190 
192 {
193  int cur = currentProgress();
194 
195  if( cur < 0 || cur > 1000 )
196  cur = 0;
197 
198  wxGauge::SetValue( cur );
199  wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
200 
201  return true; // No cancel button on a wxGauge
202 }
203 
204 
205 
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)
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.
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
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)