19#include "moc_qgsmessagelogviewer.cpp"
27#include <QTableWidget>
31#include <QPlainTextEdit>
34#include <QDesktopServices>
37 : QDialog( parent, fl )
44 connect( tabWidget, &QTabWidget::tabCloseRequested,
this, &QgsMessageLogViewer::closeTab );
46 mTabBarContextMenu =
new QMenu(
this );
47 tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
48 connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested,
this, &QgsMessageLogViewer::showContextMenuForTabBar );
51void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
58 mTabBarContextMenu->clear();
60 const int tabIndex = tabWidget->tabBar()->tabAt( point );
62 QAction *actionCloseTab =
new QAction( tr(
"Close Tab" ), mTabBarContextMenu );
63 connect( actionCloseTab, &QAction::triggered,
this, [
this, tabIndex]
68 mTabBarContextMenu->addAction( actionCloseTab );
70 QAction *actionCloseOtherTabs =
new QAction( tr(
"Close Other Tabs" ), mTabBarContextMenu );
71 actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
72 connect( actionCloseOtherTabs, &QAction::triggered,
this, [
this, tabIndex]
75 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
84 mTabBarContextMenu->addAction( actionCloseOtherTabs );
86 QAction *actionCloseAllTabs =
new QAction( tr(
"Close All Tabs" ), mTabBarContextMenu );
87 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
88 connect( actionCloseAllTabs, &QAction::triggered,
this, [
this]
91 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
97 mTabBarContextMenu->addAction( actionCloseAllTabs );
99 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
113 constexpr int MESSAGE_COUNT_LIMIT = 10000;
115 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
117 ++mMessageLoggedCount;
119 QString cleanedTag = tag;
120 if ( cleanedTag.isNull() )
121 cleanedTag = tr(
"General" );
124 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ );
126 QPlainTextEdit *w =
nullptr;
127 if ( i < tabWidget->count() )
129 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
130 tabWidget->setCurrentIndex( i );
134 w =
new QPlainTextEdit(
this );
135 w->setReadOnly(
true );
136 w->viewport()->installEventFilter(
this );
137 tabWidget->addTab( w, cleanedTag );
138 tabWidget->setCurrentIndex( tabWidget->count() - 1 );
143 const QPalette
pal = qApp->palette();
144 const QString defaultColorName =
pal.color( QPalette::WindowText ).name();
149 levelString = QStringLiteral(
"INFO" );
150 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
153 levelString = QStringLiteral(
"WARNING" );
154 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
157 levelString = QStringLiteral(
"CRITICAL" );
158 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
161 levelString = QStringLiteral(
"SUCCESS" );
162 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
165 levelString = QStringLiteral(
"NONE" );
166 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
169 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
171 const QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </font>" )
172 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
173 QString cleanedMessage = message;
174 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
175 cleanedMessage = tr(
"Message log truncated" );
177 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
178 w->appendHtml( cleanedMessage );
179 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
184void QgsMessageLogViewer::closeTab(
int index )
186 tabWidget->removeTab( index );
187 if ( tabWidget->count() == 0 )
196 switch ( event->type() )
198 case QEvent::MouseButtonPress:
200 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
202 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
203 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
205 if ( !mClickedAnchor.isEmpty() )
211 case QEvent::MouseButtonRelease:
213 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
215 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
216 const QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
218 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
220 QDesktopServices::openUrl( mClickedAnchor );
231 return QDialog::eventFilter(
object, event );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
@ Warning
Warning message.
@ Critical
Critical/error message.
@ Info
Information message.
@ Success
Used for reporting a successful operation.
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:
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.