27 #include <QStackedWidget>
28 #include <QProgressBar>
29 #include <QToolButton>
31 #include <QGridLayout>
33 #include <QMouseEvent>
40 QPalette
pal = palette();
41 pal.setBrush( backgroundRole(),
pal.window() );
43 setAutoFillBackground(
true );
44 setFrameShape( QFrame::StyledPanel );
45 setFrameShadow( QFrame::Plain );
47 mLayout =
new QGridLayout(
this );
50 mLayout->setContentsMargins( xMargin, yMargin, xMargin, yMargin );
53 mCountProgress =
new QProgressBar(
this );
54 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
55 " border-radius: 2px; background: rgba(0, 0, 0, 0);"
56 " image: url(:/images/themes/default/%1) }"
57 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
59 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
60 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
63 mCountProgress->setFixedSize( barWidth, barHeight );
64 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
65 mCountProgress->setTextVisible(
false );
66 mCountProgress->setRange( 0, 5 );
67 mCountProgress->setHidden(
true );
68 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
70 mItemCount =
new QLabel(
this );
71 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
72 mItemCount->setToolTip( tr(
"Remaining messages" ) );
73 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
74 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
76 mCloseMenu =
new QMenu(
this );
77 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
78 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
79 mCloseMenu->addAction( mActionCloseAll );
82 mCloseBtn =
new QToolButton(
this );
83 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
84 mCloseBtn->setToolTip( tr(
"Close" ) );
86 mCloseBtn->setStyleSheet(
87 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
88 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }" );
89 mCloseBtn->setCursor( Qt::PointingHandCursor );
94 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
95 mCloseBtn->setMenu( mCloseMenu );
96 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
98 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
100 mCountdownTimer =
new QTimer(
this );
101 mCountdownTimer->setInterval( 1000 );
102 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
113 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
115 if ( mCountdownTimer->isActive() )
117 mCountdownTimer->stop();
118 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerContinue.svg" ) ) );
122 mCountdownTimer->start();
123 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
132 if ( !mItems.contains( item ) )
135 if ( item == mItems.at( 0 ) )
137 mItems.removeOne( item );
138 mLayout->removeWidget( item );
143 if ( !mItems.isEmpty() )
145 showItem( mItems.at( 0 ) );
154 mItems.removeOne( item );
163 if ( !item || !mItems.contains( item ) )
172 if ( mItems.empty() )
177 popItem( mItems.at( 0 ) );
184 if ( mItems.empty() )
187 while ( !mItems.isEmpty() )
197 pushMessage( title, message, Qgis::MessageLevel::Success );
202 pushMessage( title, message, Qgis::MessageLevel::Info );
207 pushMessage( title, message, Qgis::MessageLevel::Warning );
212 pushMessage( title, message, Qgis::MessageLevel::Critical );
220 case Qgis::MessageLevel::Success:
221 case Qgis::MessageLevel::Info:
222 case Qgis::MessageLevel::NoLevel:
225 return settings.
value( QStringLiteral(
"qgis/messageTimeout" ), 5 ).toInt();
228 case Qgis::MessageLevel::Warning:
229 case Qgis::MessageLevel::Critical:
239 if ( !mItems.empty() )
242 if ( mItems.count() >= MAX_ITEMS )
243 removeLowestPriorityOldestItem();
245 if ( !mItems.empty() )
247 mLayout->removeWidget( mItems.at( 0 ) );
248 mItems.at( 0 )->hide();
251 if ( mItems.contains( item ) )
252 mItems.removeOne( item );
253 mItems.prepend( item );
255 mLayout->addWidget( item, 0, 1, 1, 1 );
260 mCountProgress->setRange( 0, item->
duration() );
261 mCountProgress->setValue( item->
duration() );
262 mCountProgress->setVisible(
true );
263 mCountdownTimer->start();
268 if ( item->
level() != mPrevLevel )
271 mPrevLevel = item->
level();
279 void QgsMessageBar::removeLowestPriorityOldestItem()
281 for (
const Qgis::MessageLevel level : { Qgis::MessageLevel::Success, Qgis::MessageLevel::Info, Qgis::MessageLevel::Warning, Qgis::MessageLevel::Critical } )
283 for (
int i = mItems.size() - 1; i >= 0; --i )
286 if ( item->
level() == level )
299 item->mMessageBar =
this;
307 QString formattedTitle;
308 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
309 formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
310 else if ( !item->
title().isEmpty() )
311 formattedTitle = item->
title();
312 else if ( !item->
text().isEmpty() )
313 formattedTitle = item->
text();
315 if ( !formattedTitle.isEmpty() )
338 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
340 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
351 mv->setWindowTitle( title );
354 QToolButton *showMoreButton =
new QToolButton();
355 QAction *act =
new QAction( showMoreButton );
356 act->setText( tr(
"Show more" ) );
357 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
358 showMoreButton->setCursor( Qt::PointingHandCursor );
359 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
360 showMoreButton->addAction( act );
361 showMoreButton->setDefaultAction( act );
362 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
363 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
381 return mItems.value( 0 );
397 return new QgsMessageBarItem( title, text, Qgis::MessageLevel::Info, 0, parent );
410 void QgsMessageBar::updateCountdown()
412 if ( !mCountdownTimer->isActive() )
417 if ( mCountProgress->value() < 2 )
423 mCountProgress->setValue( mCountProgress->value() - 1 );
427 void QgsMessageBar::resetCountdown()
429 if ( mCountdownTimer->isActive() )
430 mCountdownTimer->stop();
432 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
433 mCountProgress->setVisible(
false );
436 void QgsMessageBar::updateItemCount()
438 const bool moreMessages = mItems.count() > 1;
439 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
442 mCloseBtn->setMenu( moreMessages ? mCloseMenu :
nullptr );
443 mCloseBtn->setPopupMode( moreMessages ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );