QGIS API Documentation  2.99.0-Master (b8fd1fd)
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::closeEvent( QCloseEvent *e )
46 {
47  e->ignore();
48 }
49 
51 {
52 }
53 
54 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
55 {
56  if ( tag.isNull() )
57  tag = tr( "General" );
58 
59  int i;
60  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
61  ;
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, tag );
74  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
75  tabWidget->setTabsClosable( true );
76  }
77 
78  QString prefix = QStringLiteral( "%1\t%2\t" )
79  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ) )
80  .arg( level );
81  w->appendPlainText( message.prepend( prefix ).replace( '\n', QLatin1String( "\n\t\t\t" ) ) );
82  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
83 }
84 
85 void QgsMessageLogViewer::closeTab( int index )
86 {
87  tabWidget->removeTab( index );
88  tabWidget->setTabsClosable( tabWidget->count() > 1 );
89 }
static unsigned index
void closeEvent(QCloseEvent *e) override
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)