summaryrefslogtreecommitdiffstats
path: root/qt-models/messagehandlermodel.cpp
diff options
context:
space:
mode:
authorGravatar Tomaz Canabrava <tcanabrava@kde.org>2017-06-05 18:16:12 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2017-06-11 13:55:41 -0700
commit0ea6f13891cff45d1da7cb23a5e07a2080827b78 (patch)
treebefb85625f000382fa5263b1cc04911c775cd326 /qt-models/messagehandlermodel.cpp
parente7cd1785c420ba797477c8bd272ee02547731048 (diff)
downloadsubsurface-0ea6f13891cff45d1da7cb23a5e07a2080827b78.tar.gz
Add a messageHandler to take care of qDebug & friends on QML
all qDebug / qCDebug and friends now will be properly logged into developer -> log, on QML. Signed-off-by: Tomaz Canabrava <tcanabrava@kde.org> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-models/messagehandlermodel.cpp')
-rw-r--r--qt-models/messagehandlermodel.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/qt-models/messagehandlermodel.cpp b/qt-models/messagehandlermodel.cpp
new file mode 100644
index 000000000..daedad897
--- /dev/null
+++ b/qt-models/messagehandlermodel.cpp
@@ -0,0 +1,59 @@
+#include "messagehandlermodel.h"
+
+void logMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+ MessageHandlerModel::self()->addLog(type, msg);
+}
+
+MessageHandlerModel * MessageHandlerModel::self()
+{
+ static MessageHandlerModel *self = new MessageHandlerModel();
+ return self;
+}
+
+MessageHandlerModel::MessageHandlerModel(QObject *parent)
+{
+ // no more than one message handler.
+ qInstallMessageHandler(logMessageHandler);
+}
+
+int MessageHandlerModel::rowCount(const QModelIndex& parent) const
+{
+ Q_UNUSED(parent);
+ return m_data.size();
+}
+
+#include <iostream>
+
+void MessageHandlerModel::addLog(QtMsgType type, const QString& message)
+{
+ beginInsertRows(QModelIndex(), rowCount(), rowCount());
+ m_data.append({message, type});
+ endInsertRows();
+}
+
+QVariant MessageHandlerModel::data(const QModelIndex& idx, int role) const
+{
+ switch(role) {
+ case Message:
+ case Qt::DisplayRole:
+ return m_data.at(idx.row()).message;
+ }
+ return QVariant(QString("Role: %1").arg(role));
+};
+
+QHash<int, QByteArray> MessageHandlerModel::roleNames() const {
+ static QHash<int, QByteArray> roles = {
+ {Message, "message"},
+ {Severity, "severity"}
+ };
+ return roles;
+};
+
+void MessageHandlerModel::reset()
+{
+ beginRemoveRows(QModelIndex(), 0, m_data.size()-1);
+ m_data.clear();
+ endRemoveRows();
+
+}