26#include <QDesktopServices>
28#include <QPlainTextEdit>
32#include <QTableWidget>
36#include "moc_qgsmessagelogviewer.cpp"
38using namespace Qt::StringLiterals;
41 : QDialog( parent, fl )
47 connect( tabWidget, &QTabWidget::tabCloseRequested,
this, &QgsMessageLogViewer::closeTab );
49 connect( tabWidget, &QTabWidget::currentChanged,
this, [
this](
int index ) {
50 tabWidget->setTabIcon( index, QIcon() );
53 mTabBarContextMenu =
new QMenu(
this );
54 tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
55 connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested,
this, &QgsMessageLogViewer::showContextMenuForTabBar );
58void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
65 mTabBarContextMenu->clear();
67 const int tabIndex = tabWidget->tabBar()->tabAt( point );
69 QAction *actionCloseTab =
new QAction( tr(
"Close Tab" ), mTabBarContextMenu );
70 connect( actionCloseTab, &QAction::triggered,
this, [
this, tabIndex] {
73 mTabBarContextMenu->addAction( actionCloseTab );
75 QAction *actionCloseOtherTabs =
new QAction( tr(
"Close Other Tabs" ), mTabBarContextMenu );
76 actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
77 connect( actionCloseOtherTabs, &QAction::triggered,
this, [
this, tabIndex] {
79 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
87 mTabBarContextMenu->addAction( actionCloseOtherTabs );
89 QAction *actionCloseAllTabs =
new QAction( tr(
"Close All Tabs" ), mTabBarContextMenu );
90 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
91 connect( actionCloseAllTabs, &QAction::triggered,
this, [
this] {
93 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
98 mTabBarContextMenu->addAction( actionCloseAllTabs );
100 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
114 constexpr int MESSAGE_COUNT_LIMIT = 10000;
116 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
118 ++mMessageLoggedCount;
120 QString cleanedTag = tag;
121 if ( cleanedTag.isNull() )
122 cleanedTag = tr(
"General" );
125 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ )
128 QPlainTextEdit *w =
nullptr;
129 if ( i < tabWidget->count() )
131 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
132 if ( i != tabWidget->currentIndex() )
139 w =
new QPlainTextEdit(
this );
140 w->setReadOnly(
true );
141 w->viewport()->installEventFilter(
this );
147 const QPalette
pal = qApp->palette();
148 const QString defaultColorName =
pal.color( QPalette::WindowText ).name();
153 levelString = u
"INFO"_s;
154 colorName = settings.
value( u
"colors/info"_s, QString() ).toString();
157 levelString = u
"WARNING"_s;
158 colorName = settings.
value( u
"colors/warning"_s, QString() ).toString();
161 levelString = u
"CRITICAL"_s;
162 colorName = settings.
value( u
"colors/critical"_s, QString() ).toString();
165 levelString = u
"SUCCESS"_s;
166 colorName = settings.
value( u
"colors/success"_s, QString() ).toString();
169 levelString = u
"NONE"_s;
170 colorName = settings.
value( u
"colors/default"_s, QString() ).toString();
173 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
175 const QString prefix = u
"<font color=\"%1\">%2 %3 </font>"_s
176 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
177 QString cleanedMessage = message.toHtmlEscaped();
178 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
179 cleanedMessage = tr(
"Message log truncated" );
181 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n',
"<br> "_L1 );
182 w->appendHtml( cleanedMessage );
183 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
190 for (
int i = 0; i < tabWidget->count(); i++ )
192 if ( tabWidget->tabText( i ).remove( QChar(
'&' ) ) == tag )
194 tabWidget->setCurrentIndex( i );
200void QgsMessageLogViewer::closeTab(
int index )
202 tabWidget->removeTab( index );
203 if ( tabWidget->count() == 0 )
212 switch ( event->type() )
214 case QEvent::MouseButtonPress:
216 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
218 QMouseEvent *me =
static_cast<QMouseEvent *
>( event );
219 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
220 if ( !mClickedAnchor.isEmpty() )
226 case QEvent::MouseButtonRelease:
228 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
230 QMouseEvent *me =
static_cast<QMouseEvent *
>( event );
231 const QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
232 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
234 QDesktopServices::openUrl( mClickedAnchor );
245 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 QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsMessageLog * messageLog()
Returns the application's message log.
void closeEvent(QCloseEvent *e) override
bool eventFilter(QObject *obj, QEvent *ev) override
void showTab(const QString &tag)
Activates the tab whose title matches the given tag, if any.
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.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.