Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BlockCounter QML component which handles NotifyBlockTip signal #31

Merged
merged 4 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/qml/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ int QmlGuiMain(int argc, char* argv[])

handler_message_box.disconnect();

NodeModel node_model;
NodeModel node_model{*node};
InitExecutor init_executor{*node};
QObject::connect(&node_model, &NodeModel::requestedInitialize, &init_executor, &InitExecutor::initialize);
QObject::connect(&node_model, &NodeModel::requestedShutdown, &init_executor, &InitExecutor::shutdown);
// QObject::connect(&init_executor, &InitExecutor::initializeResult, &node_model, &NodeModel::initializeResult);
QObject::connect(&init_executor, &InitExecutor::initializeResult, &node_model, &NodeModel::initializeResult);
QObject::connect(&init_executor, &InitExecutor::shutdownResult, qGuiApp, &QGuiApplication::quit, Qt::QueuedConnection);
// QObject::connect(&init_executor, &InitExecutor::runawayException, &node_model, &NodeModel::handleRunawayException);

Expand Down
1 change: 1 addition & 0 deletions src/qml/bitcoin_qml.qrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/qml">
<file>components/BlockCounter.qml</file>
<file>pages/initerrormessage.qml</file>
<file>pages/stub.qml</file>
</qresource>
Expand Down
21 changes: 21 additions & 0 deletions src/qml/components/BlockCounter.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

// The BlockCounter component.

import QtQuick 2.12
import QtQuick.Controls 2.12

Label {
property int blockHeight: 0
background: Rectangle {
color: "black"
}
color: "orange"
padding: 16
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
font.pixelSize: height / 3
text: blockHeight
}
32 changes: 31 additions & 1 deletion src/qml/nodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@

#include <qml/nodemodel.h>

#include <QDebug>
#include <interfaces/node.h>
#include <validation.h>

NodeModel::NodeModel(interfaces::Node& node)
: m_node{node}
{
ConnectToBlockTipSignal();
}

void NodeModel::setBlockTipHeight(int new_height)
{
if (new_height != m_block_tip_height) {
m_block_tip_height = new_height;
Q_EMIT blockTipHeightChanged();
}
}

void NodeModel::startNodeInitializionThread()
{
Expand All @@ -15,3 +30,18 @@ void NodeModel::startNodeShutdown()
{
Q_EMIT requestedShutdown();
}

void NodeModel::initializeResult([[maybe_unused]] bool success, interfaces::BlockAndHeaderTipInfo tip_info)
{
// TODO: Handle the `success` parameter,
setBlockTipHeight(tip_info.block_height);
}

void NodeModel::ConnectToBlockTipSignal()
{
assert(!m_handler_notify_block_tip);
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(
[this](SynchronizationState state, interfaces::BlockTip tip, double verification_progress) {
setBlockTipHeight(tip.block_height);
});
}
28 changes: 28 additions & 0 deletions src/qml/nodemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,48 @@
#ifndef BITCOIN_QML_NODEMODEL_H
#define BITCOIN_QML_NODEMODEL_H

#include <interfaces/handler.h>
#include <interfaces/node.h>

#include <memory>

#include <QObject>

namespace interfaces {
class Node;
}

/** Model for Bitcoin network client. */
class NodeModel : public QObject
{
Q_OBJECT
Q_PROPERTY(int blockTipHeight READ blockTipHeight NOTIFY blockTipHeightChanged)

public:
explicit NodeModel(interfaces::Node& node);

int blockTipHeight() const { return m_block_tip_height; }
void setBlockTipHeight(int new_height);

Q_INVOKABLE void startNodeInitializionThread();
void startNodeShutdown();

public Q_SLOTS:
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info);

Q_SIGNALS:
void blockTipHeightChanged();
void requestedInitialize();
void requestedShutdown();

private:
// Properties that are exposed to QML.
int m_block_tip_height{0};

interfaces::Node& m_node;
std::unique_ptr<interfaces::Handler> m_handler_notify_block_tip;

void ConnectToBlockTipSignal();
};

#endif // BITCOIN_QML_NODEMODEL_H
11 changes: 10 additions & 1 deletion src/qml/pages/stub.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

import QtQml 2.12
import QtQuick 2.12
import QtQuick.Controls 2.12
import "../components" as BitcoinCoreComponents


ApplicationWindow {
id: appWindow
Expand All @@ -13,4 +15,11 @@ ApplicationWindow {
visible: true

Component.onCompleted: nodeModel.startNodeInitializionThread();

BitcoinCoreComponents.BlockCounter {
id: blockCounter
anchors.centerIn: parent
height: parent.height / 3
blockHeight: nodeModel.blockTipHeight
}
}
1 change: 0 additions & 1 deletion src/qt/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ static void RegisterMetaTypes()

qRegisterMetaType<std::function<void()>>("std::function<void()>");
qRegisterMetaType<QMessageBox::Icon>("QMessageBox::Icon");
qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");
}

static QString GetLangTerritory()
Expand Down
3 changes: 3 additions & 0 deletions src/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <qt/bitcoin.h>
#endif // USE_QML

#include <interfaces/node.h>
#include <logging.h>
#include <noui.h>
#include <util/system.h>
Expand Down Expand Up @@ -62,6 +63,8 @@ void DebugMessageHandler(QtMsgType type, const QMessageLogContext& context, cons

int main(int argc, char* argv[])
{
qRegisterMetaType<interfaces::BlockAndHeaderTipInfo>("interfaces::BlockAndHeaderTipInfo");

#ifdef WIN32
util::WinCmdLineArgs win_args;
std::tie(argc, argv) = win_args.get();
Expand Down