From f005e4643bc2459adb37f19d5e012756a355c08a Mon Sep 17 00:00:00 2001 From: tobtoht Date: Fri, 2 Jul 2021 22:47:10 +0200 Subject: [PATCH] Wizard: close wizard on wallet open via browse --- src/wizard/PageOpenWallet.cpp | 69 +++++++++++++++++------------------ src/wizard/PageOpenWallet.h | 4 ++ 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/src/wizard/PageOpenWallet.cpp b/src/wizard/PageOpenWallet.cpp index 4c0e188..ed9fa2a 100644 --- a/src/wizard/PageOpenWallet.cpp +++ b/src/wizard/PageOpenWallet.cpp @@ -17,34 +17,15 @@ PageOpenWallet::PageOpenWallet(WalletKeysFilesModel *wallets, QWidget *parent) { ui->setupUi(this); - connect(ui->btnBrowse, &QPushButton::clicked, [this]{ - QString walletDir = config()->get(Config::walletDirectory).toString(); - QString path = QFileDialog::getOpenFileName(this, "Select your wallet file", walletDir, "Wallet file (*.keys)"); - if (path.isEmpty()) - return; - - QFileInfo infoPath(path); - if(!infoPath.isReadable()) { - QMessageBox::warning(this, "Cannot read wallet file", "Permission error."); - return; - } - - if (ui->openOnStartup->isChecked()) - config()->set(Config::autoOpenWalletPath, QString("%1%2").arg(constants::networkType).arg(path)); - - emit openWallet(path); - }); - this->setTitle("Open wallet file"); this->setButtonText(QWizard::FinishButton, "Open wallet"); - ui->walletTable->setSelectionBehavior(QAbstractItemView::SelectRows); - ui->walletTable->setContextMenuPolicy(Qt::CustomContextMenu); - m_keysProxy = new WalletKeysFilesProxyModel(this, constants::networkType); m_keysProxy->setSourceModel(m_walletKeysFilesModel); m_keysProxy->setSortRole(Qt::UserRole); + ui->walletTable->setSelectionBehavior(QAbstractItemView::SelectRows); + ui->walletTable->setContextMenuPolicy(Qt::CustomContextMenu); ui->walletTable->setModel(m_keysProxy); ui->walletTable->hideColumn(WalletKeysFilesModel::NetworkType); ui->walletTable->hideColumn(WalletKeysFilesModel::Path); @@ -52,18 +33,21 @@ PageOpenWallet::PageOpenWallet(WalletKeysFilesModel *wallets, QWidget *parent) ui->walletTable->header()->setSectionResizeMode(WalletKeysFilesModel::FileName, QHeaderView::Stretch); ui->walletTable->setSortingEnabled(true); ui->walletTable->sortByColumn(WalletKeysFilesModel::Modified, Qt::DescendingOrder); - ui->walletTable->show(); connect(ui->walletTable->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](QModelIndex current, QModelIndex prev){ this->updatePath(); }); - connect(ui->walletTable, &QTreeView::doubleClicked, [this]{ - // Simulate next button click - QWizard *wizard = this->wizard(); - if (wizard) { - wizard->button(QWizard::FinishButton)->click(); - } + connect(ui->walletTable, &QTreeView::doubleClicked, this, &PageOpenWallet::nextPage); + + connect(ui->btnBrowse, &QPushButton::clicked, [this]{ + QString walletDir = config()->get(Config::walletDirectory).toString(); + m_walletFile = QFileDialog::getOpenFileName(this, "Select your wallet file", walletDir, "Wallet file (*.keys)"); + if (m_walletFile.isEmpty()) + return; + this->nextPage(); }); + + this->updatePath(); } void PageOpenWallet::initializePage() { @@ -77,25 +61,38 @@ void PageOpenWallet::updatePath() { return; } - QString path = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::Path), Qt::DisplayRole).toString(); - ui->linePath->setText(path); + m_walletFile = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::Path), Qt::DisplayRole).toString(); + ui->linePath->setText(m_walletFile); } int PageOpenWallet::nextId() const { return -1; } +void PageOpenWallet::nextPage() { + // Simulate next button click + QWizard *wizard = this->wizard(); + if (wizard) { + wizard->button(QWizard::FinishButton)->click(); + } +} + bool PageOpenWallet::validatePage() { - QModelIndex index = ui->walletTable->currentIndex(); - if(!index.isValid()) { - QMessageBox::warning(this, "Wallet not selected", "Please select a wallet from the list."); + if (m_walletFile.isEmpty()) { + QMessageBox::warning(this, "No wallet file selected", "Please select a wallet from the list."); return false; } - QString walletPath = index.model()->data(index.siblingAtColumn(WalletKeysFilesModel::Column::Path), Qt::UserRole).toString(); - auto autoWallet = ui->openOnStartup->isChecked() ? QString("%1%2").arg(constants::networkType).arg(walletPath) : ""; + QFileInfo infoPath(m_walletFile); + if (!infoPath.isReadable()) { + QMessageBox::warning(this, "Permission error", "Cannot read wallet file."); + return false; + } + + // Clear autoOpen if openOnStartup is not checked + auto autoWallet = ui->openOnStartup->isChecked() ? QString("%1%2").arg(constants::networkType).arg(m_walletFile) : ""; config()->set(Config::autoOpenWalletPath, autoWallet); - emit openWallet(walletPath); + emit openWallet(m_walletFile); return true; } diff --git a/src/wizard/PageOpenWallet.h b/src/wizard/PageOpenWallet.h index 1385203..b41e0e3 100644 --- a/src/wizard/PageOpenWallet.h +++ b/src/wizard/PageOpenWallet.h @@ -28,6 +28,9 @@ public: signals: void openWallet(QString path); +private slots: + void nextPage(); + private: void updatePath(); @@ -35,6 +38,7 @@ private: WalletKeysFilesModel *m_walletKeysFilesModel; WalletKeysFilesProxyModel *m_keysProxy; QStandardItemModel *m_model; + QString m_walletFile; }; #endif //FEATHER_OPENWALLET_H