26 #include <QStackedWidget>
27 #include <QProgressBar>
28 #include <QToolButton>
30 #include <QGridLayout>
32 #include <QMouseEvent>
39 QPalette
pal = palette();
40 pal.setBrush( backgroundRole(),
pal.window() );
42 setAutoFillBackground(
true );
43 setFrameShape( QFrame::StyledPanel );
44 setFrameShadow( QFrame::Plain );
46 mLayout =
new QGridLayout(
this );
49 mLayout->setContentsMargins( xMargin, yMargin, xMargin, yMargin );
52 mCountProgress =
new QProgressBar(
this );
53 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
54 " border-radius: 2px; background: rgba(0, 0, 0, 0);"
55 " image: url(:/images/themes/default/%1) }"
56 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
58 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
59 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
62 mCountProgress->setFixedSize( barWidth, barHeight );
63 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
64 mCountProgress->setTextVisible(
false );
65 mCountProgress->setRange( 0, 5 );
66 mCountProgress->setHidden(
true );
67 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
69 mItemCount =
new QLabel(
this );
70 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
71 mItemCount->setToolTip( tr(
"Remaining messages" ) );
72 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
73 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
75 mCloseMenu =
new QMenu(
this );
76 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
77 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
78 mCloseMenu->addAction( mActionCloseAll );
81 mCloseBtn =
new QToolButton(
this );
82 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
83 mCloseBtn->setToolTip( tr(
"Close" ) );
85 mCloseBtn->setStyleSheet(
86 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
87 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }" );
88 mCloseBtn->setCursor( Qt::PointingHandCursor );
93 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
94 mCloseBtn->setMenu( mCloseMenu );
95 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
97 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
99 mCountdownTimer =
new QTimer(
this );
100 mCountdownTimer->setInterval( 1000 );
101 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
112 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
114 if ( mCountdownTimer->isActive() )
116 mCountdownTimer->stop();
117 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerContinue.svg" ) ) );
121 mCountdownTimer->start();
122 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
131 if ( !mItems.contains( item ) )
134 if ( item == mItems.at( 0 ) )
136 mItems.removeOne( item );
137 mLayout->removeWidget( item );
142 if ( !mItems.isEmpty() )
144 showItem( mItems.at( 0 ) );
153 mItems.removeOne( item );
162 if ( !item || !mItems.contains( item ) )
171 if ( mItems.empty() )
176 popItem( mItems.at( 0 ) );
183 if ( mItems.empty() )
186 while ( !mItems.isEmpty() )
218 if ( !mItems.empty() )
221 if ( mItems.count() >= MAX_ITEMS )
222 removeLowestPriorityOldestItem();
224 if ( !mItems.empty() )
226 mLayout->removeWidget( mItems.at( 0 ) );
227 mItems.at( 0 )->hide();
230 if ( mItems.contains( item ) )
231 mItems.removeOne( item );
232 mItems.prepend( item );
234 mLayout->addWidget( item, 0, 1, 1, 1 );
239 mCountProgress->setRange( 0, item->
duration() );
240 mCountProgress->setValue( item->
duration() );
241 mCountProgress->setVisible(
true );
242 mCountdownTimer->start();
247 if ( item->
level() != mPrevLevel )
250 mPrevLevel = item->
level();
258 void QgsMessageBar::removeLowestPriorityOldestItem()
262 for (
int i = mItems.size() - 1; i >= 0; --i )
265 if ( item->
level() == level )
278 item->mMessageBar =
this;
286 QString formattedTitle;
287 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
288 formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
289 else if ( !item->
title().isEmpty() )
290 formattedTitle = item->
title();
291 else if ( !item->
text().isEmpty() )
292 formattedTitle = item->
text();
294 if ( !formattedTitle.isEmpty() )
317 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
319 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
330 mv->setWindowTitle( title );
333 QToolButton *showMoreButton =
new QToolButton();
334 QAction *act =
new QAction( showMoreButton );
335 act->setText( tr(
"Show more" ) );
336 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
337 showMoreButton->setCursor( Qt::PointingHandCursor );
338 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
339 showMoreButton->addAction( act );
340 showMoreButton->setDefaultAction( act );
341 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
342 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
355 return mItems.value( 0 );
384 void QgsMessageBar::updateCountdown()
386 if ( !mCountdownTimer->isActive() )
391 if ( mCountProgress->value() < 2 )
397 mCountProgress->setValue( mCountProgress->value() - 1 );
401 void QgsMessageBar::resetCountdown()
403 if ( mCountdownTimer->isActive() )
404 mCountdownTimer->stop();
406 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
407 mCountProgress->setVisible(
false );
410 void QgsMessageBar::updateItemCount()
412 const bool moreMessages = mItems.count() > 1;
413 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
416 mCloseBtn->setMenu( moreMessages ? mCloseMenu :
nullptr );
417 mCloseBtn->setPopupMode( moreMessages ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );