KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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 The 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 ),
34 m_finished( false ),
35 m_disabler( this ),
36 m_appProgressIndicator( parent->GetParent(), GAUGE_RANGE )
37{
39
40 m_reporter->SetImmediateMode();
41 m_downloadGauge->SetRange( GAUGE_RANGE );
42 m_overallGauge->SetRange( GAUGE_RANGE );
43
44 if( !aShowDownloadSection )
45 m_panelDownload->Hide();
46}
47
48
49void DIALOG_PCM_PROGRESS::OnCancelClicked( wxCommandEvent& event )
50{
51 SetNumPhases( 1 );
52 SetPackageProgress( 1, 1 );
53 m_reporter->Report( _( "Aborting remaining tasks." ) );
54
55 m_cancelled.store( true );
56 m_finished.store( true );
57}
58
59
60void DIALOG_PCM_PROGRESS::OnCloseClicked( wxCommandEvent& event )
61{
63}
64
65
66void DIALOG_PCM_PROGRESS::PCMReport( const wxString& aText, SEVERITY aSeverity )
67{
68 std::lock_guard<std::mutex> guard( m_mutex );
69 m_reports.push_back( std::make_pair( aText, aSeverity ) );
70}
71
72
73void DIALOG_PCM_PROGRESS::SetDownloadProgress( uint64_t aDownloaded, uint64_t aTotal )
74{
75 m_downloaded.store( std::min( aDownloaded, aTotal ) );
76 m_downloadTotal.store( aTotal );
77}
78
79
80uint64_t DIALOG_PCM_PROGRESS::toKb( uint64_t aValue )
81{
82 return ( aValue + 999 ) / 1000;
83}
84
85
86void DIALOG_PCM_PROGRESS::SetPackageProgress( uint64_t aProgress, uint64_t aTotal )
87{
88 m_currentProgress.store( std::min( aProgress, aTotal ) );
89 m_currentProgressTotal.store( aTotal );
90}
91
92
98
99
101{
102 m_finished.store( true );
103}
104
105
107{
108 bool finished = m_finished.load();
109 int phase = m_phase.load();
110 int phases = m_numPhases.load();
111 long cp = m_currentProgress.load();
112 long total = m_currentProgressTotal.load();
113 double current = ( total > 0 ) ? ( double( cp ) / double( total ) ) : 0;
114
115 if( phases > 0 )
116 current = ( phase + current ) / phases;
117
118 if( current > 1.0 || finished )
119 current = 1.0;
120
121 m_overallGauge->SetValue( current * GAUGE_RANGE );
122 m_appProgressIndicator.SetValue( current * GAUGE_RANGE );
123
124 if( m_downloadTotal.load() == 0 )
125 {
126 m_downloadText->SetLabel( wxEmptyString );
127 m_downloadGauge->SetValue( 0 );
128 }
129 else
130 {
131 m_downloadText->SetLabel( wxString::Format( _( "Downloaded %lld/%lld kB" ),
132 toKb( m_downloaded.load() ),
133 toKb( m_downloadTotal.load() ) ) );
134
135 current = m_downloaded.load() / (double) m_downloadTotal.load();
136
137 if( current > 1.0 || finished )
138 current = 1.0;
139
140 m_downloadGauge->SetValue( current * GAUGE_RANGE );
141 }
142
143 std::lock_guard<std::mutex> guard( m_mutex );
144
145 for( const std::pair<wxString, SEVERITY>& pair : m_reports )
146 m_reporter->Report( pair.first, pair.second );
147
148 m_reports.clear();
149
150 if( finished )
151 {
152 m_buttonCancel->Disable();
153 m_buttonClose->Enable();
154 }
155
156 wxYield();
157
158 return true;
159}
160
161
DIALOG_PCM_PROGRESS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=_("Applying Package Changes"), const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(600, 500), long style=wxCAPTION)
std::atomic_int64_t m_currentProgressTotal
std::atomic_int64_t m_currentProgress
void OnCloseClicked(wxCommandEvent &event) override
std::atomic_bool m_finished
wxWindowDisabler m_disabler
void OnCancelClicked(wxCommandEvent &event) override
void AdvancePhase() override
Safe to call from non-UI thread. Disables cancel button, enables close button.
wxAppProgressIndicator m_appProgressIndicator
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
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.
#define GAUGE_RANGE
#define _(s)
SEVERITY