QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  , mShowToolTips( true )
36 {
37  Q_UNUSED( statusBar )
38  setupUi( this );
39 
40  connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ),
41  this, SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
42 
43  connect( tabWidget, SIGNAL( tabCloseRequested( int ) ), this, SLOT( closeTab( int ) ) );
44 }
45 
47 {
48 }
49 
50 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
51 {
52 #ifdef ANDROID
53  mButton->setToolTip( tr( "Message(s) logged." ) );
54 #endif
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;
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  }
76 
77  QString prefix = QString( "%1\t%2\t" )
78  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ) )
79  .arg( level );
80  w->appendPlainText( message.prepend( prefix ).replace( "\n", "\n\t\t\t" ) );
81  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
82 }
83 
84 void QgsMessageLogViewer::closeTab( int index )
85 {
86  tabWidget->removeTab( index );
87 }
static unsigned index
QgsMessageLogViewer(QStatusBar *statusBar=0, QWidget *parent=0, Qt::WindowFlags fl=QgisGui::ModalDialogFlags)
void logMessage(QString message, QString tag, QgsMessageLog::MessageLevel level)
static QgsMessageLog * instance()
#define tr(sourceText)