QGIS API Documentation  2.99.0-Master (585a4d3)
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 "qgsapplication.h"
21 #include "qgsdockwidget.h"
22 
23 #include <QFile>
24 #include <QDateTime>
25 #include <QTableWidget>
26 #include <QToolButton>
27 #include <QStatusBar>
28 #include <QToolTip>
29 #include <QPlainTextEdit>
30 #include <QScrollBar>
31 #include <QDebug>
32 
33 QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
34  : QDialog( parent, fl )
35 {
36  setupUi( this );
37 
38  connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLog::messageReceived ),
39  this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLogViewer::logMessage ) );
40 
41  connect( tabWidget, &QTabWidget::tabCloseRequested, this, &QgsMessageLogViewer::closeTab );
42 }
43 
44 void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
45 {
46  e->ignore();
47 }
48 
50 {
51 }
52 
53 void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
54 {
55  QString cleanedTag = tag;
56  if ( cleanedTag.isNull() )
57  cleanedTag = tr( "General" );
58 
59  int i;
60  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ );
61 
62  QPlainTextEdit *w = nullptr;
63  if ( i < tabWidget->count() )
64  {
65  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
66  tabWidget->setCurrentIndex( i );
67  }
68  else
69  {
70  w = new QPlainTextEdit( this );
71  w->setReadOnly( true );
72  tabWidget->addTab( w, cleanedTag );
73  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
74  }
75 
76  QString levelString;
77  switch ( level )
78  {
79  case Qgis::Info:
80  levelString = QStringLiteral( "INFO" );
81  break;
82  case Qgis::Warning:
83  levelString = QStringLiteral( "WARNING" );
84  break;
85  case Qgis::Critical:
86  levelString = QStringLiteral( "CRITICAL" );
87  break;
88  case Qgis::Success:
89  levelString = QStringLiteral( "SUCCESS" );
90  break;
91  case Qgis::None:
92  levelString = QStringLiteral( "NONE" );
93  break;
94  }
95 
96  QString prefix = QStringLiteral( "%1\t%2\t" )
97  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
98  QString cleanedMessage = message;
99  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "\n\t\t\t" ) );
100  w->appendPlainText( cleanedMessage );
101  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
102 }
103 
104 void QgsMessageLogViewer::closeTab( int index )
105 {
106  if ( tabWidget->count() == 1 )
107  qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
108  else
109  tabWidget->removeTab( index );
110 }
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
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