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 mTabBarContextMenu->exec( tabWidget->tabBar()->mapToGlobal( point ) );
99 QString cleanedTag = tag;
100 if ( cleanedTag.isNull() )
101 cleanedTag = tr(
"General" );
104 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ );
106 QPlainTextEdit *w =
nullptr;
107 if ( i < tabWidget->count() )
109 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
110 tabWidget->setCurrentIndex( i );
114 w =
new QPlainTextEdit(
this );
115 w->setReadOnly(
true );
116 w->viewport()->installEventFilter(
this );
117 tabWidget->addTab( w, cleanedTag );
118 tabWidget->setCurrentIndex( tabWidget->count() - 1 );
123 QPalette
pal = qApp->palette();
124 QString defaultColorName = pal.color( QPalette::WindowText ).name();
129 levelString = QStringLiteral(
"INFO" );
130 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
133 levelString = QStringLiteral(
"WARNING" );
134 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
137 levelString = QStringLiteral(
"CRITICAL" );
138 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
141 levelString = QStringLiteral(
"SUCCESS" );
142 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
145 levelString = QStringLiteral(
"NONE" );
146 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
149 QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
151 QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </font>" )
152 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
153 QString cleanedMessage = message;
154 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
155 w->appendHtml( cleanedMessage );
156 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
159 void QgsMessageLogViewer::closeTab(
int index )
161 if ( tabWidget->count() == 1 )
162 qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
164 tabWidget->removeTab( index );
169 switch ( event->type() )
171 case QEvent::MouseButtonPress:
173 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
175 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
176 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
178 if ( !mClickedAnchor.isEmpty() )
184 case QEvent::MouseButtonRelease:
186 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
188 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
189 QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
191 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
193 QDesktopServices::openUrl( mClickedAnchor );
204 return QDialog::eventFilter(
object, event );
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.
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
void logMessage(const QString &message, const QString &tag, Qgis::MessageLevel level)
Logs a message to the viewer.
void closeEvent(QCloseEvent *e) override
MessageLevel
Level for messages This will be used both for message log and message bar in application.
bool eventFilter(QObject *obj, QEvent *ev) override
A generic dialog widget for displaying QGIS log messages.
static QgsMessageLog * messageLog()
Returns the application's message log.
QgsMessageLogViewer(QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Create a new message log viewer.
Interface for logging messages from QGIS in GUI independent way.