mirror of
https://github.com/feather-wallet/feather.git
synced 2025-01-03 17:39:49 +00:00
Resizeable QrCode
This commit is contained in:
parent
758b385aef
commit
1e30b1f4a1
9 changed files with 131 additions and 28 deletions
|
@ -209,9 +209,8 @@ void ReceiveWidget::showQrCodeDialog() {
|
||||||
}
|
}
|
||||||
QString address = index.model()->data(index.siblingAtColumn(SubaddressModel::Address), Qt::UserRole).toString();
|
QString address = index.model()->data(index.siblingAtColumn(SubaddressModel::Address), Qt::UserRole).toString();
|
||||||
QrCode qr(address, QrCode::Version::AUTO, QrCode::ErrorCorrectionLevel::HIGH);
|
QrCode qr(address, QrCode::Version::AUTO, QrCode::ErrorCorrectionLevel::HIGH);
|
||||||
auto *dialog = new QrCodeDialog(this, qr, "Address");
|
QrCodeDialog dialog{this, &qr, "Address"};
|
||||||
dialog->exec();
|
dialog.exec();
|
||||||
dialog->deleteLater();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList ReceiveWidget::getHiddenAddresses() {
|
QStringList ReceiveWidget::getHiddenAddresses() {
|
||||||
|
|
|
@ -8,15 +8,16 @@
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
|
||||||
QrCodeDialog::QrCodeDialog(QWidget *parent, const QrCode &qrCode, const QString &title)
|
QrCodeDialog::QrCodeDialog(QWidget *parent, QrCode *qrCode, const QString &title)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
, ui(new Ui::QrCodeDialog)
|
, ui(new Ui::QrCodeDialog)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
this->setWindowTitle(title);
|
this->setWindowTitle(title);
|
||||||
|
|
||||||
m_pixmap = qrCode.toPixmap(1).scaled(500, 500, Qt::KeepAspectRatio);
|
ui->qrWidget->setQrCode(qrCode);
|
||||||
ui->QrCode->setPixmap(m_pixmap);
|
|
||||||
|
m_pixmap = qrCode->toPixmap(1).scaled(500, 500, Qt::KeepAspectRatio);
|
||||||
|
|
||||||
connect(ui->btn_CopyImage, &QPushButton::clicked, this, &QrCodeDialog::copyImage);
|
connect(ui->btn_CopyImage, &QPushButton::clicked, this, &QrCodeDialog::copyImage);
|
||||||
connect(ui->btn_Save, &QPushButton::clicked, this, &QrCodeDialog::saveImage);
|
connect(ui->btn_Save, &QPushButton::clicked, this, &QrCodeDialog::saveImage);
|
||||||
|
@ -24,12 +25,7 @@ QrCodeDialog::QrCodeDialog(QWidget *parent, const QrCode &qrCode, const QString
|
||||||
accept();
|
accept();
|
||||||
});
|
});
|
||||||
|
|
||||||
this->adjustSize();
|
this->resize(500, 500);
|
||||||
}
|
|
||||||
|
|
||||||
void QrCodeDialog::setQrCode(const QrCode &qrCode) {
|
|
||||||
m_pixmap = qrCode.toPixmap(1).scaled(500, 500, Qt::KeepAspectRatio);
|
|
||||||
ui->QrCode->setPixmap(m_pixmap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void QrCodeDialog::copyImage() {
|
void QrCodeDialog::copyImage() {
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
|
||||||
#include "qrcode/QrCode.h"
|
#include "qrcode/QrCode.h"
|
||||||
|
#include "widgets/QrCodeWidget.h"
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class QrCodeDialog;
|
class QrCodeDialog;
|
||||||
|
@ -17,9 +18,8 @@ class QrCodeDialog : public QDialog
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit QrCodeDialog(QWidget *parent, const QrCode &qrCode, const QString &title = "Qr Code");
|
explicit QrCodeDialog(QWidget *parent, QrCode *qrCode, const QString &title = "Qr Code");
|
||||||
~QrCodeDialog() override;
|
~QrCodeDialog() override;
|
||||||
void setQrCode(const QrCode &qrCode);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void copyImage();
|
void copyImage();
|
||||||
|
@ -29,5 +29,4 @@ private:
|
||||||
QPixmap m_pixmap;
|
QPixmap m_pixmap;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif //FEATHER_QRCODEDIALOG_H
|
#endif //FEATHER_QRCODEDIALOG_H
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>522</width>
|
<width>520</width>
|
||||||
<height>562</height>
|
<height>446</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
|
@ -15,22 +15,19 @@
|
||||||
</property>
|
</property>
|
||||||
<layout class="QVBoxLayout" name="verticalLayout">
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="QrCode">
|
<widget class="QrCodeWidget" name="qrWidget" native="true">
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
|
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
<verstretch>0</verstretch>
|
<verstretch>0</verstretch>
|
||||||
</sizepolicy>
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="minimumSize">
|
<property name="minimumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>500</width>
|
<width>150</width>
|
||||||
<height>500</height>
|
<height>150</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
<property name="text">
|
|
||||||
<string>QrCode</string>
|
|
||||||
</property>
|
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
|
@ -88,6 +85,14 @@
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>QrCodeWidget</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>widgets/QrCodeWidget.h</header>
|
||||||
|
<container>1</container>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections/>
|
||||||
</ui>
|
</ui>
|
||||||
|
|
|
@ -154,9 +154,8 @@ void TxConfAdvDialog::unsignedQrCode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
QrCode qr(m_tx->unsignedTxToBin(), QrCode::Version::AUTO, QrCode::ErrorCorrectionLevel::LOW);
|
QrCode qr(m_tx->unsignedTxToBin(), QrCode::Version::AUTO, QrCode::ErrorCorrectionLevel::LOW);
|
||||||
auto *dialog = new QrCodeDialog(this, qr, "Unsigned Transaction");
|
QrCodeDialog dialog{this, &qr, "Unsigned Transaction"};
|
||||||
dialog->exec();
|
dialog.exec();
|
||||||
dialog->deleteLater();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TxConfAdvDialog::unsignedCopy() {
|
void TxConfAdvDialog::unsignedCopy() {
|
||||||
|
|
|
@ -164,4 +164,20 @@ QPixmap QrCode::toPixmap(const int margin) const
|
||||||
painter.end();
|
painter.end();
|
||||||
|
|
||||||
return pixmap;
|
return pixmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int QrCode::width() {
|
||||||
|
if (!isValid()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return d_ptr->m_qrcode->width;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned char* QrCode::data() {
|
||||||
|
if (!isValid()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return d_ptr->m_qrcode->data;
|
||||||
|
}
|
||||||
|
|
|
@ -71,6 +71,9 @@ public:
|
||||||
void writeSvg(QIODevice* outputDevice, const int dpi, const int margin = 4) const;
|
void writeSvg(QIODevice* outputDevice, const int dpi, const int margin = 4) const;
|
||||||
QPixmap toPixmap(const int margin = 4) const;
|
QPixmap toPixmap(const int margin = 4) const;
|
||||||
|
|
||||||
|
int width();
|
||||||
|
unsigned char* data();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void init(const QString& data, const Version version, const ErrorCorrectionLevel ecl, const bool caseSensitive);
|
void init(const QString& data, const Version version, const ErrorCorrectionLevel ecl, const bool caseSensitive);
|
||||||
|
|
||||||
|
|
60
src/widgets/QrCodeWidget.cpp
Normal file
60
src/widgets/QrCodeWidget.cpp
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
// Copyright (c) 2020-2021, The Monero Project.
|
||||||
|
|
||||||
|
#include "QrCodeWidget.h"
|
||||||
|
|
||||||
|
#include <QColor>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QPen>
|
||||||
|
|
||||||
|
QrCodeWidget::QrCodeWidget(QWidget *parent) : QWidget(parent)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void QrCodeWidget::setQrCode(QrCode *qrCode) {
|
||||||
|
m_qrcode = qrCode;
|
||||||
|
|
||||||
|
int k = m_qrcode->width();
|
||||||
|
this->setMinimumSize(k*5, k*5);
|
||||||
|
|
||||||
|
this->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void QrCodeWidget::paintEvent(QPaintEvent *event) {
|
||||||
|
// Implementation adapted from Electrum: qrcodewidget.py
|
||||||
|
if (!m_qrcode) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QColor black{0, 0, 0, 255};
|
||||||
|
QColor white{255, 255, 255, 255};
|
||||||
|
QPen blackPen{black};
|
||||||
|
blackPen.setJoinStyle(Qt::MiterJoin);
|
||||||
|
|
||||||
|
QPainter painter(this);
|
||||||
|
|
||||||
|
auto r = painter.viewport();
|
||||||
|
int k = m_qrcode->width();
|
||||||
|
int margin = 10;
|
||||||
|
int framesize = std::min(r.width(), r.height());
|
||||||
|
int boxsize = int((framesize - (2*margin)) / k);
|
||||||
|
int size = k*boxsize;
|
||||||
|
int left = (framesize - size)/2;
|
||||||
|
int top = (framesize - size)/2;
|
||||||
|
|
||||||
|
painter.setBrush(white);
|
||||||
|
painter.setPen(white);
|
||||||
|
painter.drawRect(0, 0, framesize, framesize);
|
||||||
|
|
||||||
|
painter.setBrush(black);
|
||||||
|
painter.setPen(blackPen);
|
||||||
|
|
||||||
|
unsigned char* dot = m_qrcode->data();
|
||||||
|
for (int row = 0; row < k; row++) {
|
||||||
|
for (int column = 0; column < k; column++) {
|
||||||
|
if (quint8(0x01) == (static_cast<quint8>(*dot++) & quint8(0x01))) {
|
||||||
|
painter.drawRect(int(left+(column*boxsize)), int(top+(row*boxsize)), boxsize - 1, boxsize - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
26
src/widgets/QrCodeWidget.h
Normal file
26
src/widgets/QrCodeWidget.h
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
// SPDX-License-Identifier: BSD-3-Clause
|
||||||
|
// Copyright (c) 2020-2021, The Monero Project.
|
||||||
|
|
||||||
|
#ifndef FEATHER_QRCODEWIDGET_H
|
||||||
|
#define FEATHER_QRCODEWIDGET_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
#include "qrcode/QrCode.h"
|
||||||
|
|
||||||
|
class QrCodeWidget : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit QrCodeWidget(QWidget *parent = nullptr);
|
||||||
|
void setQrCode(QrCode *qrCode);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *event) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QrCode *m_qrcode = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //FEATHER_QRCODEWIDGET_H
|
Loading…
Reference in a new issue