Skip to content

Commit

Permalink
Allow clickable links in message bar text
Browse files Browse the repository at this point in the history
Links are opened using QDesktopServices::openUrl, i.e. the
default OS handler for that link type
  • Loading branch information
nyalldawson committed Dec 15, 2017
1 parent 905a147 commit f2b70ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion python/gui/qgsmessagebaritem.sip
Expand Up @@ -9,6 +9,7 @@







class QgsMessageBarItem : QWidget class QgsMessageBarItem : QWidget
{ {


Expand Down Expand Up @@ -114,7 +115,6 @@ returns the styleSheet
emitted when the message level has changed emitted when the message level has changed
%End %End



}; };


/************************************************************************ /************************************************************************
Expand Down
42 changes: 25 additions & 17 deletions src/gui/qgsmessagebaritem.cpp
Expand Up @@ -21,7 +21,8 @@


#include <QHBoxLayout> #include <QHBoxLayout>
#include <QLabel> #include <QLabel>
#include <QTextEdit> #include <QTextBrowser>
#include <QDesktopServices>


QgsMessageBarItem::QgsMessageBarItem( const QString &text, QgsMessageBar::MessageLevel level, int duration, QWidget *parent ) QgsMessageBarItem::QgsMessageBarItem( const QString &text, QgsMessageBar::MessageLevel level, int duration, QWidget *parent )
: QWidget( parent ) : QWidget( parent )
Expand Down Expand Up @@ -72,7 +73,7 @@ void QgsMessageBarItem::writeContent()
{ {
mLayout = new QHBoxLayout( this ); mLayout = new QHBoxLayout( this );
mLayout->setContentsMargins( 0, 0, 0, 0 ); mLayout->setContentsMargins( 0, 0, 0, 0 );
mTextEdit = nullptr; mTextBrowser = nullptr;
mLblIcon = nullptr; mLblIcon = nullptr;
} }


Expand Down Expand Up @@ -111,28 +112,31 @@ void QgsMessageBarItem::writeContent()
// TITLE AND TEXT // TITLE AND TEXT
if ( mTitle.isEmpty() && mText.isEmpty() ) if ( mTitle.isEmpty() && mText.isEmpty() )
{ {
if ( mTextEdit ) if ( mTextBrowser )
{ {
delete mTextEdit; delete mTextBrowser;
mTextEdit = nullptr; mTextBrowser = nullptr;
} }
} }
else else
{ {
if ( !mTextEdit ) if ( !mTextBrowser )
{ {
mTextEdit = new QTextEdit( this ); mTextBrowser = new QTextBrowser( this );
mTextEdit->setObjectName( QStringLiteral( "textEdit" ) ); mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum ); mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
mTextEdit->setReadOnly( true ); mTextBrowser->setReadOnly( true );
mTextEdit->setFrameShape( QFrame::NoFrame ); mTextBrowser->setOpenLinks( false );
connect( mTextBrowser, &QTextBrowser::anchorClicked, this, &QgsMessageBarItem::urlClicked );

mTextBrowser->setFrameShape( QFrame::NoFrame );
// stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height // stylesheet set here so Qt-style substitued scrollbar arrows can show within limited height
// adjusts to height of font set in app options // adjusts to height of font set in app options
mTextEdit->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } " mTextBrowser->setStyleSheet( "QTextEdit { background-color: rgba(0,0,0,0); margin-top: 0.25em; max-height: 1.75em; min-height: 1.75em; } "
"QScrollBar { background-color: rgba(0,0,0,0); } " "QScrollBar { background-color: rgba(0,0,0,0); } "
"QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } " "QScrollBar::add-page,QScrollBar::sub-page,QScrollBar::handle { background-color: rgba(0,0,0,0); color: rgba(0,0,0,0); } "
"QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " ); "QScrollBar::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
mLayout->addWidget( mTextEdit ); mLayout->addWidget( mTextBrowser );
} }
QString content = mText; QString content = mText;
if ( !mTitle.isEmpty() ) if ( !mTitle.isEmpty() )
Expand All @@ -143,7 +147,7 @@ void QgsMessageBarItem::writeContent()
t += QLatin1String( ": " ); t += QLatin1String( ": " );
content.prepend( QStringLiteral( "<b>" ) + t + " </b>" ); content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
} }
mTextEdit->setText( content ); mTextBrowser->setText( content );
} }


// WIDGET // WIDGET
Expand Down Expand Up @@ -256,3 +260,7 @@ QgsMessageBarItem *QgsMessageBarItem::setDuration( int duration )
return this; return this;
} }


void QgsMessageBarItem::urlClicked( const QUrl &url )
{
QDesktopServices::openUrl( url );
}
8 changes: 6 additions & 2 deletions src/gui/qgsmessagebaritem.h
Expand Up @@ -23,10 +23,11 @@


#include <QWidget> #include <QWidget>
#include <QIcon> #include <QIcon>
#include <QTextEdit>
#include <QHBoxLayout> #include <QHBoxLayout>
#include "qgis_gui.h" #include "qgis_gui.h"


class QTextBrowser;

/** /**
* \ingroup gui * \ingroup gui
* \class QgsMessageBarItem * \class QgsMessageBarItem
Expand Down Expand Up @@ -94,6 +95,9 @@ class GUI_EXPORT QgsMessageBarItem : public QWidget
//! emitted when the message level has changed //! emitted when the message level has changed
void styleChanged( const QString &styleSheet ); void styleChanged( const QString &styleSheet );


private slots:

void urlClicked( const QUrl &url );


private: private:
void writeContent(); void writeContent();
Expand All @@ -107,7 +111,7 @@ class GUI_EXPORT QgsMessageBarItem : public QWidget
QHBoxLayout *mLayout = nullptr; QHBoxLayout *mLayout = nullptr;
QLabel *mLblIcon = nullptr; QLabel *mLblIcon = nullptr;
QString mStyleSheet; QString mStyleSheet;
QTextEdit *mTextEdit = nullptr; QTextBrowser *mTextBrowser = nullptr;
}; };


#endif // qgsmessagebaritem_H #endif // qgsmessagebaritem_H

0 comments on commit f2b70ff

Please sign in to comment.