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); }"
91 mCloseBtn->setCursor( Qt::PointingHandCursor );
95 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
96 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
97 mCloseBtn->setMenu( mCloseMenu );
98 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
100 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
102 mCountdownTimer =
new QTimer(
this );
103 mCountdownTimer->setInterval( 1000 );
104 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
115 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
117 if ( mCountdownTimer->isActive() )
119 mCountdownTimer->stop();
120 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerContinue.svg" ) ) );
124 mCountdownTimer->start();
125 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
134 if ( !mItems.contains( item ) )
137 if ( item == mItems.at( 0 ) )
139 mItems.removeOne( item );
140 mLayout->removeWidget( item );
145 if ( !mItems.isEmpty() )
147 showItem( mItems.at( 0 ) );
156 mItems.removeOne( item );
165 if ( !item || !mItems.contains( item ) )
174 if ( mItems.empty() )
179 popItem( mItems.at( 0 ) );
186 if ( mItems.empty() )
189 while ( !mItems.isEmpty() )
227 return settings.
value( QStringLiteral(
"qgis/messageTimeout" ), 5 ).toInt();
241 if ( !mItems.empty() )
244 if ( mItems.count() >= MAX_ITEMS )
245 removeLowestPriorityOldestItem();
247 if ( !mItems.empty() )
249 mLayout->removeWidget( mItems.at( 0 ) );
250 mItems.at( 0 )->hide();
253 if ( mItems.contains( item ) )
254 mItems.removeOne( item );
255 mItems.prepend( item );
257 mLayout->addWidget( item, 0, 1, 1, 1 );
262 mCountProgress->setRange( 0, item->
duration() );
263 mCountProgress->setValue( item->
duration() );
264 mCountProgress->setVisible(
true );
265 mCountdownTimer->start();
270 if ( item->
level() != mPrevLevel )
273 mPrevLevel = item->
level();
281void QgsMessageBar::removeLowestPriorityOldestItem()
285 for (
int i = mItems.size() - 1; i >= 0; --i )
288 if ( item->
level() == level )
301 item->mMessageBar =
this;
309 QString formattedTitle;
310 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
311 formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
312 else if ( !item->
title().isEmpty() )
313 formattedTitle = item->
title();
314 else if ( !item->
text().isEmpty() )
315 formattedTitle = item->
text();
317 if ( !formattedTitle.isEmpty() )
340 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
342 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
353 mv->setWindowTitle( title );
356 QToolButton *showMoreButton =
new QToolButton();
357 QAction *act =
new QAction( showMoreButton );
358 act->setText( tr(
"Show more" ) );
359 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
360 showMoreButton->setCursor( Qt::PointingHandCursor );
361 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
362 showMoreButton->addAction( act );
363 showMoreButton->setDefaultAction( act );
364 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
365 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
384 return mItems.value( 0 );
413void QgsMessageBar::updateCountdown()
415 if ( !mCountdownTimer->isActive() )
420 if ( mCountProgress->value() < 2 )
426 mCountProgress->setValue( mCountProgress->value() - 1 );
430void QgsMessageBar::resetCountdown()
432 if ( mCountdownTimer->isActive() )
433 mCountdownTimer->stop();
435 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
436 mCountProgress->setVisible(
false );
439void QgsMessageBar::updateItemCount()
441 const bool moreMessages = mItems.count() > 1;
442 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
445 mCloseBtn->setMenu( moreMessages ? mCloseMenu : nullptr );
446 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,...