20 #include "qgssettings.h"
26 #include <QTableWidget>
27 #include <QToolButton>
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 );
50 void QgsMessageLogViewer::showContextMenuForTabBar( QPoint point )
57 mTabBarContextMenu->clear();
59 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 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++ );
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 );
135 QgsSettings settings;
136 QPalette
pal = qApp->palette();
137 QString defaultColorName =
pal.color( QPalette::WindowText ).name();
141 case Qgis::MessageLevel::Info:
142 levelString = QStringLiteral(
"INFO" );
143 colorName = settings.value( QStringLiteral(
"colors/info" ), QString() ).toString();
145 case Qgis::MessageLevel::Warning:
146 levelString = QStringLiteral(
"WARNING" );
147 colorName = settings.value( QStringLiteral(
"colors/warning" ), QString() ).toString();
149 case Qgis::MessageLevel::Critical:
150 levelString = QStringLiteral(
"CRITICAL" );
151 colorName = settings.value( QStringLiteral(
"colors/critical" ), QString() ).toString();
153 case Qgis::MessageLevel::Success:
154 levelString = QStringLiteral(
"SUCCESS" );
155 colorName = settings.value( QStringLiteral(
"colors/success" ), QString() ).toString();
157 case Qgis::MessageLevel::NoLevel:
158 levelString = QStringLiteral(
"NONE" );
159 colorName = settings.value( QStringLiteral(
"colors/default" ), QString() ).toString();
162 QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
164 QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </font>" )
165 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
166 QString cleanedMessage = message;
167 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
168 w->appendHtml( cleanedMessage );
169 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
174 void QgsMessageLogViewer::closeTab(
int index )
176 tabWidget->removeTab( index );
177 if ( tabWidget->count() == 0 )
186 switch ( event->type() )
188 case QEvent::MouseButtonPress:
190 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
192 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
193 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
195 if ( !mClickedAnchor.isEmpty() )
201 case QEvent::MouseButtonRelease:
203 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
205 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
206 QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
208 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
210 QDesktopServices::openUrl( mClickedAnchor );
221 return QDialog::eventFilter(
object, event );
MessageLevel
Level for messages This will be used both for message log and message bar in application.
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.