PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gui/qgsmessagelogviewer.cpp

http://github.com/qgis/Quantum-GIS
C++ | 205 lines | 165 code | 25 blank | 15 comment | 19 complexity | e725c3cef00f410acffe77ccc4b89617 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-3.0, GPL-2.0, CC-BY-SA-3.0, MIT, 0BSD, BSD-3-Clause
  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. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************/
  16. #include "qgsmessagelogviewer.h"
  17. #include "qgsmessagelog.h"
  18. #include "qgssettings.h"
  19. #include "qgsapplication.h"
  20. #include "qgsdockwidget.h"
  21. #include <QFile>
  22. #include <QDateTime>
  23. #include <QTableWidget>
  24. #include <QToolButton>
  25. #include <QStatusBar>
  26. #include <QToolTip>
  27. #include <QPlainTextEdit>
  28. #include <QScrollBar>
  29. #include <QDebug>
  30. #include <QDesktopServices>
  31. QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
  32. : QDialog( parent, fl )
  33. {
  34. setupUi( this );
  35. connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLog::messageReceived ),
  36. this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLogViewer::logMessage ) );
  37. connect( tabWidget, &QTabWidget::tabCloseRequested, this, &QgsMessageLogViewer::closeTab );
  38. mTabBarContextMenu = new QMenu( this );
  39. tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
  40. connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested, this, &QgsMessageLogViewer::showContextMenuForTabBar );
  41. }
  42. void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
  43. {
  44. if ( point.isNull() )
  45. {
  46. return;
  47. }
  48. mTabBarContextMenu->clear();
  49. int tabIndex = tabWidget->tabBar()->tabAt( point );
  50. QAction *actionCloseTab = new QAction( tr( "Close Tab" ), mTabBarContextMenu );
  51. connect( actionCloseTab, &QAction::triggered, this, [this, tabIndex]
  52. {
  53. closeTab( tabIndex );
  54. }
  55. );
  56. mTabBarContextMenu->addAction( actionCloseTab );
  57. QAction *actionCloseOtherTabs = new QAction( tr( "Close Other Tabs" ), mTabBarContextMenu );
  58. actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
  59. connect( actionCloseOtherTabs, &QAction::triggered, this, [this, tabIndex]
  60. {
  61. int i;
  62. for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
  63. {
  64. if ( i != tabIndex )
  65. {
  66. closeTab( i );
  67. }
  68. }
  69. }
  70. );
  71. mTabBarContextMenu->addAction( actionCloseOtherTabs );
  72. mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
  73. }
  74. void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
  75. {
  76. e->ignore();
  77. }
  78. void QgsMessageLogViewer::reject()
  79. {
  80. }
  81. void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
  82. {
  83. QString cleanedTag = tag;
  84. if ( cleanedTag.isNull() )
  85. cleanedTag = tr( "General" );
  86. int i;
  87. for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ );
  88. QPlainTextEdit *w = nullptr;
  89. if ( i < tabWidget->count() )
  90. {
  91. w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
  92. tabWidget->setCurrentIndex( i );
  93. }
  94. else
  95. {
  96. w = new QPlainTextEdit( this );
  97. w->setReadOnly( true );
  98. w->viewport()->installEventFilter( this );
  99. tabWidget->addTab( w, cleanedTag );
  100. tabWidget->setCurrentIndex( tabWidget->count() - 1 );
  101. }
  102. QString levelString;
  103. QgsSettings settings;
  104. QPalette pal = qApp->palette();
  105. QString defaultColorName = pal.color( QPalette::WindowText ).name();
  106. QString colorName;
  107. switch ( level )
  108. {
  109. case Qgis::Info:
  110. levelString = QStringLiteral( "INFO" );
  111. colorName = settings.value( QStringLiteral( "colors/info" ), QString() ).toString();
  112. break;
  113. case Qgis::Warning:
  114. levelString = QStringLiteral( "WARNING" );
  115. colorName = settings.value( QStringLiteral( "colors/warning" ), QString() ).toString();
  116. break;
  117. case Qgis::Critical:
  118. levelString = QStringLiteral( "CRITICAL" );
  119. colorName = settings.value( QStringLiteral( "colors/critical" ), QString() ).toString();
  120. break;
  121. case Qgis::Success:
  122. levelString = QStringLiteral( "SUCCESS" );
  123. colorName = settings.value( QStringLiteral( "colors/success" ), QString() ).toString();
  124. break;
  125. case Qgis::None:
  126. levelString = QStringLiteral( "NONE" );
  127. colorName = settings.value( QStringLiteral( "colors/default" ), QString() ).toString();
  128. break;
  129. }
  130. QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
  131. QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
  132. .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
  133. QString cleanedMessage = message;
  134. cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
  135. w->appendHtml( cleanedMessage );
  136. w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
  137. }
  138. void QgsMessageLogViewer::closeTab( int index )
  139. {
  140. if ( tabWidget->count() == 1 )
  141. qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
  142. else
  143. tabWidget->removeTab( index );
  144. }
  145. bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
  146. {
  147. switch ( event->type() )
  148. {
  149. case QEvent::MouseButtonPress:
  150. {
  151. if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
  152. {
  153. QMouseEvent *me = static_cast< QMouseEvent *>( event );
  154. mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
  155. QString();
  156. if ( !mClickedAnchor.isEmpty() )
  157. return true;
  158. }
  159. break;
  160. }
  161. case QEvent::MouseButtonRelease:
  162. {
  163. if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
  164. {
  165. QMouseEvent *me = static_cast< QMouseEvent *>( event );
  166. QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
  167. QString();
  168. if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
  169. {
  170. QDesktopServices::openUrl( mClickedAnchor );
  171. return true;
  172. }
  173. }
  174. break;
  175. }
  176. default:
  177. break;
  178. }
  179. return QDialog::eventFilter( object, event );
  180. }