From 22cc12212002af83b68f254aedbd1b9d20898546 Mon Sep 17 00:00:00 2001 From: Strider Date: Sun, 26 Apr 2020 21:50:49 +0200 Subject: [PATCH 1/3] update// addec chatmodel.h chatmodel.cpp to realize the whole chat history --- silentdragon-lite.pro | 6 ++- src/chatmodel.cpp | 49 ++++++++++++++++++++++ src/chatmodel.h | 94 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 src/chatmodel.cpp create mode 100644 src/chatmodel.h diff --git a/silentdragon-lite.pro b/silentdragon-lite.pro index e8c50d5..b692159 100644 --- a/silentdragon-lite.pro +++ b/silentdragon-lite.pro @@ -67,7 +67,8 @@ SOURCES += \ src/liteinterface.cpp \ src/camount.cpp \ src/chatbubbleme.cpp \ - src/chatbubblepartner.cpp + src/chatbubblepartner.cpp \ + src/chatmodel.cpp HEADERS += \ src/firsttimewizard.h \ @@ -98,7 +99,8 @@ HEADERS += \ src/camount.h \ lib/silentdragonlitelib.h \ src/chatbubbleme.h \ - src/chatbubblepartner.h + src/chatbubblepartner.h \ + src/chatmodel.h FORMS += \ src/encryption.ui \ diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp new file mode 100644 index 0000000..b65bd4a --- /dev/null +++ b/src/chatmodel.cpp @@ -0,0 +1,49 @@ +#include "chatmodel.h" + +ChatModel::ChatModel(std::map chatItems) +{ + this->chatItems = chatItems; +} + +ChatModel::ChatModel(std::vector chatItems) +{ + this->setItems(chatItems); +} + +std::map ChatModel::getItems() +{ + return this->chatItems; +} + +void ChatModel::setItems(std::map items) +{ + this->chatItems = chatItems; +} + +void ChatModel::setItems(std::vector items) +{ + for(ChatItem c : items) + { + this->chatItems[c.getTimestamp()] = c; + } +} + +void ChatModel::renderChatBox(QListView &view) +{ + for(ChatItem c : items) + { + view.getItems().add(QString("[Timestamp] : lorem ipsum ....")); + } + + //todo render items to view +} + +void ChatModel::renderChatBox(QListView *view) +{ + for(ChatItem c : items) + { + view->getItems().add(QString("[Timestamp] : lorem ipsum ....")); + } + + //todo render items to view +} \ No newline at end of file diff --git a/src/chatmodel.h b/src/chatmodel.h new file mode 100644 index 0000000..5dd8904 --- /dev/null +++ b/src/chatmodel.h @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +class ChatItem +{ + private: + long timestamp; + std::string address; + std::string contact; + std::string memo; + bool outgoing = false; + + public: + ChatItem() {} + ChatItem(long timestamp, std::string address, std::string contact, std::string memo); + ChatItem(long timestamp, std::string address, std::string contact, std::string memo, bool outgoing = false); + + long getTimestamp() + { + return this->timestamp; + } + + std::string getAddress() + { + return this->address; + } + + std::string getContact() + { + return this->contact; + } + + std::string getMemo() + { + return this->memo; + } + + bool isOutgoing() + { + return this->outgoing; + } + + void setTimestamp(long timestamp) + { + this->timestamp = timestamp; + } + + void setAddress(std::string address) + { + this->address = address; + } + + void setContact(std::string contact) + { + this->contact = contact; + } + + void setMemo(std::string memo) + { + this->memo = memo; + } + + void toggleOutgo() + { + this->outgoing = true; + } + + ~ChatItem() + { + delete timestamp; + delete address; + delete contact; + delete memo; + delete outgoing; + } +}; + +class ChatModel +{ + private: + std::map chatItems; + + public: + ChatModel() {}; + ChatModel(std::map chatItems); + ChatModel(std::vector chatItems); + std::map getItems(); + void setItems(std::map items); + void setItems(std::vector items); + void renderChatBox(QListView &view); + void renderChatBox(QListView *view); +}; \ No newline at end of file From 008d3fd6c2fbe22c68e188f076c70d1aab0d45bf Mon Sep 17 00:00:00 2001 From: Strider Date: Sun, 26 Apr 2020 22:28:27 +0200 Subject: [PATCH 2/3] update// fixed stuff --- src/chatmodel.cpp | 39 +++++++++++++++++++++++++++++++-------- src/chatmodel.h | 32 ++++++++++++++++++-------------- src/controller.cpp | 24 +++++++++++++++++++++++- src/controller.h | 3 +++ 4 files changed, 75 insertions(+), 23 deletions(-) diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index b65bd4a..f6fbb84 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -28,22 +28,45 @@ void ChatModel::setItems(std::vector items) } } +void ChatModel::clear() +{ + this->chatItems.clear(); +} + +void ChatModel::addMessage(ChatItem item) +{ + this->chatItems[item.getTimestamp()] = item; +} + +void ChatModel::addMessage(long timestamp, ChatItem item) +{ + this->chatItems[timestamp] = item; +} + +void ChatModel::showMessages() +{ + for(auto &c : this->chatItems) + { + qDebug() << "[" << c.second.getTimestamp() << "] " << "<" << c.second.getAddress() << "> :" << c.second.getMemo(); + } +} + void ChatModel::renderChatBox(QListView &view) { - for(ChatItem c : items) + /*for(auto &c : this->chatItems) { - view.getItems().add(QString("[Timestamp] : lorem ipsum ....")); - } - + //view.getItems().add(QString("[Timestamp] : lorem ipsum ....")); + }*/ + qDebug() << "not implemented yet"; //todo render items to view } void ChatModel::renderChatBox(QListView *view) { - for(ChatItem c : items) + /*for(auto &c : this->chatItems) { - view->getItems().add(QString("[Timestamp] : lorem ipsum ....")); - } - + //view->getItems().add(QString("[Timestamp] : lorem ipsum ....")); + }*/ + qDebug() << "not implemented yet blyat"; //todo render items to view } \ No newline at end of file diff --git a/src/chatmodel.h b/src/chatmodel.h index 5dd8904..5a35db9 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -7,32 +7,32 @@ class ChatItem { private: long timestamp; - std::string address; - std::string contact; - std::string memo; + QString address; + QString contact; + QString memo; bool outgoing = false; public: ChatItem() {} - ChatItem(long timestamp, std::string address, std::string contact, std::string memo); - ChatItem(long timestamp, std::string address, std::string contact, std::string memo, bool outgoing = false); + ChatItem(long timestamp, QString address, QString contact, QString memo); + ChatItem(long timestamp, QString address, QString contact, QString memo, bool outgoing = false); long getTimestamp() { return this->timestamp; } - std::string getAddress() + QString getAddress() { return this->address; } - std::string getContact() + QString getContact() { return this->contact; } - std::string getMemo() + QString getMemo() { return this->memo; } @@ -47,17 +47,17 @@ class ChatItem this->timestamp = timestamp; } - void setAddress(std::string address) + void setAddress(QString address) { this->address = address; } - void setContact(std::string contact) + void setContact(QString contact) { this->contact = contact; } - void setMemo(std::string memo) + void setMemo(QString memo) { this->memo = memo; } @@ -69,11 +69,11 @@ class ChatItem ~ChatItem() { - delete timestamp; + /*delete timestamp; delete address; delete contact; delete memo; - delete outgoing; + delete outgoing;*/ } }; @@ -91,4 +91,8 @@ class ChatModel void setItems(std::vector items); void renderChatBox(QListView &view); void renderChatBox(QListView *view); + void showMessages(); + void clear(); + void addMessage(ChatItem item); + void addMessage(long timestamp, ChatItem item); }; \ No newline at end of file diff --git a/src/controller.cpp b/src/controller.cpp index 18a158e..0661b06 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -876,7 +876,18 @@ void Controller::refreshTransactions() QString memo; if (!o["memo"].is_null()) + { + ChatItem item = ChatItem( + datetime, + address, + QString(""), + memo, + true // is an outgoing message + ); + chatModel->addMessage(item); memo = QString::fromStdString(o["memo"]); + } + items.push_back(TransactionItemDetail{address, amount, memo}); total_amount = total_amount + amount; @@ -913,6 +924,15 @@ void Controller::refreshTransactions() model->markAddressUsed(address); QString memo; if (!it["memo"].is_null()) + { + ChatItem item = ChatItem( + datetime, + address, + QString(""), + memo + ); + chatModel->addMessage(item); + } memo = QString::fromStdString(it["memo"]); items.push_back( @@ -958,7 +978,9 @@ void Controller::refreshTransactions() updateUIBalances(); // Update model data, which updates the table view - transactionsTableModel->replaceData(txdata); + transactionsTableModel->replaceData(txdata); + //chatModel->renderChatBox(); + chatModel->showMessages(); }); } diff --git a/src/controller.h b/src/controller.h index 29d5c0d..5071dee 100644 --- a/src/controller.h +++ b/src/controller.h @@ -11,9 +11,12 @@ #include "mainwindow.h" #include "liteinterface.h" #include "connection.h" +#include "chatmodel.h" using json = nlohmann::json; +ChatModel *chatModel = new ChatModel(); + struct WatchedTx { QString opid; Tx tx; From a8ee1c68be5999f2a3b03544100be27e3a4c4fb5 Mon Sep 17 00:00:00 2001 From: Strider Date: Mon, 27 Apr 2020 20:18:14 +0200 Subject: [PATCH 3/3] update// moved class to a normal variable instace --- src/chatmodel.h | 57 +++++++++++++++++++++++++++++++--------------- src/controller.cpp | 7 ++++-- src/controller.h | 2 -- 3 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/chatmodel.h b/src/chatmodel.h index 5a35db9..d44a35f 100644 --- a/src/chatmodel.h +++ b/src/chatmodel.h @@ -1,3 +1,5 @@ +#ifndef CHATMODEL_H +#define CHATMODEL_H #include #include #include @@ -6,65 +8,83 @@ class ChatItem { private: - long timestamp; - QString address; - QString contact; - QString memo; - bool outgoing = false; + long _timestamp; + QString _address; + QString _contact; + QString _memo; + bool _outgoing = false; public: ChatItem() {} - ChatItem(long timestamp, QString address, QString contact, QString memo); - ChatItem(long timestamp, QString address, QString contact, QString memo, bool outgoing = false); + + ChatItem(long timestamp, QString address, QString contact, QString memo) + { + _timestamp = timestamp; + _address = address; + _contact = contact; + _memo = memo; + _outgoing = false; + + } + + ChatItem(long timestamp, QString address, QString contact, QString memo, bool outgoing) + { + _timestamp = timestamp; + _address = address; + _contact = contact; + _memo = memo; + _outgoing = outgoing; + + } long getTimestamp() { - return this->timestamp; + return _timestamp; } QString getAddress() { - return this->address; + return _address; } QString getContact() { - return this->contact; + return _contact; } QString getMemo() { - return this->memo; + return _memo; } bool isOutgoing() { - return this->outgoing; + return _outgoing; } void setTimestamp(long timestamp) { - this->timestamp = timestamp; + _timestamp = timestamp; } void setAddress(QString address) { - this->address = address; + _address = address; } void setContact(QString contact) { - this->contact = contact; + _contact = contact; } void setMemo(QString memo) { - this->memo = memo; + _memo = memo; } void toggleOutgo() { - this->outgoing = true; + _outgoing = true; } ~ChatItem() @@ -95,4 +115,5 @@ class ChatModel void clear(); void addMessage(ChatItem item); void addMessage(long timestamp, ChatItem item); -}; \ No newline at end of file +}; +#endif \ No newline at end of file diff --git a/src/controller.cpp b/src/controller.cpp index 0661b06..b7c5dba 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -10,6 +10,7 @@ template<> DataStore* DataStore::instance = nullptr; template<> bool DataStore::instanced = false; +ChatModel *chatModel = new ChatModel(); using json = nlohmann::json; @@ -877,6 +878,7 @@ void Controller::refreshTransactions() QString memo; if (!o["memo"].is_null()) { + memo = QString::fromStdString(o["memo"]); ChatItem item = ChatItem( datetime, address, @@ -885,7 +887,7 @@ void Controller::refreshTransactions() true // is an outgoing message ); chatModel->addMessage(item); - memo = QString::fromStdString(o["memo"]); + } @@ -925,6 +927,7 @@ void Controller::refreshTransactions() QString memo; if (!it["memo"].is_null()) { + memo = QString::fromStdString(it["memo"]); ChatItem item = ChatItem( datetime, address, @@ -933,7 +936,7 @@ void Controller::refreshTransactions() ); chatModel->addMessage(item); } - memo = QString::fromStdString(it["memo"]); + items.push_back( TransactionItemDetail{ diff --git a/src/controller.h b/src/controller.h index 5071dee..a5f0fee 100644 --- a/src/controller.h +++ b/src/controller.h @@ -15,8 +15,6 @@ using json = nlohmann::json; -ChatModel *chatModel = new ChatModel(); - struct WatchedTx { QString opid; Tx tx;