QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsquickmessagelogmodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsquickmessagelogmodel.cpp
3  --------------------------------------
4  date : 13.7.2016
5  copyright : (C) 2016 by Matthias Kuhn
6  email : matthias (at) opengis.ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgis.h"
17 #include "qgslogger.h"
18 #include "qgsmessagelog.h"
19 #include "qgsapplication.h"
20 
22 
24  : QAbstractListModel( parent )
25 {
26  connect( QgsApplication::messageLog(), static_cast<void ( QgsMessageLog::* )( const QString &message, const QString &tag, Qgis::MessageLevel level )>( &QgsMessageLog::messageReceived ), this, &QgsQuickMessageLogModel::onMessageReceived );
27 }
28 
29 QgsQuickMessageLogModel::LogMessage QgsQuickMessageLogModel::logMessage( const QString &tag, const QString &message, Qgis::MessageLevel level )
30 {
31  LogMessage msg;
32  msg.tag = tag;
33  msg.message = message;
34  msg.level = level;
35 
36  return msg;
37 }
38 
39 QHash<int, QByteArray> QgsQuickMessageLogModel::roleNames() const
40 {
41  QHash<int, QByteArray> roles = QAbstractListModel::roleNames();
42  roles[MessageRole] = QByteArrayLiteral( "Message" );
43  roles[MessageTagRole] = QByteArrayLiteral( "MessageTag" );
44  roles[MessageLevelRole] = QByteArrayLiteral( "MessageLevel" );
45 
46  return roles;
47 }
48 
49 int QgsQuickMessageLogModel::rowCount( const QModelIndex &parent ) const
50 {
51  Q_UNUSED( parent )
52  return mMessages.size();
53 }
54 
55 QVariant QgsQuickMessageLogModel::data( const QModelIndex &index, int role ) const
56 {
57  if ( index.row() >= mMessages.size() )
58  return QVariant();
59 
60  if ( role == MessageRole )
61  return mMessages.at( index.row() ).message;
62  else if ( role == MessageTagRole )
63  return mMessages.at( index.row() ).tag;
64  else if ( role == MessageLevelRole )
65  return mMessages.at( index.row() ).level;
66 
67  return QVariant();
68 }
69 
70 void QgsQuickMessageLogModel::onMessageReceived( const QString &message, const QString &tag, Qgis::MessageLevel level )
71 {
72  beginInsertRows( QModelIndex(), 0, 0 );
73  mMessages.prepend( logMessage( tag, message, level ) );
74  QgsDebugMsg( QStringLiteral( "Next message %1 : %2" ).arg( tag, message ) );
75  endInsertRows();
76 }
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
void messageReceived(const QString &message, const QString &tag, Qgis::MessageLevel level)
Emitted whenever the log receives a message.
MessageLevel
Level for messages This will be used both for message log and message bar in application.
Definition: qgis.h:66
static QgsMessageLog * messageLog()
Returns the application&#39;s message log.
QgsQuickMessageLogModel(QObject *parent=nullptr)
Create new message log model.
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent) const override
QHash< int, QByteArray > roleNames() const override
Interface for logging messages from QGIS in GUI independent way.
Definition: qgsmessagelog.h:38