From c33a82ea2f84caa6ea35b59be277ff02ea771560 Mon Sep 17 00:00:00 2001 From: Evgenii Tagiltsev <tagiltsev.ebgenii@gmail.com> Date: Wed, 14 Aug 2019 18:35:04 +0200 Subject: [PATCH] [+] added sort and filter --- KelvinDashboardGUI/DapCommandController.cpp | 13 ++++ KelvinDashboardGUI/DapCommandController.h | 8 +++ .../DapScreenHistoryFilterModel.cpp | 53 ++++++++++++++ .../DapScreenHistoryFilterModel.h | 32 +++++++++ KelvinDashboardGUI/DapScreenHistoryModel.cpp | 69 ++++++++++--------- KelvinDashboardGUI/DapScreenHistoryModel.h | 35 ++++++---- KelvinDashboardGUI/DapServiceController.cpp | 7 ++ KelvinDashboardGUI/DapServiceController.h | 3 + KelvinDashboardGUI/DapUiQmlScreenHistory.qml | 13 ++-- KelvinDashboardGUI/KelvinDashboardGUI.pro | 2 + KelvinDashboardGUI/main.cpp | 9 ++- 11 files changed, 195 insertions(+), 49 deletions(-) create mode 100644 KelvinDashboardGUI/DapScreenHistoryFilterModel.cpp create mode 100644 KelvinDashboardGUI/DapScreenHistoryFilterModel.h diff --git a/KelvinDashboardGUI/DapCommandController.cpp b/KelvinDashboardGUI/DapCommandController.cpp index ace19cf63..e475a6b35 100755 --- a/KelvinDashboardGUI/DapCommandController.cpp +++ b/KelvinDashboardGUI/DapCommandController.cpp @@ -52,6 +52,12 @@ void DapCommandController::getNodeLogs() connect(reply, SIGNAL(finished()), this, SLOT(processGetNodeLogs())); } +void DapCommandController::setListenerHistory() +{ + DapRpcServiceReply *reply = m_DAPRpcSocket->invokeRemoteMethod("RPCServer.getHistory"); + connect(reply, SIGNAL(finished()), this, SLOT(processGetHistory())); +} + void DapCommandController::processChangedLog() { // QStringList tempLogModel; @@ -173,6 +179,13 @@ void DapCommandController::processExecuteCommand() emit executeCommandChanged(result); } +void DapCommandController::processGetHistory() +{ + qInfo() << "processGetHistory()"; + DapRpcServiceReply *reply = static_cast<DapRpcServiceReply *>(sender()); + emit sendHistory(reply->response().result().toVariant()); +} + /// 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. diff --git a/KelvinDashboardGUI/DapCommandController.h b/KelvinDashboardGUI/DapCommandController.h index bac1d3651..75c454676 100755 --- a/KelvinDashboardGUI/DapCommandController.h +++ b/KelvinDashboardGUI/DapCommandController.h @@ -46,6 +46,9 @@ signals: void onClearLogModel(); void onLogModel(); + + void sendHistory(const QVariant& aData); + public: /// Overloaded constructor. /// @param apIODevice Data transfer device. @@ -72,6 +75,9 @@ private slots: void processGetNodeStatus(); void processExecuteCommand(); + + void processGetHistory(); + public slots: /// Show or hide GUI client by clicking on the tray icon. /// @param aIsActivated Accepts true - when requesting to @@ -103,6 +109,8 @@ public slots: void clearLogModel(); /// Get node logs. void getNodeLogs(); + + void setListenerHistory(); }; #endif // COMMANDCONTROLLER_H diff --git a/KelvinDashboardGUI/DapScreenHistoryFilterModel.cpp b/KelvinDashboardGUI/DapScreenHistoryFilterModel.cpp new file mode 100644 index 000000000..ed91771e3 --- /dev/null +++ b/KelvinDashboardGUI/DapScreenHistoryFilterModel.cpp @@ -0,0 +1,53 @@ +#include "DapScreenHistoryFilterModel.h" + +DapScreenHistoryFilterModel::DapScreenHistoryFilterModel(QObject *parent) : + QSortFilterProxyModel(parent) +{ + +} + +DapScreenHistoryFilterModel& DapScreenHistoryFilterModel::getInstance() +{ + static DapScreenHistoryFilterModel instance; + return instance; +} + +void DapScreenHistoryFilterModel::setFilterWallet(const QString& aWalletNumber) +{ + if(m_walletNumber == aWalletNumber) return; + m_walletNumber = aWalletNumber; +} + +void DapScreenHistoryFilterModel::setFilterDate(const QDateTime& aDateLeft, const QDateTime& aDateRight) +{ + if(m_dateLeft == aDateLeft || m_dateRight == aDateRight) return; + m_dateLeft = aDateLeft; + m_dateRight = aDateRight; +} + +void DapScreenHistoryFilterModel::setFilterStatus(const DapScreenHistoryModel::DapTransactionStatus aStatus) +{ + if(m_status == aStatus) return; + m_status = aStatus; +} + +bool DapScreenHistoryFilterModel::lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const +{ + QString first = source_left.data().toString(); + QString second = source_right.data().toString(); + return first < second; +} + +bool DapScreenHistoryFilterModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const +{ + QDateTime time; + QModelIndex index = sourceModel()->index(source_row, 0, source_parent); + QString timeStr = index.data(DapScreenHistoryModel::DisplayDateRole).toString(); + + if(timeStr == tr("Today")) time = QDateTime::currentDateTime(); + else time = QDateTime::fromString(MASK_FOR_MODEL); + + return (index.data(DapScreenHistoryModel::DisplayNumberWalletRole).toString() == m_walletNumber) || + (index.data(DapScreenHistoryModel::DisplayStatusRole).toInt() == m_status) || + (time >= m_dateLeft && time <= m_dateRight); +} diff --git a/KelvinDashboardGUI/DapScreenHistoryFilterModel.h b/KelvinDashboardGUI/DapScreenHistoryFilterModel.h new file mode 100644 index 000000000..44f63308f --- /dev/null +++ b/KelvinDashboardGUI/DapScreenHistoryFilterModel.h @@ -0,0 +1,32 @@ +#ifndef DAPSCREENHISTORYFILTERMODEL_H +#define DAPSCREENHISTORYFILTERMODEL_H + +#include <QSortFilterProxyModel> + +#include "DapScreenHistoryModel.h" + +class DapScreenHistoryFilterModel : public QSortFilterProxyModel +{ + Q_OBJECT + +private: + QString m_walletNumber; + QDateTime m_dateLeft; + QDateTime m_dateRight; + int m_status; + +protected: + bool lessThan(const QModelIndex& source_left, const QModelIndex& source_right) const; + bool filterAcceptsRow(int source_row, const QModelIndex& source_parent) const; + +public: + explicit DapScreenHistoryFilterModel(QObject *parent = nullptr); + static DapScreenHistoryFilterModel &getInstance(); + +public slots: + void setFilterWallet(const QString& aWalletNumber); + void setFilterDate(const QDateTime& aDateLeft, const QDateTime& aDateRight); + void setFilterStatus(const DapScreenHistoryModel::DapTransactionStatus aStatus); +}; + +#endif // DAPSCREENHISTORYFILTERMODEL_H diff --git a/KelvinDashboardGUI/DapScreenHistoryModel.cpp b/KelvinDashboardGUI/DapScreenHistoryModel.cpp index 9a6c0c361..e37605085 100644 --- a/KelvinDashboardGUI/DapScreenHistoryModel.cpp +++ b/KelvinDashboardGUI/DapScreenHistoryModel.cpp @@ -3,38 +3,7 @@ DapScreenHistoryModel::DapScreenHistoryModel(QObject *parent) : QAbstractListModel(parent) { - for(int i = 0; i < 5; i++) - { - DapTransactionItem element; - element.TokenName = QString("token %1").arg(i); - element.Date = "today"; - element.WalletNumber = "number wallet"; - element.Status = "Sent"; - element.Currency = QString("$ 1020201010%1").arg(i); - element.Cryptocurrency = QString("KLV 4443222111%1").arg(i); - m_elementList.append(element); - } - - for(int i = 0; i < 5; i++) - { - DapTransactionItem element; - element.TokenName = QString("token %1").arg(i); - element.Date = "yesterday"; - element.WalletNumber = "number wallet"; - element.Status = "Error"; - element.Currency = QString("$ 15647475623820%1").arg(i); - element.Cryptocurrency = QString("KLV 454535453%1").arg(i); - m_elementList.append(element); - } -// DapTransactionItem element; -// element.TokenName = QString("token new"); -// element.Date = "today"; -// element.WalletNumber = "number wallet 1221"; -// element.Status = "sent"; -// element.Currency = QString("$ 1555444"); -// element.Cryptocurrency = QString("KLV 44433"); -// m_elementList.append(element); } DapScreenHistoryModel& DapScreenHistoryModel::getInstance() @@ -55,6 +24,36 @@ QHash<int, QByteArray> DapScreenHistoryModel::roleNames() const return names; } +void DapScreenHistoryModel::receiveNewData(const QVariant& aData) +{ + beginResetModel(); + QList<QVariant> dataList = aData.toList(); + m_elementList.clear(); + + for(int i = 0; i < dataList.count(); i++) + { + // Description QStringList + // ------------------------ + // 0: date + // 1: status + // 2: currency + // 3: token + // 4: wallet_to + // 5: wallet_from + + QStringList dataItem = dataList.at(i).toStringList(); + DapTransactionItem item; + item.Date = QDateTime::fromString(dataItem.at(0), "ddd MMM dd h:mm:ss YYYY"); + item.Status = static_cast<DapTransactionStatus>(dataItem.at(1).toInt()); + item.Cryptocurrency = dataItem.at(2); + item.TokenName = dataItem.at(3); + item.WalletNumber = dataItem.at(5); + item.Currency = "$ 0 USD"; // TODO: + m_elementList.append(item); + } + endResetModel(); +} + int DapScreenHistoryModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; @@ -67,7 +66,13 @@ QVariant DapScreenHistoryModel::data(const QModelIndex &index, int role) const switch (role) { - case DisplayDateRole: return m_elementList.at(index.row()).Date; + case DisplayDateRole: + { + QDateTime currentTime = QDateTime::currentDateTime(); + QDateTime itemDate = m_elementList.at(index.row()).Date; + if(currentTime == itemDate) return QString("Today"); + return itemDate.toString(MASK_FOR_MODEL); + } case DisplayNameTokenRole: return m_elementList.at(index.row()).TokenName; case DisplayNumberWalletRole: return m_elementList.at(index.row()).WalletNumber; case DisplayStatusRole: return m_elementList.at(index.row()).Status; diff --git a/KelvinDashboardGUI/DapScreenHistoryModel.h b/KelvinDashboardGUI/DapScreenHistoryModel.h index aa50bf6d1..db75a9a23 100644 --- a/KelvinDashboardGUI/DapScreenHistoryModel.h +++ b/KelvinDashboardGUI/DapScreenHistoryModel.h @@ -4,22 +4,33 @@ #include <QDebug> #include <QImage> #include <QAbstractListModel> +#include <QDateTime> -struct DapTransactionItem { - QString Date; - QImage TokenPic; - QString Status; - QString TokenName; - QString WalletNumber; - QString Cryptocurrency; - QString Currency; -}; +#define MASK_FOR_MODEL QString("MMMM, dd") class DapScreenHistoryModel : public QAbstractListModel { Q_OBJECT -private: +public: + enum DapTransactionStatus { + Pending, + Sent, + Received, + Error + }; + Q_ENUM(DapTransactionStatus) + + struct DapTransactionItem { + QDateTime Date; + QImage TokenPic; + DapTransactionStatus Status; + QString TokenName; + QString WalletNumber; + QString Cryptocurrency; + QString Currency; + }; + enum { DisplayDateRole = Qt::UserRole, DisplayNameTokenRole, @@ -29,6 +40,7 @@ private: DisplayCurrency }; +private: QList<DapTransactionItem> m_elementList; public: @@ -40,8 +52,7 @@ public: QHash<int, QByteArray> roleNames() const override; public slots: - - + void receiveNewData(const QVariant& aData); }; #endif // DAPSCREENHISTORYMODEL_H diff --git a/KelvinDashboardGUI/DapServiceController.cpp b/KelvinDashboardGUI/DapServiceController.cpp index aa06a764a..bc97ed80e 100755 --- a/KelvinDashboardGUI/DapServiceController.cpp +++ b/KelvinDashboardGUI/DapServiceController.cpp @@ -55,6 +55,8 @@ void DapServiceController::init(DapServiceClient *apDapServiceClient) connect(&DapChainNodeNetworkModel::getInstance(), SIGNAL(requestNodeNetwork()), this, SLOT(getNodeNetwork())); connect(&DapChainNodeNetworkModel::getInstance(), SIGNAL(requestNodeStatus(bool)), this, SLOT(setNodeStatus(bool))); + + connect(m_pDapCommandController, SIGNAL(sendHistory(QVariant)), this, SLOT(processGetHistory(QVariant))); } QString DapServiceController::getBrand() const @@ -231,6 +233,11 @@ void DapServiceController::processGetNodeNetwork(const QVariant& aData) DapChainNodeNetworkModel::getInstance().receiveNewNetwork(aData); } +void DapServiceController::processGetHistory(const QVariant& aData) +{ + DapScreenHistoryModel::getInstance().receiveNewData(aData); +} + /// Get an instance of a class. /// @return Instance of a class. diff --git a/KelvinDashboardGUI/DapServiceController.h b/KelvinDashboardGUI/DapServiceController.h index 7222e0f7d..83cbef79f 100755 --- a/KelvinDashboardGUI/DapServiceController.h +++ b/KelvinDashboardGUI/DapServiceController.h @@ -14,6 +14,7 @@ #include "DapLogModel.h" #include "DapChainWalletsModel.h" #include "DapChainNodeNetworkModel.h" +#include "DapScreenHistoryModel.h" class DapServiceController : public QObject { @@ -113,6 +114,8 @@ private slots: void processGetNodeNetwork(const QVariant& aData); + void processGetHistory(const QVariant& aData); + public slots: void getNodeNetwork(); void setNodeStatus(const bool aIsOnline); diff --git a/KelvinDashboardGUI/DapUiQmlScreenHistory.qml b/KelvinDashboardGUI/DapUiQmlScreenHistory.qml index 5a15312b6..4e68377f3 100644 --- a/KelvinDashboardGUI/DapUiQmlScreenHistory.qml +++ b/KelvinDashboardGUI/DapUiQmlScreenHistory.qml @@ -1,7 +1,10 @@ import QtQuick 2.9 +import QtQml 2.12 import QtQuick.Controls 2.2 import QtQuick.Layouts 1.12 +import DapTransactionHistory 1.0 + Page { ListView { id: dapListView @@ -117,10 +120,12 @@ Page { font.pointSize: 12 onTextChanged: { - if(text == "Error") color = "#00081B" - else if(text == "Received") color = "#454E63" - else if(text == "Sent") color = "#959CA6" - else if(text == "Pending") color = "#E3E3E3"; + switch(text) { + case DapTransactionModel.Error: color = "#00081B"; break; + case DapTransactionModel.Received: color = "#454E63"; break; + case DapTransactionModel.Sent: color = "#959CA6"; break; + case DapTransactionModel.Pending: color = "#E3E3E3"; break; + } } } } diff --git a/KelvinDashboardGUI/KelvinDashboardGUI.pro b/KelvinDashboardGUI/KelvinDashboardGUI.pro index 0fbdf8de9..37c5639d6 100755 --- a/KelvinDashboardGUI/KelvinDashboardGUI.pro +++ b/KelvinDashboardGUI/KelvinDashboardGUI.pro @@ -42,6 +42,7 @@ ICON = icon.ico SOURCES += \ DapChainNodeNetworkExplorer.cpp \ DapChainNodeNetworkModel.cpp \ + DapScreenHistoryFilterModel.cpp \ DapScreenHistoryModel.cpp \ DapUiQmlWidgetChainTransactions.cpp \ main.cpp \ @@ -77,6 +78,7 @@ else: unix:!android: target.path = /opt/kelvin-dashboard/bin HEADERS += \ DapChainNodeNetworkExplorer.h \ DapChainNodeNetworkModel.h \ + DapScreenHistoryFilterModel.h \ DapScreenHistoryModel.h \ DapUiQmlWidgetChainBallance.h \ DapUiQmlWidgetChainBlockExplorer.h \ diff --git a/KelvinDashboardGUI/main.cpp b/KelvinDashboardGUI/main.cpp index 3aced68c1..b91434293 100755 --- a/KelvinDashboardGUI/main.cpp +++ b/KelvinDashboardGUI/main.cpp @@ -6,6 +6,7 @@ #include <QIcon> #include <QSystemSemaphore> #include <QSharedMemory> +#include <QScreen> #include "DapHalper.h" #include "DapScreenDialog.h" @@ -21,8 +22,8 @@ #include "DapChainWalletsModel.h" #include "DapChainNodeNetworkModel.h" #include "DapChainNodeNetworkExplorer.h" +#include "DapScreenHistoryFilterModel.h" -#include "DapScreenHistoryModel.h" #include <QRegExp> @@ -52,6 +53,9 @@ int main(int argc, char *argv[]) dapServiceClient.init(); controller.getNodeLogs(0, 100); controller.getWallets(); + + DapScreenHistoryFilterModel::getInstance() + .setSourceModel(&DapScreenHistoryModel::getInstance()); // controller.getNodeNetwork(); qmlRegisterType<DapScreenDialog>("KelvinDashboard", 1, 0, "DapScreenDialog"); @@ -60,14 +64,17 @@ int main(int argc, char *argv[]) qmlRegisterType<DapChainNodeNetworkExplorer>("NodeNetworkExplorer", 1, 0, "DapUiQmlWidgetNodeNetwork"); // qmlRegisterType<DapScreenHistoryModel>("") qmlRegisterSingletonType<DapUiQmlWidgetModel>("KelvinDashboard", 1, 0, "DapUiQmlWidgetModel", DapUiQmlWidgetModel::singletonProvider); + qmlRegisterType<DapScreenHistoryModel>("DapTransactionHistory", 1, 0, "DapTransactionModel"); QQmlApplicationEngine engine; + qreal dpi = QGuiApplication::primaryScreen()->physicalDotsPerInch(); engine.rootContext()->setContextProperty("dapServiceController", &DapServiceController::getInstance()); engine.rootContext()->setContextProperty("dapUiQmlWidgetModel", &DapUiQmlWidgetModel::getInstance()); engine.rootContext()->setContextProperty("dapLogModel", &DapLogModel::getInstance()); engine.rootContext()->setContextProperty("dapChainWalletsModel", &DapChainWalletsModel::getInstance()); engine.rootContext()->setContextProperty("dapNodeNetworkModel", &DapChainNodeNetworkModel::getInstance()); engine.rootContext()->setContextProperty("dapHistoryModel", &DapScreenHistoryModel::getInstance()); + engine.rootContext()->setContextProperty("pt", 1/72*dpi); engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); // DapSettings &settings = DapSettings::getInstance("Settings.json"); -- GitLab