diff --git a/KelvinDashboardGUI/DapCommandController.cpp b/KelvinDashboardGUI/DapCommandController.cpp index e3e791a8bb678b402ca47ff1954a2043c9e5fc66..ceab1158e4b30f74bc0b0861238a22ac2b9af776 100644 --- a/KelvinDashboardGUI/DapCommandController.cpp +++ b/KelvinDashboardGUI/DapCommandController.cpp @@ -30,6 +30,7 @@ void DapCommandController::messageProcessing(const DapRpcMessage &asMessage) /// Process the result of the command execution. void DapCommandController::processCommandResult() { + qInfo() << "processCommandResult()"; DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender()); if (!reply) { qWarning() << "Invalid response received"; @@ -38,6 +39,19 @@ void DapCommandController::processCommandResult() emit sigCommandResult(reply->response().result()); } +/// Handling service response for receiving node logs. +void DapCommandController::processGetNodeLogs() +{ + qInfo() << "processGetNodeLogs()"; + DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender()); + if (!reply) { + qWarning() << "Invalid response received"; + return; + } + emit sigCommandResult(reply->response().result()); + emit sigNodeLogsReceived(reply->response().result().toVariant().toStringList()); +} + /// Show or hide GUI client by clicking on the tray icon. /// @param aIsActivated Accepts true - when requesting to /// display a client, falso - when requesting to hide a client. @@ -51,3 +65,13 @@ void DapCommandController::closeClient() { emit onClientClose(); } + +/// Get node logs. +/// @param aiTimeStamp Timestamp start reading logging. +/// @param aiRowCount Number of lines displayed. +void DapCommandController::getNodeLogs(int aiTimeStamp, int aiRowCount) +{ + qInfo() << QString("getNodeLogs(%1, %2)").arg(aiTimeStamp).arg(aiRowCount); + DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getNodeLogs", aiTimeStamp, aiRowCount); + connect(reply, SIGNAL(finished()), this, SLOT(processGetNodeLogs())); +} diff --git a/KelvinDashboardGUI/DapCommandController.h b/KelvinDashboardGUI/DapCommandController.h index 7e946d342cb248270ef016121ab4b47f38927a8f..568a7b6d5764e42e1eaf71a7b23fafc6f3288455 100644 --- a/KelvinDashboardGUI/DapCommandController.h +++ b/KelvinDashboardGUI/DapCommandController.h @@ -22,6 +22,9 @@ class DapCommandController : public DapRpcService, public DapRpcServiceProvider signals: /// The signal is emitted after receiving a response from the service about the command execution. void sigCommandResult(QJsonValue ); + /// The signal is emitted when node logs are received from the service. + /// @param aNodeLogs List of node logs. + void sigNodeLogsReceived(const QStringList& aNodeLogs); /// The signal is emitted when the main application window is activated. void onClientActivate(bool aIsActivated); @@ -39,6 +42,8 @@ private slots: void messageProcessing(const DapRpcMessage &asMessage); /// Process the result of the command execution. void processCommandResult(); + /// Handling service response for receiving node logs. + void processGetNodeLogs(); public slots: /// Show or hide GUI client by clicking on the tray icon. @@ -47,6 +52,10 @@ public slots: void activateClient(bool aIsActivated); /// Shut down client. void closeClient(); + /// Get node logs. + /// @param aiTimeStamp Timestamp start reading logging. + /// @param aiRowCount Number of lines displayed. + void getNodeLogs(int aiTimeStamp, int aiRowCount); }; #endif // COMMANDCONTROLLER_H diff --git a/KelvinDashboardGUI/DapServiceController.cpp b/KelvinDashboardGUI/DapServiceController.cpp index 1d54b016469bf5e5545dba540d2a37ea666d1e49..e43d06d15b4c689645ae7ee7bf99723622efc99b 100644 --- a/KelvinDashboardGUI/DapServiceController.cpp +++ b/KelvinDashboardGUI/DapServiceController.cpp @@ -1,6 +1,8 @@ #include "DapServiceController.h" #include "DapUiQmlWidgetModel.h" -DapServiceController::DapServiceController(QObject *apParent) +#include "DapLogMessage.h" + +DapServiceController::DapServiceController(QObject *apParent) : QObject(apParent) { @@ -34,6 +36,8 @@ void DapServiceController::init(DapServiceClient *apDapServiceClient) connect(m_pDapCommandController, SIGNAL(onClientActivate(bool)), SLOT(activateClient(bool))); // Signal-slot connection that implements the client display control when you click on the tray icon connect(m_pDapCommandController, SIGNAL(onClientClose()), SLOT(closeClient())); + // Signal-slot connection for receiving node logs from the service + connect(m_pDapCommandController, SIGNAL(sigNodeLogsReceived(QStringList)), SLOT(processGetNodeLogs(QStringList))); } QString DapServiceController::getBrand() const @@ -48,6 +52,44 @@ QString DapServiceController::getVersion() const return m_sVersion; } +/// Get node logs. +/// @param aiTimeStamp Timestamp start reading logging. +/// @param aiRowCount Number of lines displayed. +void DapServiceController::getNodeLogs(int aiTimeStamp, int aiRowCount) const +{ + qInfo() << QString("getNodeLogs(%1, %2)").arg(aiTimeStamp).arg(aiRowCount); + m_pDapCommandController->getNodeLogs(aiTimeStamp, aiRowCount); +} + +/// Handling service response for receiving node logs. +/// @param aNodeLogs List of node logs. +void DapServiceController::processGetNodeLogs(const QStringList &aNodeLogs) +{ + for(QString s : aNodeLogs) + { + qDebug() << s; + QStringList tempList = s.split(" "); + DapLogMessage message; + if(tempList.at(1) == "[INF]") + message.setType(Type::Info); + else if(tempList.at(1) == "[WRN]") + message.setType(Type::Warning); + else if(tempList.at(1) == "[DBG]") + message.setType(Type::Debug); + else if(tempList.at(1) == "[ERR]") + message.setType(Type::Error); + QString str = tempList.at(0); + message.setTimeStamp(str.remove("[").remove("]")); + QStringList tempList2 = tempList.at(2).split("\t"); + QString str2 = tempList2.at(0); + message.setFile(str2.remove("[").remove("]")); + QString str3 = s.split("\t").at(1); + int pos = str3.indexOf('\n'); + message.setMessage(str3.remove(pos, str3.size()-pos)); + DapLogModel::getInstance().append(message); + } +} + /// Get an instance of a class. /// @return Instance of a class. DapServiceController &DapServiceController::getInstance() diff --git a/KelvinDashboardGUI/DapServiceController.h b/KelvinDashboardGUI/DapServiceController.h index 539a9edc98d1fe67c4d96edade9b9f6d5c11164c..cc18d73129fa5d01e71ab90db37b05c47438eec6 100644 --- a/KelvinDashboardGUI/DapServiceController.h +++ b/KelvinDashboardGUI/DapServiceController.h @@ -9,6 +9,7 @@ #include "DapCommandController.h" #include "DapServiceClient.h" +#include "DapLogModel.h" class DapServiceController : public QObject { @@ -52,7 +53,14 @@ public: /// Get app version. /// @return Application version. QString getVersion() const; + /// Get node logs. + /// @param aiTimeStamp Timestamp start reading logging. + /// @param aiRowCount Number of lines displayed. + void getNodeLogs(int aiTimeStamp, int aiRowCount) const; + DapLogModel getLogModel() const; + void setLogModel(const DapLogModel &dapLogModel); + signals: /// The signal is emitted when the Brand company property changes. void brandChanged(const QString &brand); @@ -63,6 +71,11 @@ signals: /// The signal is emitted when checking the existence of an already running copy of the application. void isExistenceClient(bool isExistenceClient); +private slots: + /// Handling service response for receiving node logs. + /// @param aNodeLogs List of node logs. + void processGetNodeLogs(const QStringList& aNodeLogs); + public slots: /// Show or hide GUI client by clicking on the tray icon. /// @param aIsActivated Accepts true - when requesting to diff --git a/KelvinDashboardGUI/DapUiQmlWidgetChainNodeLogs.ui.qml b/KelvinDashboardGUI/DapUiQmlWidgetChainNodeLogs.ui.qml index 7d7f8bd1b993dba75cbd79d579bc5a1ec99c4579..98096425efad25d92d4b5a655e17b4af10329bf6 100644 --- a/KelvinDashboardGUI/DapUiQmlWidgetChainNodeLogs.ui.qml +++ b/KelvinDashboardGUI/DapUiQmlWidgetChainNodeLogs.ui.qml @@ -1,41 +1,115 @@ import QtQuick 2.11 import QtQuick.Controls 1.4 import QtQuick.Controls 2.2 +import KelvinDashboard 1.0 Page { id: dapUiQmlWidgetChainNodeLogs title: "Logs" - TableView { - id: tableViewLogs - anchors.top: parent.top - anchors.bottom: parent.bottom - anchors.left: parent.left - anchors.right: parent.right + TabView + { + id: tabViewLogs + anchors.top: parent.top + anchors.bottom: parent.bottom + anchors.left: parent.left + anchors.right: parent.right + Repeater { + anchors.fill: parent + model: dapUiQmlWidgetModel + delegate: + Tab{ + + + + title: qsTr(name) + + TableView { + id: tableViewLogs +// anchors.top: parent.top +// anchors.bottom: parent.bottom +// anchors.left: parent.left +// anchors.right: parent.right + model: dapLogModel + clip: true + + TableViewColumn { + id: columnType +// role: "type" + title: "Type" + + delegate: + Item{ + Image { + id: names + anchors.centerIn: parent + source: "qrc:/Resources/Icons/dialog.png" + width: 14 + height: 14 + } + } + } + TableViewColumn { + id: columnTime + role: "timestamp" + title: "Timestamp" + delegate: Item { + Text { + anchors.centerIn: parent + renderType: Text.NativeRendering + text: styleData.value + } + } + } + TableViewColumn { + id: columnFile + role: "file" + title: "File" + delegate: Item { + Text { + anchors.centerIn: parent + renderType: Text.NativeRendering + text: styleData.value + } + } + } + TableViewColumn { + id: columnMessage + role: "message" + title: "Message" + delegate: Item { + Text { + renderType: Text.NativeRendering + text: styleData.value + } + } + } - TableViewColumn { - id: columnDate - role: "date" - title: "Date" - } - TableViewColumn { - id: columnTime - role: "time" - title: "Time" - } - TableViewColumn { - id: columnFile - role: "file" - title: "File" - } - TableViewColumn { - id: columnMessage - role: "Message" - title: "Message" + headerDelegate: Rectangle { + height: 20 + color: "red" + + Text { + text: styleData.value + color: "#FFF" + width: parent.width + height: parent.height + font.pointSize: 18 + minimumPointSize: 3 + fontSizeMode: Text.Fit + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + } + } + + } + } } } + + } diff --git a/KelvinDashboardGUI/Resources/Icons/dialog.png b/KelvinDashboardGUI/Resources/Icons/dialog.png new file mode 100644 index 0000000000000000000000000000000000000000..176bc26d4962885d56ed46d4c7fb9c62f195529e Binary files /dev/null and b/KelvinDashboardGUI/Resources/Icons/dialog.png differ diff --git a/KelvinDashboardGUI/main.cpp b/KelvinDashboardGUI/main.cpp index cd871c5cbb2763ff53d4089c7b23a1c78256159c..f2d8ca3f4a36747138b639dd9ca96a6904718d81 100755 --- a/KelvinDashboardGUI/main.cpp +++ b/KelvinDashboardGUI/main.cpp @@ -16,6 +16,8 @@ #include "DapServiceClient.h" #include "DapServiceController.h" #include "DapLogger.h" +#include "DapLogMessage.h" +#include "DapLogModel.h" int main(int argc, char *argv[]) { @@ -41,15 +43,18 @@ int main(int argc, char *argv[]) DapServiceController &controller = DapServiceController::getInstance(); controller.init(&dapServiceClient); dapServiceClient.init(); + controller.getNodeLogs(0, 100); qmlRegisterType<DapScreenDialog>("KelvinDashboard", 1, 0, "DapScreenDialog"); qmlRegisterType<DapScreenDialogChangeWidget>("KelvinDashboard", 1, 0, "DapScreenDialogChangeWidget"); + qmlRegisterType<DapLogMessage>("LogMessage", 1, 0, "DapLogMessage"); qmlRegisterSingletonType<DapServiceController>("KelvinDashboard", 1, 0, "DapServiceController", DapServiceController::singletonProvider); qmlRegisterSingletonType<DapUiQmlWidgetModel>("KelvinDashboard", 1, 0, "DapUiQmlWidgetModel", DapUiQmlWidgetModel::singletonProvider); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("dapServiceController", &DapServiceController::getInstance()); engine.rootContext()->setContextProperty("dapUiQmlWidgetModel", &DapUiQmlWidgetModel::getInstance()); + engine.rootContext()->setContextProperty("dapLogModel", &DapLogModel::getInstance()); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); // DapSettings &settings = DapSettings::getInstance("Settings.json"); diff --git a/KelvinDashboardGUI/qml.qrc b/KelvinDashboardGUI/qml.qrc index 8d93e1b02c9a6c266a93892ce9f6505fecba7074..9d4d9bc8622ffd701129e9a5a849cace8f002132 100755 --- a/KelvinDashboardGUI/qml.qrc +++ b/KelvinDashboardGUI/qml.qrc @@ -22,5 +22,6 @@ <file>DapUiQmlWidgetChainNodeLogs.ui.qml</file> <file>Resources/Icons/add.png</file> <file>Resources/Icons/icon.ico</file> + <file>Resources/Icons/dialog.png</file> </qresource> </RCC> diff --git a/KelvinDashboardService/DapChainDashboardService.cpp b/KelvinDashboardService/DapChainDashboardService.cpp index 5e8841333b6356a0f4ccb73da5eaf5041158ca37..fa6c7d28c7e982ccd04f05517edc41c57190e49b 100755 --- a/KelvinDashboardService/DapChainDashboardService.cpp +++ b/KelvinDashboardService/DapChainDashboardService.cpp @@ -2,6 +2,9 @@ DapChainDashboardService::DapChainDashboardService() : DapRpcService(nullptr) { + // Log reader + m_pDapLogReader = new DapLogReader(this); + connect(this, &DapChainDashboardService::onNewClientConnected, [=] { qDebug() << "New client"; }); @@ -26,6 +29,16 @@ bool DapChainDashboardService::start() return true; } +/// Get node logs. +/// @param aiTimeStamp Timestamp start reading logging. +/// @param aiRowCount Number of lines displayed. +/// @return Logs node. +QStringList DapChainDashboardService::getNodeLogs(int aiTimeStamp, int aiRowCount) +{ + qInfo() << QString("getNodeLogs(%1, %2)").arg(aiTimeStamp).arg(aiRowCount); + return m_pDapLogReader->request(aiTimeStamp, aiRowCount); +} + /// Activate the main client window by double-clicking the application icon in the system tray. /// @param reason Type of action on the icon in the system tray. diff --git a/KelvinDashboardService/DapChainDashboardService.h b/KelvinDashboardService/DapChainDashboardService.h index 06cc40a8b6c27d9b6248301eac01a2f2049c2ff9..5527ef925f1b166f7fd6bbb37188e4ff6ff6b7c0 100755 --- a/KelvinDashboardService/DapChainDashboardService.h +++ b/KelvinDashboardService/DapChainDashboardService.h @@ -23,6 +23,8 @@ #include "DapRpcTCPServer.h" #include "DapRpcService.h" +#include "DapLogReader.h" + #include <QLocalServer> typedef class DapRpcLocalServer DapUiService; typedef class QLocalServer DapUiSocketServer; @@ -31,13 +33,19 @@ class DapChainDashboardService : public DapRpcService { Q_OBJECT Q_CLASSINFO("serviceName", "RPCServer") + /// Service core. DapUiService * m_pServer {nullptr}; + /// Socket of client connection with the service. DapUiSocketServer * m_pSocketService {nullptr}; + /// Log reader. + DapLogReader * m_pDapLogReader {nullptr}; public: /// Standard Ñonstructor. explicit DapChainDashboardService(); bool start(); + + signals: /// The signal is emitted in case of successful connection of a new client. @@ -51,6 +59,11 @@ public slots: void closeClient(); /// System tray initialization. void initTray(); + /// Get node logs. + /// @param aiTimeStamp Timestamp start reading logging. + /// @param aiRowCount Number of lines displayed. + /// @return Logs node. + QStringList getNodeLogs(int aiTimeStamp, int aiRowCount); }; diff --git a/KelvinDashboardService/DapLogReader.cpp b/KelvinDashboardService/DapLogReader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9907b1c834736ec59737ce759b8f1bf47a910fe2 --- /dev/null +++ b/KelvinDashboardService/DapLogReader.cpp @@ -0,0 +1,38 @@ +#include "DapLogReader.h" + + + +DapLogReader::DapLogReader(QObject *parent) : QObject(parent) +{ + +} + +QStringList DapLogReader::parse(const QByteArray &aLogMessages) +{ + QStringList list = QString::fromLatin1(aLogMessages).split(";"); + + auto resultEnd = std::remove_if(list.begin(), list.end(), + [] (const QString& aLogMessage) + { + return !aLogMessage.contains('['); + }); + list.erase(resultEnd, list.end()); + return list; +} + +QStringList DapLogReader::request(int aiTimeStamp, int aiRowCount) +{ + QByteArray result; + QProcess process; + process.start(QString("%1 print_log ts_after %2 limit %3").arg("/home/andrey/Demlabs/build-kelvin-node/kelvin-node-cli").arg(aiTimeStamp).arg(aiRowCount)); + process.waitForFinished(-1); + result = process.readAll(); + + if(result.isEmpty()) + qDebug() << "FALSE"; + else + { + qDebug() << "TRUE"; + } + return parse(result); +} diff --git a/KelvinDashboardService/DapLogReader.h b/KelvinDashboardService/DapLogReader.h new file mode 100644 index 0000000000000000000000000000000000000000..a164d3598eb548be7bcc81ff4ea9186d2cfed976 --- /dev/null +++ b/KelvinDashboardService/DapLogReader.h @@ -0,0 +1,26 @@ +#ifndef DAPLOGREADER_H +#define DAPLOGREADER_H + +#include <QObject> +#include <QString> +#include <QProcess> +#include <algorithm> +#include <QDebug> + +#include "DapLogMessage.h" + +class DapLogReader : public QObject +{ + Q_OBJECT + +protected: + virtual QStringList parse(const QByteArray& aLogMessages); + +public: + explicit DapLogReader(QObject *parent = nullptr); + +public slots: + QStringList request(int aiTimeStamp, int aiRowCount); +}; + +#endif // DAPLOGREADER_H diff --git a/KelvinDashboardService/KelvinDashboardService.pro b/KelvinDashboardService/KelvinDashboardService.pro index 2fecb7026ec2f05b6798b88948c4d95b7dea139c..6f8faa37276801799f612ea58d8d4bd3f0e434d5 100755 --- a/KelvinDashboardService/KelvinDashboardService.pro +++ b/KelvinDashboardService/KelvinDashboardService.pro @@ -40,12 +40,14 @@ SOURCES += \ main.cpp \ DapChainDashboardService.cpp \ DapChainNode.cpp \ - DapChainNodeCache.cpp + DapChainNodeCache.cpp \ + DapLogReader.cpp HEADERS += \ DapChainDashboardService.h \ DapChainNode.h \ - DapChainNodeCache.h + DapChainNodeCache.h \ + DapLogReader.h include (../libdap-qt/libdap-qt.pri) include (../libKelvinDashboardCommon/libKelvinDashboardCommon.pri) diff --git a/KelvinDashboardService/main.cpp b/KelvinDashboardService/main.cpp index 4bbb8de1c27cb7fee8232bafee5f0609a4f2a268..7919e48f203913c5be2eefb9f56fc57c91fce1a8 100755 --- a/KelvinDashboardService/main.cpp +++ b/KelvinDashboardService/main.cpp @@ -5,6 +5,7 @@ #include "DapHalper.h" #include "DapChainDashboardService.h" #include "DapLogger.h" +#include "DapLogReader.h" int main(int argc, char *argv[]) { @@ -26,7 +27,7 @@ int main(int argc, char *argv[]) a.setOrganizationName("DEMLABS"); a.setOrganizationDomain("demlabs.com"); a.setApplicationName("KelvinDashboardService"); - + DapLogger dapLogger; /// TODO: The code is commented out at the time of developing the logging strategy in the project //#ifndef QT_DEBUG @@ -34,7 +35,6 @@ int main(int argc, char *argv[]) dapLogger.setLogFile(QString("/opt/%1/log/%2Service.log").arg(QString(DAP_BRAND)).arg(DAP_BRAND)); #endif //#endif - // Creating the main application object DapChainDashboardService service; service.start(); diff --git a/libKelvinDashboardCommon/DapLogMessage.cpp b/libKelvinDashboardCommon/DapLogMessage.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fec32450ba53bd2f7eeb9101c796953456aa740d --- /dev/null +++ b/libKelvinDashboardCommon/DapLogMessage.cpp @@ -0,0 +1,57 @@ +#include "DapLogMessage.h" + +DapLogMessage::DapLogMessage(const Type &type, const QString ×tamp, const QString &file, const QString &message, QObject *parent) : QObject(parent) +{ + m_type = type; + m_sTimeStamp = timestamp; + m_sFile = file; + m_sMessage = message; +} + +Type DapLogMessage::getType() const +{ + return m_type; +} + +void DapLogMessage::setType(const Type &type) +{ + m_type = type; + + emit typeChanged(m_type); +} + +QString DapLogMessage::getTimeStamp() const +{ + return m_sTimeStamp; +} + +void DapLogMessage::setTimeStamp(const QString &sTimeStamp) +{ + m_sTimeStamp = sTimeStamp; + + emit timeStampChanged(m_sTimeStamp); +} + +QString DapLogMessage::getFile() const +{ + return m_sFile; +} + +void DapLogMessage::setFile(const QString &sFile) +{ + m_sFile = sFile; + + emit fileChanged(m_sFile); +} + +QString DapLogMessage::getMessage() const +{ + return m_sMessage; +} + +void DapLogMessage::setMessage(const QString &sMessage) +{ + m_sMessage = sMessage; + + emit messageChanged(m_sMessage); +} diff --git a/libKelvinDashboardCommon/DapLogMessage.h b/libKelvinDashboardCommon/DapLogMessage.h new file mode 100644 index 0000000000000000000000000000000000000000..c3e0d2d4acd3ef44fc9259fa1a828e19b35aa481 --- /dev/null +++ b/libKelvinDashboardCommon/DapLogMessage.h @@ -0,0 +1,53 @@ +#ifndef DAPLOGMESSAGE_H +#define DAPLOGMESSAGE_H + +#include <QObject> + +enum Type +{ + Info, + Warning, + Debug, + Error +}; + +class DapLogMessage : public QObject +{ + Q_OBJECT + + Type m_type; + QString m_sTimeStamp; + QString m_sFile; + QString m_sMessage; + +public: + explicit DapLogMessage(QObject *parent = nullptr) {} + DapLogMessage(const Type &type, const QString ×tamp, const QString &file, const QString &message, QObject *parent = nullptr); + + + Q_PROPERTY(Type type READ getType WRITE setType NOTIFY typeChanged) + Q_PROPERTY(QString timestamp READ getTimeStamp WRITE setTimeStamp NOTIFY timeStampChanged) + Q_PROPERTY(QString file READ getFile WRITE setFile NOTIFY fileChanged) + Q_PROPERTY(QString message READ getMessage WRITE setMessage NOTIFY messageChanged) + + Type getType() const; + void setType(const Type &type); + + QString getTimeStamp() const; + void setTimeStamp(const QString &sTimeStamp); + + QString getFile() const; + void setFile(const QString &sFile); + + QString getMessage() const; + void setMessage(const QString &sMessage); + +signals: + void typeChanged(Type aType); + void timeStampChanged(const QString& aTimeStamp); + void fileChanged(const QString& aFile); + void messageChanged(const QString& aMessage); + +}; + +#endif // DAPLOGMESSAGE_H diff --git a/libKelvinDashboardCommon/DapLogModel.cpp b/libKelvinDashboardCommon/DapLogModel.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ad8d7d4f3c080b2b11bac5a6cd55b4677ef4abd6 --- /dev/null +++ b/libKelvinDashboardCommon/DapLogModel.cpp @@ -0,0 +1,98 @@ +#include "DapLogModel.h" + +DapLogModel::DapLogModel(QObject *parent) : QAbstractListModel(parent) +{ + +} + +DapLogModel &DapLogModel::getInstance() +{ + static DapLogModel instance; + return instance; +} + +int DapLogModel::rowCount(const QModelIndex &) const +{ + return m_dapLogMessage.count(); +} + +QVariant DapLogModel::data(const QModelIndex &index, int role) const +{ + if (index.row() < rowCount()) + switch (role) { + case TypeRole: return m_dapLogMessage.at(index.row())->getType(); + case TimeStampRole: return m_dapLogMessage.at(index.row())->getTimeStamp(); + case FileRole: return m_dapLogMessage.at(index.row())->getFile(); + case MessageRole: return m_dapLogMessage.at(index.row())->getMessage(); + default: + return QVariant(); + } + return QVariant(); +} + +QHash<int, QByteArray> DapLogModel::roleNames() const +{ + static const QHash<int, QByteArray> roles { + { TypeRole, "type" }, + { TimeStampRole, "timestamp" }, + { FileRole, "file" }, + { MessageRole, "message" } + }; + + return roles; +} + +QVariantMap DapLogModel::get(int row) const +{ + const DapLogMessage *widget = m_dapLogMessage.value(row); + return { {"type", widget->getType()}, {"timestamp", widget->getTimeStamp()}, {"file", widget->getFile()}, {"message", widget->getMessage()} }; +} + +void DapLogModel::append(const DapLogMessage &message) +{ + this->append(message.getType(), message.getTimeStamp(), message.getFile(), message.getMessage()); +} + +void DapLogModel::append(const Type &type, const QString ×tamp, const QString &file, const QString &message) +{ + int row = 0; + while (row < m_dapLogMessage.count() && timestamp > m_dapLogMessage.at(row)->getTimeStamp()) + ++row; + beginInsertRows(QModelIndex(), row, row); + m_dapLogMessage.insert(row, new DapLogMessage(type, timestamp, file, message)); + endInsertRows(); +} + +void DapLogModel::set(int row, const Type &type, const QString ×tamp, const QString &file, const QString &message) +{ + if (row < 0 || row >= m_dapLogMessage.count()) + return; + + DapLogMessage *widget = m_dapLogMessage.value(row); + widget->setType(type); + widget->setTimeStamp(timestamp); + widget->setFile(file); + widget->setMessage(message); + dataChanged(index(row, 0), index(row, 0), { TypeRole, TimeStampRole, FileRole, MessageRole }); +} + +void DapLogModel::remove(int row) +{ + if (row < 0 || row >= m_dapLogMessage.count()) + return; + + beginRemoveRows(QModelIndex(), row, row); + m_dapLogMessage.removeAt(row); + endRemoveRows(); +} + +/// Method that implements the singleton pattern for the qml layer. +/// @param engine QML application. +/// @param scriptEngine The QJSEngine class provides an environment for evaluating JavaScript code. +QObject *DapLogModel::singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine) +{ + Q_UNUSED(engine) + Q_UNUSED(scriptEngine) + + return &getInstance(); +} diff --git a/libKelvinDashboardCommon/DapLogModel.h b/libKelvinDashboardCommon/DapLogModel.h new file mode 100644 index 0000000000000000000000000000000000000000..0bb8decba70e3f9a66206f4deea4055dd935f8e0 --- /dev/null +++ b/libKelvinDashboardCommon/DapLogModel.h @@ -0,0 +1,55 @@ +#ifndef DAPLOGMODEL_H +#define DAPLOGMODEL_H + +#include <QObject> +#include <QAbstractListModel> +#include <QList> +#include <QQmlEngine> +#include <QJSEngine> +#include <QXmlStreamWriter> +#include <QXmlStreamReader> +#include <QXmlStreamAttribute> + +#include "DapLogMessage.h" + +enum DapLogRole { + TypeRole = Qt::DisplayRole, + TimeStampRole = Qt::UserRole, + FileRole, + MessageRole + }; + +class DapLogModel : public QAbstractListModel +{ + Q_OBJECT + + QList<DapLogMessage*> m_dapLogMessage; + + DapLogModel(QObject *parent = nullptr); +public: + + /// Get an instance of a class. + /// @return Instance of a class. + Q_INVOKABLE static DapLogModel &getInstance(); + + + Q_ENUM(DapLogRole) + + int rowCount(const QModelIndex & = QModelIndex()) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + QHash<int, QByteArray> roleNames() const; + + Q_INVOKABLE QVariantMap get(int row) const; + Q_INVOKABLE void append(const DapLogMessage &message); + Q_INVOKABLE void append(const Type &type, const QString ×tamp, const QString &file, const QString &message); + Q_INVOKABLE void set(int row, const Type &type, const QString ×tamp, const QString &file, const QString &message); + Q_INVOKABLE void remove(int row); + +public slots: + /// Method that implements the singleton pattern for the qml layer. + /// @param engine QML application. + /// @param scriptEngine The QJSEngine class provides an environment for evaluating JavaScript code. + static QObject *singletonProvider(QQmlEngine *engine, QJSEngine *scriptEngine); +}; + +#endif // DAPLOGMODEL_H diff --git a/libKelvinDashboardCommon/libKelvinDashboardCommon.pri b/libKelvinDashboardCommon/libKelvinDashboardCommon.pri index 3dfc99342e9488d181c765703c467353d6512265..ee6bc4c894696ea960690aa0efeb2f04dd0ff66c 100755 --- a/libKelvinDashboardCommon/libKelvinDashboardCommon.pri +++ b/libKelvinDashboardCommon/libKelvinDashboardCommon.pri @@ -14,9 +14,13 @@ QT += quick quickwidgets SOURCES +=\ $$PWD/DapHalper.cpp \ $$PWD/DapSettings.cpp \ - $$PWD/DapSettingsCipher.cpp + $$PWD/DapSettingsCipher.cpp \ + $$PWD/DapLogMessage.cpp \ + $$PWD/DapLogModel.cpp HEADERS +=\ $$PWD/DapHalper.h \ $$PWD/DapSettings.h \ - $$PWD/DapSettingsCipher.h + $$PWD/DapSettingsCipher.h \ + $$PWD/DapLogMessage.h \ + $$PWD/DapLogModel.h