26#include <QDesktopServices>
28#include <QPlainTextEdit>
31#include <QTableWidget>
35#include "moc_qgsmessagelogviewer.cpp"
38 : QDialog( parent, fl )
44 connect( tabWidget, &QTabWidget::tabCloseRequested,
this, &QgsMessageLogViewer::closeTab );
46 connect( tabWidget, &QTabWidget::currentChanged,
this, [
this](
int index ) {
47 tabWidget->setTabIcon( index, QIcon() );
50 mTabBarContextMenu =
new QMenu(
this );
51 tabWidget->tabBar()->setContextMenuPolicy( Qt::CustomContextMenu );
52 connect( tabWidget->tabBar(), &QWidget::customContextMenuRequested,
this, &QgsMessageLogViewer::showContextMenuForTabBar );
55void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
62 mTabBarContextMenu->clear();
64 const int tabIndex = tabWidget->tabBar()->tabAt( point );
66 QAction *actionCloseTab =
new QAction( tr(
"Close Tab" ), mTabBarContextMenu );
67 connect( actionCloseTab, &QAction::triggered,
this, [
this, tabIndex] {
70 mTabBarContextMenu->addAction( actionCloseTab );
72 QAction *actionCloseOtherTabs =
new QAction( tr(
"Close Other Tabs" ), mTabBarContextMenu );
73 actionCloseOtherTabs->setEnabled( tabWidget->tabBar()->count() > 1 );
74 connect( actionCloseOtherTabs, &QAction::triggered,
this, [
this, tabIndex] {
76 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] {
90 for ( i = tabWidget->tabBar()->count() - 1; i >= 0; i-- )
95 mTabBarContextMenu->addAction( actionCloseAllTabs );
97 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
111 constexpr int MESSAGE_COUNT_LIMIT = 10000;
113 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
115 ++mMessageLoggedCount;
117 QString cleanedTag = tag;
118 if ( cleanedTag.isNull() )
119 cleanedTag = tr(
"General" );
122 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 if ( i != tabWidget->currentIndex() )
136 w =
new QPlainTextEdit(
this );
137 w->setReadOnly(
true );
138 w->viewport()->installEventFilter(
this );
144 const QPalette
pal = qApp->palette();
145 const QString defaultColorName =
pal.color( QPalette::WindowText ).name();
150 levelString = QStringLiteral(
"INFO" );
151 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
154 levelString = QStringLiteral(
"WARNING" );
155 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
158 levelString = QStringLiteral(
"CRITICAL" );
159 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
162 levelString = QStringLiteral(
"SUCCESS" );
163 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
166 levelString = QStringLiteral(
"NONE" );
167 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
170 const QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
172 const QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </font>" )
173 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
174 QString cleanedMessage = message.toHtmlEscaped();
175 if ( mMessageLoggedCount == MESSAGE_COUNT_LIMIT )
176 cleanedMessage = tr(
"Message log truncated" );
178 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
179 w->appendHtml( cleanedMessage );
180 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
187 for (
int i = 0; i < tabWidget->count(); i++ )
189 if ( tabWidget->tabText( i ).remove( QChar(
'&' ) ) == tag )
191 tabWidget->setCurrentIndex( i );
197void QgsMessageLogViewer::closeTab(
int index )
199 tabWidget->removeTab( index );
200 if ( tabWidget->count() == 0 )
209 switch ( event->type() )
211 case QEvent::MouseButtonPress:
213 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
215 QMouseEvent *me =
static_cast<QMouseEvent *
>( event );
216 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
217 if ( !mClickedAnchor.isEmpty() )
223 case QEvent::MouseButtonRelease:
225 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
227 QMouseEvent *me =
static_cast<QMouseEvent *
>( event );
228 const QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) : QString();
229 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
231 QDesktopServices::openUrl( mClickedAnchor );
242 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.