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 {
35 }
36 
37 
39 {
40  m_phase.store( aPhase );
41  m_progress.store( 0 );
42 }
43 
44 
46 {
47  m_phase.fetch_add( 1 );
48  m_progress.store( 0 );
49 }
50 
51 
52 void PROGRESS_REPORTER::Report( const wxString& aMessage )
53 {
54  std::lock_guard<std::mutex> guard( m_mutex );
55  m_rptMessage = aMessage;
56 }
57 
58 
59 void PROGRESS_REPORTER::SetMaxProgress( int aMaxProgress )
60 {
61  m_maxProgress.store( aMaxProgress );
62 }
63 
64 
66 {
67  m_progress.fetch_add( 1 );
68 }
69 
70 
72 {
73  double current = ( 1.0 / (double) m_numPhases ) *
74  ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
75 
76  return (int)( current * 1000 );
77 }
78 
79 
81 {
82  if( aWait )
83  {
84  while( m_progress < m_maxProgress && m_maxProgress > 0 )
85  {
86  if( !updateUI() )
87  return false;
88 
89  wxMilliSleep( 20 );
90  }
91  return true;
92  }
93  else
94  {
95  return updateUI();
96  }
97 }
98 
99 
100 WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle,
101  int aNumPhases, bool aCanAbort ) :
102  PROGRESS_REPORTER( aNumPhases ),
103  wxProgressDialog( aTitle, wxT( "" ), 1, aParent,
104  // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
105  // quasi-modal dialog
106  wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
107  // up another event loop on completion which
108  // causes all sorts of grief
109  ( aCanAbort ? wxPD_CAN_ABORT : 0 ) |
110  wxPD_ELAPSED_TIME )
111 {
112 }
113 
114 
116 {
117  Destroy();
118 }
119 
120 
122 {
123  int cur = currentProgress();
124 
125  if( cur < 0 || cur > 1000 )
126  cur = 0;
127 
128  wxString message;
129  {
130  std::lock_guard<std::mutex> guard( m_mutex );
131  message = m_rptMessage;
132  }
133 
134  SetRange( 1000 );
135  return wxProgressDialog::Update( cur, message );
136 }
137 
138 
139 GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
140  PROGRESS_REPORTER( aNumPhases ),
141  wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize,
142  wxGA_HORIZONTAL, wxDefaultValidator, wxGaugeNameStr )
143 {
144 }
145 
146 
148 {
149  int cur = currentProgress();
150 
151  if( cur < 0 || cur > 1000 )
152  cur = 0;
153 
154  wxGauge::SetValue( cur );
155  wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
156 
157  return true; // No cancel button on a wxGauge
158 }
159 
160 
161 
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
int currentProgress() const
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.
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)