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 );
47 mLayout->setContentsMargins( 9, 1, 9, 1 );
50 mCountProgress =
new QProgressBar(
this );
51 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);" 52 " border-radius: 2px; background: rgba(0, 0, 0, 0);" 53 " image: url(:/images/themes/default/%1) }" 54 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
56 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
57 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
58 mCountProgress->setFixedSize( 25, 14 );
59 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
60 mCountProgress->setTextVisible(
false );
61 mCountProgress->setRange( 0, 5 );
62 mCountProgress->setHidden(
true );
63 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
65 mItemCount =
new QLabel(
this );
66 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
67 mItemCount->setToolTip( tr(
"Remaining messages" ) );
68 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
69 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
71 mCloseMenu =
new QMenu(
this );
72 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
73 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
74 mCloseMenu->addAction( mActionCloseAll );
77 mCloseBtn =
new QToolButton(
this );
78 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
79 mCloseBtn->setToolTip( tr(
"Close" ) );
80 mCloseBtn->setMinimumWidth( 40 );
81 mCloseBtn->setStyleSheet(
82 "QToolButton { background-color: rgba(0, 0, 0, 0); }" 83 "QToolButton::menu-button { background-color: rgba(0, 0, 0, 0); }" );
84 mCloseBtn->setCursor( Qt::PointingHandCursor );
86 mCloseBtn->setIconSize( QSize( 18, 18 ) );
87 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
88 mCloseBtn->setMenu( mCloseMenu );
89 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
91 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
93 mCountdownTimer =
new QTimer(
this );
94 mCountdownTimer->setInterval( 1000 );
95 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
106 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
108 if ( mCountdownTimer->isActive() )
110 mCountdownTimer->stop();
111 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerContinue.svg" ) ) );
115 mCountdownTimer->start();
116 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
125 if ( item != mCurrentItem && !mItems.contains( item ) )
128 if ( item == mCurrentItem )
132 QWidget *widget = mCurrentItem;
133 mLayout->removeWidget( widget );
134 mCurrentItem->hide();
136 mCurrentItem->deleteLater();
137 mCurrentItem =
nullptr;
140 if ( !mItems.isEmpty() )
142 showItem( mItems.at( 0 ) );
151 mItems.removeOne( item );
159 if ( !item || !mCurrentItem )
162 if ( item == mCurrentItem )
164 popItem( mCurrentItem );
170 if ( existingItem == item )
172 mItems.removeOne( existingItem );
173 existingItem->deleteLater();
196 if ( !mCurrentItem && mItems.empty() )
199 while ( !mItems.isEmpty() )
205 return !mCurrentItem && mItems.empty();
235 if ( item == mCurrentItem )
238 if ( mItems.contains( item ) )
239 mItems.removeOne( item );
243 mItems.prepend( mCurrentItem );
244 mLayout->removeWidget( mCurrentItem );
245 mCurrentItem->hide();
249 mLayout->addWidget( item, 0, 1, 1, 1 );
250 mCurrentItem->show();
254 mCountProgress->setRange( 0, item->
duration() );
255 mCountProgress->setValue( item->
duration() );
256 mCountProgress->setVisible(
true );
257 mCountdownTimer->start();
276 QString formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
305 mv->setWindowTitle( title );
308 QToolButton *showMoreButton =
new QToolButton();
309 QAction *act =
new QAction( showMoreButton );
310 act->setText( tr(
"Show more" ) );
311 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
312 showMoreButton->setCursor( Qt::PointingHandCursor );
313 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
314 showMoreButton->addAction( act );
315 showMoreButton->setDefaultAction( act );
316 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
317 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
344 void QgsMessageBar::updateCountdown()
346 if ( !mCountdownTimer->isActive() )
351 if ( mCountProgress->value() < 2 )
357 mCountProgress->setValue( mCountProgress->value() - 1 );
361 void QgsMessageBar::resetCountdown()
363 if ( mCountdownTimer->isActive() )
364 mCountdownTimer->stop();
366 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
367 mCountProgress->setVisible(
false );
370 void QgsMessageBar::updateItemCount()
372 mItemCount->setText( !mItems.isEmpty() ? tr(
"%n more",
"unread messages", mItems.count() ) : QString() );
375 mCloseBtn->setMenu( !mItems.isEmpty() ? mCloseMenu : nullptr );
376 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.
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.