Skip to content

Commit

Permalink
qml: Introduce the WalletSelect component
Browse files Browse the repository at this point in the history
WalletSelect is a Popup that appears after clicking the main
WalletBadge in the DesktopNavigation bar. It contains a ListView
that allows the user to select one of the wallets listed in the
wallet directory.
  • Loading branch information
johnny9 committed May 27, 2024
1 parent aebd0dd commit 5d191d1
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ QT_MOC_CPP = \
qml/models/moc_nodemodel.cpp \
qml/models/moc_options_model.cpp \
qml/models/moc_peerlistsortproxy.cpp \
qml/models/moc_walletlistmodel.cpp \
qml/moc_appmode.cpp \
qt/moc_addressbookpage.cpp \
qt/moc_addresstablemodel.cpp \
Expand Down Expand Up @@ -121,6 +122,7 @@ BITCOIN_QT_H = \
qml/models/nodemodel.h \
qml/models/options_model.h \
qml/models/peerlistsortproxy.h \
qml/models/walletlistmodel.h \
qml/appmode.h \
qml/bitcoin.h \
qml/guiconstants.h \
Expand Down Expand Up @@ -309,6 +311,7 @@ BITCOIN_QML_BASE_CPP = \
qml/models/nodemodel.cpp \
qml/models/options_model.cpp \
qml/models/peerlistsortproxy.cpp \
qml/models/walletlistmodel.cpp \
qml/imageprovider.cpp \
qml/util.cpp

Expand All @@ -334,6 +337,7 @@ QML_RES_ICONS = \
qml/res/icons/info.png \
qml/res/icons/network-dark.png \
qml/res/icons/network-light.png \
qml/res/icons/plus.png \
qml/res/icons/shutdown.png \
qml/res/icons/singlesig-wallet.png \
qml/res/icons/storage-dark.png \
Expand Down Expand Up @@ -410,7 +414,8 @@ QML_RES_QML = \
qml/pages/settings/SettingsStorage.qml \
qml/pages/settings/SettingsTheme.qml \
qml/pages/wallet/DesktopWallets.qml \
qml/pages/wallet/WalletBadge.qml
qml/pages/wallet/WalletBadge.qml \
qml/pages/wallet/WalletSelect.qml

if TARGET_ANDROID
BITCOIN_QT_H += qml/androidnotifier.h
Expand Down
4 changes: 4 additions & 0 deletions src/qml/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <qml/models/nodemodel.h>
#include <qml/models/options_model.h>
#include <qml/models/peerlistsortproxy.h>
#include <qml/models/walletlistmodel.h>
#include <qml/imageprovider.h>
#include <qml/util.h>
#include <qml/guiconstants.h>
Expand Down Expand Up @@ -292,11 +293,14 @@ int QmlGuiMain(int argc, char* argv[])
assert(!network_style.isNull());
engine.addImageProvider(QStringLiteral("images"), new ImageProvider{network_style.data()});

WalletListModel wallet_list_model{*node, nullptr};

engine.rootContext()->setContextProperty("networkTrafficTower", &network_traffic_tower);
engine.rootContext()->setContextProperty("nodeModel", &node_model);
engine.rootContext()->setContextProperty("chainModel", &chain_model);
engine.rootContext()->setContextProperty("peerTableModel", &peer_model);
engine.rootContext()->setContextProperty("peerListModelProxy", &peer_model_sort_proxy);
engine.rootContext()->setContextProperty("walletListModel", &wallet_list_model);

OptionsQmlModel options_model(*node, !need_onboarding.toBool());
engine.rootContext()->setContextProperty("optionsModel", &options_model);
Expand Down
2 changes: 2 additions & 0 deletions src/qml/bitcoin_qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<file>pages/settings/SettingsTheme.qml</file>
<file>pages/wallet/DesktopWallets.qml</file>
<file>pages/wallet/WalletBadge.qml</file>
<file>pages/wallet/WalletSelect.qml</file>
</qresource>
<qresource prefix="/icons">
<file alias="arrow-down">res/icons/arrow-down.png</file>
Expand All @@ -87,6 +88,7 @@
<file alias="info">res/icons/info.png</file>
<file alias="network-dark">res/icons/network-dark.png</file>
<file alias="network-light">res/icons/network-light.png</file>
<file alias="plus">res/icons/plus.png</file>
<file alias="shutdown">res/icons/shutdown.png</file>
<file alias="singlesig-wallet">res/icons/singlesig-wallet.png</file>
<file alias="storage-dark">res/icons/storage-dark.png</file>
Expand Down
2 changes: 2 additions & 0 deletions src/qml/controls/Icon.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import QtQuick.Controls 2.15

Button {
id: root
width: icon.width
height: icon.height
required property color color
required property url source
property int size: 32
Expand Down
4 changes: 4 additions & 0 deletions src/qml/imageprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,9 @@ QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize
return QIcon(":/icons/tooltip-arrow-light").pixmap(requested_size);
}

if (id == "plus") {
*size = requested_size;
return QIcon(":/icons/plus").pixmap(requested_size);
}
return {};
}
81 changes: 81 additions & 0 deletions src/qml/models/walletlistmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2024 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/models/walletlistmodel.h>

#include <interfaces/node.h>

#include <QSet>

WalletListModel::WalletListModel(interfaces::Node& node, QObject *parent)
: QAbstractListModel(parent)
, m_node(node)
{
setSelectedWallet("Singlesig Wallet");
}

void WalletListModel::listWalletDir()
{
QSet<QString> existing_names;
for (int i = 0; i < rowCount(); ++i) {
QModelIndex index = this->index(i, 0);
QString name = data(index, NameRole).toString();
existing_names.insert(name);
}

for (const std::string &name : m_node.walletLoader().listWalletDir()) {
QString qname = QString::fromStdString(name);
if (!existing_names.contains(qname)) {
addItem({ qname });
}
}
}

void WalletListModel::setSelectedWallet(QString wallet_name)
{
if (m_selected_wallet != wallet_name) {
m_selected_wallet = wallet_name;
Q_EMIT selectedWalletChanged();
}
}

QString WalletListModel::selectedWallet() const
{
return m_selected_wallet;
}

int WalletListModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_items.size();
}

QVariant WalletListModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size())
return QVariant();

const auto &item = m_items[index.row()];
switch (role) {
case Qt::DisplayRole:
case NameRole:
return item.name;
default:
return QVariant();
}
}

QHash<int, QByteArray> WalletListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[NameRole] = "name";
return roles;
}

void WalletListModel::addItem(const Item &item)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_items.append(item);
endInsertRows();
}
55 changes: 55 additions & 0 deletions src/qml/models/walletlistmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) 2024 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_MODELS_WALLETLISTMODEL_H
#define BITCOIN_QML_MODELS_WALLETLISTMODEL_H

#include <interfaces/wallet.h>
#include <QAbstractListModel>
#include <QList>

namespace interfaces {
class Node;
}

class WalletListModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(QString selectedWallet READ selectedWallet WRITE setSelectedWallet NOTIFY selectedWalletChanged)

public:
WalletListModel(interfaces::Node& node, QObject *parent = nullptr);
~WalletListModel() = default;

enum Roles {
NameRole = Qt::UserRole + 1
};

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;

void setSelectedWallet(QString wallet_name);
QString selectedWallet() const;

public Q_SLOTS:
void listWalletDir();

Q_SIGNALS:
void selectedWalletChanged();

private:
struct Item {
QString name;
};

void addItem(const Item &item);

QList<Item> m_items;
interfaces::Node& m_node;
QString m_selected_wallet;

};

#endif // BITCOIN_QML_MODELS_WALLETLISTMODEL_H
18 changes: 17 additions & 1 deletion src/qml/pages/wallet/DesktopWallets.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,23 @@ Page {
leftItem: WalletBadge {
implicitWidth: 164
implicitHeight: 60
text: qsTr("Singlesig Wallet")
text: walletListModel.selectedWallet

MouseArea {
anchors.fill: parent
onClicked: {
walletListModel.listWalletDir()
walletSelect.opened ? walletSelect.close() : walletSelect.open()
}
}

WalletSelect {
id: walletSelect
model: walletListModel
closePolicy: Popup.CloseOnPressOutside
x: 0
y: parent.height - 5
}
}
centerItem: RowLayout {
NavigationTab {
Expand Down
108 changes: 108 additions & 0 deletions src/qml/pages/wallet/WalletSelect.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright (c) 2024 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.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15

import "../../controls"

Popup {
id: root

property alias model: listView.model
implicitHeight: listView.height + arrow.height
implicitWidth: 250

background: Item {
anchors.fill: parent
Rectangle {
id: tooltipBg
color: Theme.color.neutral0
border.color: Theme.color.neutral4
radius: 5
border.width: 1
width: parent.width
height: parent.height - arrow.height - 1
anchors.top: arrow.bottom
anchors.horizontalCenter: root.horizontalCenter
anchors.topMargin: -1
}
Image {
id: arrow
source: Theme.image.tooltipArrow
width: 22
height: 10
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top
}
}

ButtonGroup {
id: buttonGroup
}

ListView {
id: listView
anchors.topMargin: arrow.height
width: 220
height: contentHeight
interactive: false
spacing: 2
model: walletListModel

header: CoreText {
text: qsTr("Wallets")
visible: listView.count > 0
bold: true
width: 220
height: 30
color: Theme.color.neutral9
font.pixelSize: 14
topPadding: 10
bottomPadding: 5
}

delegate: WalletBadge {
required property string name;

width: 220
height: 32
text: name
ButtonGroup.group: buttonGroup
showBalance: false
showIcon: false
onClicked: {
walletListModel.selectedWallet = name
root.close()
}

}

footer: RowLayout {
id: addWallet
height: 45
width: addIcon.size + addText.width + spacing
anchors.horizontalCenter: parent.horizontalCenter
Icon {
id: addIcon
Layout.alignment: Qt.AlignHCenter
source: "image://images/plus"
color: Theme.color.neutral8
size: 14
topPadding: 5
bottomPadding: 10
}
CoreText {
id: addText
Layout.alignment: Qt.AlignHCenter
text: qsTr("Add Wallet")
color: Theme.color.neutral9
font.pixelSize: 15
topPadding: 5
bottomPadding: 10
}
}
}
}
Binary file added src/qml/res/icons/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/qml/res/src/plus.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5d191d1

Please sign in to comment.