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( 1 ),
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::Report( const wxString& aMessage )
54 {
55  std::lock_guard<std::mutex> guard( m_mutex );
56  m_rptMessage = aMessage;
57 }
58 
59 
60 void PROGRESS_REPORTER::SetMaxProgress( int aMaxProgress )
61 {
62  m_maxProgress.store( aMaxProgress );
63 }
64 
65 void PROGRESS_REPORTER::SetCurrentProgress( double aProgress )
66 {
67  m_maxProgress.store( 10000 );
68  m_progress.store( (int) (aProgress * 10000.0) );
69 }
70 
71 
73 {
74  m_progress.fetch_add( 1 );
75 }
76 
77 
79 {
80  double current = ( 1.0 / (double) m_numPhases ) *
81  ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
82 
83  return (int)( current * 1000 );
84 }
85 
86 
88 {
89  if( aWait )
90  {
91  while( m_progress < m_maxProgress && m_maxProgress > 0 )
92  {
93  if( !updateUI() )
94  {
95  m_cancelled.store( true );
96  return false;
97  }
98 
99  wxMilliSleep( 20 );
100  }
101  return true;
102  }
103  else
104  {
105  if( !updateUI() )
106  {
107  m_cancelled.store( true );
108  return false;
109  }
110 
111  return true;
112  }
113 }
114 
115 
116 WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle,
117  int aNumPhases, bool aCanAbort ) :
118  PROGRESS_REPORTER( aNumPhases ),
119  wxProgressDialog( aTitle, wxT( "" ), 1, aParent,
120  // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
121  // quasi-modal dialog
122  wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
123  // up another event loop on completion which
124  // causes all sorts of grief
125  ( aCanAbort ? wxPD_CAN_ABORT : 0 ) |
126  wxPD_ELAPSED_TIME )
127 {
128 }
129 
130 
132 {
133 }
134 
135 
137 {
138  int cur = currentProgress();
139 
140  if( cur < 0 || cur > 1000 )
141  cur = 0;
142 
143  wxString message;
144  {
145  std::lock_guard<std::mutex> guard( m_mutex );
146  message = m_rptMessage;
147  }
148 
149  SetRange( 1000 );
150  return wxProgressDialog::Update( cur, message );
151 }
152 
153 
154 GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
155  PROGRESS_REPORTER( aNumPhases ),
156  wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize,
157  wxGA_HORIZONTAL, wxDefaultValidator, wxGaugeNameStr )
158 {
159 }
160 
161 
163 {
164  int cur = currentProgress();
165 
166  if( cur < 0 || cur > 1000 )
167  cur = 0;
168 
169  wxGauge::SetValue( cur );
170  wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
171 
172  return true; // No cancel button on a wxGauge
173 }
174 
175 
176 
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
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 AdvanceProgress()
Increment the progress bar length (inside the current virtual zone)