Skip to content

Commit

Permalink
Allow message bar items to be dismissed from QgsMessageBarItem
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 25, 2018
1 parent 0a02432 commit 124f738
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions python/gui/auto_generated/qgsmessagebaritem.sip.in
Expand Up @@ -82,6 +82,17 @@ returns the duration in second of the message
QString getStyleSheet();
%Docstring
returns the styleSheet
%End

public slots:

void dismiss();
%Docstring
Dismisses the item, removing it from the message bar and deleting
it. Calling this on items which have not been added to a message bar
has no effect.

.. versionadded:: 3.4
%End

signals:
Expand Down
3 changes: 3 additions & 0 deletions src/gui/qgsmessagebar.cpp
Expand Up @@ -267,6 +267,9 @@ void QgsMessageBar::showItem( QgsMessageBarItem *item )
void QgsMessageBar::pushItem( QgsMessageBarItem *item )
{
resetCountdown();

item->mMessageBar = this;

// avoid duplicated widget
popWidget( item );
showItem( item );
Expand Down
8 changes: 8 additions & 0 deletions src/gui/qgsmessagebaritem.cpp
Expand Up @@ -269,6 +269,14 @@ QgsMessageBarItem *QgsMessageBarItem::setDuration( int duration )
return this;
}

void QgsMessageBarItem::dismiss()
{
if ( !mMessageBar )
return;

mMessageBar->popWidget( this );
}

void QgsMessageBarItem::urlClicked( const QUrl &url )
{
QFileInfo file( url.toLocalFile() );
Expand Down
15 changes: 15 additions & 0 deletions src/gui/qgsmessagebaritem.h
Expand Up @@ -27,6 +27,7 @@

class QTextBrowser;
class QLabel;
class QgsMessageBar;

/**
* \ingroup gui
Expand Down Expand Up @@ -91,6 +92,17 @@ class GUI_EXPORT QgsMessageBarItem : public QWidget
//! returns the styleSheet
QString getStyleSheet() { return mStyleSheet; }

public slots:

/**
* Dismisses the item, removing it from the message bar and deleting
* it. Calling this on items which have not been added to a message bar
* has no effect.
*
* \since QGIS 3.4
*/
void dismiss();

signals:
//! emitted when the message level has changed
void styleChanged( const QString &styleSheet );
Expand All @@ -112,6 +124,9 @@ class GUI_EXPORT QgsMessageBarItem : public QWidget
QLabel *mLblIcon = nullptr;
QString mStyleSheet;
QTextBrowser *mTextBrowser = nullptr;
QgsMessageBar *mMessageBar = nullptr;

friend class QgsMessageBar;
};

#endif // qgsmessagebaritem_H

0 comments on commit 124f738

Please sign in to comment.