31#include <QProgressBar>
32#include <QStackedWidget>
38#include "moc_qgsmessagebar.cpp"
40using namespace Qt::StringLiterals;
46 QPalette
pal = palette();
47 pal.setBrush( backgroundRole(),
pal.window() );
49 setAutoFillBackground(
true );
50 setFrameShape( QFrame::StyledPanel );
51 setFrameShadow( QFrame::Plain );
53 mLayout =
new QGridLayout(
this );
56 mLayout->setContentsMargins( xMargin, yMargin, xMargin, yMargin );
59 mCountProgress =
new QProgressBar(
this );
60 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
61 " border-radius: 2px; background: rgba(0, 0, 0, 0);"
62 " image: url(:/images/themes/default/%1) }"
63 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
65 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerPause.svg"_L1 ) );
66 mCountProgress->setObjectName( u
"mCountdown"_s );
69 mCountProgress->setFixedSize( barWidth, barHeight );
70 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
71 mCountProgress->setTextVisible(
false );
72 mCountProgress->setRange( 0, 5 );
73 mCountProgress->setHidden(
true );
74 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
76 mItemCount =
new QLabel(
this );
77 mItemCount->setObjectName( u
"mItemCount"_s );
78 mItemCount->setToolTip( tr(
"Remaining messages" ) );
79 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
80 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
82 mCloseMenu =
new QMenu(
this );
83 mCloseMenu->setObjectName( u
"mCloseMenu"_s );
84 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
85 mCloseMenu->addAction( mActionCloseAll );
88 mCloseBtn =
new QToolButton(
this );
89 mCloseMenu->setObjectName( u
"mCloseMenu"_s );
90 mCloseBtn->setToolTip( tr(
"Close" ) );
92 mCloseBtn->setStyleSheet(
93 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
94 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }"
96 mCloseBtn->setCursor( Qt::PointingHandCursor );
100 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
101 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
102 mCloseBtn->setMenu( mCloseMenu );
103 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
105 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
107 mCountdownTimer =
new QTimer(
this );
108 mCountdownTimer->setInterval( 1000 );
109 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
120 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
122 if ( mCountdownTimer->isActive() )
124 mCountdownTimer->stop();
125 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerContinue.svg"_L1 ) );
129 mCountdownTimer->start();
130 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerPause.svg"_L1 ) );
139 if ( !mItems.contains( item ) )
142 if ( item == mItems.at( 0 ) )
144 mItems.removeOne( item );
145 mLayout->removeWidget( item );
150 if ( !mItems.isEmpty() )
152 showItem( mItems.at( 0 ) );
161 mItems.removeOne( item );
170 if ( !item || !mItems.contains( item ) )
179 if ( mItems.empty() )
184 popItem( mItems.at( 0 ) );
191 if ( mItems.empty() )
194 while ( !mItems.isEmpty() )
232 return settings.
value( u
"qgis/messageTimeout"_s, 5 ).toInt();
246 if ( !mItems.empty() )
249 if ( mItems.count() >= MAX_ITEMS )
250 removeLowestPriorityOldestItem();
252 if ( !mItems.empty() )
254 mLayout->removeWidget( mItems.at( 0 ) );
255 mItems.at( 0 )->hide();
258 if ( mItems.contains( item ) )
259 mItems.removeOne( item );
260 mItems.prepend( item );
262 mLayout->addWidget( item, 0, 1, 1, 1 );
267 mCountProgress->setRange( 0, item->
duration() );
268 mCountProgress->setValue( item->
duration() );
269 mCountProgress->setVisible(
true );
270 mCountdownTimer->start();
275 if ( item->
level() != mPrevLevel )
278 mPrevLevel = item->
level();
286void QgsMessageBar::removeLowestPriorityOldestItem()
290 for (
int i = mItems.size() - 1; i >= 0; --i )
292 QgsMessageBarItem *item = mItems.at( i );
293 if ( item->
level() == level )
306 item->mMessageBar =
this;
314 QString formattedTitle;
315 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
316 formattedTitle = u
"%1 : %2"_s.arg( item->
title(), item->
text() );
317 else if ( !item->
title().isEmpty() )
318 formattedTitle = item->
title();
319 else if ( !item->
text().isEmpty() )
320 formattedTitle = item->
text();
322 if ( !formattedTitle.isEmpty() )
345 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
347 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
358 mv->setWindowTitle( title );
361 QToolButton *showMoreButton =
new QToolButton();
362 QAction *act =
new QAction( showMoreButton );
363 act->setText( tr(
"Show more" ) );
364 showMoreButton->setStyleSheet( u
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;"_s );
365 showMoreButton->setCursor( Qt::PointingHandCursor );
366 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
367 showMoreButton->addAction( act );
368 showMoreButton->setDefaultAction( act );
369 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
370 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
389 return mItems.value( 0 );
418void QgsMessageBar::updateCountdown()
420 if ( !mCountdownTimer->isActive() )
425 if ( mCountProgress->value() < 2 )
431 mCountProgress->setValue( mCountProgress->value() - 1 );
435void QgsMessageBar::resetCountdown()
437 if ( mCountdownTimer->isActive() )
438 mCountdownTimer->stop();
440 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerPause.svg"_L1 ) );
441 mCountProgress->setVisible(
false );
444void QgsMessageBar::updateItemCount()
446 const bool moreMessages = mItems.count() > 1;
447 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
450 mCloseBtn->setMenu( moreMessages ? mCloseMenu :
nullptr );
451 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 text() const
Returns the text for the message.
QString title() const
Returns the title for the message.
QString getStyleSheet() const
Returns the styleSheet which should be used to style a QgsMessageBar object when this item is display...
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.
static int defaultMessageTimeout(Qgis::MessageLevel level=Qgis::MessageLevel::NoLevel)
Returns the default timeout in seconds for timed messages of the specified level.
bool popWidget(QgsMessageBarItem *item)
Remove the specified item from the bar, and display the next most recent one in the stack.
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, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
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)
Stores settings for use within QGIS.
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,...