25 #include <QStackedWidget> 26 #include <QProgressBar> 27 #include <QToolButton> 29 #include <QGridLayout> 31 #include <QMouseEvent> 38 QPalette
pal = palette();
39 pal.setBrush( backgroundRole(), pal.window() );
41 setAutoFillBackground(
true );
42 setFrameShape( QFrame::StyledPanel );
43 setFrameShadow( QFrame::Plain );
45 mLayout =
new QGridLayout(
this );
46 mLayout->setContentsMargins( 9, 1, 9, 1 );
49 mCountProgress =
new QProgressBar(
this );
50 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);" 51 " border-radius: 2px; background: rgba(0, 0, 0, 0);" 52 " image: url(:/images/themes/default/%1) }" 53 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
55 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
56 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
57 mCountProgress->setFixedSize( 25, 14 );
58 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
59 mCountProgress->setTextVisible(
false );
60 mCountProgress->setRange( 0, 5 );
61 mCountProgress->setHidden(
true );
62 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
64 mItemCount =
new QLabel(
this );
65 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
66 mItemCount->setToolTip( tr(
"Remaining messages" ) );
67 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
68 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
70 mCloseMenu =
new QMenu(
this );
71 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
72 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
73 mCloseMenu->addAction( mActionCloseAll );
76 mCloseBtn =
new QToolButton(
this );
77 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
78 mCloseBtn->setToolTip( tr(
"Close" ) );
79 mCloseBtn->setMinimumWidth( 40 );
80 mCloseBtn->setStyleSheet(
81 "QToolButton { background-color: rgba(0, 0, 0, 0); }" 82 "QToolButton::menu-button { background-color: rgba(0, 0, 0, 0); }" );
83 mCloseBtn->setCursor( Qt::PointingHandCursor );
85 mCloseBtn->setIconSize( QSize( 18, 18 ) );
86 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
87 mCloseBtn->setMenu( mCloseMenu );
88 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
90 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
92 mCountdownTimer =
new QTimer(
this );
93 mCountdownTimer->setInterval( 1000 );
94 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
105 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
107 if ( mCountdownTimer->isActive() )
109 mCountdownTimer->stop();
110 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerContinue.svg" ) ) );
114 mCountdownTimer->start();
115 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
124 if ( item != mCurrentItem && !mItems.contains( item ) )
127 if ( item == mCurrentItem )
131 QWidget *widget = mCurrentItem;
132 mLayout->removeWidget( widget );
133 mCurrentItem->hide();
135 mCurrentItem->deleteLater();
136 mCurrentItem =
nullptr;
139 if ( !mItems.isEmpty() )
141 showItem( mItems.at( 0 ) );
150 mItems.removeOne( item );
158 if ( !item || !mCurrentItem )
161 if ( item == mCurrentItem )
163 popItem( mCurrentItem );
169 if ( existingItem == item )
171 mItems.removeOne( existingItem );
172 existingItem->deleteLater();
195 if ( !mCurrentItem && mItems.empty() )
198 while ( !mItems.isEmpty() )
204 return !mCurrentItem && mItems.empty();
234 if ( item == mCurrentItem )
237 if ( mItems.contains( item ) )
238 mItems.removeOne( item );
242 mItems.prepend( mCurrentItem );
243 mLayout->removeWidget( mCurrentItem );
244 mCurrentItem->hide();
248 mLayout->addWidget( item, 0, 1, 1, 1 );
249 mCurrentItem->show();
253 mCountProgress->setRange( 0, item->
duration() );
254 mCountProgress->setValue( item->
duration() );
255 mCountProgress->setVisible(
true );
256 mCountdownTimer->start();
275 QString formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
317 void QgsMessageBar::updateCountdown()
319 if ( !mCountdownTimer->isActive() )
324 if ( mCountProgress->value() < 2 )
330 mCountProgress->setValue( mCountProgress->value() - 1 );
334 void QgsMessageBar::resetCountdown()
336 if ( mCountdownTimer->isActive() )
337 mCountdownTimer->stop();
339 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
340 mCountProgress->setVisible(
false );
343 void QgsMessageBar::updateItemCount()
345 mItemCount->setText( !mItems.isEmpty() ? tr(
"%n more",
"unread messages", mItems.count() ) : QString() );
348 mCloseBtn->setMenu( !mItems.isEmpty() ? mCloseMenu : nullptr );
349 mCloseBtn->setPopupMode( !mItems.isEmpty() ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
QgsMessageBar(QWidget *parent=nullptr)
Constructor for QgsMessageBar.
void mousePressEvent(QMouseEvent *e) override
void pushInfo(const QString &title, const QString &message)
Pushes a information message with default timeout to the message bar.
QString title() const
Returns the title for the message.
void widgetRemoved(QgsMessageBarItem *item)
emitted when a message widget was removed from the bar
QgsMessageBarItem * pushWidget(QWidget *widget, Qgis::MessageLevel level=Qgis::Info, int duration=0)
Display a widget as a message on the bar after hiding the currently visible one and putting it in a s...
A bar for displaying non-blocking messages to the user.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
bool clearWidgets()
Remove all items from the bar's widget list.
int duration() const
returns the duration in second of the message
MessageLevel
Level for messages This will be used both for message log and message bar in application.
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning)
add a message to the instance (and create it if necessary)
Qgis::MessageLevel level() const
Returns the message level for the message.
static QgsMessageBarItem * createMessage(const QString &text, QWidget *parent=nullptr)
make out a widget containing a message to be displayed on the bar
void pushWarning(const QString &title, const QString &message)
Pushes a warning with default timeout to the message bar.
QString text() const
Returns the text for the message.
void widgetAdded(QgsMessageBarItem *item)
emitted when a message widget is added to the bar
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
QString getStyleSheet()
returns the styleSheet
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar after hiding the currently visible one and putting it in a stack...
QgsMessageBarItem * setDuration(int duration)
void styleChanged(const QString &styleSheet)
emitted when the message level has changed
bool popWidget()
Remove the currently displayed widget from the bar and display the next in the stack if any or hide t...
QgsMessageBarItem * setLevel(Qgis::MessageLevel level)
void pushCritical(const QString &title, const QString &message)
Pushes a critical warning with default timeout to the message bar.