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( QStringLiteral(
"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" ) );
84 mCloseBtn->setMinimumWidth( 40 );
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 );
92 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
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( QStringLiteral(
"mIconTimerContinue.svg" ) ) );
121 mCountdownTimer->start();
122 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
131 if ( item != mCurrentItem && !mItems.contains( item ) )
134 if ( item == mCurrentItem )
138 QWidget *widget = mCurrentItem;
139 mLayout->removeWidget( widget );
140 mCurrentItem->hide();
142 mCurrentItem->deleteLater();
143 mCurrentItem =
nullptr;
146 if ( !mItems.isEmpty() )
148 showItem( mItems.at( 0 ) );
157 mItems.removeOne( item );
165 if ( !item || !mCurrentItem )
168 if ( item == mCurrentItem )
170 popItem( mCurrentItem );
176 if ( existingItem == item )
178 mItems.removeOne( existingItem );
179 existingItem->deleteLater();
202 if ( !mCurrentItem && mItems.empty() )
205 while ( !mItems.isEmpty() )
211 return !mCurrentItem && mItems.empty();
241 if ( item == mCurrentItem )
244 if ( mItems.contains( item ) )
245 mItems.removeOne( item );
249 mItems.prepend( mCurrentItem );
250 mLayout->removeWidget( mCurrentItem );
251 mCurrentItem->hide();
255 mLayout->addWidget( item, 0, 1, 1, 1 );
256 mCurrentItem->show();
260 mCountProgress->setRange( 0, item->
duration() );
261 mCountProgress->setValue( item->
duration() );
262 mCountProgress->setVisible(
true );
263 mCountdownTimer->start();
277 item->mMessageBar =
this;
285 QString formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
308 if ( mItems.count() > 20 )
309 mItems.removeFirst();
312 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
314 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
325 mv->setWindowTitle( title );
328 QToolButton *showMoreButton =
new QToolButton();
329 QAction *act =
new QAction( showMoreButton );
330 act->setText( tr(
"Show more" ) );
331 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
332 showMoreButton->setCursor( Qt::PointingHandCursor );
333 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
334 showMoreButton->addAction( act );
335 showMoreButton->setDefaultAction( act );
336 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
337 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
364 void QgsMessageBar::updateCountdown()
366 if ( !mCountdownTimer->isActive() )
371 if ( mCountProgress->value() < 2 )
377 mCountProgress->setValue( mCountProgress->value() - 1 );
381 void QgsMessageBar::resetCountdown()
383 if ( mCountdownTimer->isActive() )
384 mCountdownTimer->stop();
386 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
387 mCountProgress->setVisible(
false );
390 void QgsMessageBar::updateItemCount()
392 mItemCount->setText( !mItems.isEmpty() ? tr(
"%n more",
"unread messages", mItems.count() ) : QString() );
395 mCloseBtn->setMenu( !mItems.isEmpty() ? mCloseMenu : nullptr );
396 mCloseBtn->setPopupMode( !mItems.isEmpty() ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
QgsMessageBar(QWidget *parent=nullptr)
Constructor for QgsMessageBar.
void mousePressEvent(QMouseEvent *e) override
static const double UI_SCALE_FACTOR
UI scaling factor.
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.
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.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void setMessageAsPlainText(const QString &msg)
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)
A generic message view for displaying QGIS messages.
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.