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 (C) 2021-2022 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_phase( 0 ),
34 m_numPhases( aNumPhases ),
35 m_progress( 0 ),
36 m_maxProgress( 1000 ),
37 m_cancelled( false ),
38 m_messageChanged( 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
57void PROGRESS_REPORTER_BASE::AdvancePhase( const wxString& aMessage )
58{
60 Report( aMessage );
61}
62
63
64void PROGRESS_REPORTER_BASE::Report( const wxString& aMessage )
65{
66 std::lock_guard<std::mutex> guard( m_mutex );
67
68 m_messageChanged = m_rptMessage != aMessage;
69 m_rptMessage = aMessage;
70}
71
72
74{
75 m_maxProgress.store( aMaxProgress );
76}
77
78
80{
81 m_maxProgress.store( 1000 );
82 m_progress.store( (int) ( aProgress * 1000.0 ) );
83}
84
85
87{
88 m_progress.fetch_add( 1 );
89}
90
91
93{
94 m_numPhases = aNumPhases;
95}
96
97
99{
100 m_numPhases += aNumPhases;
101}
102
103
105{
106 double current = ( 1.0 / (double) m_numPhases ) *
107 ( (double) m_phase + ( (double) m_progress.load() / (double) m_maxProgress ) );
108
109 return (int)( current * 1000 );
110}
111
112
114{
115 if( aWait )
116 {
117 while( m_progress.load() < m_maxProgress && m_maxProgress > 0 )
118 {
119 if( !updateUI() )
120 {
121 m_cancelled.store( true );
122 return false;
123 }
124
125 wxMilliSleep( 33 /* 30 FPS refresh rate */ );
126 }
127
128 return true;
129 }
130 else
131 {
132 if( !updateUI() )
133 {
134 m_cancelled.store( true );
135 return false;
136 }
137
138 return true;
139 }
140}
141
142
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.
PROGRESS_REPORTER_BASE(int aNumPhases)
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.
A progress reporter interface for use in multi-threaded environments.