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(
61 "QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
62 " border-radius: 2px; background: rgba(0, 0, 0, 0);"
63 " image: url(:/images/themes/default/%1) }"
64 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }"
67 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerPause.svg"_L1 ) );
68 mCountProgress->setObjectName( u
"mCountdown"_s );
71 mCountProgress->setFixedSize( barWidth, barHeight );
72 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
73 mCountProgress->setTextVisible(
false );
74 mCountProgress->setRange( 0, 5 );
75 mCountProgress->setHidden(
true );
76 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
78 mItemCount =
new QLabel(
this );
79 mItemCount->setObjectName( u
"mItemCount"_s );
80 mItemCount->setToolTip( tr(
"Remaining messages" ) );
81 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
82 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
84 mCloseMenu =
new QMenu(
this );
85 mCloseMenu->setObjectName( u
"mCloseMenu"_s );
86 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
87 mCloseMenu->addAction( mActionCloseAll );
90 mCloseBtn =
new QToolButton(
this );
91 mCloseMenu->setObjectName( u
"mCloseMenu"_s );
92 mCloseBtn->setToolTip( tr(
"Close" ) );
94 mCloseBtn->setStyleSheet(
95 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
96 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }"
98 mCloseBtn->setCursor( Qt::PointingHandCursor );
102 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
103 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
104 mCloseBtn->setMenu( mCloseMenu );
105 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
107 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
109 mCountdownTimer =
new QTimer(
this );
110 mCountdownTimer->setInterval( 1000 );
111 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
122 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
124 if ( mCountdownTimer->isActive() )
126 mCountdownTimer->stop();
127 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerContinue.svg"_L1 ) );
131 mCountdownTimer->start();
132 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerPause.svg"_L1 ) );
141 if ( !mItems.contains( item ) )
144 if ( item == mItems.at( 0 ) )
146 mItems.removeOne( item );
147 mLayout->removeWidget( item );
152 if ( !mItems.isEmpty() )
154 showItem( mItems.at( 0 ) );
163 mItems.removeOne( item );
172 if ( !item || !mItems.contains( item ) )
181 if ( mItems.empty() )
186 popItem( mItems.at( 0 ) );
193 if ( mItems.empty() )
196 while ( !mItems.isEmpty() )
234 return settings.
value( u
"qgis/messageTimeout"_s, 5 ).toInt();
248 if ( !mItems.empty() )
251 if ( mItems.count() >= MAX_ITEMS )
252 removeLowestPriorityOldestItem();
254 if ( !mItems.empty() )
256 mLayout->removeWidget( mItems.at( 0 ) );
257 mItems.at( 0 )->hide();
260 if ( mItems.contains( item ) )
261 mItems.removeOne( item );
262 mItems.prepend( item );
264 mLayout->addWidget( item, 0, 1, 1, 1 );
269 mCountProgress->setRange( 0, item->
duration() );
270 mCountProgress->setValue( item->
duration() );
271 mCountProgress->setVisible(
true );
272 mCountdownTimer->start();
277 if ( item->
level() != mPrevLevel )
280 mPrevLevel = item->
level();
288void QgsMessageBar::removeLowestPriorityOldestItem()
292 for (
int i = mItems.size() - 1; i >= 0; --i )
294 QgsMessageBarItem *item = mItems.at( i );
295 if ( item->
level() == level )
308 item->mMessageBar =
this;
316 QString formattedTitle;
317 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
318 formattedTitle = u
"%1 : %2"_s.arg( item->
title(), item->
text() );
319 else if ( !item->
title().isEmpty() )
320 formattedTitle = item->
title();
321 else if ( !item->
text().isEmpty() )
322 formattedTitle = item->
text();
324 if ( !formattedTitle.isEmpty() )
347 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
349 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
360 mv->setWindowTitle( title );
363 QToolButton *showMoreButton =
new QToolButton();
364 QAction *act =
new QAction( showMoreButton );
365 act->setText( tr(
"Show more" ) );
366 showMoreButton->setStyleSheet( u
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;"_s );
367 showMoreButton->setCursor( Qt::PointingHandCursor );
368 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
369 showMoreButton->addAction( act );
370 showMoreButton->setDefaultAction( act );
371 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
372 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
385 return mItems.value( 0 );
414void QgsMessageBar::updateCountdown()
416 if ( !mCountdownTimer->isActive() )
421 if ( mCountProgress->value() < 2 )
427 mCountProgress->setValue( mCountProgress->value() - 1 );
431void QgsMessageBar::resetCountdown()
433 if ( mCountdownTimer->isActive() )
434 mCountdownTimer->stop();
436 mCountProgress->setStyleSheet( mCountStyleSheet.arg(
"mIconTimerPause.svg"_L1 ) );
437 mCountProgress->setVisible(
false );
440void QgsMessageBar::updateItemCount()
442 const bool moreMessages = mItems.count() > 1;
443 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
446 mCloseBtn->setMenu( moreMessages ? mCloseMenu :
nullptr );
447 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(), Qgis::StringFormat format=Qgis::StringFormat::PlainText)
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,...