QGIS API Documentation  2.4.0-Chugiak
 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::WindowFlags fl )
43  : QDialog( parent, fl )
44  , mButton( 0 )
45  , mCount( 0 )
46  , mShowToolTips( true )
47 {
48  setupUi( this );
49 
50  connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, QgsMessageLog::MessageLevel ) ),
51  this, SLOT( logMessage( QString, QString, QgsMessageLog::MessageLevel ) ) );
52 
53  if ( statusBar )
54  {
55  mButton = new QToolButton( parent );
56  mButton->setObjectName( "mMessageLogViewerButton" );
57  mButton->setMaximumWidth( 20 );
58  mButton->setMaximumHeight( 20 );
59  mButton->setIcon( icon( "/mIconWarn.png" ) );
60 #ifndef ANDROID
61  mButton->setToolTip( tr( "No messages." ) );
62 #endif
63  mButton->setCheckable( true );
64  mButton->hide();
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  mButton->hide();
91  }
92 }
93 
95 {
96  QWidget *w = qobject_cast<QDockWidget *>( parent() );
97 
98  if ( !w )
99  w = this;
100 
101  if ( checked )
102  w->show();
103  else
104  w->hide();
105 }
106 
108 {
109  mButton = 0;
110 }
111 
112 void QgsMessageLogViewer::logMessage( QString message, QString tag, QgsMessageLog::MessageLevel level )
113 {
114 #ifdef ANDROID
115  mCount++;
116 #else
117  mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
118 #endif
119 
120  if ( !isVisible() && level > QgsMessageLog::INFO )
121  {
122  mButton->show();
123  if ( mShowToolTips )
124  QToolTip::showText( mButton->mapToGlobal( QPoint( 0, 0 ) ), mButton->toolTip() );
125  }
126 
127  if ( tag.isNull() )
128  tag = tr( "General" );
129 
130  int i;
131  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ )
132  ;
133 
134  QTableWidget *w;
135  if ( i < tabWidget->count() )
136  {
137  w = qobject_cast<QTableWidget *>( tabWidget->widget( i ) );
138  tabWidget->setCurrentIndex( i );
139  }
140  else
141  {
142  w = new QTableWidget( 0, 3, this );
143  w->verticalHeader()->setDefaultSectionSize( 16 );
144  w->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents );
145  w->verticalHeader()->setVisible( false );
146  w->setGridStyle( Qt::DotLine );
147  w->setEditTriggers( QAbstractItemView::NoEditTriggers );
148  w->setHorizontalScrollMode( QAbstractItemView::ScrollPerPixel );
149  w->setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
150  w->setHorizontalHeaderLabels( QStringList() << tr( "Timestamp" ) << tr( "Message" ) << tr( "Level" ) );
151  tabWidget->addTab( w, tag );
152 
153  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
154  }
155 
156  int n = w->rowCount();
157 
158  w->setRowCount( n + 1 );
159  QTableWidgetItem *item = new QTableWidgetItem( QDateTime::currentDateTime().toString( Qt::ISODate ) );
160  w->setItem( n, 0, item );
161  w->setItem( n, 1, new QTableWidgetItem( message ) );
162  w->setItem( n, 2, new QTableWidgetItem( QString::number( level ) ) );
163  w->scrollToBottom();
164 
165  w->horizontalHeader()->resizeSections( QHeaderView::ResizeToContents );
166 }
167 
169 {
170  QTableWidget *w = qobject_cast<QTableWidget *>( tabWidget->widget( index ) );
171  if ( w )
172  {
173  mCount -= w->rowCount();
174  if ( mButton )
175 #ifdef ANDROID
176  mCount++;
177 #else
178  mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) );
179 #endif
180  }
181  tabWidget->removeTab( index );
182 }
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 buttonToggled(bool checked)
void hideEvent(QHideEvent *)
void logMessage(QString message, QString tag, QgsMessageLog::MessageLevel level)
void showEvent(QShowEvent *)
static QgsMessageLog * instance()
#define tr(sourceText)