QGIS API Documentation 3.43.0-Master (5df50c54ce9)
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
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
35QgsMessageLogViewer::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 ), this, static_cast<void ( QgsMessageLogViewer::* )( const QString &, const QString &, Qgis::MessageLevel )>( &QgsMessageLogViewer::logMessage ) );
41
42 connect( tabWidget, &QTabWidget::tabCloseRequested, this, &QgsMessageLogViewer::closeTab );
43
44 mTabBarContextMenu = new QMenu( this );
45 tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
46 connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested, this, &QgsMessageLogViewer::showContextMenuForTabBar );
47}
48
49void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
50{
51 if ( point.isNull() )
52 {
53 return;
54 }
55
56 mTabBarContextMenu->clear();
57
58 const int tabIndex = tabWidget->tabBar()->tabAt( point );
59
60 QAction *actionCloseTab = new QAction( tr( "Close Tab" ), mTabBarContextMenu );
61 connect( actionCloseTab, &QAction::triggered, this, [this, tabIndex] {
62 closeTab( tabIndex );
63 } );
64 mTabBarContextMenu->addAction( actionCloseTab );
65
66 QAction *actionCloseOtherTabs = new QAction( tr( "Close Other Tabs" ), mTabBarContextMenu );
67 actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
68 connect( actionCloseOtherTabs, &QAction::triggered, this, [this, tabIndex] {
69 int i;
70 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
71 {
72 if ( i != tabIndex )
73 {
74 closeTab( i );
75 }
76 }
77 } );
78 mTabBarContextMenu->addAction( actionCloseOtherTabs );
79
80 QAction *actionCloseAllTabs = new QAction( tr( "Close All Tabs" ), mTabBarContextMenu );
81 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
82 connect( actionCloseAllTabs, &QAction::triggered, this, [this] {
83 int i;
84 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
85 {
86 closeTab( i );
87 }
88 } );
89 mTabBarContextMenu->addAction( actionCloseAllTabs );
90
91 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
92}
93
94void QgsMessageLogViewer::closeEvent( QCloseEvent *e )
95{
96 e->ignore();
97}
98
102
103void QgsMessageLogViewer::logMessage( const QString &message, const QString &tag, Qgis::MessageLevel level )
104{
105 constexpr int MESSAGE_COUNT_LIMIT = 10000;
106 // Avoid logging too many messages, which might blow memory.
107 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
108 return;
109 ++mMessageLoggedCount;
110
111 QString cleanedTag = tag;
112 if ( cleanedTag.isNull() )
113 cleanedTag = tr( "General" );
114
115 int i;
116 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar( '&' ) ) != cleanedTag; i++ )
117 ;
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 const QgsSettings settings;
136 const QPalette pal = qApp->palette();
137 const QString defaultColorName = pal.color( QPalette::WindowText ).name();
138 QString colorName;
139 switch ( level )
140 {
142 levelString = QStringLiteral( "INFO" );
143 colorName = settings.value( QStringLiteral( "colors/info" ), QString() ).toString();
144 break;
146 levelString = QStringLiteral( "WARNING" );
147 colorName = settings.value( QStringLiteral( "colors/warning" ), QString() ).toString();
148 break;
150 levelString = QStringLiteral( "CRITICAL" );
151 colorName = settings.value( QStringLiteral( "colors/critical" ), QString() ).toString();
152 break;
154 levelString = QStringLiteral( "SUCCESS" );
155 colorName = settings.value( QStringLiteral( "colors/success" ), QString() ).toString();
156 break;
158 levelString = QStringLiteral( "NONE" );
159 colorName = settings.value( QStringLiteral( "colors/default" ), QString() ).toString();
160 break;
161 }
162 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
163
164 const 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.toHtmlEscaped();
167 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
168 cleanedMessage = tr( "Message log truncated" );
169
170 cleanedMessage = cleanedMessage.prepend( prefix ).replace( '\n', QLatin1String( "<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" ) );
171 w->appendHtml( cleanedMessage );
172 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
173 tabWidget->show();
174 emptyLabel->hide();
175}
176
177void QgsMessageLogViewer::closeTab( int index )
178{
179 tabWidget->removeTab( index );
180 if ( tabWidget->count() == 0 )
181 {
182 tabWidget->hide();
183 emptyLabel->show();
184 }
185}
186
187bool QgsMessageLogViewer::eventFilter( QObject *object, QEvent *event )
188{
189 switch ( event->type() )
190 {
191 case QEvent::MouseButtonPress:
192 {
193 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
194 {
195 QMouseEvent *me = static_cast<QMouseEvent *>( event );
196 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
197 if ( !mClickedAnchor.isEmpty() )
198 return true;
199 }
200 break;
201 }
202
203 case QEvent::MouseButtonRelease:
204 {
205 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
206 {
207 QMouseEvent *me = static_cast<QMouseEvent *>( event );
208 const QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
209 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
210 {
211 QDesktopServices::openUrl( mClickedAnchor );
212 return true;
213 }
214 }
215 break;
216 }
217
218 default:
219 break;
220 }
221
222 return QDialog::eventFilter( object, event );
223}
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.
Stores settings for use within QGIS.
Definition qgssettings.h:65
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.