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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@




class QgsMessageBarItem : QWidget
{

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


};

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

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

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

Expand Down Expand Up @@ -111,28 +112,31 @@ void QgsMessageBarItem::writeContent()
// TITLE AND TEXT
if ( mTitle.isEmpty() && mText.isEmpty() )
{
if ( mTextEdit )
if ( mTextBrowser )
{
delete mTextEdit;
mTextEdit = nullptr;
delete mTextBrowser;
mTextBrowser = nullptr;
}
}
else
{
if ( !mTextEdit )
if ( !mTextBrowser )
{
mTextEdit = new QTextEdit( this );
mTextEdit->setObjectName( QStringLiteral( "textEdit" ) );
mTextEdit->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
mTextEdit->setReadOnly( true );
mTextEdit->setFrameShape( QFrame::NoFrame );
mTextBrowser = new QTextBrowser( this );
mTextBrowser->setObjectName( QStringLiteral( "textEdit" ) );
mTextBrowser->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
mTextBrowser->setReadOnly( true );
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
// 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; } "
"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::up-arrow,QScrollBar::down-arrow { color: rgb(0,0,0); } " );
mLayout->addWidget( mTextEdit );
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::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); } " );
mLayout->addWidget( mTextBrowser );
}
QString content = mText;
if ( !mTitle.isEmpty() )
Expand All @@ -143,7 +147,7 @@ void QgsMessageBarItem::writeContent()
t += QLatin1String( ": " );
content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
}
mTextEdit->setText( content );
mTextBrowser->setText( content );
}

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

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

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

class QTextBrowser;

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

private slots:

void urlClicked( const QUrl &url );

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

#endif // qgsmessagebaritem_H

0 comments on commit f2b70ff

Please sign in to comment.