26 #include <QTableWidget> 27 #include <QToolButton> 30 #include <QPlainTextEdit> 33 #include <QDesktopServices> 36 : QDialog( parent, fl )
43 connect( tabWidget, &QTabWidget::tabCloseRequested,
this, &QgsMessageLogViewer::closeTab );
57 QString cleanedTag = tag;
58 if ( cleanedTag.isNull() )
59 cleanedTag = tr(
"General" );
62 for ( i = 0; i < tabWidget->count() && tabWidget->tabText( i ).remove( QChar(
'&' ) ) != cleanedTag; i++ );
64 QPlainTextEdit *w =
nullptr;
65 if ( i < tabWidget->count() )
67 w = qobject_cast<QPlainTextEdit *>( tabWidget->widget( i ) );
68 tabWidget->setCurrentIndex( i );
72 w =
new QPlainTextEdit(
this );
73 w->setReadOnly(
true );
74 w->viewport()->installEventFilter(
this );
75 tabWidget->addTab( w, cleanedTag );
76 tabWidget->setCurrentIndex( tabWidget->count() - 1 );
81 QPalette
pal = qApp->palette();
82 QString defaultColorName = pal.color( QPalette::WindowText ).name();
87 levelString = QStringLiteral(
"INFO" );
88 colorName = settings.
value( QStringLiteral(
"colors/info" ), QString() ).toString();
91 levelString = QStringLiteral(
"WARNING" );
92 colorName = settings.
value( QStringLiteral(
"colors/warning" ), QString() ).toString();
95 levelString = QStringLiteral(
"CRITICAL" );
96 colorName = settings.
value( QStringLiteral(
"colors/critical" ), QString() ).toString();
99 levelString = QStringLiteral(
"SUCCESS" );
100 colorName = settings.
value( QStringLiteral(
"colors/success" ), QString() ).toString();
103 levelString = QStringLiteral(
"NONE" );
104 colorName = settings.
value( QStringLiteral(
"colors/default" ), QString() ).toString();
107 QColor color = QColor( !colorName.isEmpty() ? colorName : defaultColorName );
109 QString prefix = QStringLiteral(
"<font color=\"%1\">%2 %3 </font>" )
110 .arg( color.name(), QDateTime::currentDateTime().toString( Qt::ISODate ), levelString );
111 QString cleanedMessage = message;
112 cleanedMessage = cleanedMessage.prepend( prefix ).replace(
'\n', QLatin1String(
"<br> " ) );
113 w->appendHtml( cleanedMessage );
114 w->verticalScrollBar()->setValue( w->verticalScrollBar()->maximum() );
117 void QgsMessageLogViewer::closeTab(
int index )
119 if ( tabWidget->count() == 1 )
120 qobject_cast<QPlainTextEdit *>( tabWidget->widget( 0 ) )->clear();
122 tabWidget->removeTab( index );
127 switch ( event->type() )
129 case QEvent::MouseButtonPress:
131 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
133 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
134 mClickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
136 if ( !mClickedAnchor.isEmpty() )
142 case QEvent::MouseButtonRelease:
144 if ( QPlainTextEdit *te = qobject_cast<QPlainTextEdit *>( object->parent() ) )
146 QMouseEvent *me =
static_cast< QMouseEvent *
>( event );
147 QString clickedAnchor = ( me->button() & Qt::LeftButton ) ? te->anchorAt( me->pos() ) :
149 if ( !clickedAnchor.isEmpty() && clickedAnchor == mClickedAnchor )
151 QDesktopServices::openUrl( mClickedAnchor );
162 return QDialog::eventFilter(
object, event );
This class is a composition of two QSettings instances:
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
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
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.