Skip to content

Commit e8f821b

Browse files
committed
qml: Add QRImageProvider
1 parent 849d3ae commit e8f821b

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

src/Makefile.qt.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ BITCOIN_QT_H = \
147147
qml/bitcoinamount.h \
148148
qml/guiconstants.h \
149149
qml/imageprovider.h \
150+
qml/qrimageprovider.h \
150151
qml/util.h \
151152
qml/walletqmlcontroller.h \
152153
qt/addressbookpage.h \
@@ -342,6 +343,7 @@ BITCOIN_QML_BASE_CPP = \
342343
qml/models/walletqmlmodel.cpp \
343344
qml/models/walletqmlmodeltransaction.cpp \
344345
qml/imageprovider.cpp \
346+
qml/qrimageprovider.cpp \
345347
qml/util.cpp \
346348
qml/walletqmlcontroller.cpp
347349

src/qml/bitcoin.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <qml/models/walletlistmodel.h>
3636
#include <qml/models/walletqmlmodel.h>
3737
#include <qml/models/walletqmlmodeltransaction.h>
38+
#include <qml/qrimageprovider.h>
3839
#include <qml/util.h>
3940
#include <qml/walletqmlcontroller.h>
4041
#include <qt/guiutil.h>
@@ -313,6 +314,7 @@ int QmlGuiMain(int argc, char* argv[])
313314
QScopedPointer<const NetworkStyle> network_style{NetworkStyle::instantiate(Params().GetChainType())};
314315
assert(!network_style.isNull());
315316
engine.addImageProvider(QStringLiteral("images"), new ImageProvider{network_style.data()});
317+
engine.addImageProvider(QStringLiteral("qr"), new QRImageProvider);
316318

317319
engine.rootContext()->setContextProperty("networkTrafficTower", &network_traffic_tower);
318320
engine.rootContext()->setContextProperty("nodeModel", &node_model);

src/qml/qrimageprovider.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2025 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <qml/qrimageprovider.h>
6+
7+
#include <QImage>
8+
#include <QQuickImageProvider>
9+
#include <QSize>
10+
#include <QString>
11+
#include <QUrl>
12+
#include <QUrlQuery>
13+
14+
#if defined(HAVE_CONFIG_H)
15+
#include <config/bitcoin-config.h> /* for USE_QRCODE */
16+
#endif
17+
18+
#ifdef USE_QRCODE
19+
#include <qrencode.h>
20+
#endif
21+
22+
QRImageProvider::QRImageProvider()
23+
: QQuickImageProvider{QQuickImageProvider::Image}
24+
{
25+
}
26+
27+
QImage QRImageProvider::requestImage(const QString& id, QSize* size, const QSize& requested_size)
28+
{
29+
#ifdef USE_QRCODE
30+
const QUrl url{"image:///" + id};
31+
const QUrlQuery query{url};
32+
const QString data{url.path().mid(1)};
33+
const QColor fg{query.queryItemValue("fg")};
34+
const QColor bg{query.queryItemValue("bg")};
35+
36+
QRcode* code = QRcode_encodeString(data.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1);
37+
38+
if (code) {
39+
QImage image{code->width, code->width, QImage::Format_ARGB32};
40+
unsigned char* p = code->data;
41+
for (int y = 0; y < code->width; ++y) {
42+
for (int x = 0; x < code->width; ++x) {
43+
image.setPixelColor(x, y, (*p & 1) ? fg : bg);
44+
++p;
45+
}
46+
}
47+
QRcode_free(code);
48+
*size = QSize(code->width, code->width);
49+
return image;
50+
}
51+
#endif // USE_QRCODE
52+
QImage pixel{1, 1, QImage::Format_ARGB32};
53+
pixel.setPixelColor(0, 0, QColorConstants::Transparent);
54+
*size = QSize(1, 1);
55+
return pixel;
56+
}

src/qml/qrimageprovider.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2025 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QML_QRIMAGEPROVIDER_H
6+
#define BITCOIN_QML_QRIMAGEPROVIDER_H
7+
8+
#include <QQuickImageProvider>
9+
10+
QT_BEGIN_NAMESPACE
11+
class QImage;
12+
class QSize;
13+
class QString;
14+
QT_END_NAMESPACE
15+
16+
class QRImageProvider : public QQuickImageProvider
17+
{
18+
public:
19+
explicit QRImageProvider();
20+
21+
QImage requestImage(const QString& id, QSize* size, const QSize& requested_size) override;
22+
};
23+
24+
#endif // BITCOIN_QML_QRIMAGEPROVIDER_H

0 commit comments

Comments
 (0)