1 /****************************************************************************************
2  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhn@kde.org>                                *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "statusbar/ProgressBar.h"
18 
19 #include "core/support/Debug.h"
20 #include "MainWindow.h"
21 
22 #include <QTimer>
23 
24 #include <QIcon>
25 #include <KLocalizedString>
26 
ProgressBar(QWidget * parent)27 ProgressBar::ProgressBar( QWidget *parent )
28         : QFrame( parent )
29 {
30     setFixedHeight( 30 );
31     setContentsMargins( 0, 0, 0, 4 );
32 
33     QVBoxLayout *box = new QVBoxLayout;
34     box->setMargin( 0 );
35     box->setSpacing( 3 );
36 
37     QHBoxLayout *descriptionLayout = new QHBoxLayout;
38     descriptionLayout->setMargin( 0 );
39     descriptionLayout->setSpacing( 2 );
40 
41     m_descriptionLabel = new QLabel;
42     m_descriptionLabel->setWordWrap( true );
43     //add with stretchfactor 1 so it takes up more space then the cancel button
44     descriptionLayout->addWidget( m_descriptionLabel, 1 );
45 
46     m_cancelButton = new QToolButton;
47     m_cancelButton->setIcon( QIcon::fromTheme( QStringLiteral("dialog-cancel-amarok") ) );
48     m_cancelButton->setToolTip( i18n( "Abort" ) );
49     m_cancelButton->setHidden( true );
50     m_cancelButton->setFixedWidth( 16 );
51     m_cancelButton->setFixedHeight( 16 );
52     m_cancelButton->setAutoFillBackground( false );
53     m_cancelButton->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
54     descriptionLayout->addWidget( m_cancelButton );
55     descriptionLayout->setAlignment( m_cancelButton, Qt::AlignRight );
56 
57     box->addLayout( descriptionLayout );
58 
59     m_progressBar = new QProgressBar;
60     m_progressBar->setMinimum( 0 );
61     m_progressBar->setMaximum( 100 );
62     m_progressBar->setFixedHeight( 5 );
63     m_progressBar->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
64     m_progressBar->setTextVisible( false );
65     box->addWidget( m_progressBar );
66     box->setAlignment( m_progressBar, Qt::AlignBottom );
67 
68     setLayout( box );
69 }
70 
71 
~ProgressBar()72 ProgressBar::~ProgressBar()
73 {
74 }
75 
76 void
setDescription(const QString & description)77 ProgressBar::setDescription( const QString &description )
78 {
79     m_descriptionLabel->setText( description );
80 }
81 
cancel()82 void ProgressBar::cancel()
83 {
84     DEBUG_BLOCK
85     debug() << "cancelling operation: " << m_descriptionLabel->text();
86     Q_EMIT( cancelled( this ) );
87 }
88 
setValue(int percentage)89 void ProgressBar::setValue( int percentage )
90 {
91     progressBar()->setValue( percentage );
92     Q_EMIT( percentageChanged( percentage ) );
93 
94     //this safety check has to be removed as KJobs sometimes start out
95     //by showing 100%, thus removing the progress info before it even gets started
96     /*if ( percentage == m_progressBar->maximum() )
97         QTimer::singleShot( POST_COMPLETION_DELAY, this, SLOT(delayedDone()) );*/
98 }
99 
delayedDone()100 void ProgressBar::delayedDone()
101 {
102     Q_EMIT( complete( this ) );
103 }
104 
percentage()105 int ProgressBar::percentage()
106 {
107     if( m_progressBar->maximum() == 100 )
108         return m_progressBar->value();
109     return (int)( ( (float) m_progressBar->value() / (float)m_progressBar->maximum() ) * 100.0 );
110 }
111 
112 
113