From bf4d620c2f17aa2561da2befc198c81f2a96d041 Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Fri, 6 Oct 2023 19:11:52 -0400 Subject: [PATCH] Implementation of EditSideStakeDialog --- src/Makefile.qt.include | 4 + src/qt/CMakeLists.txt | 1 + src/qt/editsidestakedialog.cpp | 137 +++++++++++++++++++ src/qt/editsidestakedialog.h | 53 ++++++++ src/qt/forms/editsidestakedialog.ui | 200 ++++++++++++++++++++++++++++ src/qt/optionsdialog.cpp | 20 +++ 6 files changed, 415 insertions(+) create mode 100644 src/qt/editsidestakedialog.cpp create mode 100644 src/qt/editsidestakedialog.h create mode 100644 src/qt/forms/editsidestakedialog.ui diff --git a/src/Makefile.qt.include b/src/Makefile.qt.include index de442f7f93..6e67219bec 100755 --- a/src/Makefile.qt.include +++ b/src/Makefile.qt.include @@ -82,6 +82,7 @@ QT_FORMS_UI = \ qt/forms/consolidateunspentwizardsendpage.ui \ qt/forms/diagnosticsdialog.ui \ qt/forms/editaddressdialog.ui \ + qt/forms/editsidestakedialog.ui \ qt/forms/favoritespage.ui \ qt/forms/intro.ui \ qt/forms/mrcrequestpage.ui \ @@ -143,6 +144,7 @@ QT_MOC_CPP = \ qt/moc_csvmodelwriter.cpp \ qt/moc_diagnosticsdialog.cpp \ qt/moc_editaddressdialog.cpp \ + qt/moc_editsidestakedialog.cpp \ qt/moc_favoritespage.cpp \ qt/moc_guiutil.cpp \ qt/moc_intro.cpp \ @@ -250,6 +252,7 @@ GRIDCOINRESEARCH_QT_H = \ qt/decoration.h \ qt/diagnosticsdialog.h \ qt/editaddressdialog.h \ + qt/editsidestakedialog.h \ qt/favoritespage.h \ qt/guiconstants.h \ qt/guiutil.h \ @@ -342,6 +345,7 @@ GRIDCOINRESEARCH_QT_CPP = \ qt/decoration.cpp \ qt/diagnosticsdialog.cpp \ qt/editaddressdialog.cpp \ + qt/editsidestakedialog.cpp \ qt/favoritespage.cpp \ qt/guiutil.cpp \ qt/intro.cpp \ diff --git a/src/qt/CMakeLists.txt b/src/qt/CMakeLists.txt index 03b06569f7..b62d55953b 100644 --- a/src/qt/CMakeLists.txt +++ b/src/qt/CMakeLists.txt @@ -29,6 +29,7 @@ add_library(gridcoinqt STATIC decoration.cpp diagnosticsdialog.cpp editaddressdialog.cpp + editsidestakedialog.cpp favoritespage.cpp guiutil.cpp intro.cpp diff --git a/src/qt/editsidestakedialog.cpp b/src/qt/editsidestakedialog.cpp new file mode 100644 index 0000000000..5fe71ac552 --- /dev/null +++ b/src/qt/editsidestakedialog.cpp @@ -0,0 +1,137 @@ +// Copyright (c) 2014-2023 The Gridcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or https://opensource.org/licenses/mit-license.php. + +#include "editsidestakedialog.h" +#include "ui_editsidestakedialog.h" +#include "optionsmodel.h" +#include "sidestaketablemodel.h" +#include "guiutil.h" +#include "qt/decoration.h" + +#include +#include + +EditSideStakeDialog::EditSideStakeDialog(Mode mode, QWidget* parent) + : QDialog(parent) + , ui(new Ui::EditSideStakeDialog) + , mapper(nullptr) + , mode(mode) + , model(nullptr) +{ + ui->setupUi(this); + + resize(GRC::ScaleSize(this, width(), height())); + + GUIUtil::setupAddressWidget(ui->addressLineEdit, this); + + switch (mode) + { + case NewSideStake: + setWindowTitle(tr("New SideStake")); + break; + case EditSideStake: + setWindowTitle(tr("Edit SideStake")); + ui->addressLineEdit->setEnabled(false); + break; + } + + mapper = new QDataWidgetMapper(this); + mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit); +} + +EditSideStakeDialog::~EditSideStakeDialog() +{ + delete ui; +} + +void EditSideStakeDialog::setModel(OptionsModel *model) +{ + this->model = model; + if (!model) { + return; + } + + mapper->setModel(model); + mapper->addMapping(ui->addressLineEdit, SideStakeTableModel::Address); + mapper->addMapping(ui->allocationLineEdit, SideStakeTableModel::Allocation); + mapper->addMapping(ui->descriptionLineEdit, SideStakeTableModel::Description); +} + +void EditSideStakeDialog::loadRow(int row) +{ + mapper->setCurrentIndex(row); +} + +bool EditSideStakeDialog::saveCurrentRow() +{ + if (!model) { + return false; + } + + switch (mode) + { + case NewSideStake: + address = model->getSideStakeTableModel()->addRow(ui->addressLineEdit->text(), + ui->allocationLineEdit->text(), + ui->descriptionLineEdit->text()); + break; + case EditSideStake: + if (mapper->submit()) { + address = ui->addressLineEdit->text(); + } + break; + } + return !address.isEmpty(); +} + +void EditSideStakeDialog::accept() +{ + if (!model) { + return; + } + + if (!saveCurrentRow()) + { + switch(model->getSideStakeTableModel()->getEditStatus()) + { + case SideStakeTableModel::OK: + // Failed with unknown reason. Just reject. + break; + case SideStakeTableModel::NO_CHANGES: + // No changes were made during edit operation. Just reject. + break; + case SideStakeTableModel::INVALID_ADDRESS: + QMessageBox::warning(this, windowTitle(), + tr("The entered address \"%1\" is not " + "a valid Gridcoin address.").arg(ui->addressLineEdit->text()), + QMessageBox::Ok, QMessageBox::Ok); + break; + case SideStakeTableModel::DUPLICATE_ADDRESS: + QMessageBox::warning(this, windowTitle(), + tr("The entered address \"%1\" is already " + "in the address book.").arg(ui->addressLineEdit->text()), + QMessageBox::Ok, QMessageBox::Ok); + break; + case SideStakeTableModel::INVALID_ALLOCATION: + QMessageBox::warning(this, windowTitle(), + tr("The entered allocation is not valid.").arg(ui->allocationLineEdit->text()), + QMessageBox::Ok, QMessageBox::Ok); + + } + return; + } + + QDialog::accept(); +} + +QString EditSideStakeDialog::getAddress() const +{ + return address; +} + +void EditSideStakeDialog::setAddress(const QString &address) +{ + this->address = address; + ui->addressLineEdit->setText(address); +} diff --git a/src/qt/editsidestakedialog.h b/src/qt/editsidestakedialog.h new file mode 100644 index 0000000000..b9256342b6 --- /dev/null +++ b/src/qt/editsidestakedialog.h @@ -0,0 +1,53 @@ +// Copyright (c) 2014-2023 The Gridcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or https://opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_QT_EDITSIDESTAKEDIALOG_H +#define BITCOIN_QT_EDITSIDESTAKEDIALOG_H + +#include + +QT_BEGIN_NAMESPACE +class QDataWidgetMapper; +QT_END_NAMESPACE + +namespace Ui { +class EditSideStakeDialog; +} +class OptionsModel; + +/** Dialog for editing an address and associated information. + */ +class EditSideStakeDialog : public QDialog +{ + Q_OBJECT + +public: + enum Mode { + NewSideStake, + EditSideStake + }; + + explicit EditSideStakeDialog(Mode mode, QWidget* parent = nullptr); + ~EditSideStakeDialog(); + + void setModel(OptionsModel *model); + void loadRow(int row); + + QString getAddress() const; + void setAddress(const QString &address); + +public slots: + void accept(); + +private: + bool saveCurrentRow(); + + Ui::EditSideStakeDialog *ui; + QDataWidgetMapper *mapper; + Mode mode; + OptionsModel *model; + + QString address; +}; +#endif // BITCOIN_QT_EDITSIDESTAKEDIALOG_H diff --git a/src/qt/forms/editsidestakedialog.ui b/src/qt/forms/editsidestakedialog.ui new file mode 100644 index 0000000000..5e528b2b4c --- /dev/null +++ b/src/qt/forms/editsidestakedialog.ui @@ -0,0 +1,200 @@ + + + EditSideStakeDialog + + + + 0 + 0 + 400 + 300 + + + + Add or Edit SideStake + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + Address + + + + + + + Qt::Horizontal + + + + 10 + 20 + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + Allocation + + + + + + + Qt::Horizontal + + + + 10 + 20 + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + Description + + + + + + + Qt::Horizontal + + + QSizePolicy::Expanding + + + + 10 + 20 + + + + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + EditSideStakeDialog + accept() + + + 248 + 254 + + + 157 + 274 + + + + + buttonBox + rejected() + EditSideStakeDialog + reject() + + + 316 + 260 + + + 286 + 274 + + + + + diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 25a92ba0d7..96757b2e79 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -9,6 +9,7 @@ #include "init.h" #include "miner.h" #include "sidestaketablemodel.h" +#include "editsidestakedialog.h" #include #include @@ -162,6 +163,9 @@ void OptionsDialog::setModel(OptionsModel *model) connect(ui->enableSideStaking, &QCheckBox::toggled, this, &OptionsDialog::hideSideStakeEdit); connect(ui->enableSideStaking, &QCheckBox::toggled, this, &OptionsDialog::refreshSideStakeTableModel); + + connect(ui->pushButtonNewSideStake, &QPushButton::clicked, this, &OptionsDialog::newSideStakeButton_clicked); + connect(ui->pushButtonEditSideStake, &QPushButton::clicked, this, &OptionsDialog::editSideStakeButton_clicked); } /* update the display unit, to not use the default ("BTC") */ @@ -270,12 +274,28 @@ void OptionsDialog::on_applyButton_clicked() void OptionsDialog::newSideStakeButton_clicked() { + if (!model) { + return; + } + + EditSideStakeDialog dialog(EditSideStakeDialog::NewSideStake, this); + + dialog.setModel(model); + dialog.exec(); } void OptionsDialog::editSideStakeButton_clicked() { + if (!model) { + return; + } + + EditSideStakeDialog dialog(EditSideStakeDialog::EditSideStake, this); + + dialog.setModel(model); + dialog.exec(); } void OptionsDialog::showRestartWarning_Proxy()