QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
22 #include <QFile>
23 #include <QDateTime>
24 #include <QTableWidget>
25 #include <QToolButton>
26 #include <QStatusBar>
27 #include <QToolTip>
28 #include <QDockWidget>
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( QgsMessageLog::instance(), 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 
46 {
47 }
48 
49 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
50 {
51  if ( tag.isNull() )
52  tag = tr( "General" );
53 
54  int i;
55  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
56  ;
57 
58  QPlainTextEdit *w;
59  if ( i < tabWidget->count() )
60  {
61  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
62  tabWidget->setCurrentIndex( i );
63  }
64  else
65  {
66  w = new QPlainTextEdit( this );
67  w->setReadOnly( true );
68  tabWidget->addTab( w, tag );
69  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
70  }
71 
72  QString prefix = QString( "%1\t%2\t" )
73  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ) )
74  .arg( level );
75  w->appendPlainText( message.prepend( prefix ).replace( "\n", "\n\t\t\t" ) );
76  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
77 }
78 
79 void QgsMessageLogViewer::closeTab( int index )
80 {
81  tabWidget->removeTab( index );
82 }