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 (C) 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
31
32WX_PROGRESS_REPORTER::WX_PROGRESS_REPORTER( wxWindow* aParent, const wxString& aTitle,
33 int aNumPhases, bool aCanAbort,
34 bool aReserveSpaceForMessage ) :
35 PROGRESS_REPORTER_BASE( aNumPhases ),
36 wxProgressDialog( aTitle, ( aReserveSpaceForMessage ? wxT( " " ) : wxT( "" ) ), 1, aParent,
37 // wxPD_APP_MODAL | // Don't use; messes up OSX when called from
38 // quasi-modal dialog
39 wxPD_AUTO_HIDE | // *MUST* use; otherwise wxWidgets will spin
40 // up another event loop on completion which
41 // causes all sorts of grief
42 ( aCanAbort ? wxPD_CAN_ABORT : 0 ) | wxPD_ELAPSED_TIME ),
43 m_appProgressIndicator( aParent ),
44 m_messageWidth( 0 )
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
54{
55}
56
57
59{
60 int cur = CurrentProgress();
61
62 if( cur < 0 || cur > 1000 )
63 cur = 0;
64
65 SetRange( 1000 );
66
67 wxString message;
68
69 {
70 std::lock_guard<std::mutex> guard( m_mutex );
71 message = m_rptMessage;
72 }
73
74 // Perhaps the window size is too small if the new message to display is bigger
75 // than the previous message. in this case, resize the WX_PROGRESS_REPORTER window
76 // GetTextExtent has probably bugs in wxWidgets < 3.1.6, so calling it only when
77 // the message has changed is mandatory
79 {
80 int newWidth = GetTextExtent( m_rptMessage ).x;
81
82 if( newWidth > m_messageWidth )
83 {
84 m_messageWidth = newWidth;
85 Fit();
86 }
87
88 m_messageChanged = false;
89 }
90
91 // Returns false when cancelled (if it's a cancellable dialog)
92 bool diag = wxProgressDialog::Update( cur, message );
93
94 return diag;
95}
96
97
98GAUGE_PROGRESS_REPORTER::GAUGE_PROGRESS_REPORTER( wxWindow* aParent, int aNumPhases ) :
99 PROGRESS_REPORTER_BASE( aNumPhases ),
100 wxGauge( aParent, wxID_ANY, 1000, wxDefaultPosition, wxDefaultSize, wxGA_HORIZONTAL,
101 wxDefaultValidator, wxGaugeNameStr )
102{
103}
104
105
107{
108 int cur = CurrentProgress();
109
110 if( cur < 0 || cur > 1000 )
111 cur = 0;
112
113 wxGauge::SetValue( cur );
114 wxEventLoopBase::GetActive()->YieldFor( wxEVT_CATEGORY_UI );
115
116 return true; // No cancel button on a wxGauge
117}
GAUGE_PROGRESS_REPORTER(wxWindow *aParent, int aNumPhases)
This implements all the tricky bits for thread safety, but the GUI is left to derived classes.
wxAppProgressIndicator m_appProgressIndicator
WX_PROGRESS_REPORTER(wxWindow *aParent, const wxString &aTitle, int aNumPhases, bool aCanAbort=true, bool aReserveSpaceForMessage=true)
The PROGRESS_REPORTER will stay on top of aParent.