You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.1 KiB
167 lines
4.1 KiB
11 years ago
|
#include "receiverequestdialog.h"
|
||
|
#include "ui_receiverequestdialog.h"
|
||
13 years ago
|
|
||
|
#include "bitcoinunits.h"
|
||
|
#include "guiconstants.h"
|
||
13 years ago
|
#include "guiutil.h"
|
||
13 years ago
|
#include "optionsmodel.h"
|
||
13 years ago
|
|
||
13 years ago
|
#include <QPixmap>
|
||
12 years ago
|
#if QT_VERSION < 0x050000
|
||
13 years ago
|
#include <QUrl>
|
||
12 years ago
|
#endif
|
||
13 years ago
|
|
||
11 years ago
|
#include "bitcoin-config.h" /* for USE_QRCODE */
|
||
|
|
||
|
#ifdef USE_QRCODE
|
||
13 years ago
|
#include <qrencode.h>
|
||
11 years ago
|
#endif
|
||
13 years ago
|
|
||
11 years ago
|
ReceiveRequestDialog::ReceiveRequestDialog(const QString &addr, const QString &label, quint64 amount, const QString &message, QWidget *parent) :
|
||
13 years ago
|
QDialog(parent),
|
||
11 years ago
|
ui(new Ui::ReceiveRequestDialog),
|
||
13 years ago
|
model(0),
|
||
|
address(addr)
|
||
13 years ago
|
{
|
||
|
ui->setupUi(this);
|
||
13 years ago
|
|
||
11 years ago
|
QString target = label;
|
||
|
if(target.isEmpty())
|
||
|
target = addr;
|
||
|
setWindowTitle(tr("Request payment to %1").arg(target));
|
||
13 years ago
|
|
||
11 years ago
|
ui->lnAddress->setText(addr);
|
||
|
if(amount)
|
||
|
ui->lnReqAmount->setValue(amount);
|
||
|
ui->lnReqAmount->setReadOnly(true);
|
||
13 years ago
|
ui->lnLabel->setText(label);
|
||
11 years ago
|
ui->lnMessage->setText(message);
|
||
13 years ago
|
|
||
11 years ago
|
#ifndef USE_QRCODE
|
||
|
ui->btnSaveAs->setVisible(false);
|
||
|
ui->lblQRCode->setVisible(false);
|
||
|
#endif
|
||
13 years ago
|
|
||
13 years ago
|
genCode();
|
||
|
}
|
||
|
|
||
11 years ago
|
ReceiveRequestDialog::~ReceiveRequestDialog()
|
||
13 years ago
|
{
|
||
|
delete ui;
|
||
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::setModel(OptionsModel *model)
|
||
13 years ago
|
{
|
||
|
this->model = model;
|
||
|
|
||
|
if (model)
|
||
|
connect(model, SIGNAL(displayUnitChanged(int)), this, SLOT(updateDisplayUnit()));
|
||
|
|
||
|
// update the display unit, to not use the default ("BTC")
|
||
|
updateDisplayUnit();
|
||
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::genCode()
|
||
13 years ago
|
{
|
||
13 years ago
|
QString uri = getURI();
|
||
11 years ago
|
ui->btnSaveAs->setEnabled(false);
|
||
|
ui->outUri->setPlainText(uri);
|
||
|
#ifdef USE_QRCODE
|
||
13 years ago
|
if (uri != "")
|
||
13 years ago
|
{
|
||
13 years ago
|
ui->lblQRCode->setText("");
|
||
|
|
||
|
QRcode *code = QRcode_encodeString(uri.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
|
||
13 years ago
|
if (!code)
|
||
|
{
|
||
|
ui->lblQRCode->setText(tr("Error encoding URI into QR Code."));
|
||
|
return;
|
||
|
}
|
||
13 years ago
|
myImage = QImage(code->width + 8, code->width + 8, QImage::Format_RGB32);
|
||
|
myImage.fill(0xffffff);
|
||
|
unsigned char *p = code->data;
|
||
|
for (int y = 0; y < code->width; y++)
|
||
13 years ago
|
{
|
||
13 years ago
|
for (int x = 0; x < code->width; x++)
|
||
|
{
|
||
|
myImage.setPixel(x + 4, y + 4, ((*p & 1) ? 0x0 : 0xffffff));
|
||
|
p++;
|
||
|
}
|
||
13 years ago
|
}
|
||
13 years ago
|
QRcode_free(code);
|
||
13 years ago
|
|
||
13 years ago
|
ui->lblQRCode->setPixmap(QPixmap::fromImage(myImage).scaled(300, 300));
|
||
11 years ago
|
ui->btnSaveAs->setEnabled(true);
|
||
13 years ago
|
}
|
||
11 years ago
|
#endif
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
QString ReceiveRequestDialog::getURI()
|
||
13 years ago
|
{
|
||
13 years ago
|
QString ret = QString("bitcoin:%1").arg(address);
|
||
|
int paramCount = 0;
|
||
13 years ago
|
|
||
11 years ago
|
if (ui->lnReqAmount->validate())
|
||
13 years ago
|
{
|
||
11 years ago
|
// even if we allow a non BTC unit input in lnReqAmount, we generate the URI with BTC as unit (as defined in BIP21)
|
||
|
ret += QString("?amount=%1").arg(BitcoinUnits::format(BitcoinUnits::BTC, ui->lnReqAmount->value()));
|
||
|
paramCount++;
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
if (!ui->lnLabel->text().isEmpty())
|
||
|
{
|
||
13 years ago
|
QString lbl(QUrl::toPercentEncoding(ui->lnLabel->text()));
|
||
|
ret += QString("%1label=%2").arg(paramCount == 0 ? "?" : "&").arg(lbl);
|
||
|
paramCount++;
|
||
|
}
|
||
|
|
||
13 years ago
|
if (!ui->lnMessage->text().isEmpty())
|
||
|
{
|
||
13 years ago
|
QString msg(QUrl::toPercentEncoding(ui->lnMessage->text()));
|
||
|
ret += QString("%1message=%2").arg(paramCount == 0 ? "?" : "&").arg(msg);
|
||
|
paramCount++;
|
||
|
}
|
||
|
|
||
11 years ago
|
// limit URI length
|
||
13 years ago
|
if (ret.length() > MAX_URI_LENGTH)
|
||
|
{
|
||
|
ui->lblQRCode->setText(tr("Resulting URI too long, try to reduce the text for label / message."));
|
||
13 years ago
|
return QString("");
|
||
13 years ago
|
}
|
||
|
|
||
|
return ret;
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::on_lnReqAmount_textChanged()
|
||
13 years ago
|
{
|
||
13 years ago
|
genCode();
|
||
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::on_lnLabel_textChanged()
|
||
13 years ago
|
{
|
||
13 years ago
|
genCode();
|
||
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::on_lnMessage_textChanged()
|
||
13 years ago
|
{
|
||
13 years ago
|
genCode();
|
||
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::on_btnSaveAs_clicked()
|
||
13 years ago
|
{
|
||
11 years ago
|
#ifdef USE_QRCODE
|
||
13 years ago
|
QString fn = GUIUtil::getSaveFileName(this, tr("Save QR Code"), QString(), tr("PNG Images (*.png)"));
|
||
13 years ago
|
if (!fn.isEmpty())
|
||
13 years ago
|
myImage.scaled(EXPORT_IMAGE_SIZE, EXPORT_IMAGE_SIZE).save(fn);
|
||
11 years ago
|
#endif
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
void ReceiveRequestDialog::updateDisplayUnit()
|
||
13 years ago
|
{
|
||
|
if (model)
|
||
|
{
|
||
|
// Update lnReqAmount with the current unit
|
||
|
ui->lnReqAmount->setDisplayUnit(model->getDisplayUnit());
|
||
|
}
|
||
|
}
|