Browse Source

More on QML.

cl-refactor
Gav Wood 11 years ago
parent
commit
8812b29ecc
  1. 45
      walleth/MainWin.cpp
  2. 20
      walleth/MainWin.h
  3. 4
      walleth/Simple.qml

45
walleth/MainWin.cpp

@ -69,6 +69,15 @@ Address QEthereum::address() const
return m_client->address();
}
void QEthereum::setAddress(Address _a)
{
if (m_client->address() != _a)
{
m_client->setAddress(_a);
changed();
}
}
u256 QEthereum::balance() const
{
return m_client->postState().balance(address());
@ -155,12 +164,10 @@ Main::Main(QWidget *parent) :
readSettings();
refresh();
m_refresh = new QTimer(this);
connect(m_refresh, SIGNAL(timeout()), SLOT(refresh()));
m_refresh->start(100);
m_refreshNetwork = new QTimer(this);
connect(m_refreshNetwork, SIGNAL(timeout()), SLOT(refreshNetwork()));
m_refreshNetwork->start(1000);
connect(this, SIGNAL(changed()), SLOT(refresh()));
connect(&m_webCtrl, &QNetworkAccessManager::finished, [&](QNetworkReply* _r)
{
@ -241,7 +248,7 @@ void Main::readSettings()
m_myKeys.append(KeyPair(k));
}
}
m_eth->client()->setAddress(m_myKeys.back().address());
m_eth->setAddress(m_myKeys.last().address());
m_peers = s.value("peers").toByteArray();
ui->upnp->setChecked(s.value("upnp", true).toBool());
m_clientName = s.value("clientName", "").toString();
@ -260,31 +267,23 @@ eth::State const& Main::state() const
return ui->preview->isChecked() ? m_eth->client()->postState() : m_eth->client()->state();
}
void Main::refresh(bool _override)
void Main::refresh()
{
m_eth->client()->lock();
eth::ClientGuard l(m_eth->client());
auto const& st = state();
bool c = m_eth->client()->changed();
if (c || _override)
{
auto d = m_eth->client()->blockChain().details();
auto diff = BlockInfo(m_eth->client()->blockChain().block()).difficulty;
ui->blockCount->setText(QString("#%1 @%3 T%2").arg(d.number).arg(toLog2(d.totalDifficulty)).arg(toLog2(diff)));
}
auto d = m_eth->client()->blockChain().details();
auto diff = BlockInfo(m_eth->client()->blockChain().block()).difficulty;
ui->blockCount->setText(QString("#%1 @%3 T%2").arg(d.number).arg(toLog2(d.totalDifficulty)).arg(toLog2(diff)));
if (c || m_keysChanged || _override)
m_keysChanged = false;
u256 totalBalance = 0;
for (auto i: m_myKeys)
{
m_keysChanged = false;
u256 totalBalance = 0;
for (auto i: m_myKeys)
{
u256 b = st.balance(i.address());
totalBalance += b;
}
ui->balance->setText(QString::fromStdString(formatBalance(totalBalance)));
u256 b = st.balance(i.address());
totalBalance += b;
}
m_eth->client()->unlock();
ui->balance->setText(QString::fromStdString(formatBalance(totalBalance)));
}
void Main::on_net_triggered(bool _auto)

20
walleth/MainWin.h

@ -6,7 +6,6 @@
#include <QtCore/QMutex>
#include <QtWidgets/QMainWindow>
#include <libethereum/Common.h>
using eth::u256; // workaround for Q_PROPERTY which can't handle scoped types.
namespace Ui {
class Main;
@ -23,6 +22,9 @@ class QQmlEngine;
class QJSEngine;
Q_DECLARE_METATYPE(eth::u256)
Q_DECLARE_METATYPE(eth::Address)
Q_DECLARE_METATYPE(eth::Secret)
Q_DECLARE_METATYPE(eth::KeyPair)
class U256Helper: public QObject
{
@ -50,9 +52,7 @@ public:
Q_INVOKABLE double toFinney(eth::u256 _t) const { return toWei(_t) / (double)eth::finney; }
Q_INVOKABLE double toEther(eth::u256 _t) const { return toWei(_t) / (double)eth::ether; }
Q_INVOKABLE QString toString(eth::u256 _t) const { return QString::fromStdString(eth::formatBalance(_t)); }
Q_INVOKABLE QString test() const { return "Hello"; }
Q_INVOKABLE QString stringOf(eth::u256 _t) const { return QString::fromStdString(eth::formatBalance(_t)); }
};
class KeyHelper: public QObject
@ -67,8 +67,10 @@ public:
Q_INVOKABLE eth::Secret secret(eth::KeyPair _p) const { return _p.secret(); }
Q_INVOKABLE eth::KeyPair keypair(eth::Secret _k) const { return eth::KeyPair(_k); }
Q_INVOKABLE eth::Address fromString(QString _s) const { return eth::Address(_s.toStdString()); }
Q_INVOKABLE QString toString(eth::Address _a) const { return QString::fromStdString(eth::asHex(_a.asArray())); }
Q_INVOKABLE bool isNull(eth::Address _a) const { return !_a; }
Q_INVOKABLE eth::Address addressOf(QString _s) const { return eth::Address(_s.toStdString()); }
Q_INVOKABLE QString stringOf(eth::Address _a) const { return QString::fromStdString(eth::asHex(_a.asArray())); }
Q_INVOKABLE QString toAbridged(eth::Address _a) const { return QString::fromStdString(_a.abridged()); }
};
@ -94,6 +96,7 @@ public:
public slots:
void transact(eth::Secret _secret, eth::Address _dest, eth::u256 _amount);
void setAddress(eth::Address);
signals:
void changed();
@ -103,6 +106,7 @@ protected:
private:
Q_PROPERTY(eth::u256 balance READ balance NOTIFY changed)
Q_PROPERTY(eth::Address address READ address WRITE setAddress NOTIFY changed)
std::unique_ptr<eth::Client> m_client;
};
@ -121,10 +125,10 @@ private slots:
void on_create_triggered();
void on_net_triggered(bool _auto = false);
void on_about_triggered();
void on_preview_triggered() { refresh(true); }
void on_preview_triggered() { refresh(); }
void on_quit_triggered() { close(); }
void refresh(bool _override = false);
void refresh();
void refreshNetwork();
private:

4
walleth/Simple.qml

@ -3,12 +3,12 @@ import QtQuick.Controls 1.0
import QtQuick.Layouts 1.0
//import org.ethereum 1.0
Item {
Label {
id: main
anchors.fill: parent
anchors.margins: 9
Label {
text: "Balance: " + u256.toWei(eth.balance) + "\nAccount: " + key.toString(eth.account) + "\n" + u256.test()
text: "Balance: " + u256.stringOf(eth.balance) + "\nAccount: " + key.stringOf(eth.address)
Layout.minimumHeight: 30
Layout.fillHeight: true
Layout.fillWidth: true

Loading…
Cancel
Save