Skip to content
Permalink
Browse files

Allow clickable links in message bar text

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 f2b70ffdc67eb30e453a26a42d14fe0fa9535aa0
Showing with 32 additions and 20 deletions.
  1. +1 −1 python/gui/qgsmessagebaritem.sip
  2. +25 −17 src/gui/qgsmessagebaritem.cpp
  3. +6 −2 src/gui/qgsmessagebaritem.h
@@ -9,6 +9,7 @@




class QgsMessageBarItem : QWidget
{

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


};

/************************************************************************
@@ -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 )
@@ -72,7 +73,7 @@ void QgsMessageBarItem::writeContent()
{
mLayout = new QHBoxLayout( this );
mLayout->setContentsMargins( 0, 0, 0, 0 );
mTextEdit = nullptr;
mTextBrowser = nullptr;
mLblIcon = nullptr;
}

@@ -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() )
@@ -143,7 +147,7 @@ void QgsMessageBarItem::writeContent()
t += QLatin1String( ": " );
content.prepend( QStringLiteral( "<b>" ) + t + " </b>" );
}
mTextEdit->setText( content );
mTextBrowser->setText( content );
}

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

void QgsMessageBarItem::urlClicked( const QUrl &url )
{
QDesktopServices::openUrl( url );
}
@@ -23,10 +23,11 @@

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

class QTextBrowser;

/**
* \ingroup gui
* \class QgsMessageBarItem
@@ -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();
@@ -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.
You can’t perform that action at this time.