|
|
@ -22,23 +22,45 @@ |
|
|
|
|
|
|
|
#include <QObject> |
|
|
|
#include <QQmlEngine> |
|
|
|
#include <QTextDocument> |
|
|
|
#include <QAbstractListModel> |
|
|
|
#include "TransactionListModel.h" |
|
|
|
#include "QContractDefinition.h" |
|
|
|
#include "QFunctionDefinition.h" |
|
|
|
#include "QVariableDeclaration.h" |
|
|
|
#include "libdevcore/CommonJS.h" |
|
|
|
|
|
|
|
namespace dev |
|
|
|
{ |
|
|
|
namespace mix |
|
|
|
{ |
|
|
|
TransactionListModel::TransactionListModel(QObject* _parent) : |
|
|
|
QAbstractListModel(_parent) |
|
|
|
|
|
|
|
u256 fromQString(QString const& _s) |
|
|
|
{ |
|
|
|
return dev::jsToU256(_s.toStdString()); |
|
|
|
} |
|
|
|
|
|
|
|
QString toQString(u256 _value) |
|
|
|
{ |
|
|
|
m_transactions.push_back(Transaction(0, "testTr", 0, 0, 0)); |
|
|
|
std::ostringstream s; |
|
|
|
s << _value; |
|
|
|
return QString::fromStdString(s.str()); |
|
|
|
} |
|
|
|
|
|
|
|
TransactionListItem::TransactionListItem(int _index, Transaction const& _t, QObject* _parent): |
|
|
|
QObject(_parent), m_index(_index), m_title(_t.title), m_functionId(_t.functionId), m_value(toQString(_t.value)), |
|
|
|
m_gas(toQString(_t.gas)), m_gasPrice(toQString(_t.gasPrice)) |
|
|
|
{} |
|
|
|
|
|
|
|
TransactionListModel::TransactionListModel(QObject* _parent, QTextDocument* _document): |
|
|
|
QAbstractListModel(_parent), m_document(_document) |
|
|
|
{} |
|
|
|
|
|
|
|
QHash<int, QByteArray> TransactionListModel::roleNames() const |
|
|
|
{ |
|
|
|
QHash<int, QByteArray> roles; |
|
|
|
roles[TitleRole] = "title"; |
|
|
|
roles[IdRole] = "transactionId"; |
|
|
|
roles[IdRole] = "transactionIndex"; |
|
|
|
return roles; |
|
|
|
} |
|
|
|
|
|
|
@ -48,26 +70,98 @@ int TransactionListModel::rowCount(QModelIndex const& _parent) const |
|
|
|
return m_transactions.size(); |
|
|
|
} |
|
|
|
|
|
|
|
QVariant TransactionListModel::data(QModelIndex const& _index, int role) const |
|
|
|
QVariant TransactionListModel::data(QModelIndex const& _index, int _role) const |
|
|
|
{ |
|
|
|
if(_index.row() < 0 || _index.row() >= (int)m_transactions.size()) |
|
|
|
return QVariant(); |
|
|
|
auto const& transaction = m_transactions.at(_index.row()); |
|
|
|
switch(role) |
|
|
|
switch(_role) |
|
|
|
{ |
|
|
|
case TitleRole: |
|
|
|
return QVariant(transaction.title); |
|
|
|
case IdRole: |
|
|
|
return QVariant(transaction.id); |
|
|
|
return QVariant(_index.row()); |
|
|
|
default: |
|
|
|
return QVariant(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//TODO: get parameters from code model
|
|
|
|
QList<TransactionParameterItem*> buildParameters(QTextDocument* _document, Transaction const& _transaction, QString const& _functionId) |
|
|
|
{ |
|
|
|
QList<TransactionParameterItem*> params; |
|
|
|
try |
|
|
|
{ |
|
|
|
QString code = _document->toPlainText().replace("\n", ""); //TODO: is this required?
|
|
|
|
std::unique_ptr<QContractDefinition> contract(QContractDefinition::Contract(code, nullptr)); |
|
|
|
auto functions = contract->functions(); |
|
|
|
for(auto qf : functions) |
|
|
|
{ |
|
|
|
QFunctionDefinition const& f = (QFunctionDefinition const&) *qf; |
|
|
|
if (f.name() != _functionId) |
|
|
|
continue; |
|
|
|
|
|
|
|
auto parameters = f.parameters(); |
|
|
|
for(auto qp : parameters) |
|
|
|
{ |
|
|
|
QVariableDeclaration const& p = (QVariableDeclaration const&) *qp; |
|
|
|
QString paramValue; |
|
|
|
if (f.name() == _transaction.functionId) |
|
|
|
{ |
|
|
|
auto paramValueIter = _transaction.parameterValues.find(p.name()); |
|
|
|
if (paramValueIter != _transaction.parameterValues.cend()) |
|
|
|
paramValue = toQString(paramValueIter->second); |
|
|
|
} |
|
|
|
|
|
|
|
TransactionParameterItem* item = new TransactionParameterItem(p.name(), p.type(), paramValue); |
|
|
|
QQmlEngine::setObjectOwnership(item, QQmlEngine::JavaScriptOwnership); |
|
|
|
params.append(item); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
catch (boost::exception const&) |
|
|
|
{ |
|
|
|
//TODO:
|
|
|
|
} |
|
|
|
|
|
|
|
return params; |
|
|
|
} |
|
|
|
|
|
|
|
//TODO: get fnctions from code model
|
|
|
|
QList<QString> TransactionListModel::getFunctions() |
|
|
|
{ |
|
|
|
QList<QString> functionNames; |
|
|
|
try |
|
|
|
{ |
|
|
|
QString code = m_document->toPlainText().replace("\n", ""); //TODO: is this required?
|
|
|
|
std::unique_ptr<QContractDefinition> contract(QContractDefinition::Contract(code, nullptr)); |
|
|
|
auto functions = contract->functions(); |
|
|
|
for(auto qf : functions) |
|
|
|
{ |
|
|
|
QFunctionDefinition const& f = (QFunctionDefinition const&) *qf; |
|
|
|
functionNames.append(f.name()); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (boost::exception const&) |
|
|
|
{ |
|
|
|
} |
|
|
|
return functionNames; |
|
|
|
} |
|
|
|
|
|
|
|
QVariantList TransactionListModel::getParameters(int _index, QString const& _functionId) |
|
|
|
{ |
|
|
|
Transaction const& transaction = (_index >=0 && _index < (int)m_transactions.size()) ? m_transactions[_index] : Transaction(); |
|
|
|
auto plist = buildParameters(m_document, transaction, _functionId); |
|
|
|
QVariantList vl; |
|
|
|
for(QObject* p : plist) |
|
|
|
vl.append(QVariant::fromValue(p)); |
|
|
|
return vl; |
|
|
|
} |
|
|
|
|
|
|
|
QObject* TransactionListModel::getItem(int _index) |
|
|
|
{ |
|
|
|
Transaction const& transaction = (_index >=0 && _index < (int)m_transactions.size()) ? m_transactions[_index] : Transaction(); |
|
|
|
QObject* item = new TransactionListItem(transaction, nullptr); |
|
|
|
TransactionListItem* item = new TransactionListItem(_index, transaction, nullptr); |
|
|
|
QQmlEngine::setObjectOwnership(item, QQmlEngine::JavaScriptOwnership); |
|
|
|
return item; |
|
|
|
} |
|
|
@ -75,21 +169,34 @@ QObject* TransactionListModel::getItem(int _index) |
|
|
|
void TransactionListModel::edit(QObject* _data) |
|
|
|
{ |
|
|
|
//these properties come from TransactionDialog QML object
|
|
|
|
int id = _data->property("transactionId").toInt(); |
|
|
|
const QString title = _data->property("transactionTitle").toString(); |
|
|
|
|
|
|
|
int index = _data->property("transactionIndex").toInt(); |
|
|
|
QString title = _data->property("transactionTitle").toString(); |
|
|
|
QString gas = _data->property("gas").toString(); |
|
|
|
QString gasPrice = _data->property("gasPrice").toString(); |
|
|
|
QString value = _data->property("transactionValue").toString(); |
|
|
|
QString functionId = _data->property("functionId").toString(); |
|
|
|
QAbstractListModel* paramsModel = qvariant_cast<QAbstractListModel*>(_data->property("transactionParams")); |
|
|
|
Transaction transaction(title, functionId, fromQString(value), fromQString(gas), fromQString(gasPrice)); |
|
|
|
int paramCount = paramsModel->rowCount(QModelIndex()); |
|
|
|
for(int p = 0; p < paramCount; ++p) |
|
|
|
{ |
|
|
|
QString paramName = paramsModel->data(paramsModel->index(p, 0), Qt::DisplayRole).toString(); |
|
|
|
QString paramValue = paramsModel->data(paramsModel->index(p, 0), Qt::DisplayRole + 2).toString(); |
|
|
|
if (!paramValue.isEmpty() && !paramName.isEmpty()) |
|
|
|
transaction.parameterValues[paramName] = fromQString(paramValue); |
|
|
|
} |
|
|
|
|
|
|
|
if (id >= 0 && id < (int)m_transactions.size()) |
|
|
|
if (index >= 0 && index < (int)m_transactions.size()) |
|
|
|
{ |
|
|
|
beginRemoveRows(QModelIndex(), id, id); |
|
|
|
m_transactions.erase(m_transactions.begin() + id); |
|
|
|
beginRemoveRows(QModelIndex(), index, index); |
|
|
|
m_transactions.erase(m_transactions.begin() + index); |
|
|
|
endRemoveRows(); |
|
|
|
} |
|
|
|
else |
|
|
|
id = rowCount(QModelIndex()); |
|
|
|
index = rowCount(QModelIndex()); |
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), id, id); |
|
|
|
m_transactions.push_back(Transaction(id, title, 0, 0, 0)); |
|
|
|
beginInsertRows(QModelIndex(), index, index); |
|
|
|
m_transactions.push_back(transaction); |
|
|
|
emit transactionAdded(); |
|
|
|
emit countChanged(); |
|
|
|
endInsertRows(); |
|
|
|