Skip to content

Commit

Permalink
Implementation of add, edit, and delete sidestake buttons in options
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Oct 8, 2023
1 parent e5299bc commit b5e4de8
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 105 deletions.
2 changes: 1 addition & 1 deletion src/qt/editsidestakedialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void EditSideStakeDialog::setModel(OptionsModel *model)
return;
}

mapper->setModel(model);
mapper->setModel(model->getSideStakeTableModel());
mapper->addMapping(ui->addressLineEdit, SideStakeTableModel::Address);
mapper->addMapping(ui->allocationLineEdit, SideStakeTableModel::Allocation);
mapper->addMapping(ui->descriptionLineEdit, SideStakeTableModel::Description);
Expand Down
150 changes: 50 additions & 100 deletions src/qt/forms/editsidestakedialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -15,141 +15,91 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="1" column="0">
<widget class="QLabel" name="addressLabel">
<property name="text">
<string>Address</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<item row="1" column="1">
<widget class="QLineEdit" name="addressLineEdit"/>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<item row="3" column="0">
<widget class="QLabel" name="allocationLabel">
<property name="text">
<string>Allocation</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<item row="3" column="1">
<widget class="QLineEdit" name="allocationLineEdit"/>
</item>
<item row="5" column="0">
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>Description</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="descriptionLineEdit"/>
</item>
<item row="4" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLineEdit" name="allocationLineEdit"/>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="descriptionLabel">
<property name="text">
<string>Description</string>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<spacer name="horizontalSpacer_3">
<item row="6" column="1">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>10</width>
<height>20</height>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLineEdit" name="descriptionLineEdit"/>
<item row="0" column="1">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
Expand Down
17 changes: 17 additions & 0 deletions src/qt/forms/optionsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@
</item>
<item>
<widget class="QPushButton" name="pushButtonEditSideStake">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Edit</string>
</property>
Expand All @@ -351,6 +354,20 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButtonDeleteSideStake">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Delete</string>
</property>
<property name="icon">
<iconset resource="../bitcoin.qrc">
<normaloff>:/icons/remove</normaloff>:/icons/remove</iconset>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_SideStake">
<property name="orientation">
Expand Down
80 changes: 76 additions & 4 deletions src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "sidestaketablemodel.h"
#include "editsidestakedialog.h"

#include <QSortFilterProxyModel>
#include <QDir>
#include <QIntValidator>
#include <QLocale>
Expand Down Expand Up @@ -147,7 +148,19 @@ void OptionsDialog::setModel(OptionsModel *model)

sidestake_model->refresh();

ui->sidestakingTableView->setModel(sidestake_model);
m_sidestake_proxy_model = new QSortFilterProxyModel(this);
m_sidestake_proxy_model->setSourceModel(sidestake_model);
m_sidestake_proxy_model->setDynamicSortFilter(true);
m_sidestake_proxy_model->setSortCaseSensitivity(Qt::CaseInsensitive);
m_sidestake_proxy_model->setFilterCaseSensitivity(Qt::CaseInsensitive);

m_sidestake_filter_proxy_model = new QSortFilterProxyModel(this);
m_sidestake_filter_proxy_model->setSourceModel(m_sidestake_proxy_model);
m_sidestake_filter_proxy_model->setDynamicSortFilter(true);
m_sidestake_filter_proxy_model->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_sidestake_filter_proxy_model->setFilterKeyColumn(-1); // All columns

ui->sidestakingTableView->setModel(m_sidestake_filter_proxy_model);
ui->sidestakingTableView->verticalHeader()->hide();
ui->sidestakingTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->sidestakingTableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
Expand All @@ -161,11 +174,17 @@ void OptionsDialog::setModel(OptionsModel *model)
ui->sidestakingTableView->horizontalHeader()->setStretchLastSection(true);
ui->sidestakingTableView->setShowGrid(true);

ui->sidestakingTableView->sortByColumn(0, Qt::AscendingOrder);

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);
connect(ui->pushButtonDeleteSideStake, &QPushButton::clicked, this, &OptionsDialog::deleteSideStakeButton_clicked);

connect(ui->sidestakingTableView->selectionModel(), &QItemSelectionModel::selectionChanged,
this, &OptionsDialog::sidestakeSelectionChanged);
}

/* update the display unit, to not use the default ("BTC") */
Expand Down Expand Up @@ -287,22 +306,54 @@ void OptionsDialog::newSideStakeButton_clicked()

void OptionsDialog::editSideStakeButton_clicked()
{
if (!model) {
if (!model || !ui->sidestakingTableView->selectionModel()) {
return;
}

QModelIndexList indexes = ui->sidestakingTableView->selectionModel()->selectedRows();

if (indexes.isEmpty()) {
return;
}

EditSideStakeDialog dialog(EditSideStakeDialog::EditSideStake, this);

dialog.setModel(model);
GRC::SideStake* sidestake = static_cast<GRC::SideStake*>(indexes.at(0).internalPointer());

QModelIndex orig_index = m_sidestake_filter_proxy_model->mapToSource(indexes.at(0));
orig_index = m_sidestake_proxy_model->mapToSource(orig_index);

dialog.setModel(model);
dialog.loadRow(orig_index.row());
//dialog.loadRow(indexes.at(0).row());
dialog.exec();
}

void OptionsDialog::deleteSideStakeButton_clicked()
{
if (!model || !ui->sidestakingTableView->selectionModel()) {
return;
}

QModelIndexList indexes = ui->sidestakingTableView->selectionModel()->selectedRows();

if (indexes.isEmpty()) {
return;
}

if (indexes.size() > 1) {
QMessageBox::warning(this, tr("Error"), tr("You can only delete one sidestake at a time."), QMessageBox::Ok);
}

model->getSideStakeTableModel()->removeRows(indexes.at(0).row(), 1);
}

void OptionsDialog::showRestartWarning_Proxy()
{
if(!fRestartWarningDisplayed_Proxy)
{
QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect after restarting Gridcoin."), QMessageBox::Ok);
QMessageBox::warning(this, tr("Warning"), tr("This setting will take effect"
" after restarting Gridcoin."), QMessageBox::Ok);
fRestartWarningDisplayed_Proxy = true;
}
}
Expand Down Expand Up @@ -499,3 +550,24 @@ bool OptionsDialog::eventFilter(QObject *object, QEvent *event)
}
return QDialog::eventFilter(object, event);
}

void OptionsDialog::sidestakeSelectionChanged()
{
QTableView *table = ui->sidestakingTableView;

if (table->selectionModel()->hasSelection()) {
QModelIndexList indexes = ui->sidestakingTableView->selectionModel()->selectedRows();

if (indexes.size() > 1) {
ui->pushButtonEditSideStake->setEnabled(false);
ui->pushButtonDeleteSideStake->setEnabled(false);
} else if (static_cast<GRC::SideStake*>(indexes.at(0).internalPointer())->m_status
== GRC::SideStakeStatus::MANDATORY) {
ui->pushButtonEditSideStake->setEnabled(false);
ui->pushButtonDeleteSideStake->setEnabled(false);
} else {
ui->pushButtonEditSideStake->setEnabled(true);
ui->pushButtonDeleteSideStake->setEnabled(true);
}
}
}
7 changes: 7 additions & 0 deletions src/qt/optionsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Ui {
class OptionsDialog;
}
class OptionsModel;
class QSortFilterProxyModel;
class MonitoredDataMapper;
class QValidatedLineEdit;

Expand Down Expand Up @@ -42,6 +43,7 @@ private slots:

void newSideStakeButton_clicked();
void editSideStakeButton_clicked();
void deleteSideStakeButton_clicked();

void showRestartWarning_Proxy();
void showRestartWarning_Lang();
Expand Down Expand Up @@ -72,6 +74,9 @@ private slots:
bool fStakingEfficiencyValid;
bool fMinStakeSplitValueValid;

QSortFilterProxyModel *m_sidestake_proxy_model;
QSortFilterProxyModel *m_sidestake_filter_proxy_model;

enum SideStakeTableColumnWidths
{
ADDRESS_COLUMN_WIDTH = 200,
Expand All @@ -81,6 +86,8 @@ private slots:
STATUS_COLUMN_WIDTH = 150
};

private slots:
void sidestakeSelectionChanged();
};

#endif // BITCOIN_QT_OPTIONSDIALOG_H
Loading

0 comments on commit b5e4de8

Please sign in to comment.