QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
34 QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
35  : QDialog( parent, fl )
36 {
37  setupUi( this );
38 
39  connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLog::messageReceived ),
40  this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLogViewer::logMessage ) );
41 
42  connect( tabWidget, &QTabWidget::tabCloseRequested, this, &QgsMessageLogViewer::closeTab );
43 }
44 
45 void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
46 {
47  e->ignore();
48 }
49 
51 {
52 }
53 
54 void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
55 {
56  QString cleanedTag = tag;
57  if ( cleanedTag.isNull() )
58  cleanedTag = tr( "General" );
59 
60  int i;
61  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ );
62 
63  QPlainTextEdit *w = nullptr;
64  if ( i < tabWidget->count() )
65  {
66  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
67  tabWidget->setCurrentIndex( i );
68  }
69  else
70  {
71  w = new QPlainTextEdit( this );
72  w->setReadOnly( true );
73  tabWidget->addTab( w, cleanedTag );
74  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
75  }
76 
77  QString levelString;
78  QgsSettings settings;
79  QColor color;
80  switch ( level )
81  {
82  case Qgis::Info:
83  levelString = QStringLiteral( "INFO" );
84  color = QColor( settings.value( QStringLiteral( "colors/info" ), QStringLiteral( "#000000" ) ).toString() );
85  break;
86  case Qgis::Warning:
87  levelString = QStringLiteral( "WARNING" );
88  color = QColor( settings.value( QStringLiteral( "colors/warning" ), QStringLiteral( "#000000" ) ).toString() );
89  break;
90  case Qgis::Critical:
91  levelString = QStringLiteral( "CRITICAL" );
92  color = QColor( settings.value( QStringLiteral( "colors/critical" ), QStringLiteral( "#000000" ) ).toString() );
93  break;
94  case Qgis::Success:
95  levelString = QStringLiteral( "SUCCESS" );
96  color = QColor( settings.value( QStringLiteral( "colors/success" ), QStringLiteral( "#000000" ) ).toString() );
97  break;
98  case Qgis::None:
99  levelString = QStringLiteral( "NONE" );
100  color = QColor( settings.value( QStringLiteral( "colors/default" ), QStringLiteral( "#000000" ) ).toString() );
101  break;
102  }
103 
104  QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
105  .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
106  QString cleanedMessage = message;
107  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
108  w->appendHtml( cleanedMessage );
109  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
110 }
111 
112 void QgsMessageLogViewer::closeTab( int index )
113 {
114  if ( tabWidget->count() == 1 )
115  qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
116  else
117  tabWidget->removeTab( index );
118 }
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:78
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