yann300
10 years ago
committed by
yann300
13 changed files with 502 additions and 51 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,88 @@ |
|||
/*
|
|||
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 <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 _value, T2 _otherValue) const { return _value + _otherValue; } |
|||
}; |
|||
|
|||
struct subtract: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 _value, T2 _otherValue) const { return _value - _otherValue; } |
|||
}; |
|||
|
|||
struct multiply: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 _value, T2 _otherValue) const { return _value * _otherValue; } |
|||
}; |
|||
|
|||
struct divide: public boost::static_visitor<BigIntVariant> |
|||
{ |
|||
template<class T1, class T2> |
|||
BigIntVariant operator()(T1 _value, T2 _otherValue) const { return _value / _otherValue; } |
|||
}; |
|||
|
|||
class QBigInt: public QObject |
|||
{ |
|||
Q_OBJECT |
|||
|
|||
public: |
|||
QBigInt(dev::u256 const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) {} |
|||
QBigInt(dev::bigint const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value) {} |
|||
QBigInt(BigIntVariant const& _value, QObject* _parent = 0): QObject(_parent), m_internalValue(_value){} |
|||
~QBigInt() {} |
|||
|
|||
BigIntVariant internalValue() { return m_internalValue; } |
|||
Q_INVOKABLE QString value() const; |
|||
Q_INVOKABLE QBigInt* subtract(QBigInt* const& _value) const; |
|||
Q_INVOKABLE QBigInt* add(QBigInt* const& _value) const; |
|||
Q_INVOKABLE QBigInt* multiply(QBigInt* const& _value) const; |
|||
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()))); |
|||
} |
|||
|
|||
QString QEther::unit() const |
|||
{ |
|||
QMetaEnum units = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("EtherUnit")); |
|||
const char* key = units.valueToKey(m_currentUnit); |
|||
return QString(key); |
|||
} |
|||
|
|||
void QEther::setUnit(QString const& _unit) |
|||
{ |
|||
QMetaEnum units = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("EtherUnit")); |
|||
m_currentUnit = static_cast<EtherUnit>(units.keysToValue(_unit.toStdString().c_str())); |
|||
} |
|||
|
|||
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).toStdString()) |
|||
return multiply(new QBigInt(rawUnit.first)); |
|||
} |
|||
return new QBigInt(dev::u256(0)); |
|||
} |
@ -0,0 +1,86 @@ |
|||
/*
|
|||
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 Ether value 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(QString 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::ether) {} |
|||
QEther(dev::u256 _value, EtherUnit _unit, QObject* _parent = 0): QBigInt(_value, _parent), m_currentUnit(_unit) {} |
|||
~QEther() {} |
|||
|
|||
Q_INVOKABLE QString format() const; |
|||
Q_INVOKABLE QBigInt* toWei() const; |
|||
Q_INVOKABLE void setValue(QString const& _value) { m_internalValue = dev::jsToU256(_value.toStdString()); } |
|||
Q_INVOKABLE QString unit() const; |
|||
Q_INVOKABLE void setUnit(QString const& _unit); |
|||
dev::u256 toU256Wei() { return boost::get<dev::u256>(toWei()->internalValue()); } |
|||
|
|||
private: |
|||
EtherUnit m_currentUnit; |
|||
|
|||
signals: |
|||
void valueChanged(); |
|||
void unitChanged(); |
|||
}; |
|||
|
|||
} |
|||
} |
|||
|
@ -0,0 +1,106 @@ |
|||
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() |
|||
{ |
|||
etherValueEdit.text = value.value; |
|||
selectUnit(value.unit); |
|||
} |
|||
|
|||
function selectUnit(unit) |
|||
{ |
|||
for(var i = 0; i < unitsModel.count; ++i) |
|||
{ |
|||
if (unitsModel.get(i).text === unit) |
|||
{ |
|||
units.currentIndex = i; |
|||
return; |
|||
} |
|||
} |
|||
} |
|||
|
|||
RowLayout |
|||
{ |
|||
anchors.fill: parent; |
|||
id: row |
|||
width: 200 |
|||
height: parent.height |
|||
Rectangle |
|||
{ |
|||
width : 200 |
|||
color: edit ? "blue" : "white" |
|||
TextField |
|||
{ |
|||
onTextChanged: |
|||
{ |
|||
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: |
|||
{ |
|||
value.setUnit(currentText); |
|||
formattedValue.text = value.format(); |
|||
} |
|||
model: ListModel { |
|||
id: unitsModel |
|||
ListElement { text: "wei"; } |
|||
ListElement { text: "Kwei"; } |
|||
ListElement { text: "Mwei"; } |
|||
ListElement { text: "Gwei"; } |
|||
ListElement { text: "szabo"; } |
|||
ListElement { text: "finney"; } |
|||
ListElement { text: "ether"; } |
|||
ListElement { text: "grand"; } |
|||
ListElement { text: "Mether"; } |
|||
ListElement { text: "Gether"; } |
|||
ListElement { text: "Tether"; } |
|||
ListElement { text: "Pether"; } |
|||
ListElement { text: "Eether"; } |
|||
ListElement { text: "Zether"; } |
|||
ListElement { text: "Yether"; } |
|||
ListElement { text: "Nether"; } |
|||
ListElement { text: "Dether"; } |
|||
ListElement { text: "Vether"; } |
|||
ListElement { text: "Uether"; } |
|||
} |
|||
} |
|||
Rectangle |
|||
{ |
|||
anchors.verticalCenter: parent.verticalCenter |
|||
anchors.left: units.right |
|||
visible: displayFormattedValue |
|||
width: 20 |
|||
Text |
|||
{ |
|||
id: formattedValue |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,12 @@ |
|||
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: "1000000" |
|||
unit: "ether" |
|||
} |
Loading…
Reference in new issue