QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  QPalette pal = qApp->palette();
82  QString defaultColorName = pal.color( QPalette::WindowText ).name();
83  QString colorName;
84  switch ( level )
85  {
86  case Qgis::Info:
87  levelString = QStringLiteral( "INFO" );
88  colorName = settings.value( QStringLiteral( "colors/info" ), QString() ).toString();
89  break;
90  case Qgis::Warning:
91  levelString = QStringLiteral( "WARNING" );
92  colorName = settings.value( QStringLiteral( "colors/warning" ), QString() ).toString();
93  break;
94  case Qgis::Critical:
95  levelString = QStringLiteral( "CRITICAL" );
96  colorName = settings.value( QStringLiteral( "colors/critical" ), QString() ).toString();
97  break;
98  case Qgis::Success:
99  levelString = QStringLiteral( "SUCCESS" );
100  colorName = settings.value( QStringLiteral( "colors/success" ), QString() ).toString();
101  break;
102  case Qgis::None:
103  levelString = QStringLiteral( "NONE" );
104  colorName = settings.value( QStringLiteral( "colors/default" ), QString() ).toString();
105  break;
106  }
107  QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
108 
109  QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
110  .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
111  QString cleanedMessage = message;
112  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
113  w->appendHtml( cleanedMessage );
114  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
115 }
116 
117 void QgsMessageLogViewer::closeTab( int index )
118 {
119  if ( tabWidget->count() == 1 )
120  qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
121  else
122  tabWidget->removeTab( index );
123 }
124 
125 bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
126 {
127  switch ( event->type() )
128  {
129  case QEvent::MouseButtonPress:
130  {
131  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
132  {
133  QMouseEvent *me = static_cast< QMouseEvent *>( event );
134  mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
135  QString();
136  if ( !mClickedAnchor.isEmpty() )
137  return true;
138  }
139  break;
140  }
141 
142  case QEvent::MouseButtonRelease:
143  {
144  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
145  {
146  QMouseEvent *me = static_cast< QMouseEvent *>( event );
147  QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
148  QString();
149  if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
150  {
151  QDesktopServices::openUrl( mClickedAnchor );
152  return true;
153  }
154  }
155  break;
156  }
157 
158  default:
159  break;
160  }
161 
162  return QDialog::eventFilter( object, event );
163 }
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