26 #include <QStackedWidget> 27 #include <QProgressBar> 28 #include <QToolButton> 30 #include <QGridLayout> 32 #include <QMouseEvent> 39 QPalette
pal = palette();
40 pal.setBrush( backgroundRole(), pal.window() );
42 setAutoFillBackground(
true );
43 setFrameShape( QFrame::StyledPanel );
44 setFrameShadow( QFrame::Plain );
46 mLayout =
new QGridLayout(
this );
49 mLayout->setContentsMargins( xMargin, yMargin, xMargin, yMargin );
52 mCountProgress =
new QProgressBar(
this );
53 mCountStyleSheet = QString(
"QProgressBar { border: 1px solid rgba(0, 0, 0, 75%);" 54 " border-radius: 2px; background: rgba(0, 0, 0, 0);" 55 " image: url(:/images/themes/default/%1) }" 56 "QProgressBar::chunk { background-color: rgba(0, 0, 0, 30%); width: 5px; }" );
58 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
59 mCountProgress->setObjectName( QStringLiteral(
"mCountdown" ) );
62 mCountProgress->setFixedSize( barWidth, barHeight );
63 mCountProgress->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
64 mCountProgress->setTextVisible(
false );
65 mCountProgress->setRange( 0, 5 );
66 mCountProgress->setHidden(
true );
67 mLayout->addWidget( mCountProgress, 0, 0, 1, 1 );
69 mItemCount =
new QLabel(
this );
70 mItemCount->setObjectName( QStringLiteral(
"mItemCount" ) );
71 mItemCount->setToolTip( tr(
"Remaining messages" ) );
72 mItemCount->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
73 mLayout->addWidget( mItemCount, 0, 2, 1, 1 );
75 mCloseMenu =
new QMenu(
this );
76 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
77 mActionCloseAll =
new QAction( tr(
"Close All" ),
this );
78 mCloseMenu->addAction( mActionCloseAll );
81 mCloseBtn =
new QToolButton(
this );
82 mCloseMenu->setObjectName( QStringLiteral(
"mCloseMenu" ) );
83 mCloseBtn->setToolTip( tr(
"Close" ) );
84 mCloseBtn->setMinimumWidth( 40 );
85 mCloseBtn->setStyleSheet(
86 "QToolButton { border:none; background-color: rgba(0, 0, 0, 0); }" 87 "QToolButton::menu-button { border:none; background-color: rgba(0, 0, 0, 0); }" );
88 mCloseBtn->setCursor( Qt::PointingHandCursor );
92 mCloseBtn->setIconSize( QSize( iconSize, iconSize ) );
93 mCloseBtn->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Maximum );
94 mCloseBtn->setMenu( mCloseMenu );
95 mCloseBtn->setPopupMode( QToolButton::MenuButtonPopup );
97 mLayout->addWidget( mCloseBtn, 0, 3, 1, 1 );
99 mCountdownTimer =
new QTimer(
this );
100 mCountdownTimer->setInterval( 1000 );
101 connect( mCountdownTimer, &QTimer::timeout,
this, &QgsMessageBar::updateCountdown );
112 if ( mCountProgress == childAt( e->pos() ) && e->button() == Qt::LeftButton )
114 if ( mCountdownTimer->isActive() )
116 mCountdownTimer->stop();
117 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerContinue.svg" ) ) );
121 mCountdownTimer->start();
122 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
131 if ( item != mCurrentItem && !mItems.contains( item ) )
134 if ( item == mCurrentItem )
136 mLayout->removeWidget( mCurrentItem );
137 mCurrentItem->hide();
139 mCurrentItem->deleteLater();
140 mCurrentItem =
nullptr;
142 if ( !mItems.isEmpty() )
144 showItem( mItems.at( 0 ) );
153 mItems.removeOne( item );
161 if ( !item || !mCurrentItem )
164 if ( item == mCurrentItem )
166 popItem( mCurrentItem );
172 if ( existingItem == item )
174 mItems.removeOne( existingItem );
175 existingItem->deleteLater();
198 if ( !mCurrentItem && mItems.empty() )
201 while ( !mItems.isEmpty() )
207 return !mCurrentItem && mItems.empty();
237 if ( item == mCurrentItem )
240 if ( mItems.contains( item ) )
241 mItems.removeOne( item );
245 mItems.prepend( mCurrentItem );
246 mLayout->removeWidget( mCurrentItem );
247 mCurrentItem->hide();
251 mLayout->addWidget( item, 0, 1, 1, 1 );
252 mCurrentItem->show();
256 mCountProgress->setRange( 0, item->
duration() );
257 mCountProgress->setValue( item->
duration() );
258 mCountProgress->setVisible(
true );
259 mCountdownTimer->start();
273 item->mMessageBar =
this;
281 QString formattedTitle;
282 if ( !item->
title().isEmpty() && !item->
text().isEmpty() )
283 formattedTitle = QStringLiteral(
"%1 : %2" ).arg( item->
title(), item->
text() );
284 else if ( !item->
title().isEmpty() )
285 formattedTitle = item->
title();
286 else if ( !item->
text().isEmpty() )
287 formattedTitle = item->
text();
289 if ( !formattedTitle.isEmpty() )
312 if ( mItems.count() > 20 )
313 mItems.removeFirst();
316 for (
auto it = mItems.constBegin(); it != mItems.constEnd(); ++it )
318 if ( level == ( *it )->level() && title == ( *it )->title() && text == ( *it )->text() )
329 mv->setWindowTitle( title );
332 QToolButton *showMoreButton =
new QToolButton();
333 QAction *act =
new QAction( showMoreButton );
334 act->setText( tr(
"Show more" ) );
335 showMoreButton->setStyleSheet( QStringLiteral(
"background-color: rgba(255, 255, 255, 0); color: black; text-decoration: underline;" ) );
336 showMoreButton->setCursor( Qt::PointingHandCursor );
337 showMoreButton->setSizePolicy( QSizePolicy::Maximum, QSizePolicy::Preferred );
338 showMoreButton->addAction( act );
339 showMoreButton->setDefaultAction( act );
340 connect( showMoreButton, &QToolButton::triggered, mv, &QDialog::exec );
341 connect( showMoreButton, &QToolButton::triggered, showMoreButton, &QObject::deleteLater );
368 void QgsMessageBar::updateCountdown()
370 if ( !mCountdownTimer->isActive() )
375 if ( mCountProgress->value() < 2 )
381 mCountProgress->setValue( mCountProgress->value() - 1 );
385 void QgsMessageBar::resetCountdown()
387 if ( mCountdownTimer->isActive() )
388 mCountdownTimer->stop();
390 mCountProgress->setStyleSheet( mCountStyleSheet.arg( QStringLiteral(
"mIconTimerPause.svg" ) ) );
391 mCountProgress->setVisible(
false );
394 void QgsMessageBar::updateItemCount()
396 mItemCount->setText( !mItems.isEmpty() ? tr(
"%n more",
"unread messages", mItems.count() ) : QString() );
399 mCloseBtn->setMenu( !mItems.isEmpty() ? mCloseMenu : nullptr );
400 mCloseBtn->setPopupMode( !mItems.isEmpty() ? QToolButton::MenuButtonPopup : QToolButton::DelayedPopup );
QgsMessageBar(QWidget *parent=nullptr)
Constructor for QgsMessageBar.
void mousePressEvent(QMouseEvent *e) override
static const double UI_SCALE_FACTOR
UI scaling factor.
void pushInfo(const QString &title, const QString &message)
Pushes a information message with default timeout to the message bar.
QString title() const
Returns the title for the message.
void widgetRemoved(QgsMessageBarItem *item)
emitted when a message widget was removed from the bar
QgsMessageBarItem * pushWidget(QWidget *widget, Qgis::MessageLevel level=Qgis::Info, int duration=0)
Display a widget as a message on the bar after hiding the currently visible one and putting it in a s...
A bar for displaying non-blocking messages to the user.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
bool clearWidgets()
Remove all items from the bar's widget list.
int duration() const
returns the duration in second of the message
MessageLevel
Level for messages This will be used both for message log and message bar in application.
void pushSuccess(const QString &title, const QString &message)
Pushes a success message with default timeout to the message bar.
Qgis::MessageLevel level() const
Returns the message level for the message.
static QgsMessageBarItem * createMessage(const QString &text, QWidget *parent=nullptr)
make out a widget containing a message to be displayed on the bar
void pushWarning(const QString &title, const QString &message)
Pushes a warning with default timeout to the message bar.
QString text() const
Returns the text for the message.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::Warning, bool notifyUser=true)
Adds a message to the log instance (and creates it if necessary).
void setMessageAsPlainText(const QString &msg)
void widgetAdded(QgsMessageBarItem *item)
emitted when a message widget is added to the bar
void pushMessage(const QString &text, Qgis::MessageLevel level=Qgis::Info, int duration=5)
convenience method for pushing a message to the bar
QSize iconSize(bool dockableToolbar)
Returns the user-preferred size of a window's toolbar icons.
QString getStyleSheet()
returns the styleSheet
void pushItem(QgsMessageBarItem *item)
Display a message item on the bar after hiding the currently visible one and putting it in a stack...
QgsMessageBarItem * setDuration(int duration)
A generic message view for displaying QGIS messages.
void styleChanged(const QString &styleSheet)
emitted when the message level has changed
bool popWidget()
Remove the currently displayed widget from the bar and display the next in the stack if any or hide t...
QgsMessageBarItem * setLevel(Qgis::MessageLevel level)
void pushCritical(const QString &title, const QString &message)
Pushes a critical warning with default timeout to the message bar.