Gav Wood
10 years ago
19 changed files with 582 additions and 97 deletions
@ -0,0 +1,59 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file QBigInt.cpp
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#include <boost/variant/multivisitors.hpp> |
|||
#include <boost/variant.hpp> |
|||
#include <libdevcore/CommonJS.h> |
|||
#include "QBigInt.h" |
|||
|
|||
using namespace dev; |
|||
using namespace dev::mix; |
|||
|
|||
QString QBigInt::value() const |
|||
{ |
|||
std::ostringstream s; |
|||
s << m_internalValue; |
|||
return QString::fromStdString(s.str()); |
|||
} |
|||
|
|||
QBigInt* QBigInt::subtract(QBigInt* const& _value) const |
|||
{ |
|||
BigIntVariant toSubtract = _value->internalValue(); |
|||
return new QBigInt(boost::apply_visitor(mix::subtract(), m_internalValue, toSubtract)); |
|||
} |
|||
|
|||
QBigInt* QBigInt::add(QBigInt* const& _value) const |
|||
{ |
|||
BigIntVariant toAdd = _value->internalValue(); |
|||
return new QBigInt(boost::apply_visitor(mix::add(), m_internalValue, toAdd)); |
|||
} |
|||
|
|||
QBigInt* QBigInt::multiply(QBigInt* const& _value) const |
|||
{ |
|||
BigIntVariant toMultiply = _value->internalValue(); |
|||
return new QBigInt(boost::apply_visitor(mix::multiply(), m_internalValue, toMultiply)); |
|||
} |
|||
|
|||
QBigInt* QBigInt::divide(QBigInt* const& _value) const |
|||
{ |
|||
BigIntVariant toDivide = _value->internalValue(); |
|||
return new QBigInt(boost::apply_visitor(mix::divide(), m_internalValue, toDivide)); |
|||
} |
@ -0,0 +1,102 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file QBigInt.h
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2015 |
|||
* Represent a big integer (u256, bigint) to be used in QML. |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include "boost/variant.hpp" |
|||
#include "boost/variant/multivisitors.hpp" |
|||
#include <QObject> |
|||
#include <QQmlEngine> |
|||
#include <libdevcore/CommonJS.h> |
|||
#include <libdevcore/Common.h> |
|||
|
|||
using namespace dev; |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace mix |
|||
{ |
|||
|
|||
using BigIntVariant = boost::variant<dev::u256, dev::bigint>; |
|||
|
|||
struct add: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 const& _value, T2 const& _otherValue) const { return _value + _otherValue; } |
|||
}; |
|||
|
|||
struct subtract: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 const& _value, T2 const& _otherValue) const { return _value - _otherValue; } |
|||
}; |
|||
|
|||
struct multiply: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 const& _value, T2 const& _otherValue) const { return _value * _otherValue; } |
|||
}; |
|||
|
|||
struct divide: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 const& _value, T2 const& _otherValue) const { return _value / _otherValue; } |
|||
}; |
|||
|
|||
/*
|
|||
* Represent big integer like big int and u256 in QML. |
|||
* The ownership is set by default to Javascript. |
|||
*/ |
|||
class QBigInt: public QObject |
|||
{ |
|||
Q_OBJECT |
|||
|
|||
public: |
|||
QBigInt(QObject* _parent = 0): QObject(_parent), m_internalValue(dev::u256(0)) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } |
|||
QBigInt(dev::u256 const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } |
|||
QBigInt(dev::bigint const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) { QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } |
|||
QBigInt(BigIntVariant const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value){ QQmlEngine::setObjectOwnership(this, QQmlEngine::JavaScriptOwnership); } |
|||
~QBigInt() {} |
|||
|
|||
/// @returns the current used big integer.
|
|||
BigIntVariant internalValue() { return m_internalValue; } |
|||
/// @returns a string representation of the big integer used. Invokable from QML.
|
|||
Q_INVOKABLE QString value() const; |
|||
/// Set the value of the BigInteger used. Will use u256 type. Invokable from QML.
|
|||
Q_INVOKABLE void setValue(QString const& _value) { m_internalValue = dev::jsToU256(_value.toStdString()); } |
|||
/// Subtract by @a _value. Invokable from QML.
|
|||
Q_INVOKABLE QBigInt* subtract(QBigInt* const& _value) const; |
|||
/// Add @a _value to the current big integer. Invokable from QML.
|
|||
Q_INVOKABLE QBigInt* add(QBigInt* const& _value) const; |
|||
/// Multiply by @a _value. Invokable from QML.
|
|||
Q_INVOKABLE QBigInt* multiply(QBigInt* const& _value) const; |
|||
/// divide by @a _value. Invokable from QML.
|
|||
Q_INVOKABLE QBigInt* divide(QBigInt* const& _value) const; |
|||
|
|||
protected: |
|||
BigIntVariant m_internalValue; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
|||
|
@ -0,0 +1,55 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file QEther.cpp
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include <QMetaEnum> |
|||
#include "QEther.h" |
|||
|
|||
using namespace dev::mix; |
|||
|
|||
QString QEther::format() const |
|||
{ |
|||
return QString::fromStdString(dev::eth::formatBalance(boost::get<dev::u256>(toWei()->internalValue()))); |
|||
} |
|||
|
|||
QBigInt* QEther::toWei() const |
|||
{ |
|||
QMetaEnum units = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("EtherUnit")); |
|||
const char* key = units.valueToKey(m_currentUnit); |
|||
for (std::pair<dev::u256, std::string> rawUnit: dev::eth::units()) |
|||
{ |
|||
if (rawUnit.second == QString(key).toLower().toStdString()) |
|||
return multiply(new QBigInt(rawUnit.first)); |
|||
} |
|||
return new QBigInt(dev::u256(0)); |
|||
} |
|||
|
|||
void QEther::setUnit(QString const& _unit) |
|||
{ |
|||
QMetaEnum units = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("EtherUnit")); |
|||
for (int k = 0; k < units.keyCount(); k++) |
|||
{ |
|||
if (QString(units.key(k)).toLower() == _unit) |
|||
{ |
|||
m_currentUnit = static_cast<EtherUnit>(units.keysToValue(units.key(k))); |
|||
return; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,92 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file QEther.h
|
|||
* @author Yann yann@ethdev.com |
|||
* @date 2014 |
|||
* Represent an amount of Ether in QML (mapped to u256 in c++). |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <QObject> |
|||
#include <libethcore/CommonEth.h> |
|||
#include "QBigInt.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace mix |
|||
{ |
|||
|
|||
class QEther: public QBigInt |
|||
{ |
|||
Q_OBJECT |
|||
Q_ENUMS(EtherUnit) |
|||
Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged) |
|||
Q_PROPERTY(EtherUnit unit READ unit WRITE setUnit NOTIFY unitChanged) |
|||
|
|||
public: |
|||
enum EtherUnit |
|||
{ |
|||
Uether, |
|||
Vether, |
|||
Dether, |
|||
Nether, |
|||
Yether, |
|||
Zether, |
|||
Eether, |
|||
Pether, |
|||
Tether, |
|||
Gether, |
|||
Mether, |
|||
Grand, |
|||
Ether, |
|||
Finney, |
|||
Szabo, |
|||
Gwei, |
|||
Mwei, |
|||
Kwei, |
|||
Wei |
|||
}; |
|||
|
|||
QEther(QObject* _parent = 0): QBigInt(dev::u256(0), _parent), m_currentUnit(EtherUnit::Wei) {} |
|||
QEther(dev::u256 _value, EtherUnit _unit, QObject* _parent = 0): QBigInt(_value, _parent), m_currentUnit(_unit) {} |
|||
~QEther() {} |
|||
|
|||
/// @returns user-friendly string representation of the amount of ether. Invokable from QML.
|
|||
Q_INVOKABLE QString format() const; |
|||
/// @returns the current amount of Ether in Wei. Invokable from QML.
|
|||
Q_INVOKABLE QBigInt* toWei() const; |
|||
/// @returns the current unit used. Invokable from QML.
|
|||
Q_INVOKABLE EtherUnit unit() const { return m_currentUnit; } |
|||
/// Set the unit to be used. Invokable from QML.
|
|||
Q_INVOKABLE void setUnit(EtherUnit const& _unit) { m_currentUnit = _unit; } |
|||
/// Set the unit to be used. Invokable from QML.
|
|||
Q_INVOKABLE void setUnit(QString const& _unit); |
|||
/// @returns the u256 value of the current amount of Ether in Wei.
|
|||
dev::u256 toU256Wei() { return boost::get<dev::u256>(toWei()->internalValue()); } |
|||
|
|||
private: |
|||
EtherUnit m_currentUnit; |
|||
|
|||
signals: |
|||
void valueChanged(); |
|||
void unitChanged(); |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
@ -0,0 +1,13 @@ |
|||
/* |
|||
* Used to instanciate a QEther obj using Qt.createComponent function. |
|||
*/ |
|||
import QtQuick 2.2 |
|||
import QtQuick.Controls 1.1 |
|||
import QtQuick.Layouts 1.1 |
|||
import QtQuick.Controls.Styles 1.1 |
|||
import org.ethereum.qml.QBigInt 1.0 |
|||
|
|||
QBigInt |
|||
{ |
|||
id: bigInt |
|||
} |
@ -0,0 +1,115 @@ |
|||
/* |
|||
* Display a row containing : |
|||
* - The amount of Ether. |
|||
* - The unit used. |
|||
* - User-friendly string representation of the amout of Ether (if displayFormattedValue == true). |
|||
* 'value' has to be a QEther obj. |
|||
*/ |
|||
import QtQuick 2.2 |
|||
import QtQuick.Controls 1.1 |
|||
import QtQuick.Layouts 1.1 |
|||
import QtQuick.Controls.Styles 1.1 |
|||
|
|||
Rectangle { |
|||
id: etherEdition |
|||
property bool displayFormattedValue; |
|||
property bool edit; |
|||
property variant value; |
|||
onValueChanged: update() |
|||
Component.onCompleted: update() |
|||
|
|||
function update() |
|||
{ |
|||
if (value !== undefined) |
|||
{ |
|||
etherValueEdit.text = value.value; |
|||
selectUnit(value.unit); |
|||
} |
|||
} |
|||
|
|||
function selectUnit(unit) |
|||
{ |
|||
units.currentIndex = unit; |
|||
} |
|||
|
|||
RowLayout |
|||
{ |
|||
anchors.fill: parent; |
|||
id: row |
|||
width: 200 |
|||
height: parent.height |
|||
Rectangle |
|||
{ |
|||
width : 200 |
|||
color: edit ? "blue" : "white" |
|||
TextField |
|||
{ |
|||
onTextChanged: |
|||
{ |
|||
if (value !== undefined) |
|||
{ |
|||
value.setValue(text) |
|||
formattedValue.text = value.format(); |
|||
} |
|||
} |
|||
width: parent.width |
|||
readOnly: !edit |
|||
visible: edit |
|||
id: etherValueEdit; |
|||
} |
|||
} |
|||
|
|||
Rectangle |
|||
{ |
|||
Layout.fillWidth: true |
|||
id: unitContainer |
|||
width: 20 |
|||
anchors.verticalCenter: parent.verticalCenter |
|||
ComboBox |
|||
{ |
|||
id: units |
|||
onCurrentTextChanged: |
|||
{ |
|||
if (value !== undefined) |
|||
{ |
|||
value.setUnit(currentText); |
|||
formattedValue.text = value.format(); |
|||
} |
|||
} |
|||
model: ListModel { |
|||
id: unitsModel |
|||
ListElement { text: "Uether"; } |
|||
ListElement { text: "Vether"; } |
|||
ListElement { text: "Dether"; } |
|||
ListElement { text: "Nether"; } |
|||
ListElement { text: "Yether"; } |
|||
ListElement { text: "Zether"; } |
|||
ListElement { text: "Eether"; } |
|||
ListElement { text: "Pether"; } |
|||
ListElement { text: "Tether"; } |
|||
ListElement { text: "Gether"; } |
|||
ListElement { text: "Mether"; } |
|||
ListElement { text: "grand"; } |
|||
ListElement { text: "ether"; } |
|||
ListElement { text: "finney"; } |
|||
ListElement { text: "szabo"; } |
|||
ListElement { text: "Gwei"; } |
|||
ListElement { text: "Mwei"; } |
|||
ListElement { text: "Kwei"; } |
|||
ListElement { text: "wei"; } |
|||
} |
|||
} |
|||
Rectangle |
|||
{ |
|||
anchors.verticalCenter: parent.verticalCenter |
|||
anchors.left: units.right |
|||
visible: displayFormattedValue |
|||
width: 20 |
|||
Text |
|||
{ |
|||
id: formattedValue |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,15 @@ |
|||
/* |
|||
* Used to instanciate a QEther obj using Qt.createComponent function. |
|||
*/ |
|||
import QtQuick 2.2 |
|||
import QtQuick.Controls 1.1 |
|||
import QtQuick.Layouts 1.1 |
|||
import QtQuick.Controls.Styles 1.1 |
|||
import org.ethereum.qml.QEther 1.0 |
|||
|
|||
QEther |
|||
{ |
|||
id: basicEther |
|||
value: "100000000000" |
|||
unit: QEther.Wei |
|||
} |
Loading…
Reference in new issue