23 #include "qgssettings.h"
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:
224 QgsSettings settings;
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 (
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 );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
static const double UI_SCALE_FACTOR
UI scaling factor.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Represents an item shown within a QgsMessageBar widget.
void styleChanged(const QString &styleSheet)
Emitted when the item's message level has changed and the message bar style will need to be updated a...
QgsMessageBarItem * setLevel(Qgis::MessageLevel level)
Sets the message level for the item, which controls how the message bar is styled when the item is di...
int duration() const
Returns the duration (in seconds) of the message.
QString getStyleSheet()
Returns the styleSheet which should be used to style a QgsMessageBar object when this item is display...
QString text() const
Returns the text for the message.
QString title() const
Returns the title for the message.
Qgis::MessageLevel level() const
Returns the message level for the message.
QgsMessageBarItem * setDuration(int duration)
Sets the duration (in seconds) to show the message for.
A bar for displaying non-blocking messages to the user.
static int defaultMessageTimeout(Qgis::MessageLevel level=Qgis::MessageLevel::NoLevel)
Returns the default timeout in seconds for timed messages of the specified level.
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=-1)
A convenience method for pushing a message with the specified text to the bar.
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar, after hiding the currently visible one and putting it in a stack.
void widgetAdded(QgsMessageBarItem *item)
Emitted whenever an item is added to the bar.
QgsMessageBarItem * currentItem()
Returns the current visible item, or nullptr if no item is shown.
void widgetRemoved(QgsMessageBarItem *item)
Emitted whenever an item was removed from the bar.
QgsMessageBarItem * pushWidget(QWidget *widget, Qgis::MessageLevel level=Qgis::MessageLevel::Info, int duration=0)
Display a widget as a message on the bar, after hiding the currently visible one and putting it in a ...
static QgsMessageBarItem * createMessage(const QString &text, QWidget *parent=nullptr)
Creates message bar item widget containing a message text to be displayed on the bar.
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar.
QgsMessageBar(QWidget *parent=nullptr)
Constructor for QgsMessageBar.
bool popWidget()
Remove the currently displayed item from the bar and display the next item in the stack.
void pushCritical(const QString &title, const QString &message)
Pushes a critical warning message that must be manually dismissed by the user.
QList< QgsMessageBarItem * > items()
Returns a list of all items currently visible or queued for the bar.
bool clearWidgets()
Removes all items from the bar.
void pushInfo(const QString &title, const QString &message)
Pushes a information message with default timeout to the message bar.
void mousePressEvent(QMouseEvent *e) override
void pushWarning(const QString &title, const QString &message)
Pushes a warning message that must be manually dismissed by the user.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
A generic message view for displaying QGIS messages.
void setMessageAsPlainText(const QString &msg)
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...