QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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 "moc_qgsmessagelogviewer.cpp"
20#include "qgsmessagelog.h"
21#include "qgssettings.h"
22#include "qgsapplication.h"
23#include "qgsdockwidget.h"
24
25#include <QFile>
26#include <QDateTime>
27#include <QTableWidget>
28#include <QToolButton>
29#include <QStatusBar>
30#include <QToolTip>
31#include <QPlainTextEdit>
32#include <QScrollBar>
33#include <QDebug>
34#include <QDesktopServices>
35
36QgsMessageLogViewer::QgsMessageLogViewer( QWidget *parent, Qt::WindowFlags fl )
37 : QDialog( parent, fl )
38{
39 setupUi( this );
40
41 connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLog::messageReceived ), 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
50void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
51{
52 if ( point.isNull() )
53 {
54 return;
55 }
56
57 mTabBarContextMenu->clear();
58
59 const 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 closeTab( tabIndex );
64 } );
65 mTabBarContextMenu->addAction( actionCloseTab );
66
67 QAction *actionCloseOtherTabs = new QAction( tr( "Close Other Tabs" ), mTabBarContextMenu );
68 actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
69 connect( actionCloseOtherTabs, &QAction::triggered, this, [this, tabIndex] {
70 int i;
71 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
72 {
73 if ( i != tabIndex )
74 {
75 closeTab( i );
76 }
77 }
78 } );
79 mTabBarContextMenu->addAction( actionCloseOtherTabs );
80
81 QAction *actionCloseAllTabs = new QAction( tr( "Close All Tabs" ), mTabBarContextMenu );
82 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
83 connect( actionCloseAllTabs, &QAction::triggered, this, [this] {
84 int i;
85 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
86 {
87 closeTab( i );
88 }
89 } );
90 mTabBarContextMenu->addAction( actionCloseAllTabs );
91
92 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
93}
94
95void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
96{
97 e->ignore();
98}
99
103
104void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
105{
106 constexpr int MESSAGE_COUNT_LIMIT = 10000;
107 // Avoid logging too many messages, which might blow memory.
108 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
109 return;
110 ++mMessageLoggedCount;
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
120 QPlainTextEdit *w = nullptr;
121 if ( i < tabWidget->count() )
122 {
123 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
124 tabWidget->setCurrentIndex( i );
125 }
126 else
127 {
128 w = new QPlainTextEdit( this );
129 w->setReadOnly( true );
130 w->viewport()->installEventFilter( this );
131 tabWidget->addTab( w, cleanedTag );
132 tabWidget->setCurrentIndex( tabWidget->count() - 1 );
133 }
134
135 QString levelString;
136 const QgsSettings settings;
137 const QPalette pal = qApp->palette();
138 const QString defaultColorName = pal.color( QPalette::WindowText ).name();
139 QString colorName;
140 switch ( level )
141 {
143 levelString = QStringLiteral( "INFO" );
144 colorName = settings.value( QStringLiteral( "colors/info" ), QString() ).toString();
145 break;
147 levelString = QStringLiteral( "WARNING" );
148 colorName = settings.value( QStringLiteral( "colors/warning" ), QString() ).toString();
149 break;
151 levelString = QStringLiteral( "CRITICAL" );
152 colorName = settings.value( QStringLiteral( "colors/critical" ), QString() ).toString();
153 break;
155 levelString = QStringLiteral( "SUCCESS" );
156 colorName = settings.value( QStringLiteral( "colors/success" ), QString() ).toString();
157 break;
159 levelString = QStringLiteral( "NONE" );
160 colorName = settings.value( QStringLiteral( "colors/default" ), QString() ).toString();
161 break;
162 }
163 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
164
165 const QString prefix = QStringLiteral( "<font color=\"%1\">%2 &nbsp;&nbsp;&nbsp; %3 &nbsp;&nbsp;&nbsp;</font>" )
166 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
167 QString cleanedMessage = message;
168 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
169 cleanedMessage = tr( "Message log truncated" );
170
171 cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
172 w->appendHtml( cleanedMessage );
173 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
174 tabWidget->show();
175 emptyLabel->hide();
176}
177
178void QgsMessageLogViewer::closeTab( int index )
179{
180 tabWidget->removeTab( index );
181 if ( tabWidget->count() == 0 )
182 {
183 tabWidget->hide();
184 emptyLabel->show();
185 }
186}
187
188bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
189{
190 switch ( event->type() )
191 {
192 case QEvent::MouseButtonPress:
193 {
194 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
195 {
196 QMouseEvent *me = static_cast<QMouseEvent *>( event );
197 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
198 if ( !mClickedAnchor.isEmpty() )
199 return true;
200 }
201 break;
202 }
203
204 case QEvent::MouseButtonRelease:
205 {
206 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
207 {
208 QMouseEvent *me = static_cast<QMouseEvent *>( event );
209 const QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
210 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
211 {
212 QDesktopServices::openUrl( mClickedAnchor );
213 return true;
214 }
215 }
216 break;
217 }
218
219 default:
220 break;
221 }
222
223 return QDialog::eventFilter( object, event );
224}
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition qgis.h:154
@ NoLevel
No level.
Definition qgis.h:159
@ Warning
Warning message.
Definition qgis.h:156
@ Critical
Critical/error message.
Definition qgis.h:157
@ Info
Information message.
Definition qgis.h:155
@ Success
Used for reporting a successful operation.
Definition qgis.h:158
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.
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.