Skip to content

Commit d66b6b3

Browse files
committedJan 10, 2013
Add optional message timeout with countdown progress bar to QgsMessageBar
1 parent 7ca3656 commit d66b6b3

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed
 

‎src/gui/qgsmessagebar.cpp

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
#include <QPalette>
2323
#include <QStackedWidget>
2424
#include <QLabel>
25+
#include <QProgressBar>
2526
#include <QToolButton>
27+
#include <QTimer>
2628
#include <QGridLayout>
2729
#include <QMenu>
2830

@@ -41,11 +43,28 @@ QgsMessageBar::QgsMessageBar( QWidget *parent )
4143
mLayout->setContentsMargins( 9, 1, 9, 1 );
4244
setLayout( mLayout );
4345

46+
mCountProgress = new QProgressBar( this );
47+
48+
mCountProgress->setStyleSheet( "QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
49+
" border-radius: 2px; background: rgba(0, 0, 0, 0); }"
50+
"QProgressBar::chunk { background-color: rgba(0, 0, 0, 50%); width: 5px; }" );
51+
mCountProgress->setObjectName( "mCountdown" );
52+
mCountProgress->setToolTip( tr( "Countdown" ) );
53+
mCountProgress->setMinimumWidth( 25 );
54+
mCountProgress->setMaximumWidth( 25 );
55+
mCountProgress->setMinimumHeight( 10 );
56+
mCountProgress->setMaximumHeight( 10 );
57+
mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
58+
mCountProgress->setTextVisible( false );
59+
mCountProgress->setRange( 0, 5 );
60+
mCountProgress->setHidden( true );
61+
mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
62+
4463
mItemCount = new QLabel( this );
4564
mItemCount->setObjectName( "mItemCount" );
4665
mItemCount->setToolTip( tr( "Remaining messages" ) );
4766
mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
48-
mLayout->addWidget( mItemCount, 0, 1, 1, 1 );
67+
mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
4968

5069
mCloseMenu = new QMenu( this );
5170
mCloseMenu->setObjectName( "mCloseMenu" );
@@ -64,7 +83,11 @@ QgsMessageBar::QgsMessageBar( QWidget *parent )
6483
mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
6584
mCloseBtn->setMenu( mCloseMenu );
6685
connect( mCloseBtn, SIGNAL( clicked() ), this, SLOT( popWidget() ) );
67-
mLayout->addWidget( mCloseBtn, 0, 2, 1, 1 );
86+
mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
87+
88+
mCountdownTimer = new QTimer( this );
89+
mCountdownTimer->setInterval( 1000 );
90+
connect( mCountdownTimer, SIGNAL( timeout() ), this, SLOT( updateCountdown() ) );
6891

6992
connect( this, SIGNAL( widgetAdded( QWidget* ) ), this, SLOT( updateItemCount() ) );
7093
connect( this, SIGNAL( widgetRemoved( QWidget* ) ), this, SLOT( updateItemCount() ) );
@@ -148,6 +171,8 @@ bool QgsMessageBar::popWidget()
148171
if ( !mCurrentItem )
149172
return false;
150173

174+
resetCountdown();
175+
151176
QgsMessageBarItem *item = mCurrentItem;
152177
popItem( item );
153178

@@ -186,17 +211,27 @@ void QgsMessageBar::pushItem( QgsMessageBarItem *item )
186211
}
187212

188213
mCurrentItem = item;
189-
mLayout->addWidget( item->widget(), 0, 0, 1, 1 );
214+
mLayout->addWidget( item->widget(), 0, 1, 1, 1 );
190215
mCurrentItem->widget()->show();
191216

217+
if ( item->duration() > 0 )
218+
{
219+
mCountProgress->setRange( 0, item->duration() );
220+
mCountProgress->setValue( item->duration() );
221+
mCountProgress->setVisible( true );
222+
mCountdownTimer->start();
223+
}
224+
192225
setStyleSheet( item->styleSheet() );
193226
show();
194227

195228
emit widgetAdded( item->widget() );
196229
}
197230

198-
void QgsMessageBar::pushWidget( QWidget *widget, int level )
231+
void QgsMessageBar::pushWidget( QWidget *widget, int level, int duration )
199232
{
233+
resetCountdown();
234+
200235
QString stylesheet;
201236
if ( level >= 2 )
202237
{
@@ -215,18 +250,18 @@ void QgsMessageBar::pushWidget( QWidget *widget, int level )
215250
}
216251
stylesheet += "QLabel#mMsgTitle { font-weight: bold; } "
217252
"QLabel#mItemCount { font-style: italic; }";
218-
pushWidget( widget, stylesheet );
253+
pushWidget( widget, stylesheet, duration );
219254
}
220255

221-
void QgsMessageBar::pushWidget( QWidget *widget, const QString &styleSheet )
256+
void QgsMessageBar::pushWidget( QWidget *widget, const QString &styleSheet, int duration )
222257
{
223258
if ( !widget )
224259
return;
225260

226261
// avoid duplicated widget
227262
popWidget( widget );
228263

229-
pushItem( new QgsMessageBarItem( widget, styleSheet ) );
264+
pushItem( new QgsMessageBarItem( widget, styleSheet, duration ) );
230265
}
231266

232267
QWidget* QgsMessageBar::createMessage( const QString &title, const QString &text, const QIcon &icon, QWidget *parent )
@@ -259,6 +294,31 @@ QWidget* QgsMessageBar::createMessage( const QString &title, const QString &text
259294
return widget;
260295
}
261296

297+
void QgsMessageBar::updateCountdown()
298+
{
299+
if ( !mCountdownTimer->isActive() )
300+
{
301+
resetCountdown();
302+
return;
303+
}
304+
if ( mCountProgress->value() < 2 )
305+
{
306+
popWidget();
307+
}
308+
else
309+
{
310+
mCountProgress->setValue( mCountProgress->value() - 1 );
311+
}
312+
}
313+
314+
void QgsMessageBar::resetCountdown()
315+
{
316+
if ( mCountdownTimer->isActive() )
317+
mCountdownTimer->stop();
318+
319+
mCountProgress->setVisible( false );
320+
}
321+
262322
void QgsMessageBar::updateItemCount()
263323
{
264324
mItemCount->setText( mList.count() > 0 ? QString::number( mList.count() ) + QString( " " ) + tr( "more" ) : QString( "" ) );

‎src/gui/qgsmessagebar.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
class QWidget;
2929
class QGridLayout;
3030
class QMenu;
31+
class QProgressBar;
3132
class QToolButton;
3233
class QLabel;
3334
class QAction;
35+
class QTimer;
3436

3537
/** \ingroup gui
3638
* A bar for displaying non-blocking messages to the user.
@@ -48,8 +50,9 @@ class GUI_EXPORT QgsMessageBar: public QFrame
4850
* and putting it in a stack
4951
* @param widget widget to add
5052
* @param level is 0 for information, 1 for warning, 2 for critical
53+
* @param duration timeout duration of message in seconds, 0 value indicates no timeout
5154
*/
52-
void pushWidget( QWidget *widget, int level = 0 );
55+
void pushWidget( QWidget *widget, int level = 0, int duration = 0 );
5356

5457
/*! remove the passed widget from the bar (if previously added),
5558
* then display the next one in the stack if any or hide the bar
@@ -90,20 +93,21 @@ class GUI_EXPORT QgsMessageBar: public QFrame
9093
class QgsMessageBarItem
9194
{
9295
public:
93-
QgsMessageBarItem( QWidget *widget, const QString &styleSheet ):
94-
mWidget( widget ), mStyleSheet( styleSheet ) {}
96+
QgsMessageBarItem( QWidget *widget, const QString &styleSheet, int duration = 0 ):
97+
mWidget( widget ), mStyleSheet( styleSheet ), mDuration( duration ) {}
9598
~QgsMessageBarItem() {}
9699

97100
QWidget* widget() const { return mWidget; }
98101
QString styleSheet() const { return mStyleSheet; }
102+
int duration() const { return mDuration; }
99103

100104
private:
101105
QWidget *mWidget;
102106
QString mStyleSheet;
107+
int mDuration; // 0 value indicates no timeout duration
103108
};
104109

105-
//! display a widget on the bar
106-
void pushWidget( QWidget *widget, const QString &styleSheet );
110+
void pushWidget( QWidget *widget, const QString &styleSheet, int duration = 0 );
107111

108112
void popItem( QgsMessageBarItem *item );
109113
void pushItem( QgsMessageBarItem *item );
@@ -115,10 +119,16 @@ class GUI_EXPORT QgsMessageBar: public QFrame
115119
QGridLayout *mLayout;
116120
QLabel *mItemCount;
117121
QAction *mActionCloseAll;
122+
QTimer *mCountdownTimer;
123+
QProgressBar *mCountProgress;
118124

119125
private slots:
120126
//! updates count of items in widget list
121127
void updateItemCount();
128+
129+
//! updates the countdown for widgets that have a timeout duration
130+
void updateCountdown();
131+
void resetCountdown();
122132
};
123133

124134
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.