Skip to content
Permalink
Browse files

Make URLS in message log clickable

  • Loading branch information
nyalldawson committed Jan 21, 2019
1 parent 9da842d commit dc7a4ce53c5120ae828ef711ea80d2a11e50ba9c
@@ -39,6 +39,8 @@ Logs a ``message`` to the viewer.

virtual void reject();

virtual bool eventFilter( QObject *obj, QEvent *ev );


};

@@ -30,6 +30,7 @@
#include <QPlainTextEdit>
#include <QScrollBar>
#include <QDebug>
#include <QDesktopServices>

QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
@@ -70,6 +71,7 @@ void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag
{
w = new QPlainTextEdit( this );
w->setReadOnly( true );
w->viewport()->installEventFilter( this );
tabWidget->addTab( w, cleanedTag );
tabWidget->setCurrentIndex( tabWidget->count() - 1 );
}
@@ -116,3 +118,43 @@ void QgsMessageLogViewer::closeTab( int index )
else
tabWidget->removeTab( index );
}

bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
{
switch ( event->type() )
{
case QEvent::MouseButtonPress:
{
if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
{
QMouseEvent *me = static_cast< QMouseEvent *>( event );
mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
QString();
if ( !mClickedAnchor.isEmpty() )
return true;
}
break;
}

case QEvent::MouseButtonRelease:
{
if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
{
QMouseEvent *me = static_cast< QMouseEvent *>( event );
QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
QString();
if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
{
QDesktopServices::openUrl( mClickedAnchor );
return true;
}
}
break;
}

default:
break;
}

return QDialog::eventFilter( object, event );
}
@@ -53,9 +53,14 @@ class GUI_EXPORT QgsMessageLogViewer: public QDialog, private Ui::QgsMessageLogV
protected:
void closeEvent( QCloseEvent *e ) override;
void reject() override;
bool eventFilter( QObject *obj, QEvent *ev ) override;

private slots:
void closeTab( int index );

private:

QString mClickedAnchor;
};

#endif

0 comments on commit dc7a4ce

Please sign in to comment.
You can’t perform that action at this time.