Files
thehub/hub-qt/sendcoinsentry.cpp
T

281 lines
8.7 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2011-2015 The Bitcoin Core developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2013-11-04 16:20:43 +01:00
2011-07-16 19:01:05 +02:00
#include "sendcoinsentry.h"
#include "ui_sendcoinsentry.h"
2013-01-23 21:51:02 +01:00
2011-07-16 19:01:05 +02:00
#include "addressbookpage.h"
#include "addresstablemodel.h"
2013-04-13 00:13:08 -05:00
#include "guiutil.h"
#include "optionsmodel.h"
2015-07-28 15:20:14 +02:00
#include "platformstyle.h"
2013-04-13 00:13:08 -05:00
#include "walletmodel.h"
2011-07-16 19:01:05 +02:00
2011-08-07 16:09:49 +02:00
#include <QApplication>
#include <QClipboard>
2011-07-16 19:01:05 +02:00
2015-07-28 15:20:14 +02:00
SendCoinsEntry::SendCoinsEntry(const PlatformStyle *platformStyle, QWidget *parent) :
QStackedWidget(parent),
2011-07-16 19:01:05 +02:00
ui(new Ui::SendCoinsEntry),
2015-07-28 15:20:14 +02:00
model(0),
platformStyle(platformStyle)
2011-07-16 19:01:05 +02:00
{
ui->setupUi(this);
2015-07-28 15:20:14 +02:00
ui->addressBookButton->setIcon(platformStyle->SingleColorIcon(":/icons/address-book"));
ui->pasteButton->setIcon(platformStyle->SingleColorIcon(":/icons/editpaste"));
ui->deleteButton->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
ui->deleteButton_is->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
ui->deleteButton_s->setIcon(platformStyle->SingleColorIcon(":/icons/remove"));
2014-11-06 19:55:52 +00:00
setCurrentWidget(ui->SendCoins);
2015-07-28 15:20:14 +02:00
if (platformStyle->getUseExtraSpacing())
ui->payToLayout->setSpacing(4);
#if QT_VERSION >= 0x040700
ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
#endif
2011-07-16 19:01:05 +02:00
// normal bitcoin address field
2011-07-16 19:01:05 +02:00
GUIUtil::setupAddressWidget(ui->payTo, this);
// just a label for displaying bitcoin address(es)
ui->payTo_is->setFont(GUIUtil::fixedPitchFont());
// Connect signals
connect(ui->payAmount, SIGNAL(valueChanged()), this, SIGNAL(payAmountChanged()));
2014-07-23 14:34:36 +02:00
connect(ui->checkboxSubtractFeeFromAmount, SIGNAL(toggled(bool)), this, SIGNAL(subtractFeeFromAmountChanged()));
connect(ui->deleteButton, SIGNAL(clicked()), this, SLOT(deleteClicked()));
connect(ui->deleteButton_is, SIGNAL(clicked()), this, SLOT(deleteClicked()));
connect(ui->deleteButton_s, SIGNAL(clicked()), this, SLOT(deleteClicked()));
2011-07-16 19:01:05 +02:00
}
SendCoinsEntry::~SendCoinsEntry()
{
delete ui;
}
void SendCoinsEntry::on_pasteButton_clicked()
{
// Paste text from clipboard into recipient field
ui->payTo->setText(QApplication::clipboard()->text());
}
void SendCoinsEntry::on_addressBookButton_clicked()
{
2011-11-08 21:18:36 +01:00
if(!model)
return;
2015-07-28 15:20:14 +02:00
AddressBookPage dlg(platformStyle, AddressBookPage::ForSelection, AddressBookPage::SendingTab, this);
2011-07-16 19:01:05 +02:00
dlg.setModel(model->getAddressTableModel());
if(dlg.exec())
{
ui->payTo->setText(dlg.getReturnValue());
ui->payAmount->setFocus();
}
}
void SendCoinsEntry::on_payTo_textChanged(const QString &address)
{
updateLabel(GUIUtil::convertCashBitcoinAddress(address));
}
2011-07-16 19:01:05 +02:00
void SendCoinsEntry::setModel(WalletModel *model)
{
this->model = model;
if (model && model->getOptionsModel())
connect(model->getOptionsModel(), SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
clear();
2011-07-16 19:01:05 +02:00
}
void SendCoinsEntry::clear()
{
// clear UI elements for normal payment
2011-07-16 19:01:05 +02:00
ui->payTo->clear();
ui->addAsLabel->clear();
2011-07-22 17:06:37 +02:00
ui->payAmount->clear();
2014-07-23 14:34:36 +02:00
ui->checkboxSubtractFeeFromAmount->setCheckState(Qt::Unchecked);
ui->messageTextLabel->clear();
ui->messageTextLabel->hide();
ui->messageLabel->hide();
2015-03-18 11:22:27 +01:00
// clear UI elements for unauthenticated payment request
ui->payTo_is->clear();
ui->memoTextLabel_is->clear();
ui->payAmount_is->clear();
2015-03-18 11:22:27 +01:00
// clear UI elements for authenticated payment request
ui->payTo_s->clear();
ui->memoTextLabel_s->clear();
ui->payAmount_s->clear();
// update the display unit, to not use the default ("BTC")
updateDisplayUnit();
2011-07-16 19:01:05 +02:00
}
void SendCoinsEntry::deleteClicked()
2011-07-16 19:01:05 +02:00
{
2015-07-14 13:59:05 +02:00
Q_EMIT removeEntry(this);
2011-07-16 19:01:05 +02:00
}
bool SendCoinsEntry::validate()
{
if (!model)
return false;
2011-07-16 19:01:05 +02:00
// Check input validity
bool retval = true;
// Skip checks for payment request
if (recipient.paymentRequest.IsInitialized())
return retval;
if (!model->validateAddress(GUIUtil::convertCashBitcoinAddress(ui->payTo->text())))
2011-07-16 19:01:05 +02:00
{
ui->payTo->setValid(false);
retval = false;
}
if (!ui->payAmount->validate())
2013-08-08 13:09:07 +10:00
{
retval = false;
}
// Sending a zero amount is invalid
if (ui->payAmount->value(0) <= 0)
{
ui->payAmount->setValid(false);
retval = false;
}
2013-08-08 13:09:07 +10:00
// Reject dust outputs:
if (retval && GUIUtil::isDust(GUIUtil::convertCashBitcoinAddress(ui->payTo->text()), ui->payAmount->value())) {
2013-08-08 13:09:07 +10:00
ui->payAmount->setValid(false);
retval = false;
}
2011-07-16 19:01:05 +02:00
return retval;
}
SendCoinsRecipient SendCoinsEntry::getValue()
{
// Payment request
if (recipient.paymentRequest.IsInitialized())
return recipient;
2011-07-16 19:01:05 +02:00
// Normal payment
recipient.address = GUIUtil::convertCashBitcoinAddress(ui->payTo->text());
recipient.label = ui->addAsLabel->text();
recipient.amount = ui->payAmount->value();
recipient.message = ui->messageTextLabel->text();
2014-07-23 14:34:36 +02:00
recipient.fSubtractFeeFromAmount = (ui->checkboxSubtractFeeFromAmount->checkState() == Qt::Checked);
2011-07-16 19:01:05 +02:00
return recipient;
2011-07-16 19:01:05 +02:00
}
QWidget *SendCoinsEntry::setupTabChain(QWidget *prev)
{
QWidget::setTabOrder(prev, ui->payTo);
2014-01-02 09:32:03 +01:00
QWidget::setTabOrder(ui->payTo, ui->addAsLabel);
QWidget *w = ui->payAmount->setupTabChain(ui->addAsLabel);
2014-07-23 14:34:36 +02:00
QWidget::setTabOrder(w, ui->checkboxSubtractFeeFromAmount);
QWidget::setTabOrder(ui->checkboxSubtractFeeFromAmount, ui->addressBookButton);
2011-07-16 19:01:05 +02:00
QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
2014-01-02 09:32:03 +01:00
return ui->deleteButton;
2011-07-16 19:01:05 +02:00
}
2011-08-07 16:04:48 +02:00
void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
{
recipient = value;
if (recipient.paymentRequest.IsInitialized()) // payment request
{
2015-03-18 11:22:27 +01:00
if (recipient.authenticatedMerchant.isEmpty()) // unauthenticated
{
ui->payTo_is->setText(recipient.address);
ui->memoTextLabel_is->setText(recipient.message);
ui->payAmount_is->setValue(recipient.amount);
ui->payAmount_is->setReadOnly(true);
2015-03-18 11:22:27 +01:00
setCurrentWidget(ui->SendCoins_UnauthenticatedPaymentRequest);
}
2015-03-18 11:22:27 +01:00
else // authenticated
{
ui->payTo_s->setText(recipient.authenticatedMerchant);
ui->memoTextLabel_s->setText(recipient.message);
ui->payAmount_s->setValue(recipient.amount);
ui->payAmount_s->setReadOnly(true);
2015-03-18 11:22:27 +01:00
setCurrentWidget(ui->SendCoins_AuthenticatedPaymentRequest);
}
}
else // normal payment
{
// message
ui->messageTextLabel->setText(recipient.message);
ui->messageTextLabel->setVisible(!recipient.message.isEmpty());
ui->messageLabel->setVisible(!recipient.message.isEmpty());
ui->addAsLabel->clear();
ui->payTo->setText(recipient.address); // this may set a label from addressbook
2015-04-28 14:48:28 +00:00
if (!recipient.label.isEmpty()) // if a label had been set from the addressbook, don't overwrite with an empty label
ui->addAsLabel->setText(recipient.label);
ui->payAmount->setValue(recipient.amount);
}
2011-08-07 16:04:48 +02:00
}
void SendCoinsEntry::setAddress(const QString &address)
{
ui->payTo->setText(address);
ui->payAmount->setFocus();
}
2011-08-07 16:04:48 +02:00
bool SendCoinsEntry::isClear()
{
return ui->payTo->text().isEmpty() && ui->payTo_is->text().isEmpty() && ui->payTo_s->text().isEmpty();
2011-08-07 16:04:48 +02:00
}
void SendCoinsEntry::setFocus()
{
ui->payTo->setFocus();
}
void SendCoinsEntry::updateDisplayUnit()
{
if(model && model->getOptionsModel())
{
// Update payAmount with the current unit
ui->payAmount->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
ui->payAmount_is->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
ui->payAmount_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
}
}
2013-10-16 17:11:39 +02:00
bool SendCoinsEntry::updateLabel(const QString &address)
{
if(!model)
return false;
// Fill in label from address book, if address has an associated label
QString associatedLabel = model->getAddressTableModel()->labelForAddress(address);
if(!associatedLabel.isEmpty())
{
ui->addAsLabel->setText(associatedLabel);
return true;
}
return false;
}