From b8bd8a19054c7f21b733f398028f63912dca6ba5 Mon Sep 17 00:00:00 2001 From: Nathan Hourt Date: Mon, 13 Jul 2015 16:21:01 -0400 Subject: [PATCH] [GUI] Add debugging UI for fetching accounts --- programs/light_client/ClientDataModel.cpp | 25 ++++++++------- programs/light_client/ClientDataModel.hpp | 10 ++++-- programs/light_client/qml/main.qml | 38 ++++++++++++++++++++--- 3 files changed, 53 insertions(+), 20 deletions(-) diff --git a/programs/light_client/ClientDataModel.cpp b/programs/light_client/ClientDataModel.cpp index 3c3fcfd3..5664f2f5 100644 --- a/programs/light_client/ClientDataModel.cpp +++ b/programs/light_client/ClientDataModel.cpp @@ -10,7 +10,7 @@ using namespace graphene::app; ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent ) :QObject(parent),m_thread(&t){} -Account* ChainDataModel::getAccount(qint64 id) +Account* ChainDataModel::getAccount(qint64 id) { auto itr = m_accounts.find( id ); if( itr != m_accounts.end() ) @@ -22,7 +22,7 @@ Account* ChainDataModel::getAccount(qint64 id) auto insert_result = m_accounts.insert( acct ); /** execute in app thread */ - m_thread->async( [=](){ + m_thread->async( [=](){ try { auto result = m_db_api->get_accounts( {account_id_type(id)} ); if( result.size() && result.front().valid() ) @@ -30,7 +30,7 @@ Account* ChainDataModel::getAccount(qint64 id) QString name = QString::fromStdString( result.front()->name ); /** execute in main */ Q_EMIT queueExecute( [=](){ - this->m_accounts.modify( insert_result.first, + this->m_accounts.modify( insert_result.first, [=]( Account* a ){ a->setProperty("name", name ); } ); }); @@ -43,9 +43,10 @@ Account* ChainDataModel::getAccount(qint64 id) m_accounts.erase( insert_result.first ); }); } - } + } catch ( const fc::exception& e ) { + edump((e.to_detail_string())); Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) ); } }); @@ -53,7 +54,7 @@ Account* ChainDataModel::getAccount(qint64 id) return acct; } -Account* ChainDataModel::getAccount(QString name) +Account* ChainDataModel::getAccount(QString name) { auto itr = m_accounts.get<::by_name>().find(name); if( itr != m_accounts.get<::by_name>().end() ) @@ -61,23 +62,22 @@ Account* ChainDataModel::getAccount(QString name) return *itr; } - auto acct = new Account(this); acct->setProperty("id", --m_account_query_num ); acct->setProperty("name", name); auto insert_result = m_accounts.insert( acct ); /** execute in app thread */ - m_thread->async( [=](){ + m_thread->async( [=](){ try { auto result = m_db_api->lookup_account_names( {name.toStdString()} ); if( result.size() && result.front().valid() ) { /** execute in main */ Q_EMIT queueExecute( [=](){ - this->m_accounts.modify( insert_result.first, - [=]( Account* a ){ - a->setProperty("id", result.front()->id.instance() ); + this->m_accounts.modify( insert_result.first, + [=]( Account* a ){ + a->setProperty("id", result.front()->id.instance() ); } ); }); @@ -90,14 +90,15 @@ Account* ChainDataModel::getAccount(QString name) m_accounts.erase( insert_result.first ); }); } - } + } catch ( const fc::exception& e ) { + edump((e.to_detail_string())); Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) ); } }); - return nullptr; + return acct; } QQmlListProperty Account::balances() diff --git a/programs/light_client/ClientDataModel.hpp b/programs/light_client/ClientDataModel.hpp index 2ff7214f..9aab8112 100644 --- a/programs/light_client/ClientDataModel.hpp +++ b/programs/light_client/ClientDataModel.hpp @@ -47,8 +47,8 @@ class Balance : public QObject { class Account : public QObject { Q_OBJECT - Q_PROPERTY(QString name MEMBER name) - Q_PROPERTY(qint64 id MEMBER id) + Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged) + Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged) Q_PROPERTY(QQmlListProperty balances READ balances) QString name; @@ -63,9 +63,13 @@ public: qint64 getId()const { return id; } QQmlListProperty balances(); + +signals: + void nameChanged(); + void idChanged(); }; -struct by_id; +struct by_id; struct by_name; /** * @ingroup object_index diff --git a/programs/light_client/qml/main.qml b/programs/light_client/qml/main.qml index a07764ba..f7e4ecef 100644 --- a/programs/light_client/qml/main.qml +++ b/programs/light_client/qml/main.qml @@ -38,13 +38,41 @@ ApplicationWindow { id: appSettings category: "appSettings" } + Connections { + target: app + onExceptionThrown: console.log("Exception from app: " + message) + } - Button { - text: "Transfer" + Column { anchors.centerIn: parent - onClicked: formBox.showForm(Qt.createComponent("TransferForm.qml"), function() { - console.log("Closed form") - }) + Button { + text: "Transfer" + onClicked: formBox.showForm(Qt.createComponent("TransferForm.qml"), function() { + console.log("Closed form") + }) + } + TextField { + id: nameField + onAccepted: lookupButton.clicked() + focus: true + } + Button { + id: lookupButton + text: "Lookup" + onClicked: { + var acct = app.model.getAccount(nameField.text) + // @disable-check M126 + if (acct == null) + console.log("Got back null account") + else if (acct.id >= 0) + console.log(JSON.stringify(acct)) + else + console.log("Waiting for result...") + acct.idChanged.connect(function(loadedAcct) { + console.log(JSON.stringify(loadedAcct)) + }) + } + } } FormBox {