KiCad PCB EDA Suite
dialog_pcm_progress.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) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
5 * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "dialog_pcm_progress.h"
22#include <wx/msgdlg.h>
23
24
25#define GAUGE_RANGE 1000
26
27DIALOG_PCM_PROGRESS::DIALOG_PCM_PROGRESS( wxWindow* parent, bool aShowDownloadSection ) :
30 m_downloaded( 0 ),
31 m_downloadTotal( 0 ),
32 m_currentProgress( 0 ),
33 m_currentProgressTotal( 0 ),
34 m_finished( false )
35#if wxCHECK_VERSION( 3, 1, 0 )
36 ,
37 m_appProgressIndicator( parent->GetParent(), GAUGE_RANGE )
38#endif
39{
40#if wxCHECK_VERSION( 3, 1, 0 )
41 m_appProgressIndicator.Pulse();
42#endif
43
45 m_downloadGauge->SetRange( GAUGE_RANGE );
46 m_overallGauge->SetRange( GAUGE_RANGE );
47
48 if( !aShowDownloadSection )
49 m_panelDownload->Hide();
50}
51
52
53void DIALOG_PCM_PROGRESS::OnCancelClicked( wxCommandEvent& event )
54{
55 SetNumPhases( 1 );
56 SetPackageProgress( 1, 1 );
57 m_reporter->Report( _( "Aborting remaining tasks." ) );
58
59 m_cancelled.store( true );
60 m_finished.store( true );
61}
62
63
64void DIALOG_PCM_PROGRESS::OnCloseClicked( wxCommandEvent& event )
65{
67}
68
69
70void DIALOG_PCM_PROGRESS::PCMReport( const wxString& aText, SEVERITY aSeverity )
71{
72 std::lock_guard<std::mutex> guard( m_mutex );
73 m_reports.push_back( std::make_pair( aText, aSeverity ) );
74}
75
76
77void DIALOG_PCM_PROGRESS::SetDownloadProgress( uint64_t aDownloaded, uint64_t aTotal )
78{
79 m_downloaded.store( std::min( aDownloaded, aTotal ) );
80 m_downloadTotal.store( aTotal );
81}
82
83
84uint64_t DIALOG_PCM_PROGRESS::toKb( uint64_t aValue )
85{
86 return ( aValue + 999 ) / 1000;
87}
88
89
90void DIALOG_PCM_PROGRESS::SetPackageProgress( uint64_t aProgress, uint64_t aTotal )
91{
92 m_currentProgress.store( std::min( aProgress, aTotal ) );
93 m_currentProgressTotal.store( aTotal );
94}
95
96
98{
100 m_currentProgress.store( 0 );
101}
102
103
105{
106 m_finished.store( true );
107}
108
109
111{
112 bool finished = m_finished.load();
113 int phase = m_phase.load();
114 int phases = m_numPhases.load();
115 long cp = m_currentProgress.load();
116 long total = m_currentProgressTotal.load();
117 double current = ( total > 0 ) ? ( double( cp ) / double( total ) ) : 0;
118
119 if( phases > 0 )
120 current = ( phase + current ) / phases;
121
122 if( current > 1.0 || finished )
123 current = 1.0;
124
125 m_overallGauge->SetValue( current * GAUGE_RANGE );
126#if wxCHECK_VERSION( 3, 1, 0 )
127 m_appProgressIndicator.SetValue( current * GAUGE_RANGE );
128#endif
129
130 if( m_downloadTotal.load() == 0 )
131 {
132 m_downloadText->SetLabel( wxEmptyString );
133 m_downloadGauge->SetValue( 0 );
134 }
135 else
136 {
137 m_downloadText->SetLabel( wxString::Format( _( "Downloaded %lld/%lld kB" ),
138 toKb( m_downloaded.load() ),
139 toKb( m_downloadTotal.load() ) ) );
140
141 current = m_downloaded.load() / (double) m_downloadTotal.load();
142
143 if( current > 1.0 || finished )
144 current = 1.0;
145
146 m_downloadGauge->SetValue( current * GAUGE_RANGE );
147 }
148
149 std::lock_guard<std::mutex> guard( m_mutex );
150
151 for( const std::pair<wxString, SEVERITY>& pair : m_reports )
152 m_reporter->Report( pair.first, pair.second );
153
154 m_reports.clear();
155
156 if( finished )
157 {
158 m_buttonCancel->Disable();
159 m_buttonClose->Enable();
160 }
161
162 wxYield();
163
164 return true;
165}
166
167
Class DIALOG_PCM_PROGRESS_BASE.
std::atomic_int64_t m_currentProgressTotal
std::atomic_int64_t m_currentProgress
void OnCloseClicked(wxCommandEvent &event) override
std::atomic_bool m_finished
void OnCancelClicked(wxCommandEvent &event) override
void AdvancePhase() override
Safe to call from non-UI thread. Disables cancel button, enables close button.
void SetDownloadProgress(uint64_t aDownloaded, uint64_t aTotal)
Safe to call from non-UI thread. Sets the download prgress of the current package.
void SetPackageProgress(uint64_t aProgress, uint64_t aTotal)
Safe to call from non-UI thread. Advances to the next package.
std::vector< std::pair< wxString, SEVERITY > > m_reports
void PCMReport(const wxString &aText, SEVERITY aSeverity)
Safe to call from non-UI thread. Sets the download progress of the current zip entry.
static uint64_t toKb(uint64_t aValue)
DIALOG_PCM_PROGRESS(wxWindow *parent, bool aShowDownloadSection=true)
Constructor.
std::atomic_int64_t m_downloadTotal
std::atomic_int64_t m_downloaded
This implements all the tricky bits for thread safety, but the GUI is left to derived classes.
virtual void AdvancePhase() override
Use the next available virtual zone of the dialog progress bar.
void SetNumPhases(int aNumPhases) override
Set the number of phases.
void SetImmediateMode()
In immediate mode, messages are flushed as they are added.
REPORTER & Report(const wxString &aText, SEVERITY aSeverity=RPT_SEVERITY_UNDEFINED) override
Report a string with a given severity.
#define GAUGE_RANGE
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
SEVERITY