19#include "moc_qgsmessagebar.cpp"
28#include <QStackedWidget>
29#include <QProgressBar>
41 QPalette
pal = palette();
42 pal.setBrush( backgroundRole(),
pal.window() );
44 setAutoFillBackground(
true );
45 setFrameShape( QFrame::StyledPanel );
46 setFrameShadow( QFrame::Plain );
48 mLayout =
new QGridLayout(
this );
51 mLayout->setContentsMargins( xMargin, yMargin, xMargin, yMargin );
54 mCountProgress =
new QProgressBar(
this );
55 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
56 " border-radius: 2px; background: rgba(0, 0, 0, 0);"
57 " image: url(:/images/themes/default/%1) }"
58 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
60 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
61 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
64 mCountProgress->setFixedSize( barWidth, barHeight );
65 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
66 mCountProgress->setTextVisible(
false );
67 mCountProgress->setRange( 0, 5 );
68 mCountProgress->setHidden(
true );
69 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
71 mItemCount =
new QLabel(
this );
72 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
73 mItemCount->setToolTip( tr(
"Remaining messages" ) );
74 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
75 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
77 mCloseMenu =
new QMenu(
this );
78 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
79 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
80 mCloseMenu->addAction( mActionCloseAll );
83 mCloseBtn =
new QToolButton(
this );
84 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
85 mCloseBtn->setToolTip( tr(
"Close" ) );
87 mCloseBtn->setStyleSheet(
88 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
89 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }" );
90 mCloseBtn->setCursor( Qt::PointingHandCursor );
94 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
95 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
96 mCloseBtn->setMenu( mCloseMenu );
97 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
99 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
101 mCountdownTimer =
new QTimer(
this );
102 mCountdownTimer->setInterval( 1000 );
103 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
114 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
116 if ( mCountdownTimer->isActive() )
118 mCountdownTimer->stop();
119 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerContinue.svg" ) ) );
123 mCountdownTimer->start();
124 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
133 if ( !mItems.contains( item ) )
136 if ( item == mItems.at( 0 ) )
138 mItems.removeOne( item );
139 mLayout->removeWidget( item );
144 if ( !mItems.isEmpty() )
146 showItem( mItems.at( 0 ) );
155 mItems.removeOne( item );
164 if ( !item || !mItems.contains( item ) )
173 if ( mItems.empty() )
178 popItem( mItems.at( 0 ) );
185 if ( mItems.empty() )
188 while ( !mItems.isEmpty() )
226 return settings.
value( QStringLiteral(
"qgis/messageTimeout" ), 5 ).toInt();
240 if ( !mItems.empty() )
243 if ( mItems.count() >= MAX_ITEMS )
244 removeLowestPriorityOldestItem();
246 if ( !mItems.empty() )
248 mLayout->removeWidget( mItems.at( 0 ) );
249 mItems.at( 0 )->hide();
252 if ( mItems.contains( item ) )
253 mItems.removeOne( item );
254 mItems.prepend( item );
256 mLayout->addWidget( item, 0, 1, 1, 1 );
261 mCountProgress->setRange( 0, item->
duration() );
262 mCountProgress->setValue( item->
duration() );
263 mCountProgress->setVisible(
true );
264 mCountdownTimer->start();
269 if ( item->
level() != mPrevLevel )
272 mPrevLevel = item->
level();
280void QgsMessageBar::removeLowestPriorityOldestItem()
284 for (
int i = mItems.size() - 1; i >= 0; --i )
287 if ( item->
level() == level )
300 item->mMessageBar =
this;
308 QString formattedTitle;
309 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
310 formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
311 else if ( !item->
title().isEmpty() )
312 formattedTitle = item->
title();
313 else if ( !item->
text().isEmpty() )
314 formattedTitle = item->
text();
316 if ( !formattedTitle.isEmpty() )
339 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
341 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
352 mv->setWindowTitle( title );
355 QToolButton *showMoreButton =
new QToolButton();
356 QAction *act =
new QAction( showMoreButton );
357 act->setText( tr(
"Show more" ) );
358 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
359 showMoreButton->setCursor( Qt::PointingHandCursor );
360 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
361 showMoreButton->addAction( act );
362 showMoreButton->setDefaultAction( act );
363 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
364 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
382 return mItems.value( 0 );
411void QgsMessageBar::updateCountdown()
413 if ( !mCountdownTimer->isActive() )
418 if ( mCountProgress->value() < 2 )
424 mCountProgress->setValue( mCountProgress->value() - 1 );
428void QgsMessageBar::resetCountdown()
430 if ( mCountdownTimer->isActive() )
431 mCountdownTimer->stop();
433 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
434 mCountProgress->setVisible(
false );
437void QgsMessageBar::updateItemCount()
439 const bool moreMessages = mItems.count() > 1;
440 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
443 mCloseBtn->setMenu( moreMessages ? mCloseMenu : nullptr );
444 mCloseBtn->setPopupMode( moreMessages ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
@ Warning
Warning message.
@ Critical
Critical/error message.
@ Info
Information message.
@ Success
Used for reporting a successful operation.
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)
This class is a composition of two QSettings instances:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
int scaleIconSize(int standardSize)
Scales an icon size to compensate for display pixel density, making the icon size hi-dpi friendly,...