KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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, see <https://www.gnu.org/licenses/>.
21 */
22
23#include <wx/evtloop.h>
24#include <thread>
26
29 m_phase( 0 ),
30 m_numPhases( aNumPhases ),
31 m_progress( 0 ),
32 m_maxProgress( 1000 ),
33 m_cancelled( false ),
34 m_messageChanged( 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
53void PROGRESS_REPORTER_BASE::AdvancePhase( const wxString& aMessage )
54{
56 Report( aMessage );
57}
58
59
60void PROGRESS_REPORTER_BASE::Report( const wxString& aMessage )
61{
62 std::lock_guard<std::mutex> guard( m_mutex );
63
64 m_messageChanged = m_rptMessage != aMessage;
65 m_rptMessage = aMessage;
66}
67
68
70{
71 m_maxProgress.store( aMaxProgress );
72}
73
74
76{
77 m_maxProgress.store( 1000 );
78 m_progress.store( (int) ( aProgress * 1000.0 ) );
79}
80
81
83{
84 m_progress.fetch_add( 1 );
85}
86
87
89{
90 m_numPhases = aNumPhases;
91}
92
93
95{
96 m_numPhases += aNumPhases;
97}
98
99
101{
102 double current = ( 1.0 / (double) m_numPhases ) *
103 ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
104
105 return (int)( current * 1000 );
106}
107
108
110{
111 if( aWait )
112 {
113 while( m_progress.load() < m_maxProgress && m_maxProgress > 0 )
114 {
115 if( !updateUI() )
116 {
117 m_cancelled.store( true );
118 return false;
119 }
120
121 wxMilliSleep( 33 /* 30 FPS refresh rate */ );
122 }
123
124 return true;
125 }
126 else
127 {
128 if( !updateUI() )
129 {
130 m_cancelled.store( true );
131 return false;
132 }
133
134 return true;
135 }
136}
137
138
virtual bool updateUI()=0
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.
virtual void SetCurrentProgress(double aProgress) override
Set the progress value to aProgress (0..1).
void SetMaxProgress(int aMaxProgress) override
Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).
virtual void Report(const wxString &aMessage) override
Display aMessage in the progress bar dialog.
void AddPhases(int aNumPhases) override
void SetNumPhases(int aNumPhases) override
Set the number of phases.
bool KeepRefreshing(bool aWait=false) override
Update the UI dialog.
virtual void BeginPhase(int aPhase) override
Initialize the aPhase virtual zone of the dialog progress bar.