QGIS API Documentation  2.99.0-Master (08c2e66)
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 
32 
33 QgsMessageLogViewer::QgsMessageLogViewer( QStatusBar *statusBar, QWidget *parent, Qt::WindowFlags fl )
34  : QDialog( parent, fl )
35 {
36  Q_UNUSED( statusBar )
37  setupUi( this );
38 
39  connect( QgsApplication::messageLog(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ),
40  this, SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
41 
42  connect( tabWidget, SIGNAL( tabCloseRequested( int ) ), this, SLOT( closeTab( int ) ) );
43 }
44 
45 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
46 {
47  if ( tag.isNull() )
48  tag = tr( "General" );
49 
50  int i;
51  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
52  ;
53 
54  QPlainTextEdit *w = nullptr;
55  if ( i < tabWidget->count() )
56  {
57  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
58  tabWidget->setCurrentIndex( i );
59  }
60  else
61  {
62  w = new QPlainTextEdit( this );
63  w->setReadOnly( true );
64  tabWidget->addTab( w, tag );
65  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
66  }
67 
68  QString prefix = QStringLiteral( "%1\t%2\t" )
69  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ) )
70  .arg( level );
71  w->appendPlainText( message.prepend( prefix ).replace( '\n', QLatin1String( "\n\t\t\t" ) ) );
72  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
73 }
74 
75 void QgsMessageLogViewer::closeTab( int index )
76 {
77  if ( tabWidget->count() > 1 )
78  tabWidget->removeTab( index );
79 }
static unsigned index
QgsMessageLogViewer(QStatusBar *statusBar=nullptr, QWidget *parent=nullptr, Qt::WindowFlags fl=QgisGui::ModalDialogFlags)
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
void logMessage(QString message, QString tag, QgsMessageLog::MessageLevel level)