lbrycrd/src/qt/sendcoinsentry.cpp

218 lines
5.4 KiB
C++
Raw Normal View History

2011-07-16 19:01:05 +02:00
#include "sendcoinsentry.h"
#include "ui_sendcoinsentry.h"
2011-07-16 19:01:05 +02:00
#include "guiutil.h"
#include "bitcoinunits.h"
2011-07-16 19:01:05 +02:00
#include "addressbookpage.h"
#include "walletmodel.h"
#include "optionsmodel.h"
2011-07-16 19:01:05 +02:00
#include "addresstablemodel.h"
2011-08-07 16:09:49 +02:00
#include <QApplication>
#include <QClipboard>
2011-07-16 19:01:05 +02:00
SendCoinsEntry::SendCoinsEntry(QWidget *parent) :
QStackedWidget(parent),
2011-07-16 19:01:05 +02:00
ui(new Ui::SendCoinsEntry),
model(0)
{
ui->setupUi(this);
setCurrentWidget(ui->SendCoinsInsecure);
#ifdef Q_OS_MAC
ui->payToLayout->setSpacing(4);
#endif
#if QT_VERSION >= 0x040700
/* Do not move this to the XML file, Qt before 4.7 will choke on it */
ui->addAsLabel->setPlaceholderText(tr("Enter a label for this address to add it to your address book"));
ui->payTo->setPlaceholderText(tr("Enter a Bitcoin address (e.g. 1NS17iag9jJgTHD1VXjvLCEnZuQ3rJDE9L)"));
#endif
2011-07-16 19:01:05 +02:00
setFocusPolicy(Qt::TabFocus);
setFocusProxy(ui->payTo);
GUIUtil::setupAddressWidget(ui->payTo, this);
}
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;
2013-10-16 15:14:26 +02:00
AddressBookPage dlg(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(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::setRemoveEnabled(bool enabled)
{
ui->deleteButton->setEnabled(enabled);
}
void SendCoinsEntry::clear()
{
// clear UI elements for insecure payments
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();
// and the ones for secure payments just to be sure
ui->payTo_s->clear();
ui->memoTextLabel_s->clear();
ui->payAmount_s->clear();
2011-07-16 19:01:05 +02:00
ui->payTo->setFocus();
// update the display unit, to not use the default ("BTC")
updateDisplayUnit();
2011-07-16 19:01:05 +02:00
}
void SendCoinsEntry::on_deleteButton_clicked()
{
emit removeEntry(this);
}
bool SendCoinsEntry::validate()
{
// Check input validity
bool retval = true;
if (!recipient.authenticatedMerchant.isEmpty())
return retval;
if (!ui->payTo->hasAcceptableInput() ||
(model && !model->validateAddress(ui->payTo->text())))
2011-07-16 19:01:05 +02:00
{
ui->payTo->setValid(false);
2011-07-16 19:01:05 +02:00
retval = false;
}
if(!ui->payAmount->validate())
{
retval = false;
}
// Reject dust outputs:
if (retval && GUIUtil::isDust(ui->payTo->text(), ui->payAmount->value())) {
ui->payAmount->setValid(false);
2011-07-16 19:01:05 +02:00
retval = false;
}
return retval;
}
SendCoinsRecipient SendCoinsEntry::getValue()
{
if (!recipient.authenticatedMerchant.isEmpty())
return recipient;
2011-07-16 19:01:05 +02:00
// User-entered or non-authenticated:
recipient.address = ui->payTo->text();
recipient.label = ui->addAsLabel->text();
recipient.amount = ui->payAmount->value();
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);
QWidget::setTabOrder(ui->payTo, ui->addressBookButton);
QWidget::setTabOrder(ui->addressBookButton, ui->pasteButton);
QWidget::setTabOrder(ui->pasteButton, ui->deleteButton);
QWidget::setTabOrder(ui->deleteButton, ui->addAsLabel);
return ui->payAmount->setupTabChain(ui->addAsLabel);
}
void SendCoinsEntry::setValue(const SendCoinsRecipient &value)
{
recipient = value;
if (recipient.authenticatedMerchant.isEmpty())
{
ui->payTo->setText(recipient.address);
ui->addAsLabel->setText(recipient.label);
ui->payAmount->setValue(recipient.amount);
}
else
{
const payments::PaymentDetails& details = recipient.paymentRequest.getDetails();
ui->payTo_s->setText(recipient.authenticatedMerchant);
ui->memoTextLabel_s->setText(QString::fromStdString(details.memo()));
ui->payAmount_s->setValue(recipient.amount);
ui->payAmount_s->setReadOnly(true);
setCurrentWidget(ui->SendCoinsSecure);
}
}
void SendCoinsEntry::setAddress(const QString &address)
{
ui->payTo->setText(address);
ui->payAmount->setFocus();
}
bool SendCoinsEntry::isClear()
{
return ui->payTo->text().isEmpty() && ui->payTo_s->text().isEmpty();
}
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_s->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
}
}
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;
}