QGIS API Documentation  2.99.0-Master (ae4d26a)
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 &, QgsMessageLog::MessageLevel )>( &QgsMessageLog::messageReceived ),
39  this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, QgsMessageLog::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, QgsMessageLog::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  tabWidget->setTabsClosable( true );
75  }
76 
77  QString levelString;
78  switch ( level )
79  {
81  levelString = QStringLiteral( "INFO" );
82  break;
84  levelString = QStringLiteral( "WARNING" );
85  break;
87  levelString = QStringLiteral( "CRITICAL" );
88  break;
90  levelString = QStringLiteral( "NONE" );
91  break;
92  }
93 
94  QString prefix = QStringLiteral( "%1\t%2\t" )
95  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
96  QString cleanedMessage = message;
97  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "\n\t\t\t" ) );
98  w->appendPlainText( cleanedMessage );
99  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
100 }
101 
102 void QgsMessageLogViewer::closeTab( int index )
103 {
104  tabWidget->removeTab( index );
105  tabWidget->setTabsClosable( tabWidget->count() > 1 );
106 }
void messageReceived(const QString &message, const QString &tag, QgsMessageLog::MessageLevel level)
void closeEvent(QCloseEvent *e) 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.
void logMessage(const QString &message, const QString &tag, QgsMessageLog::MessageLevel level)
Logs a message to the viewer.
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:37