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,
37 int aNumPhases, int aCanAbort,
38 bool aReserveSpaceForMessage ) :
39 PROGRESS_REPORTER_BASE( aNumPhases ),
40 WX_PROGRESS_REPORTER_BASE( aTitle, aReserveSpaceForMessage
42 : wxString( wxEmptyString ),
43 1, aParent,
44 // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
45 // quasi-modal dialog
46 wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
47 // up another event loop on completion which
48 // causes all sorts of grief
49 aCanAbort | wxPD_ELAPSED_TIME ),
50 m_appProgressIndicator( aParent ),
51 m_reservedSize( aReserveSpaceForMessage ),
52 m_updateThrottle( std::chrono::milliseconds( 100 ) ),
53 m_lastUpdateResult( true )
54{
55 // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
56 // with multithreaded setting of those values making a mess
57 // the cop out is just to set the progress to "indeterminate"
59}
60
61
65
66
68{
69 if( !shouldRefresh( m_progress.load(), m_maxProgress.load(), m_phase.load(), m_numPhases.load(),
71 {
72 return !m_cancelled.load() && m_lastUpdateResult.load();
73 }
74
75 int cur = CurrentProgress();
76
77 if( cur < 0 || cur > 1000 )
78 cur = 0;
79
80 SetRange( 1000 );
81
82 wxString message; // Empty message means unchanged
83 {
84 std::lock_guard<std::mutex> guard( m_mutex );
85 bool messageChanged = m_messageChanged.exchange( false );
86
87 if( messageChanged )
88 message = m_rptMessage;
89 }
90
91 // Remove trailing newline
92 if( !message.empty() && message.Last() == '\n' )
93 message.RemoveLast();
94
95 if( m_reservedSize && !message.empty() )
96 {
97 wxSize maxSize = GetTextExtent( getPlaceholderText() );
98
99 wxClientDC dc( this );
100 message = wxControl::Ellipsize( message, dc, wxELLIPSIZE_MIDDLE, maxSize.x );
101 }
102
103 // Allowing interaction with other windows has unintended consequences
104 wxWindowDisabler ed( this );
105
106 // Returns false when cancelled (if it's a cancellable dialog)
107 bool diag = 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)
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