Resizeable QrCode

This commit is contained in:
tobtoht 2021-07-04 00:06:24 +02:00
parent 758b385aef
commit 1e30b1f4a1
No known key found for this signature in database
GPG key ID: 1CADD27F41F45C3C
9 changed files with 131 additions and 28 deletions

View file

@ -209,9 +209,8 @@ void ReceiveWidget::showQrCodeDialog() {
}
QString address = index.model()->data(index.siblingAtColumn(SubaddressModel::Address), Qt::UserRole).toString();
QrCode qr(address, QrCode::Version::AUTO, QrCode::ErrorCorrectionLevel::HIGH);
auto *dialog = new QrCodeDialog(this, qr, "Address");
dialog->exec();
dialog->deleteLater();
QrCodeDialog dialog{this, &qr, "Address"};
dialog.exec();
}
QStringList ReceiveWidget::getHiddenAddresses() {

View file

@ -8,15 +8,16 @@
#include <QFileDialog>
#include <QMessageBox>
QrCodeDialog::QrCodeDialog(QWidget *parent, const QrCode &qrCode, const QString &title)
QrCodeDialog::QrCodeDialog(QWidget *parent, QrCode *qrCode, const QString &title)
: QDialog(parent)
, ui(new Ui::QrCodeDialog)
{
ui->setupUi(this);
this->setWindowTitle(title);
m_pixmap = qrCode.toPixmap(1).scaled(500, 500, Qt::KeepAspectRatio);
ui->QrCode->setPixmap(m_pixmap);
ui->qrWidget->setQrCode(qrCode);
m_pixmap = qrCode->toPixmap(1).scaled(500, 500, Qt::KeepAspectRatio);
connect(ui->btn_CopyImage, &QPushButton::clicked, this, &QrCodeDialog::copyImage);
connect(ui->btn_Save, &QPushButton::clicked, this, &QrCodeDialog::saveImage);
@ -24,12 +25,7 @@ QrCodeDialog::QrCodeDialog(QWidget *parent, const QrCode &qrCode, const QString
accept();
});
this->adjustSize();
}
void QrCodeDialog::setQrCode(const QrCode &qrCode) {
m_pixmap = qrCode.toPixmap(1).scaled(500, 500, Qt::KeepAspectRatio);
ui->QrCode->setPixmap(m_pixmap);
this->resize(500, 500);
}
void QrCodeDialog::copyImage() {

View file

@ -7,6 +7,7 @@
#include <QDialog>
#include "qrcode/QrCode.h"
#include "widgets/QrCodeWidget.h"
namespace Ui {
class QrCodeDialog;
@ -17,9 +18,8 @@ class QrCodeDialog : public QDialog
Q_OBJECT
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;
void setQrCode(const QrCode &qrCode);
private:
void copyImage();
@ -29,5 +29,4 @@ private:
QPixmap m_pixmap;
};
#endif //FEATHER_QRCODEDIALOG_H

View file

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>522</width>
<height>562</height>
<width>520</width>
<height>446</height>
</rect>
</property>
<property name="windowTitle">
@ -15,22 +15,19 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="QrCode">
<widget class="QrCodeWidget" name="qrWidget" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>500</width>
<height>500</height>
<width>150</width>
<height>150</height>
</size>
</property>
<property name="text">
<string>QrCode</string>
</property>
</widget>
</item>
<item>
@ -88,6 +85,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QrCodeWidget</class>
<extends>QWidget</extends>
<header>widgets/QrCodeWidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View file

@ -154,9 +154,8 @@ void TxConfAdvDialog::unsignedQrCode() {
}
QrCode qr(m_tx->unsignedTxToBin(), QrCode::Version::AUTO, QrCode::ErrorCorrectionLevel::LOW);
auto *dialog = new QrCodeDialog(this, qr, "Unsigned Transaction");
dialog->exec();
dialog->deleteLater();
QrCodeDialog dialog{this, &qr, "Unsigned Transaction"};
dialog.exec();
}
void TxConfAdvDialog::unsignedCopy() {

View file

@ -165,3 +165,19 @@ QPixmap QrCode::toPixmap(const int margin) const
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;
}

View file

@ -71,6 +71,9 @@ public:
void writeSvg(QIODevice* outputDevice, const int dpi, const int margin = 4) const;
QPixmap toPixmap(const int margin = 4) const;
int width();
unsigned char* data();
private:
void init(const QString& data, const Version version, const ErrorCorrectionLevel ecl, const bool caseSensitive);

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

View 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