-
Notifications
You must be signed in to change notification settings - Fork 50
Add ChainModel #40
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
Closed
Closed
Add ChainModel #40
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3c383d8
qml: Set tab focus behavior
promag eb30189
qml: Use window color instead of background
promag 7c48323
qml: Remove explicit height of BlockCounter
promag 66af330
qml: Drop import qualifier BitcoinCoreComponents
promag b4bebd3
qml: Use ColumnLayout in application window
promag 7d19f8b
qml: Add OptionButton control
promag fe7e781
qml: Add ConnectionOptions component
promag 4c4aa30
qml: Show ConnectionOptions
promag 99a0473
qml: Decouple InitExecutor from NodeModel
promag f656ee3
qt: Expose ready property in InitExecutor
promag de0be5a
qml: Factor out Engine class
promag d483874
qml: Instantiate NodeModel in qml
promag aab42ba
qml: Update block tip in the gui thread
promag 418ef76
qml: Expose chain from engine
promag d662b59
qml: Add ChainModel
promag cb28239
qml: Demo ChainModel with a list view
promag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// 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. | ||
|
||
#include <qml/chainmodel.h> | ||
|
||
#include <interfaces/chain.h> | ||
#include <interfaces/handler.h> | ||
#include <interfaces/node.h> | ||
#include <qml/engine.h> | ||
#include <qt/guiutil.h> | ||
|
||
#include <vector> | ||
|
||
struct Block | ||
{ | ||
const int height; | ||
const std::string hash; | ||
}; | ||
|
||
struct ChainModelPrivate | ||
{ | ||
std::unique_ptr<interfaces::Handler> handler_notify_block_tip; | ||
std::vector<Block> blocks; | ||
}; | ||
|
||
ChainModel::ChainModel(QObject* parent) | ||
: QAbstractListModel(parent) | ||
, d(new ChainModelPrivate) | ||
{ | ||
} | ||
|
||
ChainModel::~ChainModel() | ||
{ | ||
} | ||
|
||
void ChainModel::classBegin() | ||
{ | ||
} | ||
|
||
void ChainModel::componentComplete() | ||
{ | ||
assert(!d->handler_notify_block_tip); | ||
d->handler_notify_block_tip = Engine::node(this).handleNotifyBlockTip( | ||
[this](SynchronizationState state, interfaces::BlockTip tip, double verification_progress) { | ||
// TODO: update existing model incrementally instead of reset | ||
GUIUtil::ObjectInvoke(this, [this] { | ||
beginResetModel(); | ||
d->blocks.clear(); | ||
endResetModel(); | ||
}); | ||
}); | ||
} | ||
|
||
QHash<int, QByteArray> ChainModel::roleNames() const | ||
{ | ||
return { | ||
{ BlockHeightRole, "blockHeight" }, | ||
{ BlockHashRole, "blockHash" } | ||
}; | ||
} | ||
|
||
bool ChainModel::canFetchMore(const QModelIndex&) const | ||
{ | ||
return d->blocks.size() == 0 || d->blocks[0].height > 0; | ||
} | ||
|
||
void ChainModel::fetchMore(const QModelIndex& parent) | ||
{ | ||
auto& chain = Engine::chain(this); | ||
int height = d->blocks.size() > 0 ? d->blocks[d->blocks.size() - 1].height - 1 : *chain.getHeight(); | ||
// TODO: make page size configurable | ||
// TODO: refactor to call beginInsertRows before the loop | ||
for (int count = 10; count > 0 && height >= 0; --count, --height) { | ||
const auto hash = chain.getBlockHash(height).ToString(); | ||
beginInsertRows(QModelIndex(), d->blocks.size(), d->blocks.size()); | ||
d->blocks.push_back({height, hash}); | ||
endInsertRows(); | ||
} | ||
} | ||
|
||
int ChainModel::rowCount(const QModelIndex& parent) const | ||
{ | ||
return d->blocks.size(); | ||
} | ||
|
||
QVariant ChainModel::data(const QModelIndex& index, int role) const | ||
{ | ||
switch (role) { | ||
case BlockHeightRole: return d->blocks[index.row()].height; | ||
case BlockHashRole: return QString::fromStdString(d->blocks[index.row()].hash); | ||
} // no default case, so the compiler can warn about missing cases | ||
assert(false); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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. | ||
|
||
#ifndef BITCOIN_QML_CHAINMODEL_H | ||
#define BITCOIN_QML_CHAINMODEL_H | ||
|
||
#include <memory> | ||
|
||
#include <QAbstractListModel> | ||
#include <QQmlParserStatus> | ||
|
||
class ChainModelPrivate; | ||
class ChainModel : public QAbstractListModel, public QQmlParserStatus | ||
{ | ||
Q_OBJECT | ||
Q_INTERFACES(QQmlParserStatus) | ||
std::unique_ptr<ChainModelPrivate> d; | ||
|
||
public: | ||
enum Role { | ||
BlockHeightRole = Qt::UserRole + 1, | ||
BlockHashRole, | ||
}; | ||
|
||
explicit ChainModel(QObject* parent = nullptr); | ||
~ChainModel(); | ||
|
||
// QQmlParserStatus | ||
void classBegin() override; | ||
void componentComplete() override; | ||
|
||
// QAbstractListModel | ||
QHash<int, QByteArray> roleNames() const override; | ||
bool canFetchMore(const QModelIndex& parent) const override; | ||
void fetchMore(const QModelIndex& parent) override; | ||
int rowCount(const QModelIndex& parent) const override; | ||
QVariant data(const QModelIndex& index, int role) const override; | ||
}; | ||
|
||
#endif // BITCOIN_QML_CHAINMODEL_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
|
||
import BitcoinCore 1.0 | ||
import QtQuick 2.12 | ||
import QtQuick.Controls 2.12 | ||
import QtQuick.Layouts 1.11 | ||
import "../controls" | ||
|
||
ListView { | ||
spacing: 15 | ||
model: ChainModel { | ||
} | ||
delegate: ItemDelegate { | ||
id: delegate | ||
padding: 15 | ||
background: Rectangle { | ||
border.width: 1 | ||
border.color: delegate.hovered ? "white" : "#999999" | ||
radius: 10 | ||
color: "transparent" | ||
} | ||
contentItem: ColumnLayout { | ||
spacing: 3 | ||
Label { | ||
color: "white" | ||
font.family: "Inter" | ||
font.styleName: "Regular" | ||
font.pixelSize: 28 | ||
text: qsTrId('Block #%1').arg(blockHeight) | ||
} | ||
Label { | ||
Layout.fillWidth: true | ||
Layout.preferredWidth: 0 | ||
color: "white" | ||
elide: Text.ElideRight | ||
font.family: "Inter" | ||
font.styleName: "Regular" | ||
font.pixelSize: 13 | ||
text: blockHash | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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. | ||
|
||
import QtQuick 2.12 | ||
import QtQuick.Controls 2.12 | ||
import QtQuick.Layouts 1.11 | ||
import "../controls" | ||
|
||
ColumnLayout { | ||
spacing: 15 | ||
|
||
ButtonGroup { | ||
id: group | ||
} | ||
|
||
OptionButton { | ||
ButtonGroup.group: group | ||
Layout.fillWidth: true | ||
text: qsTr("Fast always on") | ||
description: qsTr("Loads quickly at all times and uses as much cellular data as needed.") | ||
recommended: true | ||
} | ||
|
||
OptionButton { | ||
ButtonGroup.group: group | ||
Layout.fillWidth: true | ||
checked: true | ||
text: qsTr("Slow always on") | ||
description: qsTr("Loads quickly at all times and uses as much cellular data as needed.") | ||
} | ||
|
||
OptionButton { | ||
ButtonGroup.group: group | ||
Layout.fillWidth: true | ||
text: qsTr("Only when on Wi-Fi") | ||
description: qsTr("Loads quickly when on wi-fi and pauses when on cellular data.") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
qDebug() << Q_FUNC_INFO;
while testing (scrolling the list) to see how lazy loading works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ryanofsky using
interfaces::Chain
here.