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
28WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle,
29 int aNumPhases, int aCanAbort,
30 bool aReserveSpaceForMessage ) :
31 PROGRESS_REPORTER_BASE( aNumPhases ),
33 ( aReserveSpaceForMessage ? wxString( ' ', 80 ) : wxString( wxT( "" ) ) ),
34 1, aParent,
35 // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
36 // quasi-modal dialog
37 wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
38 // up another event loop on completion which
39 // causes all sorts of grief
40 aCanAbort | wxPD_ELAPSED_TIME ),
41 m_appProgressIndicator( aParent ),
42 m_messageWidth( 0 ),
43 m_updateThrottle( std::chrono::milliseconds( 100 ) ),
44 m_lastUpdateResult( true )
45{
46 // wxAppProgressIndicator doesn't like value > max, ever. However there are some risks
47 // with multithreaded setting of those values making a mess
48 // the cop out is just to set the progress to "indeterminate"
50}
51
52
56
57
59{
60 if( !shouldRefresh( m_progress.load(), m_maxProgress.load(), m_phase.load(), m_numPhases.load(),
62 {
63 return !m_cancelled.load() && m_lastUpdateResult.load();
64 }
65
66 int cur = CurrentProgress();
67
68 if( cur < 0 || cur > 1000 )
69 cur = 0;
70
71 SetRange( 1000 );
72
73 wxString message;
74 bool messageChanged;
75
76 {
77 std::lock_guard<std::mutex> guard( m_mutex );
78 message = m_rptMessage;
79 messageChanged = m_messageChanged.exchange( false );
80 }
81
82 // Perhaps the window size is too small if the new message to display is bigger
83 // than the previous message. in this case, resize the WX_PROGRESS_REPORTER window
84 // GetTextExtent has probably bugs in wxWidgets < 3.1.6, so calling it only when
85 // the message has changed is mandatory
86 if( messageChanged )
87 {
88 int newWidth = GetTextExtent( message ).x;
89
90 if( newWidth > m_messageWidth )
91 {
92 m_messageWidth = newWidth;
93 Fit();
94 }
95 }
96
97 // Allowing interaction with other windows has unintended consequences
98 wxWindowDisabler ed( this );
99
100 // Returns false when cancelled (if it's a cancellable dialog)
101 bool diag = WX_PROGRESS_REPORTER_BASE::Update( cur, message );
102
104
105 m_lastUpdateResult.store( diag );
106
107 return diag;
108}
109
110
111GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
112 PROGRESS_REPORTER_BASE( aNumPhases ),
113 wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL,
114 wxDefaultValidator, wxGaugeNameStr )
115{
116}
117
118
120{
121 int cur = CurrentProgress();
122
123 if( cur < 0 || cur > 1000 )
124 cur = 0;
125
126 wxGauge::SetValue( cur );
127
128 DrainPendingEvents( wxEVT_CATEGORY_UI );
129
130 return true; // No cancel button on a wxGauge
131}
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.
#define WX_PROGRESS_REPORTER_BASE