QGIS API Documentation  2.0.1-Dufour
 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  mButton->setToolTip( tr( "No messages." ) );
60  mButton->setCheckable( true );
61  mButton->hide();
62  connect( mButton, SIGNAL( toggled( bool ) ), this, SLOT( buttonToggled( bool ) ) );
63  connect( mButton, SIGNAL( destroyed() ), this, SLOT( buttonDestroyed() ) );
64  statusBar->addPermanentWidget( mButton, 0 );
65  }
66 
67  connect( tabWidget, SIGNAL( tabCloseRequested( int ) ), this, SLOT( closeTab( int ) ) );
68 }
69 
71 {
72 }
73 
74 void QgsMessageLogViewer::hideEvent( QHideEvent * )
75 {
76  if ( mButton )
77  {
78  mButton->setChecked( false );
79  }
80 }
81 
82 void QgsMessageLogViewer::showEvent( QShowEvent * )
83 {
84  if ( mButton )
85  {
86  mButton->setChecked( true );
87  mButton->hide();
88  }
89 }
90 
92 {
93  QWidget *w = qobject_cast<QDockWidget *>( parent() );
94 
95  if ( !w )
96  w = this;
97 
98  if ( checked )
99  w->show();
100  else
101  w->hide();
102 }
103 
105 {
106  mButton = 0;
107 }
108 
109 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
110 {
111  mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
112 
113  if ( !isVisible() && level > QgsMessageLog::INFO )
114  {
115  mButton->show();
116  QToolTip::showText( mButton->mapToGlobal( QPoint( 0, 0 ) ), mButton->toolTip() );
117  }
118 
119  if ( tag.isNull() )
120  tag = tr( "General" );
121 
122  int i;
123  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
124  ;
125 
126  QTableWidget *w;
127  if ( i < tabWidget->count() )
128  {
129  w = qobject_cast<QTableWidget *>( tabWidget->widget( i ) );
130  tabWidget->setCurrentIndex( i );
131  }
132  else
133  {
134  w = new QTableWidget( 0, 3, this );
135  w->verticalHeader()->setDefaultSectionSize( 16 );
136  w->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
137  w->verticalHeader()->setVisible( false );
138  w->setGridStyle( Qt::DotLine );
139  w->setEditTriggers( QAbstractItemView::NoEditTriggers );
140  w->setHorizontalScrollMode( QAbstractItemView::ScrollPerPixel );
141  w->setHorizontalHeaderLabels( QStringList() << tr( "Timestamp" ) << tr( "Message" ) << tr( "Level" ) );
142  tabWidget->addTab( w, tag );
143 
144  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
145  }
146 
147  int n = w->rowCount();
148 
149  w->setRowCount( n + 1 );
150  QTableWidgetItem *item = new QTableWidgetItem( QDateTime::currentDateTime().toString( Qt::ISODate ) );
151  w->setItem( n, 0, item );
152  w->setItem( n, 1, new QTableWidgetItem( message ) );
153  w->setItem( n, 2, new QTableWidgetItem( QString::number( level ) ) );
154  w->scrollToBottom();
155 
156  w->horizontalHeader()->resizeSections( QHeaderView::ResizeToContents );
157 }
158 
160 {
161  QTableWidget *w = qobject_cast<QTableWidget *>( tabWidget->widget( index ) );
162  if ( w )
163  {
164  mCount -= w->rowCount();
165  if ( mButton )
166  mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
167  }
168  tabWidget->removeTab( index );
169 }