File indexing completed on 2024-05-05 04:49:22

0001 /****************************************************************************************
0002  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "statusbar/ProgressBar.h"
0018 
0019 #include "core/support/Debug.h"
0020 #include "MainWindow.h"
0021 
0022 #include <QTimer>
0023 
0024 #include <QIcon>
0025 #include <KLocalizedString>
0026 
0027 ProgressBar::ProgressBar( QWidget *parent )
0028         : QFrame( parent )
0029 {
0030     setFixedHeight( 30 );
0031     setContentsMargins( 0, 0, 0, 4 );
0032 
0033     QVBoxLayout *box = new QVBoxLayout;
0034     box->setMargin( 0 );
0035     box->setSpacing( 3 );
0036 
0037     QHBoxLayout *descriptionLayout = new QHBoxLayout;
0038     descriptionLayout->setMargin( 0 );
0039     descriptionLayout->setSpacing( 2 );
0040 
0041     m_descriptionLabel = new QLabel;
0042     m_descriptionLabel->setWordWrap( true );
0043     //add with stretchfactor 1 so it takes up more space then the cancel button
0044     descriptionLayout->addWidget( m_descriptionLabel, 1 );
0045 
0046     m_cancelButton = new QToolButton;
0047     m_cancelButton->setIcon( QIcon::fromTheme( QStringLiteral("dialog-cancel-amarok") ) );
0048     m_cancelButton->setToolTip( i18n( "Abort" ) );
0049     m_cancelButton->setHidden( true );
0050     m_cancelButton->setFixedWidth( 16 );
0051     m_cancelButton->setFixedHeight( 16 );
0052     m_cancelButton->setAutoFillBackground( false );
0053     m_cancelButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
0054     descriptionLayout->addWidget( m_cancelButton );
0055     descriptionLayout->setAlignment( m_cancelButton, Qt::AlignRight );
0056 
0057     box->addLayout( descriptionLayout );
0058 
0059     m_progressBar = new QProgressBar;
0060     m_progressBar->setMinimum( 0 );
0061     m_progressBar->setMaximum( 100 );
0062     m_progressBar->setFixedHeight( 5 );
0063     m_progressBar->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
0064     m_progressBar->setTextVisible( false );
0065     box->addWidget( m_progressBar );
0066     box->setAlignment( m_progressBar, Qt::AlignBottom );
0067 
0068     setLayout( box );
0069 }
0070 
0071 
0072 ProgressBar::~ProgressBar()
0073 {
0074 }
0075 
0076 void
0077 ProgressBar::setDescription( const QString &description )
0078 {
0079     m_descriptionLabel->setText( description );
0080 }
0081 
0082 void ProgressBar::cancel()
0083 {
0084     DEBUG_BLOCK
0085     debug() << "cancelling operation: " << m_descriptionLabel->text();
0086     Q_EMIT( cancelled( this ) );
0087 }
0088 
0089 void ProgressBar::setValue( int percentage )
0090 {
0091     progressBar()->setValue( percentage );
0092     Q_EMIT( percentageChanged( percentage ) );
0093 
0094     //this safety check has to be removed as KJobs sometimes start out
0095     //by showing 100%, thus removing the progress info before it even gets started
0096     /*if ( percentage == m_progressBar->maximum() )
0097         QTimer::singleShot( POST_COMPLETION_DELAY, this, SLOT(delayedDone()) );*/
0098 }
0099 
0100 void ProgressBar::delayedDone()
0101 {
0102     Q_EMIT( complete( this ) );
0103 }
0104 
0105 int ProgressBar::percentage()
0106 {
0107     if( m_progressBar->maximum() == 100 )
0108         return m_progressBar->value();
0109     return (int)( ( (float) m_progressBar->value() / (float)m_progressBar->maximum() ) * 100.0 );
0110 }
0111 
0112