19#include "moc_qgsmessagelogviewer.cpp"
26#include <QTableWidget>
30#include <QPlainTextEdit>
33#include <QDesktopServices>
36 : QDialog( parent, fl )
42 connect( tabWidget, &QTabWidget::tabCloseRequested,
this, &QgsMessageLogViewer::closeTab );
44 mTabBarContextMenu =
new QMenu(
this );
45 tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
46 connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested,
this, &QgsMessageLogViewer::showContextMenuForTabBar );
49void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
56 mTabBarContextMenu->clear();
58 const int tabIndex = tabWidget->tabBar()->tabAt( point );
60 QAction *actionCloseTab =
new QAction( tr(
"Close Tab" ), mTabBarContextMenu );
61 connect( actionCloseTab, &QAction::triggered,
this, [
this, tabIndex] {
64 mTabBarContextMenu->addAction( actionCloseTab );
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] {
70 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
78 mTabBarContextMenu->addAction( actionCloseOtherTabs );
80 QAction *actionCloseAllTabs =
new QAction( tr(
"Close All Tabs" ), mTabBarContextMenu );
81 actionCloseAllTabs->setEnabled( tabWidget->tabBar()->count() > 0 );
82 connect( actionCloseAllTabs, &QAction::triggered,
this, [
this] {
84 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
89 mTabBarContextMenu->addAction( actionCloseAllTabs );
91 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
105 constexpr int MESSAGE_COUNT_LIMIT = 10000;
107 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
109 ++mMessageLoggedCount;
111 QString cleanedTag = tag;
112 if ( cleanedTag.isNull() )
113 cleanedTag = tr(
"General" );
116 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ )
119 QPlainTextEdit *w =
nullptr;
120 if ( i < tabWidget->count() )
122 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
123 tabWidget->setCurrentIndex( i );
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 );
136 const QPalette
pal = qApp->palette();
137 const QString defaultColorName =
pal.color( QPalette::WindowText ).name();
142 levelString = QStringLiteral(
"INFO" );
143 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
146 levelString = QStringLiteral(
"WARNING" );
147 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
150 levelString = QStringLiteral(
"CRITICAL" );
151 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
154 levelString = QStringLiteral(
"SUCCESS" );
155 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
158 levelString = QStringLiteral(
"NONE" );
159 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
162 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
164 const QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </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" );
170 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
171 w->appendHtml( cleanedMessage );
172 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
177void QgsMessageLogViewer::closeTab(
int index )
179 tabWidget->removeTab( index );
180 if ( tabWidget->count() == 0 )
189 switch ( event->type() )
191 case QEvent::MouseButtonPress:
193 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
195 QMouseEvent *me =
static_cast<QMouseEvent *
>( event );
196 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
197 if ( !mClickedAnchor.isEmpty() )
203 case QEvent::MouseButtonRelease:
205 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
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 )
211 QDesktopServices::openUrl( mClickedAnchor );
222 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.
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.