diff --git a/src/Makefile.qt.include b/src/Makefile.qt.include index 8f7e560357..749ab2c614 100644 --- a/src/Makefile.qt.include +++ b/src/Makefile.qt.include @@ -37,6 +37,7 @@ QT_FORMS_UI = \ QT_MOC_CPP = \ qml/moc_appmode.cpp \ qml/moc_nodemodel.cpp \ + qml/moc_options_model.cpp \ qt/moc_addressbookpage.cpp \ qt/moc_addresstablemodel.cpp \ qt/moc_askpassphrasedialog.cpp \ @@ -112,6 +113,7 @@ BITCOIN_QT_H = \ qml/bitcoin.h \ qml/imageprovider.h \ qml/nodemodel.h \ + qml/options_model.h \ qml/util.h \ qt/addressbookpage.h \ qt/addresstablemodel.h \ @@ -291,6 +293,7 @@ BITCOIN_QML_BASE_CPP = \ qml/bitcoin.cpp \ qml/imageprovider.cpp \ qml/nodemodel.cpp \ + qml/options_model.cpp \ qml/util.cpp QML_RES_FONTS = \ diff --git a/src/qml/bitcoin.cpp b/src/qml/bitcoin.cpp index 489171d3ed..5eccb1a43a 100644 --- a/src/qml/bitcoin.cpp +++ b/src/qml/bitcoin.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -185,6 +186,9 @@ int QmlGuiMain(int argc, char* argv[]) engine.rootContext()->setContextProperty("nodeModel", &node_model); + OptionsQmlModel options_model{*node}; + engine.rootContext()->setContextProperty("optionsModel", &options_model); + #ifdef __ANDROID__ AppMode app_mode(AppMode::MOBILE); #else diff --git a/src/qml/components/StorageOptions.qml b/src/qml/components/StorageOptions.qml index fbd1f2aa33..b8e42a5146 100644 --- a/src/qml/components/StorageOptions.qml +++ b/src/qml/components/StorageOptions.qml @@ -19,11 +19,18 @@ ColumnLayout { description: qsTr("Uses about 2GB. For simple wallet use.") recommended: true checked: true + onClicked: { + optionsModel.prune = true + optionsModel.pruneSizeGB = 2 + } } OptionButton { Layout.fillWidth: true ButtonGroup.group: group text: qsTr("Store all data") description: qsTr("Uses about 550GB. Support the network.") + onClicked: { + optionsModel.prune = false + } } } diff --git a/src/qml/components/StorageSettings.qml b/src/qml/components/StorageSettings.qml index d8bd83e14c..236653356b 100644 --- a/src/qml/components/StorageSettings.qml +++ b/src/qml/components/StorageSettings.qml @@ -11,21 +11,18 @@ ColumnLayout { spacing: 20 Setting { Layout.fillWidth: true - header: qsTr("Store Recent blocks only") - actionItem: OptionSwitch {} - } - Setting { - Layout.fillWidth: true - header: qsTr("Storage limit") - actionItem: ValueInput { - description: qsTr("2 GB") + header: qsTr("Store recent blocks only") + actionItem: OptionSwitch { + checked: optionsModel.prune + onToggled: optionsModel.prune = checked } } Setting { Layout.fillWidth: true - header: qsTr("Data location") + header: qsTr("Storage limit (GB)") actionItem: ValueInput { - description: "c://.../data" + description: optionsModel.pruneSizeGB + onEditingFinished: optionsModel.pruneSizeGB = parseInt(text) } } } diff --git a/src/qml/options_model.cpp b/src/qml/options_model.cpp new file mode 100644 index 0000000000..ed934121d4 --- /dev/null +++ b/src/qml/options_model.cpp @@ -0,0 +1,46 @@ +// Copyright (c) 2022 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 + +#include +#include +#include +#include +#include +#include + +#include + +OptionsQmlModel::OptionsQmlModel(interfaces::Node& node) + : m_node{node} +{ + int64_t prune_value{SettingToInt(m_node.getPersistentSetting("prune"), 0)}; + m_prune = (prune_value > 1); + m_prune_size_gb = m_prune ? PruneMiBtoGB(prune_value) : DEFAULT_PRUNE_TARGET_GB; +} + +void OptionsQmlModel::setPrune(bool new_prune) +{ + if (new_prune != m_prune) { + m_prune = new_prune; + m_node.updateRwSetting("prune", pruneSetting()); + Q_EMIT pruneChanged(new_prune); + } +} + +void OptionsQmlModel::setPruneSizeGB(int new_prune_size_gb) +{ + if (new_prune_size_gb != m_prune_size_gb) { + m_prune_size_gb = new_prune_size_gb; + m_node.updateRwSetting("prune", pruneSetting()); + Q_EMIT pruneSizeGBChanged(new_prune_size_gb); + } +} + +util::SettingsValue OptionsQmlModel::pruneSetting() const +{ + assert(!m_prune || m_prune_size_gb >= 1); + return m_prune ? PruneGBtoMiB(m_prune_size_gb) : 0; +} diff --git a/src/qml/options_model.h b/src/qml/options_model.h new file mode 100644 index 0000000000..7e227ee6b1 --- /dev/null +++ b/src/qml/options_model.h @@ -0,0 +1,45 @@ +// Copyright (c) 2022 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_OPTIONS_MODEL_H +#define BITCOIN_QML_OPTIONS_MODEL_H + +#include + +#include + +namespace interfaces { +class Node; +} + +/** Model for Bitcoin client options. */ +class OptionsQmlModel : public QObject +{ + Q_OBJECT + Q_PROPERTY(bool prune READ prune WRITE setPrune NOTIFY pruneChanged) + Q_PROPERTY(int pruneSizeGB READ pruneSizeGB WRITE setPruneSizeGB NOTIFY pruneSizeGBChanged) + +public: + explicit OptionsQmlModel(interfaces::Node& node); + + bool prune() const { return m_prune; } + void setPrune(bool new_prune); + int pruneSizeGB() const { return m_prune_size_gb; } + void setPruneSizeGB(int new_prune_size); + +Q_SIGNALS: + void pruneChanged(bool new_prune); + void pruneSizeGBChanged(int new_prune_size_gb); + +private: + interfaces::Node& m_node; + + // Properties that are exposed to QML. + bool m_prune; + int m_prune_size_gb; + + util::SettingsValue pruneSetting() const; +}; + +#endif // BITCOIN_QML_OPTIONS_MODEL_H diff --git a/src/qml/pages/settings/SettingsStorage.qml b/src/qml/pages/settings/SettingsStorage.qml index 3c880b416c..14233059a8 100644 --- a/src/qml/pages/settings/SettingsStorage.qml +++ b/src/qml/pages/settings/SettingsStorage.qml @@ -14,5 +14,11 @@ InformationPage { headerText: qsTr("Storage settings") headerMargin: 0 detailActive: true - detailItem: StorageSettings {} + detailItem: StorageSettings { + // Set default prune values + Component.onCompleted: { + optionsModel.prune = true + optionsModel.pruneSizeGB = 2 + } + } }