diff --git a/plugins/devtoolsplugin/devtoolsplugin.cpp b/plugins/devtoolsplugin/devtoolsplugin.cpp index db8ffd3..df80d05 100644 --- a/plugins/devtoolsplugin/devtoolsplugin.cpp +++ b/plugins/devtoolsplugin/devtoolsplugin.cpp @@ -1,15 +1,34 @@ #include "devtoolsplugin.h" +#include + #include #include #include #include +#include +#include #include "mainwindow.h" +#include "logmodel.h" +#include "logdialog.h" + +std::shared_ptr model; +QtMessageHandler previousHandler; + +void myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message) +{ + previousHandler(type, context, message); + + if(!model) + model = std::make_shared(); + model->log(type, context.file, context.line, context.function, context.category, message); +} + void registerMessageHandler() { - qDebug() << "called"; + previousHandler = qInstallMessageHandler(myMessageHandler); } Q_COREAPP_STARTUP_FUNCTION(registerMessageHandler) @@ -36,5 +55,7 @@ DevToolsPlugin::DevToolsPlugin(QObject *parent) : void DevToolsPlugin::attachTo(MainWindow &mainWindow) { - //TODO + auto dialog = new LogDialog(&mainWindow); + dialog->setModel(model.get()); + mainWindow.menuTools()->addAction(tr("Show log"), dialog, &QDialog::open); } diff --git a/plugins/devtoolsplugin/devtoolsplugin.pro b/plugins/devtoolsplugin/devtoolsplugin.pro index b22008b..d856846 100644 --- a/plugins/devtoolsplugin/devtoolsplugin.pro +++ b/plugins/devtoolsplugin/devtoolsplugin.pro @@ -14,11 +14,16 @@ DEPENDPATH += $$PWD/../../zeiterfassungcorelib $$PWD/../../zeiterfassungguilib DEFINES += QT_DEPRECATED_WARNINGS QT_DISABLE_DEPRECATED_BEFORE=0x060000 QT_MESSAGELOGCONTEXT -HEADERS += devtoolsplugin.h +HEADERS += devtoolsplugin.h \ + logmodel.h \ + logdialog.h -SOURCES += devtoolsplugin.cpp +SOURCES += devtoolsplugin.cpp \ + logmodel.cpp \ + logdialog.cpp -FORMS += +FORMS += \ + logdialog.ui RESOURCES += diff --git a/plugins/devtoolsplugin/logdialog.cpp b/plugins/devtoolsplugin/logdialog.cpp new file mode 100644 index 0000000..8605398 --- /dev/null +++ b/plugins/devtoolsplugin/logdialog.cpp @@ -0,0 +1,19 @@ +#include "logdialog.h" +#include "ui_logdialog.h" + +LogDialog::LogDialog(QWidget *parent) : + QDialog(parent), + ui(new Ui::LogDialog) +{ + ui->setupUi(this); +} + +LogDialog::~LogDialog() +{ + delete ui; +} + +void LogDialog::setModel(QAbstractItemModel *model) +{ + ui->treeView->setModel(model); +} diff --git a/plugins/devtoolsplugin/logdialog.h b/plugins/devtoolsplugin/logdialog.h new file mode 100644 index 0000000..52ae161 --- /dev/null +++ b/plugins/devtoolsplugin/logdialog.h @@ -0,0 +1,24 @@ +#ifndef LOGDIALOG_H +#define LOGDIALOG_H + +#include + +class QAbstractItemModel; + +namespace Ui { class LogDialog; } + +class LogDialog : public QDialog +{ + Q_OBJECT + +public: + explicit LogDialog(QWidget *parent = 0); + ~LogDialog(); + + void setModel(QAbstractItemModel *model); + +private: + Ui::LogDialog *ui; +}; + +#endif // LOGDIALOG_H diff --git a/plugins/devtoolsplugin/logdialog.ui b/plugins/devtoolsplugin/logdialog.ui new file mode 100644 index 0000000..eb5c262 --- /dev/null +++ b/plugins/devtoolsplugin/logdialog.ui @@ -0,0 +1,67 @@ + + + LogDialog + + + + 0 + 0 + 400 + 300 + + + + Log + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + + + + buttonBox + accepted() + LogDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + LogDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/plugins/devtoolsplugin/logmodel.cpp b/plugins/devtoolsplugin/logmodel.cpp new file mode 100644 index 0000000..2da7363 --- /dev/null +++ b/plugins/devtoolsplugin/logmodel.cpp @@ -0,0 +1,70 @@ +#include "logmodel.h" + +LogModel::LogModel(QObject *parent) : + QAbstractListModel(parent) +{ +} + +void LogModel::log(QtMsgType type, const char *fileName, int lineNumber, const char *functionName, const char *categoryName, const QString &message) +{ + beginInsertRows(QModelIndex(), m_entries.count(), m_entries.count()); + m_entries.append(Entry { QDateTime::currentDateTime(), type, fileName, lineNumber, functionName, categoryName, message }); + endInsertRows(); +} + +int LogModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return m_entries.count(); +} + +int LogModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent) + return 4; +} + +QVariant LogModel::data(const QModelIndex &index, int role) const +{ + Q_ASSERT(index.row() < m_entries.count()); + const auto &entry = m_entries.at(index.row()); + + switch(role) + { + case Qt::DisplayRole: + case Qt::EditRole: + switch(index.column()) + { + case 0: return entry.type; + case 1: return entry.dateTime.toString(QStringLiteral("dd.MM.yyyy HH:mm:ss.zzz")); + case 2: return entry.functionName; + case 3: return entry.message; + } + } + + return QVariant(); +} + +QVariant LogModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + switch(orientation) + { + case Qt::Horizontal: + switch(role) + { + case Qt::DisplayRole: + case Qt::EditRole: + switch(section) + { + case 0: return tr("Type"); + case 1: return tr("Timestamp"); + case 2: return tr("Function"); + case 3: return tr("Message"); + } + } + default: + qt_noop(); + } + + return QVariant(); +} diff --git a/plugins/devtoolsplugin/logmodel.h b/plugins/devtoolsplugin/logmodel.h new file mode 100644 index 0000000..75d9eb5 --- /dev/null +++ b/plugins/devtoolsplugin/logmodel.h @@ -0,0 +1,39 @@ +#ifndef LOGMODEL_H +#define LOGMODEL_H + +#include +#include +#include +#include + +class LogModel : public QAbstractListModel +{ + Q_OBJECT + +public: + explicit LogModel(QObject *parent = Q_NULLPTR); + + void log(QtMsgType type, const char *fileName, int lineNumber, const char *functionName, const char *categoryName, const QString &message); + + // QAbstractItemModel interface + int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; + int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE; + QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE; + QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE; + +private: + struct Entry + { + QDateTime dateTime; + QtMsgType type; + const char *fileName; + int lineNumber; + const char *functionName; + const char *categoryName; + QString message; + }; + + QList m_entries; +}; + +#endif // LOGMODEL_H