31#include <QProgressBar>
32#include <QStackedWidget>
37#include "moc_qgsmessagebar.cpp"
43 QPalette
pal = palette();
44 pal.setBrush( backgroundRole(),
pal.window() );
46 setAutoFillBackground(
true );
47 setFrameShape( QFrame::StyledPanel );
48 setFrameShadow( QFrame::Plain );
50 mLayout =
new QGridLayout(
this );
53 mLayout->setContentsMargins( xMargin, yMargin, xMargin, yMargin );
56 mCountProgress =
new QProgressBar(
this );
57 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);"
58 " border-radius: 2px; background: rgba(0, 0, 0, 0);"
59 " image: url(:/images/themes/default/%1) }"
60 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
62 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
63 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
66 mCountProgress->setFixedSize( barWidth, barHeight );
67 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
68 mCountProgress->setTextVisible(
false );
69 mCountProgress->setRange( 0, 5 );
70 mCountProgress->setHidden(
true );
71 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
73 mItemCount =
new QLabel(
this );
74 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
75 mItemCount->setToolTip( tr(
"Remaining messages" ) );
76 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
77 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
79 mCloseMenu =
new QMenu(
this );
80 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
81 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
82 mCloseMenu->addAction( mActionCloseAll );
85 mCloseBtn =
new QToolButton(
this );
86 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
87 mCloseBtn->setToolTip( tr(
"Close" ) );
89 mCloseBtn->setStyleSheet(
90 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }"
91 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }"
93 mCloseBtn->setCursor( Qt::PointingHandCursor );
97 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
98 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
99 mCloseBtn->setMenu( mCloseMenu );
100 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
102 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
104 mCountdownTimer =
new QTimer(
this );
105 mCountdownTimer->setInterval( 1000 );
106 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
117 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
119 if ( mCountdownTimer->isActive() )
121 mCountdownTimer->stop();
122 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerContinue.svg" ) ) );
126 mCountdownTimer->start();
127 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
136 if ( !mItems.contains( item ) )
139 if ( item == mItems.at( 0 ) )
141 mItems.removeOne( item );
142 mLayout->removeWidget( item );
147 if ( !mItems.isEmpty() )
149 showItem( mItems.at( 0 ) );
158 mItems.removeOne( item );
167 if ( !item || !mItems.contains( item ) )
176 if ( mItems.empty() )
181 popItem( mItems.at( 0 ) );
188 if ( mItems.empty() )
191 while ( !mItems.isEmpty() )
229 return settings.
value( QStringLiteral(
"qgis/messageTimeout" ), 5 ).toInt();
243 if ( !mItems.empty() )
246 if ( mItems.count() >= MAX_ITEMS )
247 removeLowestPriorityOldestItem();
249 if ( !mItems.empty() )
251 mLayout->removeWidget( mItems.at( 0 ) );
252 mItems.at( 0 )->hide();
255 if ( mItems.contains( item ) )
256 mItems.removeOne( item );
257 mItems.prepend( item );
259 mLayout->addWidget( item, 0, 1, 1, 1 );
264 mCountProgress->setRange( 0, item->
duration() );
265 mCountProgress->setValue( item->
duration() );
266 mCountProgress->setVisible(
true );
267 mCountdownTimer->start();
272 if ( item->
level() != mPrevLevel )
275 mPrevLevel = item->
level();
283void QgsMessageBar::removeLowestPriorityOldestItem()
287 for (
int i = mItems.size() - 1; i >= 0; --i )
289 QgsMessageBarItem *item = mItems.at( i );
290 if ( item->
level() == level )
303 item->mMessageBar =
this;
311 QString formattedTitle;
312 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
313 formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
314 else if ( !item->
title().isEmpty() )
315 formattedTitle = item->
title();
316 else if ( !item->
text().isEmpty() )
317 formattedTitle = item->
text();
319 if ( !formattedTitle.isEmpty() )
342 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
344 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
355 mv->setWindowTitle( title );
358 QToolButton *showMoreButton =
new QToolButton();
359 QAction *act =
new QAction( showMoreButton );
360 act->setText( tr(
"Show more" ) );
361 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
362 showMoreButton->setCursor( Qt::PointingHandCursor );
363 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
364 showMoreButton->addAction( act );
365 showMoreButton->setDefaultAction( act );
366 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
367 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
386 return mItems.value( 0 );
415void QgsMessageBar::updateCountdown()
417 if ( !mCountdownTimer->isActive() )
422 if ( mCountProgress->value() < 2 )
428 mCountProgress->setValue( mCountProgress->value() - 1 );
432void QgsMessageBar::resetCountdown()
434 if ( mCountdownTimer->isActive() )
435 mCountdownTimer->stop();
437 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QLatin1String(
"mIconTimerPause.svg" ) ) );
438 mCountProgress->setVisible(
false );
441void QgsMessageBar::updateItemCount()
443 const bool moreMessages = mItems.count() > 1;
444 mItemCount->setText( moreMessages ? tr(
"%n more",
"unread messages", mItems.count() - 1 ) : QString() );
447 mCloseBtn->setMenu( moreMessages ? mCloseMenu :
nullptr );
448 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,...