QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 "qgssettings.h"
21 #include "qgsapplication.h"
22 #include "qgsdockwidget.h"
23 
24 #include <QFile>
25 #include <QDateTime>
26 #include <QTableWidget>
27 #include <QToolButton>
28 #include <QStatusBar>
29 #include <QToolTip>
30 #include <QPlainTextEdit>
31 #include <QScrollBar>
32 #include <QDebug>
33 #include <QDesktopServices>
34 
35 QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
36  : QDialog( parent, fl )
37 {
38  setupUi( this );
39 
40  connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLog::messageReceived ),
41  this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLogViewer::logMessage ) );
42 
43  connect( tabWidget, &QTabWidget::tabCloseRequested, this, &QgsMessageLogViewer::closeTab );
44 
45  mTabBarContextMenu = new QMenu( this );
46  tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
47  connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested, this, &QgsMessageLogViewer::showContextMenuForTabBar );
48 }
49 
50 void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
51 {
52  if ( point.isNull() )
53  {
54  return;
55  }
56 
57  mTabBarContextMenu->clear();
58 
59  int tabIndex = tabWidget->tabBar()->tabAt( point );
60 
61  QAction *actionCloseTab = new QAction( tr( "Close Tab" ), mTabBarContextMenu );
62  connect( actionCloseTab, &QAction::triggered, this, [this, tabIndex]
63  {
64  closeTab( tabIndex );
65  }
66  );
67  mTabBarContextMenu->addAction( actionCloseTab );
68 
69  QAction *actionCloseOtherTabs = new QAction( tr( "Close Other Tabs" ), mTabBarContextMenu );
70  actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
71  connect( actionCloseOtherTabs, &QAction::triggered, this, [this, tabIndex]
72  {
73  int i;
74  for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
75  {
76  if ( i != tabIndex )
77  {
78  closeTab( i );
79  }
80  }
81  }
82  );
83  mTabBarContextMenu->addAction( actionCloseOtherTabs );
84 
85  QAction *actionCloseAllTabs = new QAction( tr( "Close All Tabs" ), mTabBarContextMenu );
86  actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
87  connect( actionCloseAllTabs, &QAction::triggered, this, [this]
88  {
89  int i;
90  for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
91  {
92  closeTab( i );
93  }
94  }
95  );
96  mTabBarContextMenu->addAction( actionCloseAllTabs );
97 
98  mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
99 }
100 
101 void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
102 {
103  e->ignore();
104 }
105 
107 {
108 }
109 
110 void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
111 {
112  QString cleanedTag = tag;
113  if ( cleanedTag.isNull() )
114  cleanedTag = tr( "General" );
115 
116  int i;
117  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ );
118 
119  QPlainTextEdit *w = nullptr;
120  if ( i < tabWidget->count() )
121  {
122  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
123  tabWidget->setCurrentIndex( i );
124  }
125  else
126  {
127  w = new QPlainTextEdit( this );
128  w->setReadOnly( true );
129  w->viewport()->installEventFilter( this );
130  tabWidget->addTab( w, cleanedTag );
131  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
132  }
133 
134  QString levelString;
135  QgsSettings settings;
136  QPalette pal = qApp->palette();
137  QString defaultColorName = pal.color( QPalette::WindowText ).name();
138  QString colorName;
139  switch ( level )
140  {
141  case Qgis::MessageLevel::Info:
142  levelString = QStringLiteral( "INFO" );
143  colorName = settings.value( QStringLiteral( "colors/info" ), QString() ).toString();
144  break;
145  case Qgis::MessageLevel::Warning:
146  levelString = QStringLiteral( "WARNING" );
147  colorName = settings.value( QStringLiteral( "colors/warning" ), QString() ).toString();
148  break;
149  case Qgis::MessageLevel::Critical:
150  levelString = QStringLiteral( "CRITICAL" );
151  colorName = settings.value( QStringLiteral( "colors/critical" ), QString() ).toString();
152  break;
153  case Qgis::MessageLevel::Success:
154  levelString = QStringLiteral( "SUCCESS" );
155  colorName = settings.value( QStringLiteral( "colors/success" ), QString() ).toString();
156  break;
157  case Qgis::MessageLevel::NoLevel:
158  levelString = QStringLiteral( "NONE" );
159  colorName = settings.value( QStringLiteral( "colors/default" ), QString() ).toString();
160  break;
161  }
162  QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
163 
164  QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
165  .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
166  QString cleanedMessage = message;
167  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
168  w->appendHtml( cleanedMessage );
169  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
170  tabWidget->show();
171  emptyLabel->hide();
172 }
173 
174 void QgsMessageLogViewer::closeTab( int index )
175 {
176  tabWidget->removeTab( index );
177  if ( tabWidget->count() == 0 )
178  {
179  tabWidget->hide();
180  emptyLabel->show();
181  }
182 }
183 
184 bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
185 {
186  switch ( event->type() )
187  {
188  case QEvent::MouseButtonPress:
189  {
190  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
191  {
192  QMouseEvent *me = static_cast< QMouseEvent *>( event );
193  mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
194  QString();
195  if ( !mClickedAnchor.isEmpty() )
196  return true;
197  }
198  break;
199  }
200 
201  case QEvent::MouseButtonRelease:
202  {
203  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
204  {
205  QMouseEvent *me = static_cast< QMouseEvent *>( event );
206  QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
207  QString();
208  if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
209  {
210  QDesktopServices::openUrl( mClickedAnchor );
211  return true;
212  }
213  }
214  break;
215  }
216 
217  default:
218  break;
219  }
220 
221  return QDialog::eventFilter( object, event );
222 }
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:105
static QgsMessageLog * messageLog()
Returns the application's message log.
A generic dialog widget for displaying QGIS log messages.
void closeEvent(QCloseEvent *e) override
bool eventFilter(QObject *obj, QEvent *ev) override
QgsMessageLogViewer(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Create a new message log viewer.
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to the viewer.
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:40
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.