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 static QIcon icon( QString icon )
33 {
34  // try active theme
35  QString path = QgsApplication::activeThemePath();
36  if ( QFile::exists( path + icon ) )
37  path += icon;
38  else
40 
41  return QIcon( path );
42 }
43 
44 QgsMessageLogViewer::QgsMessageLogViewer( QStatusBar *statusBar, QWidget *parent, Qt::WindowFlags fl )
45  : QDialog( parent, fl )
46  , mButton( 0 )
47  , mShowToolTips( true )
48 {
49  setupUi( this );
50 
51  connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ),
52  this, SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
53 
54  if ( statusBar )
55  {
56  mButton = new QToolButton( parent );
57  mButton->setObjectName( "mMessageLogViewerButton" );
58  mButton->setMaximumWidth( 20 );
59  mButton->setMaximumHeight( 20 );
60  mButton->setIcon( icon( "/mIconWarn.png" ) );
61 #ifndef ANDROID
62  mButton->setToolTip( tr( "No messages." ) );
63 #endif
64  mButton->setCheckable( true );
65  connect( mButton, SIGNAL( toggled( bool ) ), this, SLOT( buttonToggled( bool ) ) );
66  connect( mButton, SIGNAL( destroyed() ), this, SLOT( buttonDestroyed() ) );
67  statusBar->addPermanentWidget( mButton, 0 );
68  }
69 
70  connect( tabWidget, SIGNAL( tabCloseRequested( int ) ), this, SLOT( closeTab( int ) ) );
71 }
72 
74 {
75 }
76 
77 void QgsMessageLogViewer::hideEvent( QHideEvent * )
78 {
79  if ( mButton )
80  {
81  mButton->setChecked( false );
82  }
83 }
84 
85 void QgsMessageLogViewer::showEvent( QShowEvent * )
86 {
87  if ( mButton )
88  {
89  mButton->setChecked( true );
90  }
91 }
92 
93 void QgsMessageLogViewer::buttonToggled( bool checked )
94 {
95  QWidget *w = qobject_cast<QDockWidget *>( parent() );
96 
97  if ( !w )
98  w = this;
99 
100  if ( checked )
101  w->show();
102  else
103  w->hide();
104 }
105 
106 void QgsMessageLogViewer::buttonDestroyed()
107 {
108  mButton = 0;
109 }
110 
111 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
112 {
113 #ifdef ANDROID
114  mButton->setToolTip( tr( "Message(s) logged." ) );
115 #endif
116 
117  if ( !isVisible() && level > QgsMessageLog::INFO )
118  {
119  mButton->show();
120  if ( mShowToolTips )
121  QToolTip::showText( mButton->mapToGlobal( QPoint( 0, 0 ) ), mButton->toolTip() );
122  }
123 
124  if ( tag.isNull() )
125  tag = tr( "General" );
126 
127  int i;
128  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
129  ;
130 
131  QPlainTextEdit *w;
132  if ( i < tabWidget->count() )
133  {
134  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
135  tabWidget->setCurrentIndex( i );
136  }
137  else
138  {
139  w = new QPlainTextEdit( this );
140  w->setReadOnly( true );
141  tabWidget->addTab( w, tag );
142  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
143  }
144 
145  QString prefix = QString( "%1\t%2\t" )
146  .arg( QDateTime::currentDateTime().toString( Qt::ISODate ) )
147  .arg( level );
148  w->appendPlainText( message.prepend( prefix ).replace( "\n", "\n\t\t\t" ) );
149  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
150 }
151 
152 void QgsMessageLogViewer::closeTab( int index )
153 {
154  tabWidget->removeTab( index );
155 }
static unsigned index
static const QString activeThemePath()
Returns the path to the currently active theme directory.
static QIcon icon(QString icon)
QgsMessageLogViewer(QStatusBar *statusBar=0, QWidget *parent=0, Qt::WindowFlags fl=QgisGui::ModalDialogFlags)
static const QString defaultThemePath()
Returns the path to the default theme directory.
void hideEvent(QHideEvent *)
void logMessage(QString message, QString tag, QgsMessageLog::MessageLevel level)
void showEvent(QShowEvent *)
static QgsMessageLog * instance()
#define tr(sourceText)