]> Nutra Git (v1) - gamesguru/feather.git/commitdiff
Resizeable QrCode
authortobtoht <thotbot@protonmail.com>
Sat, 3 Jul 2021 22:06:24 +0000 (00:06 +0200)
committertobtoht <thotbot@protonmail.com>
Sat, 3 Jul 2021 22:06:24 +0000 (00:06 +0200)
src/ReceiveWidget.cpp
src/dialog/QrCodeDialog.cpp
src/dialog/QrCodeDialog.h
src/dialog/QrCodeDialog.ui
src/dialog/TxConfAdvDialog.cpp
src/qrcode/QrCode.cpp
src/qrcode/QrCode.h
src/widgets/QrCodeWidget.cpp [new file with mode: 0644]
src/widgets/QrCodeWidget.h [new file with mode: 0644]

index 2dbeba5269dd460b32656d7301659e58e4d30eac..477e51cf4355e53692a0fea9958a8a5abae15c7e 100644 (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() {
index 5cbc442b33352a2b879f4783e8165ab773eb1378..4b1c7cf01f2975c595d4386b08980280e3dc7da1 100644 (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() {
index 1420f9b8d2ff1cddcb538cdac6492019696d54ec..706b120b47f32a383ecb04fb27cbf144c78d877f 100644 (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
index c160dc98d281c0f9d27061f57e2e9b855966aa0d..50a47905ca338a7a5246acfe6f5c79ff7dc0dfc2 100644 (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">
   </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>
    </item>
   </layout>
  </widget>
+ <customwidgets>
+  <customwidget>
+   <class>QrCodeWidget</class>
+   <extends>QWidget</extends>
+   <header>widgets/QrCodeWidget.h</header>
+   <container>1</container>
+  </customwidget>
+ </customwidgets>
  <resources/>
  <connections/>
 </ui>
index ea88dc15171a1fb06a63aded1885b30ffc555c5d..fda8f53a13db9d649edb37193345924a6cdccff4 100644 (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() {
index 6769540c746a08db5af6741a0dde68bfa891a239..7326826e57cbc1f4e7b6096861b25d83c8ee0bd7 100644 (file)
@@ -164,4 +164,20 @@ QPixmap QrCode::toPixmap(const int margin) const
     painter.end();
 
     return pixmap;
-}
\ No newline at end of file
+}
+
+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;
+}
index 50173b44d8c793effb89efc08fd811b0f7aa3c3e..879be77f2ade147665c588e7e93ab114827e228a 100644 (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);
 
diff --git a/src/widgets/QrCodeWidget.cpp b/src/widgets/QrCodeWidget.cpp
new file mode 100644 (file)
index 0000000..e82902b
--- /dev/null
@@ -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);
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/src/widgets/QrCodeWidget.h b/src/widgets/QrCodeWidget.h
new file mode 100644 (file)
index 0000000..855936e
--- /dev/null
@@ -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