diff --git a/src/addressbook.cpp b/src/addressbook.cpp index 1ff71db..2504317 100644 --- a/src/addressbook.cpp +++ b/src/addressbook.cpp @@ -251,10 +251,11 @@ void AddressBook::addAddressLabel(QString label, QString address) { void AddressBook::removeAddressLabel(QString label, QString address) { // Iterate over the list and remove the label/address for (int i=0; i < allLabels.size(); i++) { - if (allLabels[i].first == label && allLabels[i].second == address) + if (allLabels[i].first == label && allLabels[i].second == address) { allLabels.removeAt(i); writeToStorage(); return; + } } } @@ -273,4 +274,16 @@ QString AddressBook::getLabelForAddress(QString addr) { return ""; } -AddressBook* AddressBook::instance = nullptr; \ No newline at end of file +QString AddressBook::addLabelToAddress(QString addr) { + QString label = AddressBook::getInstance()->getLabelForAddress(addr); + if (!label.isEmpty()) + return label + "/" + addr; + else + return addr; +} + +QString AddressBook::addressFromAddressLabel(const QString& lblAddr) { + return lblAddr.trimmed().split("/").last(); +} + +AddressBook* AddressBook::instance = nullptr; diff --git a/src/addressbook.h b/src/addressbook.h index b4b38e0..e8b8f10 100644 --- a/src/addressbook.h +++ b/src/addressbook.h @@ -35,6 +35,8 @@ public: static void open(MainWindow* parent, QLineEdit* target = nullptr); static AddressBook* getInstance(); + static QString addLabelToAddress(QString addr); + static QString addressFromAddressLabel(const QString& lblAddr); // Add a new address/label to the database void addAddressLabel(QString label, QString address); diff --git a/src/addresscombo.cpp b/src/addresscombo.cpp new file mode 100644 index 0000000..3a14e7f --- /dev/null +++ b/src/addresscombo.cpp @@ -0,0 +1,41 @@ +#include "addresscombo.h" + +#include "addressbook.h" +#include "settings.h" + +AddressCombo::AddressCombo(QWidget* parent) : + QComboBox(parent) { + +} + +QString AddressCombo::itemText(int i) { + QString txt = QComboBox::itemText(i); + return AddressBook::addressFromAddressLabel(txt.split("(")[0].trimmed()); +} + +QString AddressCombo::currentText() { + QString txt = QComboBox::currentText(); + return AddressBook::addressFromAddressLabel(txt.split("(")[0].trimmed()); +} + +void AddressCombo::setCurrentText(const QString& text) { + for (int i=0; i < count(); i++) { + if (itemText(i) == text) { + QComboBox::setCurrentIndex(i); + } + } +} + +void AddressCombo::addItem(const QString& text, double bal) { + QString txt = AddressBook::addLabelToAddress(text); + if (bal > 0) + txt = txt % "(" % Settings::getZECDisplayFormat(bal) % ")"; + + QComboBox::addItem(txt); +} + +void AddressCombo::insertItem(int index, const QString& text, double bal) { + QString txt = AddressBook::addLabelToAddress(text) % + "(" % Settings::getZECDisplayFormat(bal) % ")"; + QComboBox::insertItem(index, txt); +} \ No newline at end of file diff --git a/src/addresscombo.h b/src/addresscombo.h new file mode 100644 index 0000000..8088906 --- /dev/null +++ b/src/addresscombo.h @@ -0,0 +1,24 @@ +#ifndef ADDRESSCOMBO_H +#define ADDRESSCOMBO_H + +#include "precompiled.h" + +class AddressCombo : public QComboBox +{ + Q_OBJECT; +public: + explicit AddressCombo(QWidget* parent = nullptr); + + QString itemText(int i); + QString currentText(); + + void addItem(const QString& itemText, double bal); + void insertItem(int index, const QString& text, double bal = 0.0); + +public slots: + void setCurrentText(const QString& itemText); + +private: +}; + +#endif // ADDRESSCOMBO_H diff --git a/src/balancestablemodel.cpp b/src/balancestablemodel.cpp index a9ca0aa..e83a1ef 100644 --- a/src/balancestablemodel.cpp +++ b/src/balancestablemodel.cpp @@ -1,4 +1,5 @@ #include "balancestablemodel.h" +#include "addressbook.h" #include "settings.h" @@ -84,17 +85,15 @@ QVariant BalancesTableModel::data(const QModelIndex &index, int role) const if (role == Qt::DisplayRole) { switch (index.column()) { - case 0: return std::get<0>(modeldata->at(index.row())); + case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row()))); case 1: return Settings::getZECDisplayFormat(std::get<1>(modeldata->at(index.row()))); } } if(role == Qt::ToolTipRole) { switch (index.column()) { - case 0: return std::get<0>(modeldata->at(index.row())); - case 1: { - return Settings::getUSDFormat(std::get<1>(modeldata->at(index.row()))); - } + case 0: return AddressBook::addLabelToAddress(std::get<0>(modeldata->at(index.row()))); + case 1: return Settings::getUSDFormat(std::get<1>(modeldata->at(index.row()))); } } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index b3c5a5f..500e903 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -219,13 +219,13 @@ void MainWindow::turnstileDoMigration(QString fromAddr) { return bal; }; - //turnstile.migrateZaddList->addItem("All Sprout z-Addrs"); turnstile.fromBalance->setText(Settings::getZECUSDDisplayFormat(fnGetAllSproutBalance())); for (auto addr : *rpc->getAllZAddresses()) { + auto bal = rpc->getAllBalances()->value(addr); if (Settings::getInstance()->isSaplingAddress(addr)) { - turnstile.migrateTo->addItem(addr); + turnstile.migrateTo->addItem(addr, bal); } else { - turnstile.migrateZaddList->addItem(addr); + turnstile.migrateZaddList->addItem(addr, bal); } } @@ -458,7 +458,7 @@ void MainWindow::addressBook() { void MainWindow::donate() { // Set up a donation to me :) ui->Address1->setText(Settings::getDonationAddr( - Settings::getInstance()->isSaplingAddress(ui->inputsCombo->currentText()))); + Settings::getInstance()->isSaplingAddress(ui->inputsCombo->currentText()))); ui->Address1->setCursorPosition(0); ui->Amount1->setText("0.01"); ui->MemoTxt1->setText("Thanks for supporting zec-qt-wallet!"); @@ -735,7 +735,8 @@ void MainWindow::setupBalancesTab() { auto fnDoSendFrom = [=](const QString& addr, const QString& to = QString(), bool sendMax = false) { // Find the inputs combo for (int i = 0; i < ui->inputsCombo->count(); i++) { - if (ui->inputsCombo->itemText(i).startsWith(addr)) { + auto inputComboAddress = ui->inputsCombo->itemText(i); + if (inputComboAddress.startsWith(addr)) { ui->inputsCombo->setCurrentIndex(i); break; } @@ -772,7 +773,8 @@ void MainWindow::setupBalancesTab() { if (index.row() < 0) return; index = index.sibling(index.row(), 0); - auto addr = ui->balancesTable->model()->data(index).toString(); + auto addr = AddressBook::addressFromAddressLabel( + ui->balancesTable->model()->data(index).toString()); QMenu menu(this); @@ -910,7 +912,7 @@ void MainWindow::addNewZaddr(bool sapling) { // Just double make sure the z-address is still checked if (( sapling && ui->rdioZSAddr->isChecked()) || (!sapling && ui->rdioZAddr->isChecked())) { - ui->listRecieveAddresses->insertItem(0, addr); + ui->listRecieveAddresses->insertItem(0, addr); ui->listRecieveAddresses->setCurrentIndex(0); ui->statusBar->showMessage(QString::fromStdString("Created new zAddr") % @@ -931,8 +933,10 @@ std::function MainWindow::addZAddrsToComboList(bool sapling) { std::for_each(addrs->begin(), addrs->end(), [=] (auto addr) { if ( (sapling && Settings::getInstance()->isSaplingAddress(addr)) || - (!sapling && !Settings::getInstance()->isSaplingAddress(addr))) - ui->listRecieveAddresses->addItem(addr); + (!sapling && !Settings::getInstance()->isSaplingAddress(addr))) { + auto bal = rpc->getAllBalances()->value(addr); + ui->listRecieveAddresses->addItem(addr, bal); + } }); // If z-addrs are empty, then create a new one. @@ -969,7 +973,8 @@ void MainWindow::setupRecieveTab() { std::for_each(utxos->begin(), utxos->end(), [=] (auto& utxo) { auto addr = utxo.address; if (addr.startsWith("t") && ui->listRecieveAddresses->findText(addr) < 0) { - ui->listRecieveAddresses->addItem(addr); + auto bal = rpc->getAllBalances()->value(addr); + ui->listRecieveAddresses->addItem(addr, bal); } }); @@ -1026,7 +1031,8 @@ void MainWindow::setupRecieveTab() { // Select item in address list QObject::connect(ui->listRecieveAddresses, - QOverload::of(&QComboBox::currentIndexChanged), [=] (const QString& addr) { + QOverload::of(&QComboBox::currentIndexChanged), [=] (int index) { + QString addr = ui->listRecieveAddresses->itemText(index); if (addr.isEmpty()) { // Draw empty stuff diff --git a/src/mainwindow.h b/src/mainwindow.h index 7b3a7bc..4d742b7 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -71,7 +71,7 @@ private: void cancelButton(); void sendButton(); - void inputComboTextChanged(const QString& text); + void inputComboTextChanged(int index); void addAddressSection(); void maxAmountChecked(int checked); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 4fbcf14..b9aa53d 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -228,7 +228,7 @@ - + @@ -316,8 +316,8 @@ 0 0 - 928 - 380 + 920 + 334 @@ -628,7 +628,7 @@ - + 0 @@ -849,7 +849,7 @@ 0 0 968 - 19 + 22 @@ -970,6 +970,11 @@ QLabel
fillediconlabel.h
+ + AddressCombo + QComboBox +
addresscombo.h
+
tabWidget diff --git a/src/precompiled.h b/src/precompiled.h index 7719f1a..db0c100 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include diff --git a/src/qrcodelabel.h b/src/qrcodelabel.h index 683dc6a..9bf3abd 100644 --- a/src/qrcodelabel.h +++ b/src/qrcodelabel.h @@ -7,7 +7,7 @@ class QRCodeLabel : public QLabel { Q_OBJECT public: - explicit QRCodeLabel(QWidget *parent = 0); + explicit QRCodeLabel(QWidget *parent = nullptr); virtual QSize sizeHint() const; void setAddress(QString address); diff --git a/src/rpc.cpp b/src/rpc.cpp index 0ef8b17..62bbba7 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -1,5 +1,6 @@ #include "rpc.h" +#include "addressbook.h" #include "settings.h" #include "senttxstore.h" #include "turnstile.h" @@ -635,14 +636,13 @@ void RPC::updateUI(bool anyUnconfirmed) { balancesTableModel->setNewData(allBalances, utxos); // Add all the addresses into the inputs combo box - auto lastFromAddr = ui->inputsCombo->currentText().split("(")[0].trimmed(); + auto lastFromAddr = ui->inputsCombo->currentText(); ui->inputsCombo->clear(); auto i = allBalances->constBegin(); while (i != allBalances->constEnd()) { - QString item = i.key() % "(" % Settings::getZECDisplayFormat(i.value()) % ")"; - ui->inputsCombo->addItem(item); - if (item.startsWith(lastFromAddr)) ui->inputsCombo->setCurrentText(item); + ui->inputsCombo->addItem(i.key(), i.value()); + if (i.key() == lastFromAddr) ui->inputsCombo->setCurrentText(i.key()); ++i; } diff --git a/src/sendtab.cpp b/src/sendtab.cpp index b543ac2..9ffab4f 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -26,7 +26,7 @@ void MainWindow::setupSendTab() { QObject::connect(ui->cancelSendButton, &QPushButton::clicked, this, &MainWindow::cancelButton); // Input Combobox current text changed - QObject::connect(ui->inputsCombo, QOverload::of(&QComboBox::currentIndexChanged), + QObject::connect(ui->inputsCombo, QOverload::of(&QComboBox::currentIndexChanged), this, &MainWindow::inputComboTextChanged); // Hook up add address button click @@ -52,7 +52,7 @@ void MainWindow::setupSendTab() { // This is the damnest thing ever. If we do AddressBook::readFromStorage() directly, the whole file // doesn't get read. It needs to run in a timer after everything has finished to be able to read // the file properly. - QTimer::singleShot(100, [=]() { updateLabelsAutoComplete(); }); + QTimer::singleShot(2000, [=]() { updateLabelsAutoComplete(); }); // The first address book button QObject::connect(ui->AddressBook1, &QPushButton::clicked, [=] () { @@ -117,7 +117,7 @@ void MainWindow::setDefaultPayFrom() { for (int i=0; i < ui->inputsCombo->count(); i++) { auto addr = ui->inputsCombo->itemText(i); if (addr.startsWith(startsWith)) { - auto amt = rpc->getAllBalances()->value(addr.split("(")[0]); + auto amt = rpc->getAllBalances()->value(addr); if (max_amt < amt) { max_amt = amt; idx = i; @@ -139,8 +139,9 @@ void MainWindow::setDefaultPayFrom() { } }; -void MainWindow::inputComboTextChanged(const QString& text) { - auto bal = rpc->getAllBalances()->value(text.split("(")[0].trimmed()); +void MainWindow::inputComboTextChanged(int index) { + auto addr = ui->inputsCombo->itemText(index); + auto bal = rpc->getAllBalances()->value(addr); auto balFmt = Settings::getZECDisplayFormat(bal); ui->sendAddressBalance->setText(balFmt); @@ -244,7 +245,7 @@ void MainWindow::addAddressSection() { } void MainWindow::addressChanged(int itemNumber, const QString& text) { - auto addr = Settings::addressFromAddressLabel(text); + auto addr = AddressBook::addressFromAddressLabel(text); setMemoEnabled(itemNumber, addr.startsWith("z")); } @@ -267,7 +268,7 @@ void MainWindow::setMemoEnabled(int number, bool enabled) { void MainWindow::memoButtonClicked(int number) { // Memos can only be used with zAddrs. So check that first auto addr = ui->sendToWidgets->findChild(QString("Address") + QString::number(number)); - if (!Settings::addressFromAddressLabel(addr->text()).startsWith("z")) { + if (!AddressBook::addressFromAddressLabel(addr->text()).startsWith("z")) { QMessageBox msg(QMessageBox::Critical, "Memos can only be used with z-addresses", "The memo field can only be used with a z-address.\n" + addr->text() + "\ndoesn't look like a z-address", QMessageBox::Ok, this); @@ -357,7 +358,7 @@ void MainWindow::maxAmountChecked(int checked) { } sumAllAmounts += Settings::getTotalFee(); - auto addr = Settings::addressFromAddressLabel(ui->inputsCombo->currentText().split("(")[0]); + auto addr = ui->inputsCombo->currentText(); auto maxamount = rpc->getAllBalances()->value(addr) - sumAllAmounts; maxamount = (maxamount < 0) ? 0 : maxamount; @@ -373,14 +374,14 @@ void MainWindow::maxAmountChecked(int checked) { Tx MainWindow::createTxFromSendPage() { Tx tx; // Gather the from / to addresses - tx.fromAddr = ui->inputsCombo->currentText().split("(")[0].trimmed(); + tx.fromAddr = ui->inputsCombo->currentText(); // For each addr/amt in the sendTo tab int totalItems = ui->sendToWidgets->children().size() - 2; // The last one is a spacer, so ignore that for (int i=0; i < totalItems; i++) { QString addr = ui->sendToWidgets->findChild(QString("Address") % QString::number(i+1))->text().trimmed(); // Remove label if it exists - addr = Settings::addressFromAddressLabel(addr); + addr = AddressBook::addressFromAddressLabel(addr); double amt = ui->sendToWidgets->findChild(QString("Amount") % QString::number(i+1))->text().trimmed().toDouble(); QString memo = ui->sendToWidgets->findChild(QString("MemoTxt") % QString::number(i+1))->text().trimmed(); diff --git a/src/settings.cpp b/src/settings.cpp index c10179b..8abc579 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -206,6 +206,3 @@ bool Settings::isValidAddress(QString addr) { ztsexp.exactMatch(addr) || zsexp.exactMatch(addr); } -QString Settings::addressFromAddressLabel(const QString& lblAddr) { - return lblAddr.trimmed().split("/").last(); -} \ No newline at end of file diff --git a/src/settings.h b/src/settings.h index 43b1d16..fb18779 100644 --- a/src/settings.h +++ b/src/settings.h @@ -72,8 +72,6 @@ public: static double getTotalFee(); static bool isValidAddress(QString addr); - static QString addressFromAddressLabel(const QString& lblAddr); - static QString addressLabelFromAddress(const QString& addr); static const int updateSpeed = 20 * 1000; // 20 sec static const int quickUpdateSpeed = 5 * 1000; // 5 sec diff --git a/src/turnstile.ui b/src/turnstile.ui index c418e0a..e39a1f7 100644 --- a/src/turnstile.ui +++ b/src/turnstile.ui @@ -59,7 +59,7 @@
- + 0 @@ -108,7 +108,7 @@ - + @@ -190,6 +190,13 @@
+ + + AddressCombo + QComboBox +
addresscombo.h
+
+
diff --git a/zec-qt-wallet.pro b/zec-qt-wallet.pro index 40a0f9d..dde32bf 100644 --- a/zec-qt-wallet.pro +++ b/zec-qt-wallet.pro @@ -50,7 +50,8 @@ SOURCES += \ src/connection.cpp \ src/fillediconlabel.cpp \ src/addressbook.cpp \ - src/logger.cpp + src/logger.cpp \ + src/addresscombo.cpp HEADERS += \ src/mainwindow.h \ @@ -69,7 +70,8 @@ HEADERS += \ src/connection.h \ src/fillediconlabel.h \ src/addressbook.h \ - src/logger.h + src/logger.h \ + src/addresscombo.h FORMS += \ src/mainwindow.ui \