QGIS API Documentation  2.2.0-Valmiera
 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 
30 static QIcon icon( QString icon )
31 {
32  // try active theme
33  QString path = QgsApplication::activeThemePath();
34  if ( QFile::exists( path + icon ) )
35  path += icon;
36  else
38 
39  return QIcon( path );
40 }
41 
42 QgsMessageLogViewer::QgsMessageLogViewer( QStatusBar *statusBar, QWidget *parent, Qt::WFlags fl )
43  : QDialog( parent, fl )
44  , mButton( 0 )
45  , mCount( 0 )
46 {
47  setupUi( this );
48 
49  connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ),
50  this, SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
51 
52  if ( statusBar )
53  {
54  mButton = new QToolButton( parent );
55  mButton->setObjectName( "mMessageLogViewerButton" );
56  mButton->setMaximumWidth( 20 );
57  mButton->setMaximumHeight( 20 );
58  mButton->setIcon( icon( "/mIconWarn.png" ) );
59 #ifndef ANDROID
60  mButton->setToolTip( tr( "No messages." ) );
61 #endif
62  mButton->setCheckable( true );
63  mButton->hide();
64  connect( mButton, SIGNAL( toggled( bool ) ), this, SLOT( buttonToggled( bool ) ) );
65  connect( mButton, SIGNAL( destroyed() ), this, SLOT( buttonDestroyed() ) );
66  statusBar->addPermanentWidget( mButton, 0 );
67  }
68 
69  connect( tabWidget, SIGNAL( tabCloseRequested( int ) ), this, SLOT( closeTab( int ) ) );
70 }
71 
73 {
74 }
75 
76 void QgsMessageLogViewer::hideEvent( QHideEvent * )
77 {
78  if ( mButton )
79  {
80  mButton->setChecked( false );
81  }
82 }
83 
84 void QgsMessageLogViewer::showEvent( QShowEvent * )
85 {
86  if ( mButton )
87  {
88  mButton->setChecked( true );
89  mButton->hide();
90  }
91 }
92 
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 
107 {
108  mButton = 0;
109 }
110 
111 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
112 {
113 #ifdef ANDROID
114  mCount++;
115 #else
116  mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
117 #endif
118 
119  if ( !isVisible() && level > QgsMessageLog::INFO )
120  {
121  mButton->show();
122  QToolTip::showText( mButton->mapToGlobal( QPoint( 0, 0 ) ), mButton->toolTip() );
123  }
124 
125  if ( tag.isNull() )
126  tag = tr( "General" );
127 
128  int i;
129  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
130  ;
131 
132  QTableWidget *w;
133  if ( i < tabWidget->count() )
134  {
135  w = qobject_cast<QTableWidget *>( tabWidget->widget( i ) );
136  tabWidget->setCurrentIndex( i );
137  }
138  else
139  {
140  w = new QTableWidget( 0, 3, this );
141  w->verticalHeader()->setDefaultSectionSize( 16 );
142  w->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
143  w->verticalHeader()->setVisible( false );
144  w->setGridStyle( Qt::DotLine );
145  w->setEditTriggers( QAbstractItemView::NoEditTriggers );
146  w->setHorizontalScrollMode( QAbstractItemView::ScrollPerPixel );
147  w->setHorizontalHeaderLabels( QStringList() << tr( "Timestamp" ) << tr( "Message" ) << tr( "Level" ) );
148  tabWidget->addTab( w, tag );
149 
150  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
151  }
152 
153  int n = w->rowCount();
154 
155  w->setRowCount( n + 1 );
156  QTableWidgetItem *item = new QTableWidgetItem( QDateTime::currentDateTime().toString( Qt::ISODate ) );
157  w->setItem( n, 0, item );
158  w->setItem( n, 1, new QTableWidgetItem( message ) );
159  w->setItem( n, 2, new QTableWidgetItem( QString::number( level ) ) );
160  w->scrollToBottom();
161 
162  w->horizontalHeader()->resizeSections( QHeaderView::ResizeToContents );
163 }
164 
166 {
167  QTableWidget *w = qobject_cast<QTableWidget *>( tabWidget->widget( index ) );
168  if ( w )
169  {
170  mCount -= w->rowCount();
171  if ( mButton )
172 #ifdef ANDROID
173  mCount++;
174 #else
175  mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
176 #endif
177  }
178  tabWidget->removeTab( index );
179 }