QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
86 }
87 
88 void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
89 {
90  e->ignore();
91 }
92 
94 {
95 }
96 
97 void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
98 {
99  QString cleanedTag = tag;
100  if ( cleanedTag.isNull() )
101  cleanedTag = tr( "General" );
102 
103  int i;
104  for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ );
105 
106  QPlainTextEdit *w = nullptr;
107  if ( i < tabWidget->count() )
108  {
109  w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
110  tabWidget->setCurrentIndex( i );
111  }
112  else
113  {
114  w = new QPlainTextEdit( this );
115  w->setReadOnly( true );
116  w->viewport()->installEventFilter( this );
117  tabWidget->addTab( w, cleanedTag );
118  tabWidget->setCurrentIndex( tabWidget->count() - 1 );
119  }
120 
121  QString levelString;
122  QgsSettings settings;
123  QPalette pal = qApp->palette();
124  QString defaultColorName = pal.color( QPalette::WindowText ).name();
125  QString colorName;
126  switch ( level )
127  {
128  case Qgis::Info:
129  levelString = QStringLiteral( "INFO" );
130  colorName = settings.value( QStringLiteral( "colors/info" ), QString() ).toString();
131  break;
132  case Qgis::Warning:
133  levelString = QStringLiteral( "WARNING" );
134  colorName = settings.value( QStringLiteral( "colors/warning" ), QString() ).toString();
135  break;
136  case Qgis::Critical:
137  levelString = QStringLiteral( "CRITICAL" );
138  colorName = settings.value( QStringLiteral( "colors/critical" ), QString() ).toString();
139  break;
140  case Qgis::Success:
141  levelString = QStringLiteral( "SUCCESS" );
142  colorName = settings.value( QStringLiteral( "colors/success" ), QString() ).toString();
143  break;
144  case Qgis::None:
145  levelString = QStringLiteral( "NONE" );
146  colorName = settings.value( QStringLiteral( "colors/default" ), QString() ).toString();
147  break;
148  }
149  QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
150 
151  QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
152  .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
153  QString cleanedMessage = message;
154  cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
155  w->appendHtml( cleanedMessage );
156  w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
157 }
158 
159 void QgsMessageLogViewer::closeTab( int index )
160 {
161  if ( tabWidget->count() == 1 )
162  qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
163  else
164  tabWidget->removeTab( index );
165 }
166 
167 bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
168 {
169  switch ( event->type() )
170  {
171  case QEvent::MouseButtonPress:
172  {
173  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
174  {
175  QMouseEvent *me = static_cast< QMouseEvent *>( event );
176  mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
177  QString();
178  if ( !mClickedAnchor.isEmpty() )
179  return true;
180  }
181  break;
182  }
183 
184  case QEvent::MouseButtonRelease:
185  {
186  if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
187  {
188  QMouseEvent *me = static_cast< QMouseEvent *>( event );
189  QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
190  QString();
191  if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
192  {
193  QDesktopServices::openUrl( mClickedAnchor );
194  return true;
195  }
196  }
197  break;
198  }
199 
200  default:
201  break;
202  }
203 
204  return QDialog::eventFilter( object, event );
205 }
QgsMessageLogViewer::QgsMessageLogViewer
QgsMessageLogViewer(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Create a new message log viewer.
Definition: qgsmessagelogviewer.cpp:35
QgsMessageLogViewer::eventFilter
bool eventFilter(QObject *obj, QEvent *ev) override
Definition: qgsmessagelogviewer.cpp:167
QgsMessageLogViewer::logMessage
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to the viewer.
Definition: qgsmessagelogviewer.cpp:97
QgsMessageLogViewer::closeEvent
void closeEvent(QCloseEvent *e) override
Definition: qgsmessagelogviewer.cpp:88
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
Qgis::Warning
@ Warning
Definition: qgis.h:91
Qgis::Success
@ Success
Definition: qgis.h:93
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
qgsdockwidget.h
QgsMessageLog::messageReceived
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
pal
Definition: qgsdiagramrenderer.h:49
qgsapplication.h
QgsApplication::messageLog
static QgsMessageLog * messageLog()
Returns the application's message log.
Definition: qgsapplication.cpp:2248
QgsMessageLogViewer::reject
void reject() override
Definition: qgsmessagelogviewer.cpp:93
Qgis::Info
@ Info
Definition: qgis.h:90
QgsMessageLog
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:39
qgsmessagelogviewer.h
QgsMessageLogViewer
A generic dialog widget for displaying QGIS log messages.
Definition: qgsmessagelogviewer.h:37
Qgis::MessageLevel
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:89
qgssettings.h
Qgis::Critical
@ Critical
Definition: qgis.h:92
Qgis::None
@ None
Definition: qgis.h:94
qgsmessagelog.h