RestoreHeightDialog: rework

This commit is contained in:
tobtoht 2021-05-25 02:26:28 +02:00
parent ba2b0ac550
commit 08d5fd26ff
No known key found for this signature in database
GPG key ID: 1CADD27F41F45C3C
12 changed files with 203 additions and 346 deletions

View file

@ -0,0 +1,40 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include "RestoreHeightDialog.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QDialogButtonBox>
RestoreHeightDialog::RestoreHeightDialog(QWidget *parent, quint64 currentRestoreHeight)
: QDialog(parent)
, m_restoreHeightWidget(new RestoreHeightWidget(this))
{
auto *layout = new QVBoxLayout(this);
auto *label = new QLabel(this);
label->setText("Enter a wallet creation date or restore height.");
auto *buttonBox = new QDialogButtonBox(this);
buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
layout->addWidget(label);
layout->addWidget(m_restoreHeightWidget);
layout->addWidget(buttonBox);
this->setLayout(layout);
if (currentRestoreHeight) {
m_restoreHeightWidget->setHeight(currentRestoreHeight);
}
connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
this->adjustSize();
}
int RestoreHeightDialog::getHeight() {
return m_restoreHeightWidget->getHeight();
}

View file

@ -0,0 +1,23 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef FEATHER_RESTOREHEIGHTDIALOG_H
#define FEATHER_RESTOREHEIGHTDIALOG_H
#include <QDialog>
#include "widgets/RestoreHeightWidget.h"
class RestoreHeightDialog : public QDialog
{
Q_OBJECT
public:
explicit RestoreHeightDialog(QWidget *parent = nullptr, quint64 currentRestoreHeight = 0);
int getHeight();
private:
RestoreHeightWidget *m_restoreHeightWidget;
};
#endif //FEATHER_RESTOREHEIGHTDIALOG_H

View file

@ -1,38 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include "restoredialog.h"
#include "ui_restoredialog.h"
#include "constants.h"
RestoreDialog::RestoreDialog(QSharedPointer<AppContext> ctx, QWidget *parent)
: QDialog(parent)
, ui(new Ui::RestoreDialog)
, m_ctx(std::move(ctx))
{
ui->setupUi(this);
this->setWindowIcon(QIcon("://assets/images/appicons/64x64.png"));
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &RestoreDialog::accepted);
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &RestoreDialog::rejected);
if(constants::networkType == NetworkType::Type::TESTNET) {
ui->restoreHeightWidget->hideSlider();
} else {
// load restoreHeight lookup db
ui->restoreHeightWidget->initRestoreHeights(appData()->restoreHeights[constants::networkType]);
}
}
int RestoreDialog::getHeight() {
return ui->restoreHeightWidget->getHeight();
}
void RestoreDialog::initRestoreHeights(RestoreHeightLookup *lookup) {
ui->restoreHeightWidget->initRestoreHeights(lookup);
}
RestoreDialog::~RestoreDialog() {
delete ui;
}

View file

@ -1,39 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef RESTOREDIALOG_H
#define RESTOREDIALOG_H
#include <QPushButton>
#include <QDialogButtonBox>
#include <QDialog>
#include <QStandardItemModel>
#include <QAbstractButton>
#include "utils/RestoreHeightLookup.h"
#include "appcontext.h"
namespace Ui {
class RestoreDialog;
}
class RestoreDialog : public QDialog
{
Q_OBJECT
public:
explicit RestoreDialog(QSharedPointer<AppContext> ctx, QWidget *parent = nullptr);
void initRestoreHeights(RestoreHeightLookup *lookup);
int getHeight();
~RestoreDialog() override;
signals:
void accepted();
void rejected();
private:
Ui::RestoreDialog *ui;
QSharedPointer<AppContext> m_ctx;
};
#endif // RESTOREDIALOG_H

View file

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RestoreDialog</class>
<widget class="QDialog" name="RestoreDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>584</width>
<height>99</height>
</rect>
</property>
<property name="windowTitle">
<string>specify restore height</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="RestoreHeightWidget" name="restoreHeightWidget" native="true"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>RestoreHeightWidget</class>
<extends>QWidget</extends>
<header>widgets/restoreheightwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View file

@ -735,26 +735,17 @@ void MainWindow::updatePasswordIcon() {
void MainWindow::showRestoreHeightDialog() {
// settings custom restore height is only available for 25 word seeds
auto seed = m_ctx->wallet->getCacheAttribute("feather.seed");
if(!seed.isEmpty()) {
if(!seed.isEmpty()) { // TODO: update this warning (import tx, delete cache, restore from seed)
const auto msg = "This wallet has a 14 word mnemonic seed which has the restore height embedded.";
QMessageBox::warning(this, "Cannot set custom restore height", msg);
return;
}
m_restoreDialog = new RestoreDialog(m_ctx, this);
m_restoreDialog->show();
connect(m_restoreDialog, &RestoreDialog::accepted, [=]{
auto height = m_restoreDialog->getHeight();
m_restoreDialog->disconnect();
m_restoreDialog->deleteLater();
m_ctx->onSetRestoreHeight(height);
});
connect(m_restoreDialog, &RestoreDialog::rejected, [=]{
m_restoreDialog->disconnect();
m_restoreDialog->deleteLater();
});
RestoreHeightDialog dialog{this, m_ctx->wallet->getWalletCreationHeight()};
if (dialog.exec() == QDialog::Accepted) {
int restoreHeight = dialog.getHeight();
m_ctx->onSetRestoreHeight(restoreHeight);
}
}
void MainWindow::showKeysDialog() {

View file

@ -21,7 +21,7 @@
#include "dialog/passwordchangedialog.h"
#include "dialog/keysdialog.h"
#include "dialog/aboutdialog.h"
#include "dialog/restoredialog.h"
#include "dialog/RestoreHeightDialog.h"
#include "dialog/splashdialog.h"
#include "libwalletqt/Wallet.h"
#include "model/SubaddressModel.h"
@ -212,7 +212,6 @@ private:
Settings *m_windowSettings = nullptr;
CalcWindow *m_windowCalc = nullptr;
RestoreDialog *m_restoreDialog = nullptr;
SplashDialog *m_splashDialog = nullptr;
XMRigWidget *m_xmrig = nullptr;

View file

@ -0,0 +1,61 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include "RestoreHeightWidget.h"
#include "ui_RestoreHeightWidget.h"
#include <QValidator>
#include "AppData.h"
#include "constants.h"
RestoreHeightWidget::RestoreHeightWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::RestoreHeightWidget)
{
ui->setupUi(this);
QRegExp yearRe(R"(\d{2,4}-\d{1,2}-\d{1,2})");
QValidator *yearValidator = new QRegExpValidator(yearRe, this);
ui->line_creationDate->setValidator(yearValidator);
QRegExp heightRe(R"(\d{7})");
QValidator *heightValidator = new QRegExpValidator(heightRe, this);
ui->line_restoreHeight->setValidator(heightValidator);
connect(ui->line_creationDate, &QLineEdit::textEdited, this, &RestoreHeightWidget::onCreationDateChanged);
connect(ui->line_restoreHeight, &QLineEdit::textEdited, this, &RestoreHeightWidget::onRestoreHeightChanged);
}
void RestoreHeightWidget::setHeight(quint64 restoreHeight) {
ui->line_restoreHeight->setText(QString::number(restoreHeight));
this->onRestoreHeightChanged();
}
int RestoreHeightWidget::getHeight() {
return ui->line_restoreHeight->text().toInt();
}
void RestoreHeightWidget::onCreationDateChanged() {
auto curDate = QDateTime::currentDateTime().addDays(-7);
auto date = QDateTime::fromString(ui->line_creationDate->text(), "yyyy-MM-dd");
if (!date.isValid()) {
return;
}
QDateTime restoreDate = date > curDate ? curDate : date;
qint64 timestamp = restoreDate.toSecsSinceEpoch();
QString restoreHeight = QString::number(appData()->restoreHeights[constants::networkType]->dateToHeight(timestamp));
ui->line_restoreHeight->setText(restoreHeight);
}
void RestoreHeightWidget::onRestoreHeightChanged() {
int restoreHeight = ui->line_restoreHeight->text().toInt();
QDateTime date = appData()->restoreHeights[constants::networkType]->heightToDate(restoreHeight);
ui->line_creationDate->setText(date.toString("yyyy-MM-dd"));
}
RestoreHeightWidget::~RestoreHeightWidget() {
delete ui;
}

View file

@ -1,13 +1,10 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#ifndef RESTOREHEIGHTWIDGET_H
#define RESTOREHEIGHTWIDGET_H
#ifndef FEATHER_RESTOREHEIGHTWIDGET_H
#define FEATHER_RESTOREHEIGHTWIDGET_H
#include <QWidget>
#include <QItemSelection>
#include "appcontext.h"
namespace Ui {
class RestoreHeightWidget;
@ -19,17 +16,18 @@ class RestoreHeightWidget : public QWidget
public:
explicit RestoreHeightWidget(QWidget *parent = nullptr);
void initRestoreHeights(RestoreHeightLookup *lookup);
void setHeight(int height);
int getHeight();
void hideSlider();
~RestoreHeightWidget() override;
private:
void updateTimestamp(int date);
void setHeight(quint64 restoreHeight);
int getHeight();
RestoreHeightLookup *m_restoreHeightLookup = nullptr;
private slots:
void onCreationDateChanged();
void onRestoreHeightChanged();
private:
Ui::RestoreHeightWidget *ui;
};
#endif // RESTOREHEIGHTWIDGET_H
#endif //FEATHER_RESTOREHEIGHTWIDGET_H

View file

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RestoreHeightWidget</class>
<widget class="QWidget" name="RestoreHeightWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>622</width>
<height>88</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Wallet creation date:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="line_creationDate"/>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_2">
<property name="text">
<string>(YYYY-MM-DD)</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Restore height:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="line_restoreHeight"/>
</item>
<item row="1" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View file

@ -1,71 +0,0 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2020-2021, The Monero Project.
#include <QTableWidget>
#include <QMessageBox>
#include <QDesktopServices>
#include "restoreheightwidget.h"
#include "ui_restoreheightwidget.h"
RestoreHeightWidget::RestoreHeightWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::RestoreHeightWidget)
{
ui->setupUi(this);
ui->lineEdit_restoreHeight->setValidator(new QIntValidator(0, 2147483647, this));
connect(ui->lineEdit_restoreHeight, &QLineEdit::textEdited, [this](const QString &val){
if (val.isEmpty()) return;
this->setHeight(val.toInt());
});
}
void RestoreHeightWidget::hideSlider(){
ui->restoreGrid->hide();
}
void RestoreHeightWidget::setHeight(int height) {
if (height < 0)
height = 0;
// Update lineEdit
ui->lineEdit_restoreHeight->setText(QString::number(height));
// Update slider
int date = m_restoreHeightLookup->heightToTimestamp(height);
ui->restoreSlider->setValue(date);
this->updateTimestamp(date);
}
void RestoreHeightWidget::initRestoreHeights(RestoreHeightLookup *lookup) {
// init slider
m_restoreHeightLookup = lookup;
int now = std::time(nullptr);
QList<int> blockDates = m_restoreHeightLookup->data.keys();
ui->restoreSlider->setMinimum(blockDates[0]);
ui->restoreSlider->setMaximum(now);
connect(ui->restoreSlider, &QSlider::sliderMoved, [this](int date){
// Update lineEdit
int blockHeight = m_restoreHeightLookup->dateToHeight(date);
ui->lineEdit_restoreHeight->setText(QString::number(blockHeight));
this->updateTimestamp(date);
});
}
void RestoreHeightWidget::updateTimestamp(int date) {
QDateTime timestamp;
timestamp.setTime_t(date);
ui->label_restoreHeightDate->setText(timestamp.toString("yyyy-MM-dd"));
}
int RestoreHeightWidget::getHeight() {
return ui->lineEdit_restoreHeight->text().toInt();
}
RestoreHeightWidget::~RestoreHeightWidget() {
delete ui;
}

View file

@ -1,112 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>RestoreHeightWidget</class>
<widget class="QWidget" name="RestoreHeightWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>360</width>
<height>89</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QFrame" name="restoreGrid">
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Plain</enum>
</property>
<property name="lineWidth">
<number>0</number>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="1">
<widget class="QLabel" name="label_restoreHeightDate">
<property name="text">
<string>2014-04-18</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_restoreHeight">
<property name="text">
<string>Restore height</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QSlider" name="restoreSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QGridLayout" name="restoreHeightLayout">
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineEdit_restoreHeight">
<property name="text">
<string>1</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>