19#include "moc_qgsmessagelogviewer.cpp"
27#include <QTableWidget>
31#include <QPlainTextEdit>
34#include <QDesktopServices>
37 : 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] {
65 mTabBarContextMenu->addAction( actionCloseTab );
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] {
71 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
79 mTabBarContextMenu->addAction( actionCloseOtherTabs );
81 QAction *actionCloseAllTabs =
new QAction( tr(
"Close All Tabs" ), mTabBarContextMenu );
82 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
83 connect( actionCloseAllTabs, &QAction::triggered,
this, [
this] {
85 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
90 mTabBarContextMenu->addAction( actionCloseAllTabs );
92 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
106 constexpr int MESSAGE_COUNT_LIMIT = 10000;
108 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
110 ++mMessageLoggedCount;
112 QString cleanedTag = tag;
113 if ( cleanedTag.isNull() )
114 cleanedTag = tr(
"General" );
117 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ )
120 QPlainTextEdit *w =
nullptr;
121 if ( i < tabWidget->count() )
123 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
124 tabWidget->setCurrentIndex( i );
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 );
137 const QPalette
pal = qApp->palette();
138 const QString defaultColorName =
pal.color( QPalette::WindowText ).name();
143 levelString = QStringLiteral(
"INFO" );
144 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
147 levelString = QStringLiteral(
"WARNING" );
148 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
151 levelString = QStringLiteral(
"CRITICAL" );
152 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
155 levelString = QStringLiteral(
"SUCCESS" );
156 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
159 levelString = QStringLiteral(
"NONE" );
160 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
163 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
165 const QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </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" );
171 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
172 w->appendHtml( cleanedMessage );
173 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
178void QgsMessageLogViewer::closeTab(
int index )
180 tabWidget->removeTab( index );
181 if ( tabWidget->count() == 0 )
190 switch ( event->type() )
192 case QEvent::MouseButtonPress:
194 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
196 QMouseEvent *me =
static_cast<QMouseEvent *
>( event );
197 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
198 if ( !mClickedAnchor.isEmpty() )
204 case QEvent::MouseButtonRelease:
206 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
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 )
212 QDesktopServices::openUrl( mClickedAnchor );
223 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.