Skip to content

Commit

Permalink
Make URLS in message log clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 21, 2019
1 parent 9da842d commit dc7a4ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/gui/auto_generated/qgsmessagelogviewer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ Logs a ``message`` to the viewer.

virtual void reject();

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


};

Expand Down
42 changes: 42 additions & 0 deletions src/gui/qgsmessagelogviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QPlainTextEdit>
#include <QScrollBar>
#include <QDebug>
#include <QDesktopServices>

QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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 );
}
5 changes: 5 additions & 0 deletions src/gui/qgsmessagelogviewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.