KiCad PCB EDA Suite
Loading...
Searching...
No Matches
wx_progress_reporters.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 <thread>
26
27
28static wxString getPlaceholderText()
29{
30 const int c_numChars = 80;
31
32 return wxString( L'\u2002', c_numChars ); // EN SPACE
33}
34
35
36WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle, int aNumPhases, int aCanAbort,
37 bool aReserveSpaceForMessage ) :
38 PROGRESS_REPORTER_BASE( aNumPhases ),
39 WX_PROGRESS_REPORTER_BASE( aTitle, aReserveSpaceForMessage ? getPlaceholderText() : wxString( wxEmptyString ),
40 1, aParent,
41 // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
42 // quasi-modal dialog
43 wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
44 // up another event loop on completion which
45 // causes all sorts of grief
46 aCanAbort | wxPD_ELAPSED_TIME ),
47 m_appProgressIndicator( aParent ),
48 m_reservedSize( aReserveSpaceForMessage ),
49 m_updateThrottle( std::chrono::milliseconds( 100 ) ),
50 m_lastUpdateResult( true ),
51 m_indeterminate( false )
52{
53 // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
54 // with multithreaded setting of those values making a mess
55 // the cop out is just to set the progress to "indeterminate"
57}
58
59
63
64
66{
67 if( !shouldRefresh( m_progress.load(), m_maxProgress.load(), m_phase.load(), m_numPhases.load(),
69 {
70 return !m_cancelled.load() && m_lastUpdateResult.load();
71 }
72
73 int cur = CurrentProgress();
74
75 if( cur < 0 || cur > 1000 )
76 cur = 0;
77
78 SetRange( 1000 );
79
80 wxString message; // Empty message means unchanged
81 {
82 std::lock_guard<std::mutex> guard( m_mutex );
83 bool messageChanged = m_messageChanged.exchange( false );
84
85 if( messageChanged )
87 }
88
89 // Remove trailing newline
90 if( !message.empty() && message.Last() == '\n' )
91 message.RemoveLast();
92
93 if( m_reservedSize && !message.empty() )
94 {
95 wxSize maxSize = GetTextExtent( getPlaceholderText() );
96
97 wxClientDC dc( this );
98 message = wxControl::Ellipsize( message, dc, wxELLIPSIZE_MIDDLE, maxSize.x );
99 }
100
101 // Allowing interaction with other windows has unintended consequences
102 wxWindowDisabler ed( this );
103
104 // Returns false when cancelled (if it's a cancellable dialog). A phase whose total is not
105 // known yet pulses instead, so the gauge shows that work is still going on.
106 bool diag = m_indeterminate.load() ? WX_PROGRESS_REPORTER_BASE::Pulse( message )
107 : WX_PROGRESS_REPORTER_BASE::Update( cur, message );
108
109 if( !message.empty() )
110 Fit();
111
113
114 m_lastUpdateResult.store( diag );
115
116 return diag;
117}
118
119
120GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
121 PROGRESS_REPORTER_BASE( aNumPhases ),
122 wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL,
123 wxDefaultValidator, wxGaugeNameStr )
124{
125}
126
127
129{
130 int cur = CurrentProgress();
131
132 if( cur < 0 || cur > 1000 )
133 cur = 0;
134
135 wxGauge::SetValue( cur );
136
137 DrainPendingEvents( wxEVT_CATEGORY_UI );
138
139 return true; // No cancel button on a wxGauge
140}
GAUGE_PROGRESS_REPORTER(wxWindow *aParent, int aNumPhases)
std::atomic_bool m_lastUpdateResult
wxAppProgressIndicator m_appProgressIndicator
WX_PROGRESS_REPORTER(wxWindow *aParent, const wxString &aTitle, int aNumPhases, int aCanAbort, bool aReserveSpaceForMessage=true)
The PROGRESS_REPORTER will stay on top of aParent.
static bool shouldRefresh(int aProgress, int aMaxProgress, int aPhase, int aNumPhases, THROTTLE &aThrottle)
std::atomic_bool m_indeterminate
std::string message
STL namespace.
void DrainPendingEvents(long aCategories=wxEVT_CATEGORY_TIMER)
Drain pending events of the given categories from the active event loop.
static wxString getPlaceholderText()
#define WX_PROGRESS_REPORTER_BASE