one entry for korganizer in khelpcenters navigation tree is enough
[kdepim.git] / libkdepim / statusbarprogresswidget.cpp
blob2bd2697b201bd072c1ad63db9ae4193cd10d5a7d
1 /*
2 statusbarprogresswidget.cpp
4 (C) 2004 Till Adam <adam@kde.org>
5 Don Sanders
6 David Faure <dfaure@kde.org>
8 Copyright 2004 David Faure <faure@kde.org>
9 Includes StatusbarProgressWidget which is based on KIOLittleProgressDlg
10 by Matt Koss <koss@miesto.sk>
12 KMail is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License, version 2 or above,
14 as published by the Free Software Foundation.
16 KMail is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 In addition, as a special exception, the copyright holders give
26 permission to link the code of this program with any edition of
27 the Qt library by Trolltech AS, Norway (or with modified versions
28 of Qt that use the same license as Qt), and distribute linked
29 combinations including the two. You must obey the GNU General
30 Public License in all respects for all of the code used other than
31 Qt. If you modify this file, you may extend this exception to
32 your version of the file, but you are not obligated to do so. If
33 you do not wish to do so, delete this exception statement from
34 your version.
37 #include "statusbarprogresswidget.h"
38 #include "progressdialog.h"
39 #include "ssllabel.h"
40 using KPIM::SSLLabel;
41 #include "progressmanager.h"
42 using KPIM::ProgressItem;
43 using KPIM::ProgressManager;
45 #include <KLocale>
46 #include <KIconLoader>
47 #include <KDebug>
49 #include <QEvent>
50 #include <QFrame>
51 #include <QHBoxLayout>
52 #include <QLabel>
53 #include <QMouseEvent>
54 #include <QProgressBar>
55 #include <QPushButton>
56 #include <QStackedWidget>
57 #include <QTimer>
59 using namespace KPIM;
61 //-----------------------------------------------------------------------------
62 StatusbarProgressWidget::StatusbarProgressWidget( ProgressDialog* progressDialog, QWidget* parent, bool button )
63 : QFrame( parent ), mCurrentItem( 0 ), mProgressDialog( progressDialog ),
64 mDelayTimer( 0 ), mBusyTimer( 0 ), mCleanTimer( 0 )
66 m_bShowButton = button;
67 int w = fontMetrics().width( " 999.9 kB/s 00:00:01 " ) + 8;
68 box = new QHBoxLayout( this );
69 box->setMargin(0);
70 box->setSpacing(0);
72 m_pButton = new QPushButton( this );
73 m_pButton->setSizePolicy( QSizePolicy( QSizePolicy::Minimum,
74 QSizePolicy::Minimum ) );
75 QPixmap smallIcon = SmallIcon( "go-up" );
76 m_pButton->setIcon( smallIcon );
77 box->addWidget( m_pButton );
78 stack = new QStackedWidget( this );
79 int maximumHeight = qMax( smallIcon.height(), fontMetrics().height() );
80 stack->setMaximumHeight( maximumHeight );
81 box->addWidget( stack );
83 m_sslLabel = new SSLLabel( this );
84 box->addWidget( m_sslLabel );
86 m_pButton->setToolTip( i18n("Open detailed progress dialog") );
88 m_pProgressBar = new QProgressBar( this );
89 m_pProgressBar->installEventFilter( this );
90 m_pProgressBar->setMinimumWidth( w );
91 stack->insertWidget( 1,m_pProgressBar );
93 m_pLabel = new QLabel( QString(), this );
94 m_pLabel->setAlignment( Qt::AlignHCenter | Qt::AlignVCenter );
95 m_pLabel->installEventFilter( this );
96 m_pLabel->setMinimumWidth( w );
97 stack->insertWidget( 2, m_pLabel );
98 m_pButton->setMaximumHeight( maximumHeight );
99 setMinimumWidth( minimumSizeHint().width() );
101 mode = None;
102 setMode();
104 connect( m_pButton, SIGNAL( clicked() ),
105 progressDialog, SLOT( slotToggleVisibility() ) );
107 connect ( ProgressManager::instance(), SIGNAL( progressItemAdded( KPIM::ProgressItem * ) ),
108 this, SLOT( slotProgressItemAdded( KPIM::ProgressItem * ) ) );
109 connect ( ProgressManager::instance(), SIGNAL( progressItemCompleted( KPIM::ProgressItem * ) ),
110 this, SLOT( slotProgressItemCompleted( KPIM::ProgressItem * ) ) );
111 connect ( ProgressManager::instance(), SIGNAL(progressItemUsesBusyIndicator(KPIM::ProgressItem*,bool)),
112 this, SLOT( updateBusyMode() ) );
114 connect ( progressDialog, SIGNAL( visibilityChanged( bool )),
115 this, SLOT( slotProgressDialogVisible( bool ) ) );
117 mDelayTimer = new QTimer( this );
118 mDelayTimer->setSingleShot( true );
119 connect ( mDelayTimer, SIGNAL( timeout() ),
120 this, SLOT( slotShowItemDelayed() ) );
122 mCleanTimer = new QTimer( this );
123 mCleanTimer->setSingleShot( true );
124 connect ( mCleanTimer, SIGNAL(timeout()),
125 this, SLOT(slotClean()) );
128 // There are three cases: no progressitem, one progressitem (connect to it directly),
129 // or many progressitems (display busy indicator). Let's call them 0,1,N.
130 // In slot..Added we can only end up in 1 or N.
131 // In slot..Removed we can end up in 0, 1, or we can stay in N if we were already.
133 void StatusbarProgressWidget::updateBusyMode()
135 connectSingleItem(); // if going to 1 item
136 if ( mCurrentItem ) { // Exactly one item
137 delete mBusyTimer;
138 mBusyTimer = 0;
139 mDelayTimer->start( 1000 );
141 else { // N items
142 if ( !mBusyTimer ) {
143 mBusyTimer = new QTimer( this );
144 connect( mBusyTimer, SIGNAL( timeout() ),
145 this, SLOT( slotBusyIndicator() ) );
146 mDelayTimer->start( 1000 );
151 void StatusbarProgressWidget::slotProgressItemAdded( ProgressItem *item )
153 if ( item->parent() )
154 return; // we are only interested in top level items
156 updateBusyMode();
159 void StatusbarProgressWidget::slotProgressItemCompleted( ProgressItem *item )
161 if ( item->parent() ) return; // we are only interested in top level items
162 connectSingleItem(); // if going back to 1 item
163 if ( ProgressManager::instance()->isEmpty() ) { // No item
164 // Done. In 5s the progress-widget will close, then we can clean up the statusbar
165 mCleanTimer->start( 5000 );
166 } else if ( mCurrentItem ) { // Exactly one item
167 delete mBusyTimer;
168 mBusyTimer = 0;
169 activateSingleItemMode();
173 void StatusbarProgressWidget::connectSingleItem()
175 if ( mCurrentItem ) {
176 disconnect ( mCurrentItem, SIGNAL( progressItemProgress( KPIM::ProgressItem *, unsigned int ) ),
177 this, SLOT( slotProgressItemProgress( KPIM::ProgressItem *, unsigned int ) ) );
178 mCurrentItem = 0;
180 mCurrentItem = ProgressManager::instance()->singleItem();
181 if ( mCurrentItem ) {
182 connect ( mCurrentItem, SIGNAL( progressItemProgress( KPIM::ProgressItem *, unsigned int ) ),
183 this, SLOT( slotProgressItemProgress( KPIM::ProgressItem *, unsigned int ) ) );
187 void StatusbarProgressWidget::activateSingleItemMode()
189 m_pProgressBar->setMaximum( 100 );
190 m_pProgressBar->setValue( mCurrentItem->progress() );
191 m_pProgressBar->setTextVisible( true );
194 void StatusbarProgressWidget::slotShowItemDelayed()
196 bool noItems = ProgressManager::instance()->isEmpty();
197 if ( mCurrentItem ) {
198 activateSingleItemMode();
199 } else if ( !noItems ) { // N items
200 m_pProgressBar->setMaximum( 0 );
201 m_pProgressBar->setTextVisible( false );
202 Q_ASSERT( mBusyTimer );
203 if ( mBusyTimer )
204 mBusyTimer->start( 100 );
207 if ( !noItems && mode == None ) {
208 mode = Progress;
209 setMode();
213 void StatusbarProgressWidget::slotBusyIndicator()
215 int p = m_pProgressBar->value();
216 m_pProgressBar->setValue( p + 10 );
219 void StatusbarProgressWidget::slotProgressItemProgress( ProgressItem *item, unsigned int value )
221 Q_ASSERT( item == mCurrentItem); // the only one we should be connected to
222 Q_UNUSED( item );
223 m_pProgressBar->setValue( value );
226 void StatusbarProgressWidget::slotSetSSL( bool ssl )
228 m_sslLabel->setEncrypted( ssl );
231 void StatusbarProgressWidget::setMode() {
232 switch ( mode ) {
233 case None:
234 if ( m_bShowButton ) {
235 m_pButton->hide();
237 m_sslLabel->setState( SSLLabel::Done );
238 // show the empty label in order to make the status bar look better
239 stack->show();
240 stack->setCurrentWidget( m_pLabel );
241 break;
243 #if 0
244 case Label:
245 if ( m_bShowButton ) {
246 m_pButton->show();
248 m_sslLabel->setState( m_sslLabel->lastState() );
249 stack->show();
250 stack->raiseWidget( m_pLabel );
251 break;
252 #endif
254 case Progress:
255 stack->show();
256 stack->setCurrentWidget( m_pProgressBar );
257 if ( m_bShowButton ) {
258 m_pButton->show();
260 m_sslLabel->setState( m_sslLabel->lastState() );
261 break;
265 void StatusbarProgressWidget::slotClean()
267 // check if a new item showed up since we started the timer. If not, clear
268 if ( ProgressManager::instance()->isEmpty() ) {
269 m_pProgressBar->setValue( 0 );
270 //m_pLabel->clear();
271 mode = None;
272 setMode();
276 bool StatusbarProgressWidget::eventFilter( QObject *, QEvent *ev )
278 if ( ev->type() == QEvent::MouseButtonPress ) {
279 QMouseEvent *e = (QMouseEvent*)ev;
281 if ( e->button() == Qt::LeftButton && mode != None ) { // toggle view on left mouse button
282 // Consensus seems to be that we should show/hide the fancy dialog when the user
283 // clicks anywhere in the small one.
284 mProgressDialog->slotToggleVisibility();
285 return true;
288 return false;
291 void StatusbarProgressWidget::slotProgressDialogVisible( bool b )
293 // Update the hide/show button when the detailed one is shown/hidden
294 if ( b ) {
295 m_pButton->setIcon( SmallIcon( "go-down" ) );
296 m_pButton->setToolTip( i18n("Hide detailed progress window") );
297 setMode();
298 } else {
299 m_pButton->setIcon( SmallIcon( "go-up" ) );
300 m_pButton->setToolTip( i18n("Show detailed progress window") );
304 #include "statusbarprogresswidget.moc"