2020-10-26 03:54:36 -07:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2021 Andrew Lutsenko, anlutsenko at gmail dot com
|
|
|
|
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dialog_pcm_progress.h"
|
|
|
|
#include <wx/msgdlg.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define GAUGE_RANGE 1000
|
|
|
|
|
|
|
|
DIALOG_PCM_PROGRESS::DIALOG_PCM_PROGRESS( wxWindow* parent, bool aShowDownloadSection ) :
|
2021-11-10 22:11:52 +00:00
|
|
|
DIALOG_PCM_PROGRESS_BASE( parent ),
|
|
|
|
PROGRESS_REPORTER_BASE( 1 ),
|
|
|
|
m_finished( false )
|
2020-10-26 03:54:36 -07:00
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
|
|
|
,
|
|
|
|
m_appProgressIndicator( parent->GetParent(), GAUGE_RANGE )
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
|
|
|
m_appProgressIndicator.Pulse();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
m_reporter->SetImmediateMode();
|
|
|
|
m_downloadGauge->SetRange( GAUGE_RANGE );
|
|
|
|
m_overallGauge->SetRange( GAUGE_RANGE );
|
|
|
|
|
|
|
|
if( !aShowDownloadSection )
|
|
|
|
m_panelDownload->Hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_PCM_PROGRESS::OnCancelClicked( wxCommandEvent& event )
|
|
|
|
{
|
2021-11-10 22:11:52 +00:00
|
|
|
SetNumPhases( 1 );
|
|
|
|
SetOverallProgress( 1, 1 );
|
|
|
|
Report( _( "Aborting remaining tasks." ) );
|
|
|
|
|
2020-10-26 03:54:36 -07:00
|
|
|
m_cancelled.store( true );
|
2021-11-10 22:11:52 +00:00
|
|
|
m_finished.store( true );
|
2020-10-26 03:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_PCM_PROGRESS::OnCloseClicked( wxCommandEvent& event )
|
|
|
|
{
|
2021-11-10 22:11:52 +00:00
|
|
|
m_progress.store( m_maxProgress );
|
2020-10-26 03:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_PCM_PROGRESS::Report( const wxString& aText, SEVERITY aSeverity )
|
|
|
|
{
|
2021-11-10 22:11:52 +00:00
|
|
|
std::lock_guard<std::mutex> guard( m_mutex );
|
|
|
|
m_reports.push_back( std::make_pair( aText, aSeverity ) );
|
2020-10-26 03:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DIALOG_PCM_PROGRESS::SetDownloadProgress( uint64_t aDownloaded, uint64_t aTotal )
|
|
|
|
{
|
2021-11-10 22:11:52 +00:00
|
|
|
m_downloaded.store( std::min( aDownloaded, aTotal ) );
|
|
|
|
m_downloadTotal.store( aTotal );
|
|
|
|
}
|
2020-10-26 03:54:36 -07:00
|
|
|
|
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
uint64_t DIALOG_PCM_PROGRESS::toKb( uint64_t aValue )
|
|
|
|
{
|
|
|
|
return ( aValue + 1023 ) / 1024;
|
|
|
|
}
|
2020-10-26 03:54:36 -07:00
|
|
|
|
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
void DIALOG_PCM_PROGRESS::SetOverallProgress( uint64_t aProgress, uint64_t aTotal )
|
|
|
|
{
|
|
|
|
m_overallProgress.store( std::min( aProgress, aTotal ) );
|
|
|
|
m_overallProgressTotal.store( aTotal );
|
2020-10-26 03:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
void DIALOG_PCM_PROGRESS::SetFinished()
|
2020-10-26 03:54:36 -07:00
|
|
|
{
|
2021-11-10 22:11:52 +00:00
|
|
|
m_finished.store( true );
|
2020-10-26 03:54:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
bool DIALOG_PCM_PROGRESS::updateUI()
|
2020-10-26 03:54:36 -07:00
|
|
|
{
|
2021-11-10 22:11:52 +00:00
|
|
|
int phase = m_phase.load();
|
|
|
|
int phases = m_numPhases.load();
|
|
|
|
double current = m_overallProgress.load() / (double) m_overallProgressTotal.load();
|
|
|
|
|
|
|
|
if( phases > 0 )
|
|
|
|
current = ( phase + current ) / phases;
|
2020-10-26 03:54:36 -07:00
|
|
|
|
|
|
|
if( current > 1.0 )
|
|
|
|
current = 1.0;
|
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
m_overallGauge->SetValue( current * GAUGE_RANGE );
|
2020-10-26 03:54:36 -07:00
|
|
|
#if wxCHECK_VERSION( 3, 1, 0 )
|
2021-11-10 22:11:52 +00:00
|
|
|
m_appProgressIndicator.SetValue( current * GAUGE_RANGE );
|
2020-10-26 03:54:36 -07:00
|
|
|
#endif
|
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
if( m_downloadTotal.load() == 0 )
|
|
|
|
{
|
|
|
|
m_downloadText->SetLabel( wxEmptyString );
|
|
|
|
m_downloadGauge->SetValue( 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_downloadText->SetLabel( wxString::Format( _( "Downloaded %lld/%lld Kb" ),
|
|
|
|
toKb( m_downloaded ),
|
|
|
|
toKb( m_downloadTotal ) ) );
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
current = m_downloaded.load() / (double) m_downloadTotal.load();
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
if( current > 1.0 )
|
|
|
|
current = 1.0;
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
m_downloadGauge->SetValue( current * GAUGE_RANGE );
|
|
|
|
}
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
std::lock_guard<std::mutex> guard( m_mutex );
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
for( const std::pair<wxString, SEVERITY>& pair : m_reports )
|
|
|
|
m_reporter->Report( pair.first, pair.second );
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
m_reports.clear();
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
if( m_finished.load() )
|
|
|
|
{
|
|
|
|
m_buttonCancel->Disable();
|
|
|
|
m_buttonClose->Enable();
|
|
|
|
}
|
2020-10-26 03:54:36 -07:00
|
|
|
|
2021-11-10 22:11:52 +00:00
|
|
|
wxYield();
|
|
|
|
|
|
|
|
return true;
|
2020-10-26 03:54:36 -07:00
|
|
|
}
|
2021-11-10 22:11:52 +00:00
|
|
|
|
|
|
|
|