Quantum GIS API Documentation
1.8
|
00001 /*************************************************************************** 00002 qgsmessagelogviewer.cpp - description 00003 ------------------- 00004 begin : October 2011 00005 copyright : (C) 2011 by Juergen E. Fischer 00006 email : jef at norbit dot de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "qgsmessagelogviewer.h" 00019 #include "qgsmessagelog.h" 00020 #include "qgsapplication.h" 00021 00022 #include <QFile> 00023 #include <QDateTime> 00024 #include <QTableWidget> 00025 #include <QToolButton> 00026 #include <QStatusBar> 00027 #include <QToolTip> 00028 #include <QDockWidget> 00029 00030 static QIcon icon( QString icon ) 00031 { 00032 // try active theme 00033 QString path = QgsApplication::activeThemePath(); 00034 if ( QFile::exists( path + icon ) ) 00035 path += icon; 00036 else 00037 path = QgsApplication::defaultThemePath() + icon; 00038 00039 return QIcon( path ); 00040 } 00041 00042 QgsMessageLogViewer::QgsMessageLogViewer( QStatusBar *statusBar, QWidget *parent, Qt::WFlags fl ) 00043 : QDialog( parent, fl ) 00044 , mButton( 0 ) 00045 , mCount( 0 ) 00046 { 00047 setupUi( this ); 00048 00049 connect( QgsMessageLog::instance(), SIGNAL( messageReceived( QString, QString, int ) ), 00050 this, SLOT( logMessage( QString, QString, int ) ) ); 00051 00052 if ( statusBar ) 00053 { 00054 mButton = new QToolButton( parent ); 00055 mButton->setObjectName( "mMessageLogViewerButton" ); 00056 mButton->setMaximumWidth( 20 ); 00057 mButton->setMaximumHeight( 20 ); 00058 mButton->setIcon( icon( "/mIconWarn.png" ) ); 00059 mButton->setToolTip( tr( "No messages." ) ); 00060 mButton->setCheckable( true ); 00061 mButton->hide(); 00062 connect( mButton, SIGNAL( toggled( bool ) ), this, SLOT( buttonToggled( bool ) ) ); 00063 connect( mButton, SIGNAL( destroyed() ), this, SLOT( buttonDestroyed() ) ); 00064 statusBar->addPermanentWidget( mButton, 0 ); 00065 } 00066 00067 connect( tabWidget, SIGNAL( tabCloseRequested( int ) ), this, SLOT( closeTab( int ) ) ); 00068 } 00069 00070 QgsMessageLogViewer::~QgsMessageLogViewer() 00071 { 00072 } 00073 00074 void QgsMessageLogViewer::hideEvent( QHideEvent * ) 00075 { 00076 if ( mButton ) 00077 { 00078 mButton->setChecked( false ); 00079 } 00080 } 00081 00082 void QgsMessageLogViewer::showEvent( QShowEvent * ) 00083 { 00084 if ( mButton ) 00085 { 00086 mButton->setChecked( true ); 00087 mButton->hide(); 00088 } 00089 } 00090 00091 void QgsMessageLogViewer::buttonToggled( bool checked ) 00092 { 00093 QWidget *w = qobject_cast<QDockWidget *>( parent() ); 00094 00095 if ( !w ) 00096 w = this; 00097 00098 if ( checked ) 00099 w->show(); 00100 else 00101 w->hide(); 00102 } 00103 00104 void QgsMessageLogViewer::buttonDestroyed() 00105 { 00106 mButton = 0; 00107 } 00108 00109 void QgsMessageLogViewer::logMessage( QString message, QString tag, int level ) 00110 { 00111 mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) ); 00112 00113 if ( !isVisible() ) 00114 { 00115 mButton->show(); 00116 QToolTip::showText( mButton->mapToGlobal( QPoint( 0, 0 ) ), mButton->toolTip() ); 00117 } 00118 00119 if ( tag.isNull() ) 00120 tag = tr( "General" ); 00121 00122 int i; 00123 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ) != tag; i++ ) 00124 ; 00125 00126 QTableWidget *w; 00127 if ( i < tabWidget->count() ) 00128 { 00129 w = qobject_cast<QTableWidget *>( tabWidget->widget( i ) ); 00130 tabWidget->setCurrentIndex( i ); 00131 } 00132 else 00133 { 00134 w = new QTableWidget( 0, 3, this ); 00135 w->verticalHeader()->setDefaultSectionSize( 16 ); 00136 w->verticalHeader()->setResizeMode( QHeaderView::ResizeToContents ); 00137 w->verticalHeader()->setVisible( false ); 00138 w->setGridStyle( Qt::DotLine ); 00139 w->setEditTriggers( QAbstractItemView::NoEditTriggers ); 00140 w->setHorizontalHeaderLabels( QStringList() << tr( "Timestamp" ) << tr( "Message" ) << tr( "Level" ) ); 00141 tabWidget->addTab( w, tag ); 00142 00143 tabWidget->setCurrentIndex( tabWidget->count() - 1 ); 00144 } 00145 00146 int n = w->rowCount(); 00147 00148 w->setRowCount( n + 1 ); 00149 QTableWidgetItem *item = new QTableWidgetItem( QDateTime::currentDateTime().toString( Qt::ISODate ) ); 00150 w->setItem( n, 0, item ); 00151 w->setItem( n, 1, new QTableWidgetItem( message ) ); 00152 w->setItem( n, 2, new QTableWidgetItem( QString::number( level ) ) ); 00153 w->scrollToBottom(); 00154 00155 w->horizontalHeader()->resizeSections( QHeaderView::ResizeToContents ); 00156 } 00157 00158 void QgsMessageLogViewer::closeTab( int index ) 00159 { 00160 QTableWidget *w = qobject_cast<QTableWidget *>( tabWidget->widget( index ) ); 00161 if ( w ) 00162 { 00163 mCount -= w->rowCount(); 00164 if ( mButton ) 00165 mButton->setToolTip( tr( "%1 message(s) logged." ).arg( mCount++ ) ); 00166 } 00167 tabWidget->removeTab( index ); 00168 }