QGIS API Documentation  3.6.0-Noosa (5873452)
qgsmessagelogviewer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmessagelogviewer.cpp - description
3  -------------------
4  begin : October 2011
5  copyright : (C) 2011 by Juergen E. Fischer
6  email : jef at norbit dot de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsmessagelogviewer.h"
19 #include "qgsmessagelog.h"
20 #include "qgssettings.h"
21 #include "qgsapplication.h"
22 #include "qgsdockwidget.h"
23 
24 #include <QFile>
25 #include <QDateTime>
26 #include <QTableWidget>
27 #include <QToolButton>
28 #include <QStatusBar>
29 #include <QToolTip>
30 #include <QPlainTextEdit>
31 #include <QScrollBar>
32 #include <QDebug>
33 #include <QDesktopServices>
34 
35 QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
36  : QDialog( parent, fl )
37 {
38  setupUi( this );
39 
40  connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLog::messageReceived ),
41  this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLogViewer::logMessage ) );
42 
43  connect( tabWidget, &QTabWidget::tabCloseRequested, this, &QgsMessageLogViewer::closeTab );
44 }
45 
46 void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
47 {
48  e->ignore();
49 }
50 
52 {
53 }
54 
55 void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
56 {
57  QString cleanedTag = tag;
58  if ( cleanedTag.isNull() )
59  cleanedTag = tr( "General" );
60 
61  int i;
62  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ );
63 
64  QPlainTextEdit *w = nullptr;
65  if ( i < tabWidget->count() )
66  {
67  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
68  tabWidget->setCurrentIndex( i );
69  }
70  else
71  {
72  w = new QPlainTextEdit( this );
73  w->setReadOnly( true );
74  w->viewport()->installEventFilter( this );
75  tabWidget->addTab( w, cleanedTag );
76  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
77  }
78 
79  QString levelString;
80  QgsSettings settings;
81  QColor color;
82  switch ( level )
83  {
84  case Qgis::Info:
85  levelString = QStringLiteral( "INFO" );
86  color = QColor( settings.value( QStringLiteral( "colors/info" ), QStringLiteral( "#000000" ) ).toString() );
87  break;
88  case Qgis::Warning:
89  levelString = QStringLiteral( "WARNING" );
90  color = QColor( settings.value( QStringLiteral( "colors/warning" ), QStringLiteral( "#000000" ) ).toString() );
91  break;
92  case Qgis::Critical:
93  levelString = QStringLiteral( "CRITICAL" );
94  color = QColor( settings.value( QStringLiteral( "colors/critical" ), QStringLiteral( "#000000" ) ).toString() );
95  break;
96  case Qgis::Success:
97  levelString = QStringLiteral( "SUCCESS" );
98  color = QColor( settings.value( QStringLiteral( "colors/success" ), QStringLiteral( "#000000" ) ).toString() );
99  break;
100  case Qgis::None:
101  levelString = QStringLiteral( "NONE" );
102  color = QColor( settings.value( QStringLiteral( "colors/default" ), QStringLiteral( "#000000" ) ).toString() );
103  break;
104  }
105 
106  QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
107  .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
108  QString cleanedMessage = message;
109  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
110  w->appendHtml( cleanedMessage );
111  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
112 }
113 
114 void QgsMessageLogViewer::closeTab( int index )
115 {
116  if ( tabWidget->count() == 1 )
117  qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
118  else
119  tabWidget->removeTab( index );
120 }
121 
122 bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
123 {
124  switch ( event->type() )
125  {
126  case QEvent::MouseButtonPress:
127  {
128  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
129  {
130  QMouseEvent *me = static_cast< QMouseEvent *>( event );
131  mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
132  QString();
133  if ( !mClickedAnchor.isEmpty() )
134  return true;
135  }
136  break;
137  }
138 
139  case QEvent::MouseButtonRelease:
140  {
141  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
142  {
143  QMouseEvent *me = static_cast< QMouseEvent *>( event );
144  QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
145  QString();
146  if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
147  {
148  QDesktopServices::openUrl( mClickedAnchor );
149  return true;
150  }
151  }
152  break;
153  }
154 
155  default:
156  break;
157  }
158 
159  return QDialog::eventFilter( object, event );
160 }
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to the viewer.
void closeEvent(QCloseEvent *e) override
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:66
bool eventFilter(QObject *obj, QEvent *ev) override
A generic dialog widget for displaying QGIS log messages.
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
QgsMessageLogViewer(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Create a new message log viewer.
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:38