26#include <QTableWidget>
30#include <QPlainTextEdit>
33#include <QDesktopServices>
36 : QDialog( parent, fl )
43 connect( tabWidget, &QTabWidget::tabCloseRequested,
this, &QgsMessageLogViewer::closeTab );
45 mTabBarContextMenu =
new QMenu(
this );
46 tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
47 connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested,
this, &QgsMessageLogViewer::showContextMenuForTabBar );
50void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
57 mTabBarContextMenu->clear();
59 const int tabIndex = tabWidget->tabBar()->tabAt( point );
61 QAction *actionCloseTab =
new QAction( tr(
"Close Tab" ), mTabBarContextMenu );
62 connect( actionCloseTab, &QAction::triggered,
this, [
this, tabIndex]
67 mTabBarContextMenu->addAction( actionCloseTab );
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]
74 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
83 mTabBarContextMenu->addAction( actionCloseOtherTabs );
85 QAction *actionCloseAllTabs =
new QAction( tr(
"Close All Tabs" ), mTabBarContextMenu );
86 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
87 connect( actionCloseAllTabs, &QAction::triggered,
this, [
this]
90 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
96 mTabBarContextMenu->addAction( actionCloseAllTabs );
98 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
112 constexpr int MESSAGE_COUNT_LIMIT = 10000;
114 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
116 ++mMessageLoggedCount;
118 QString cleanedTag = tag;
119 if ( cleanedTag.isNull() )
120 cleanedTag = tr(
"General" );
123 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ );
125 QPlainTextEdit *w =
nullptr;
126 if ( i < tabWidget->count() )
128 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
129 tabWidget->setCurrentIndex( i );
133 w =
new QPlainTextEdit(
this );
134 w->setReadOnly(
true );
135 w->viewport()->installEventFilter(
this );
136 tabWidget->addTab( w, cleanedTag );
137 tabWidget->setCurrentIndex( tabWidget->count() - 1 );
142 const QPalette
pal = qApp->palette();
143 const QString defaultColorName =
pal.color( QPalette::WindowText ).name();
148 levelString = QStringLiteral(
"INFO" );
149 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
152 levelString = QStringLiteral(
"WARNING" );
153 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
156 levelString = QStringLiteral(
"CRITICAL" );
157 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
160 levelString = QStringLiteral(
"SUCCESS" );
161 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
164 levelString = QStringLiteral(
"NONE" );
165 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
168 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
170 const QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </font>" )
171 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
172 QString cleanedMessage = message;
173 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
174 cleanedMessage = tr(
"Message log truncated" );
176 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
177 w->appendHtml( cleanedMessage );
178 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
183void QgsMessageLogViewer::closeTab(
int index )
185 tabWidget->removeTab( index );
186 if ( tabWidget->count() == 0 )
195 switch ( event->type() )
197 case QEvent::MouseButtonPress:
199 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
201 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
202 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
204 if ( !mClickedAnchor.isEmpty() )
210 case QEvent::MouseButtonRelease:
212 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
214 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
215 const QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
217 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
219 QDesktopServices::openUrl( mClickedAnchor );
230 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.