KiCad PCB EDA Suite
progress_reporter_base.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  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * Author: Tomasz Wlostowski <[email protected]>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, you may find one here:
21  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
22  * or you may search the http://www.gnu.org website for the version 2 license,
23  * or you may write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26 
27 #include <wx/evtloop.h>
28 #include <thread>
30 
33  m_msgChanged( false ),
34  m_phase( 0 ),
35  m_numPhases( aNumPhases ),
36  m_progress( 0 ),
37  m_maxProgress( 1000 ),
38  m_cancelled( false )
39 {
40 }
41 
42 
44 {
45  m_phase.store( aPhase );
46  m_progress.store( 0 );
47 }
48 
49 
51 {
52  m_phase.fetch_add( 1 );
53  m_progress.store( 0 );
54 }
55 
56 
57 void PROGRESS_REPORTER_BASE::AdvancePhase( const wxString& aMessage )
58 {
59  AdvancePhase();
60  Report( aMessage );
61 }
62 
63 
64 void PROGRESS_REPORTER_BASE::Report( const wxString& aMessage )
65 {
66  std::lock_guard<std::mutex> guard( m_mutex );
67  m_rptMessage = aMessage;
68  m_msgChanged = true;
69 }
70 
71 
73 {
74  m_maxProgress.store( aMaxProgress );
75 }
76 
77 
79 {
80  m_maxProgress.store( 1000 );
81  m_progress.store( (int) ( aProgress * 1000.0 ) );
82 }
83 
84 
86 {
87  m_progress.fetch_add( 1 );
88 }
89 
90 
92 {
93  m_numPhases = aNumPhases;
94 }
95 
96 
97 void PROGRESS_REPORTER_BASE::AddPhases( int aNumPhases )
98 {
99  m_numPhases += aNumPhases;
100 }
101 
102 
104 {
105  double current = ( 1.0 / (double) m_numPhases ) *
106  ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
107 
108  return (int)( current * 1000 );
109 }
110 
111 
113 {
114  if( aWait )
115  {
116  while( m_progress.load() < m_maxProgress && m_maxProgress > 0 )
117  {
118  if( !updateUI() )
119  {
120  m_cancelled.store( true );
121  return false;
122  }
123 
124  wxMilliSleep( 20 );
125  }
126 
127  return true;
128  }
129  else
130  {
131  if( !updateUI() )
132  {
133  m_cancelled.store( true );
134  return false;
135  }
136 
137  return true;
138  }
139 }
140 
141 
void AdvanceProgress() override
Increment the progress bar length (inside the current virtual zone).
virtual void AdvancePhase() override
Use the next available virtual zone of the dialog progress bar.
A progress reporter interface for use in multi-threaded environments.
virtual bool updateUI()=0
virtual void Report(const wxString &aMessage) override
Display aMessage in the progress bar dialog.
void SetNumPhases(int aNumPhases) override
Set the number of phases.
void SetMaxProgress(int aMaxProgress) override
Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).
virtual void BeginPhase(int aPhase) override
Initialize the aPhase virtual zone of the dialog progress bar.
bool KeepRefreshing(bool aWait=false) override
Update the UI dialog.
PROGRESS_REPORTER_BASE(int aNumPhases)
void AddPhases(int aNumPhases) override
virtual void SetCurrentProgress(double aProgress) override
Set the progress value to aProgress (0..1).