updating UI for asset supprot

This commit is contained in:
Daniel Larimer 2015-07-14 17:30:15 -04:00
parent d1c3c7a698
commit 80319893c2
4 changed files with 213 additions and 42 deletions

View file

@ -134,10 +134,12 @@ int main( int argc, char** argv )
wdata.ws_password = options.at("server-rpc-password").as<std::string>(); wdata.ws_password = options.at("server-rpc-password").as<std::string>();
fc::http::websocket_client client; fc::http::websocket_client client;
idump((wdata.ws_server));
auto con = client.connect( wdata.ws_server ); auto con = client.connect( wdata.ws_server );
auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con); auto apic = std::make_shared<fc::rpc::websocket_api_connection>(*con);
auto remote_api = apic->get_remote_api< login_api >(1); auto remote_api = apic->get_remote_api< login_api >(1);
edump((wdata.ws_user)(wdata.ws_password) );
FC_ASSERT( remote_api->login( wdata.ws_user, wdata.ws_password ) ); FC_ASSERT( remote_api->login( wdata.ws_user, wdata.ws_password ) );
auto wapiptr = std::make_shared<wallet_api>(remote_api); auto wapiptr = std::make_shared<wallet_api>(remote_api);

View file

@ -10,6 +10,130 @@ using namespace graphene::app;
ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent ) ChainDataModel::ChainDataModel( fc::thread& t, QObject* parent )
:QObject(parent),m_thread(&t){} :QObject(parent),m_thread(&t){}
Asset* ChainDataModel::getAsset(qint64 id)
{
auto& by_id_idx = m_assets.get<::by_id>();
auto itr = by_id_idx.find(id);
if( itr == by_id_idx.end() )
{
auto tmp = new Asset;
tmp->id = id; --m_account_query_num;
tmp->symbol = QString::number( --m_account_query_num);
auto result = m_assets.insert( tmp );
assert( result.second );
/** execute in app thread */
m_thread->async( [this,id](){
try {
ilog( "look up symbol.." );
auto result = m_db_api->get_assets( {asset_id_type(id)} );
wdump((result));
/** execute in main */
Q_EMIT queueExecute( [this,result,id](){
wlog( "process result" );
auto& by_id_idx = this->m_assets.get<::by_id>();
auto itr = by_id_idx.find(id);
assert( itr != by_id_idx.end() );
if( result.size() == 0 || !result.front() )
{
elog( "delete later" );
(*itr)->deleteLater();
by_id_idx.erase( itr );
}
else
{
by_id_idx.modify( itr,
[=]( Asset* a ){
a->setProperty("symbol", QString::fromStdString(result.front()->symbol) );
a->setProperty("precision", result.front()->precision );
}
);
}
});
}
catch ( const fc::exception& e )
{
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
}
});
return *result.first;
}
return *itr;
}
Asset* ChainDataModel::getAsset(QString symbol)
{
auto& by_symbol_idx = m_assets.get<by_symbol_name>();
auto itr = by_symbol_idx.find(symbol);
if( itr == by_symbol_idx.end() )
{
auto tmp = new Asset;
tmp->id = --m_account_query_num;
tmp->symbol = symbol;
auto result = m_assets.insert( tmp );
assert( result.second );
/** execute in app thread */
m_thread->async( [this,symbol](){
try {
ilog( "look up symbol.." );
auto result = m_db_api->lookup_asset_symbols( {symbol.toStdString()} );
/** execute in main */
Q_EMIT queueExecute( [this,result,symbol](){
wlog( "process result" );
auto& by_symbol_idx = this->m_assets.get<by_symbol_name>();
auto itr = by_symbol_idx.find(symbol);
assert( itr != by_symbol_idx.end() );
if( result.size() == 0 || !result.front() )
{
elog( "delete later" );
(*itr)->deleteLater();
by_symbol_idx.erase( itr );
}
else
{
by_symbol_idx.modify( itr,
[=]( Asset* a ){
a->setProperty("id", result.front()->id.instance() );
a->setProperty("precision", result.front()->precision );
}
);
}
});
}
catch ( const fc::exception& e )
{
Q_EMIT exceptionThrown( QString::fromStdString(e.to_string()) );
}
});
return *result.first;
}
return *itr;
}
Account* ChainDataModel::getAccount(qint64 id) Account* ChainDataModel::getAccount(qint64 id)
{ {
auto& by_id_idx = m_accounts.get<::by_id>(); auto& by_id_idx = m_accounts.get<::by_id>();
@ -80,19 +204,19 @@ Account* ChainDataModel::getAccount(QString name)
/** execute in main */ /** execute in main */
Q_EMIT queueExecute( [this,result,name](){ Q_EMIT queueExecute( [this,result,name](){
wlog( "process result" ); wlog( "process result" );
auto& by_name_idx = this->m_accounts.get<by_account_name>(); auto& by_symbol_idx = this->m_accounts.get<by_account_name>();
auto itr = by_name_idx.find(name); auto itr = by_symbol_idx.find(name);
assert( itr != by_name_idx.end() ); assert( itr != by_symbol_idx.end() );
if( result.size() == 0 || !result.front() ) if( result.size() == 0 || !result.front() )
{ {
elog( "delete later" ); elog( "delete later" );
(*itr)->deleteLater(); (*itr)->deleteLater();
by_name_idx.erase( itr ); by_symbol_idx.erase( itr );
} }
else else
{ {
by_name_idx.modify( itr, by_symbol_idx.modify( itr,
[=]( Account* a ){ [=]( Account* a ){
a->setProperty("id", result.front()->id.instance() ); a->setProperty("id", result.front()->id.instance() );
} }

View file

@ -19,67 +19,79 @@ using namespace boost::multi_index;
Q_DECLARE_METATYPE(std::function<void()>) Q_DECLARE_METATYPE(std::function<void()>)
class GrapheneObject : public QObject
{
Q_OBJECT
Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged)
class Asset : public QObject { public:
qint64 id;
Q_SIGNALS:
void idChanged();
};
class Asset : public GrapheneObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString symbol MEMBER symbol) Q_PROPERTY(QString symbol MEMBER symbol)
Q_PROPERTY(qint64 id MEMBER id) Q_PROPERTY(quint32 precision MEMBER precision)
Q_PROPERTY(quint8 precision MEMBER precision)
QString symbol; public:
qint64 id; QString symbol;
quint8 precision; quint32 precision;
Q_SIGNALS:
void symbolChanged();
}; };
class Balance : public QObject { struct by_id;
struct by_symbol_name;
typedef multi_index_container<
Asset*,
indexed_by<
hashed_unique< tag<by_id>, member<GrapheneObject, qint64, &GrapheneObject::id > >,
ordered_unique< tag<by_symbol_name>, member<Asset, QString, &Asset::symbol> >
>
> asset_multi_index_type;
class Balance : public GrapheneObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(Asset* type MEMBER type) Q_PROPERTY(Asset* type MEMBER type)
Q_PROPERTY(qint64 amount MEMBER amount) Q_PROPERTY(qint64 amount MEMBER amount)
Q_PROPERTY(qint64 id MEMBER id)
Asset* type; Asset* type;
qint64 amount; qint64 amount;
qint64 id;
}; };
class Account : public QObject { class Account : public GrapheneObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged) Q_PROPERTY(QString name MEMBER name NOTIFY nameChanged)
Q_PROPERTY(qint64 id MEMBER id NOTIFY idChanged)
Q_PROPERTY(QQmlListProperty<Balance> balances READ balances) Q_PROPERTY(QQmlListProperty<Balance> balances READ balances)
QList<Balance*> m_balances; QList<Balance*> m_balances;
public: public:
// Account(QObject* parent = nullptr) const QString& getName()const { return name; }
// : QObject(parent){}
const QString& getName()const { return name; } QQmlListProperty<Balance> balances();
qint64 getId()const { return id; }
QQmlListProperty<Balance> balances(); QString name;
QString name; Q_SIGNALS:
qint64 id; void nameChanged();
signals:
void nameChanged();
void idChanged();
}; };
struct by_id;
struct by_account_name; struct by_account_name;
/**
* @ingroup object_index
*/
typedef multi_index_container< typedef multi_index_container<
Account*, Account*,
indexed_by< indexed_by<
hashed_unique< tag<by_id>, const_mem_fun<Account, qint64, &Account::getId > >, hashed_unique< tag<by_id>, member<GrapheneObject, qint64, &GrapheneObject::id > >,
ordered_unique< tag<by_account_name>, const_mem_fun<Account, const QString&, &Account::getName> > ordered_unique< tag<by_account_name>, member<Account, QString, &Account::name> >
> >
> account_multi_index_type; > account_multi_index_type;
@ -92,6 +104,8 @@ class ChainDataModel : public QObject {
public: public:
Q_INVOKABLE Account* getAccount(qint64 id); Q_INVOKABLE Account* getAccount(qint64 id);
Q_INVOKABLE Account* getAccount(QString name); Q_INVOKABLE Account* getAccount(QString name);
Q_INVOKABLE Asset* getAsset(qint64 id);
Q_INVOKABLE Asset* getAsset(QString symbol);
ChainDataModel(){} ChainDataModel(){}
ChainDataModel( fc::thread& t, QObject* parent = nullptr ); ChainDataModel( fc::thread& t, QObject* parent = nullptr );
@ -109,6 +123,7 @@ private:
qint64 m_account_query_num = -1; qint64 m_account_query_num = -1;
account_multi_index_type m_accounts; account_multi_index_type m_accounts;
asset_multi_index_type m_assets;
}; };

View file

@ -75,24 +75,24 @@ ApplicationWindow {
else else
{ {
console.log("Waiting for result...") console.log("Waiting for result...")
acct.idChanged.connect(function(loadedAcct) { acct.idChanged.connect(function() {
console.log( "ID CHANGED" ); console.log( "ID CHANGED" );
console.log(JSON.stringify(loadedAcct)) console.log(JSON.stringify(acct))
}) })
} }
} }
} }
TextField { TextField {
id: idField id: accountIdField
onAccepted: lookupIdButton.clicked() onAccepted: lookupAccountIdButton.clicked()
focus: true focus: true
} }
Button { Button {
id: lookupIdButton id: lookupAccountIdButton
text: "Lookup ID" text: "Lookup Account ID"
onClicked: { onClicked: {
var acct = app.model.getAccount(parseInt(idField.text)) var acct = app.model.getAccount(parseInt(accountIdField.text))
console.log(JSON.stringify(acct)) console.log(JSON.stringify(acct))
// @disable-check M126 // @disable-check M126
if (acct == null) if (acct == null)
@ -105,7 +105,37 @@ ApplicationWindow {
else else
{ {
console.log("Waiting for result...") console.log("Waiting for result...")
acct.nameChanged.connect(function(loadedAcct) { acct.nameChanged.connect(function() {
console.log( "NAME CHANGED" );
console.log(JSON.stringify(acct))
})
}
}
}
TextField {
id: assetIdField
onAccepted: lookupassetIdButton.clicked()
focus: true
}
Button {
id: lookupassetIdButton
text: "Lookup Asset ID"
onClicked: {
var acct = app.model.getAsset(parseInt(assetIdField.text))
console.log(JSON.stringify(acct))
// @disable-check M126
if (acct == null)
console.log("Got back null asset")
else if ( !(parseInt(acct.name) <= 0) )
{
console.log("NAME ALREADY SET" );
console.log(JSON.stringify(acct))
}
else
{
console.log("Waiting for result...")
acct.nameChanged.connect(function() {
console.log( "NAME CHANGED" ); console.log( "NAME CHANGED" );
console.log(JSON.stringify(acct)) console.log(JSON.stringify(acct))
}) })