Browse Source

Labels (#59)

* hacked up labels

* Custom widget for addresses with labels

* turnstile to use labels
recurring
adityapk00 6 years ago
committed by GitHub
parent
commit
44d26ddab7
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      src/addressbook.cpp
  2. 2
      src/addressbook.h
  3. 41
      src/addresscombo.cpp
  4. 24
      src/addresscombo.h
  5. 9
      src/balancestablemodel.cpp
  6. 28
      src/mainwindow.cpp
  7. 2
      src/mainwindow.h
  8. 15
      src/mainwindow.ui
  9. 1
      src/precompiled.h
  10. 2
      src/qrcodelabel.h
  11. 8
      src/rpc.cpp
  12. 21
      src/sendtab.cpp
  13. 3
      src/settings.cpp
  14. 2
      src/settings.h
  15. 11
      src/turnstile.ui
  16. 6
      zec-qt-wallet.pro

17
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;
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;

2
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);

41
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);
}

24
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

9
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())));
}
}

28
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<void(bool)> 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<const QString &>::of(&QComboBox::currentIndexChanged), [=] (const QString& addr) {
QOverload<int>::of(&QComboBox::currentIndexChanged), [=] (int index) {
QString addr = ui->listRecieveAddresses->itemText(index);
if (addr.isEmpty()) {
// Draw empty stuff

2
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);

15
src/mainwindow.ui

@ -228,7 +228,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_8">
<item>
<widget class="QComboBox" name="inputsCombo"/>
<widget class="AddressCombo" name="inputsCombo"/>
</item>
</layout>
</item>
@ -316,8 +316,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>928</width>
<height>380</height>
<width>920</width>
<height>334</height>
</rect>
</property>
<layout class="QVBoxLayout" name="sendToLayout">
@ -628,7 +628,7 @@
<item>
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QComboBox" name="listRecieveAddresses">
<widget class="AddressCombo" name="listRecieveAddresses">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@ -849,7 +849,7 @@
<x>0</x>
<y>0</y>
<width>968</width>
<height>19</height>
<height>22</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
@ -970,6 +970,11 @@
<extends>QLabel</extends>
<header>fillediconlabel.h</header>
</customwidget>
<customwidget>
<class>AddressCombo</class>
<extends>QComboBox</extends>
<header>addresscombo.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>tabWidget</tabstop>

1
src/precompiled.h

@ -22,6 +22,7 @@
#include <QHeaderView>
#include <QMessageBox>
#include <QCheckBox>
#include <QComboBox>
#include <QScrollBar>
#include <QPainter>
#include <QMovie>

2
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);

8
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;
}

21
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<const QString &>::of(&QComboBox::currentIndexChanged),
QObject::connect(ui->inputsCombo, QOverload<int>::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<QLineEdit*>(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<QLineEdit*>(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<QLineEdit*>(QString("Amount") % QString::number(i+1))->text().trimmed().toDouble();
QString memo = ui->sendToWidgets->findChild<QLabel*>(QString("MemoTxt") % QString::number(i+1))->text().trimmed();

3
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();
}

2
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

11
src/turnstile.ui

@ -59,7 +59,7 @@
</widget>
</item>
<item row="2" column="1" colspan="2">
<widget class="QComboBox" name="migrateZaddList">
<widget class="AddressCombo" name="migrateZaddList">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@ -108,7 +108,7 @@
</widget>
</item>
<item row="4" column="1" colspan="2">
<widget class="QComboBox" name="migrateTo"/>
<widget class="AddressCombo" name="migrateTo"/>
</item>
<item row="1" column="0" colspan="3">
<widget class="Line" name="line">
@ -190,6 +190,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>AddressCombo</class>
<extends>QComboBox</extends>
<header>addresscombo.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>

6
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 \

Loading…
Cancel
Save