From 2affa115ff0e78d5e0cf8d5dd3b6b60878db8e9e Mon Sep 17 00:00:00 2001 From: Ali Hatami Date: Sun, 2 Jun 2024 17:32:37 +0330 Subject: [PATCH] Add basic log table model --- gui/gui.pro | 6 ++- gui/main.cpp | 12 +++-- gui/viewmodel/table/LogTableModel.cpp | 65 +++++++++++++++++++++++++++ gui/viewmodel/table/LogTableModel.h | 51 +++++++++++++++++++++ 4 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 gui/viewmodel/table/LogTableModel.cpp create mode 100644 gui/viewmodel/table/LogTableModel.h diff --git a/gui/gui.pro b/gui/gui.pro index 8ec7fb8..f16d8f5 100644 --- a/gui/gui.pro +++ b/gui/gui.pro @@ -17,10 +17,12 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp \ - view/MainWindow.cpp + view/MainWindow.cpp \ + viewmodel/table/LogTableModel.cpp HEADERS += \ - view/MainWindow.h + view/MainWindow.h \ + viewmodel/table/LogTableModel.h FORMS += \ view/MainWindow.ui diff --git a/gui/main.cpp b/gui/main.cpp index 582f6dd..ca3ee16 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -7,18 +7,22 @@ #include #include +#include "parser/JsonParser.h" +#include "viewmodel/table/LogTableModel.h" + int main(int argc, char* argv[]) { QApplication a(argc, argv); MainWindow w; QString jsonString = R"( - [{"id":"stderr","level":1,"location":{"file":"../../../host-projects/sono/logic/src/viewModel/factory/ApplicationStarter.cpp","func":"init","lineno":69},"msg":["FUCK\n"],"scope":"global","threadId":124330260010752,"timestamp":1716911725703}] + {"id":"stderr","level":1,"location":{"file":"../../../host-projects/sono/logic/src/viewModel/factory/ApplicationStarter.cpp","func":"init","lineno":69},"msg":["FUCK\n"],"scope":"global","threadId":124330260010752,"timestamp":1716911725703} )"; - QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8()); - QJsonArray jsonArray = jsonDoc.array(); - + auto warehouse = std::make_shared(); + warehouse->addData(JsonParser().parse(jsonString)); + auto model = new LogTableModel(warehouse); + w.setTable(model); w.show(); return a.exec(); diff --git a/gui/viewmodel/table/LogTableModel.cpp b/gui/viewmodel/table/LogTableModel.cpp new file mode 100644 index 0000000..a9d6795 --- /dev/null +++ b/gui/viewmodel/table/LogTableModel.cpp @@ -0,0 +1,65 @@ +#include "viewmodel/table/LogTableModel.h" + +/*************************************************************************************************/ +LogTableModel::LogTableModel(LogWarehouse_ptr warehouse) : _warehouse(warehouse) +{ + initColumnMap(); +} + +/*************************************************************************************************/ +LogTableModel::~LogTableModel() +{ +} + +/*************************************************************************************************/ +int LogTableModel::rowCount(const QModelIndex& parent) const { + Q_UNUSED(parent) + + auto size = _warehouse->getLength(); + + return size; +} + +/*************************************************************************************************/ +int LogTableModel::columnCount(const QModelIndex& parent) const { + Q_UNUSED(parent) + + return _columnMap.size(); +} + +/*************************************************************************************************/ +QVariant LogTableModel::data(const QModelIndex& index, int role) const { + if(!index.isValid() || role != Qt::DisplayRole) + { + return QVariant(); + } + + return QVariant(); +} + +/*************************************************************************************************/ +QVariant LogTableModel::headerData(int section, Qt::Orientation orientation, int role) const { + if(role == Qt::DisplayRole) + { + if(orientation == Qt::Horizontal) + { + return _columnMap[section]; + } + } + + return QVariant(); +} + +/*************************************************************************************************/ +void LogTableModel::initColumnMap() +{ + _columnMap[id] = "Logger Id"; + _columnMap[level] = "Level"; + _columnMap[timestamp] = "Time"; + _columnMap[file] = "File Address"; + _columnMap[func] = "Function"; + _columnMap[lineno] = "Line Number"; + _columnMap[msg] = "Message"; + _columnMap[scope] = "Scope"; + _columnMap[threadId] = "Thread"; +} diff --git a/gui/viewmodel/table/LogTableModel.h b/gui/viewmodel/table/LogTableModel.h new file mode 100644 index 0000000..3cc9810 --- /dev/null +++ b/gui/viewmodel/table/LogTableModel.h @@ -0,0 +1,51 @@ +#ifndef BASEJSONMODEL_H +#define BASEJSONMODEL_H + +#include +#include +#include +#include +#include + +#include +#include "LogWarehouse.h" + +class LogTableModel : public QAbstractTableModel +{ + Q_OBJECT + +public: + LogTableModel(LogWarehouse_ptr warehouse); + + ~LogTableModel() override; + + int rowCount(const QModelIndex& parent = QModelIndex()) const override; + + int columnCount(const QModelIndex& parent = QModelIndex()) const override; + + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; + + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const override; + +private: + enum ELogColumn : int + { + id, + level, + timestamp, + file, + func, + lineno, + msg, + scope, + threadId, + }; + + QMap _columnMap; + LogWarehouse_ptr _warehouse; + + void initColumnMap(); +}; + +#endif //BASEJSONMODEL_H