Skip to content

Commit

Permalink
added the QT image provider as a library
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyTheCo committed Aug 28, 2022
1 parent 57fefef commit 97ed122
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 19 deletions.
33 changes: 17 additions & 16 deletions qrGen/CMakeLists.txt → QrGen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

#project(qrCode VERSION 0.1 DESCRIPTION "library for qr codes" LANGUAGES CXX)
project(QrCodeGenLib VERSION 0.1 DESCRIPTION "library for qr codes generation" LANGUAGES CXX)


set(default_build_type "Release")
Expand All @@ -17,42 +17,43 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_CXX_EXTENSIONS OFF)


add_library(qrGen src/qrcodegen.cpp src/utils.cpp include/qrcodegen.hpp)
add_library(QrGen src/qrcodegen.cpp src/utils.cpp include/qrcodegen.hpp)

set_target_properties(QrGen PROPERTIES POSITION_INDEPENDENT_CODE ON)

target_compile_features(qrGen PUBLIC cxx_std_11)
target_compile_features(QrGen PUBLIC cxx_std_11)



target_include_directories(qrGen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
target_include_directories(QrGen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
"$<INSTALL_INTERFACE:include>")

install(TARGETS qrGen EXPORT qrGenTargets DESTINATION lib)
install(TARGETS QrGen EXPORT QrGenTargets DESTINATION lib)
install(DIRECTORY include/ DESTINATION include/)

install(EXPORT qrGenTargets
FILE qrGenTargets.cmake
install(EXPORT QrGenTargets
FILE QrGenTargets.cmake
NAMESPACE qr::
DESTINATION lib/cmake/qrGen
DESTINATION lib/cmake/QrGen
)
include(CMakePackageConfigHelpers)
# generate the config file that is includes the exports
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/qrGenConfig.cmake"
INSTALL_DESTINATION "lib/cmake/qrGen"
"${CMAKE_CURRENT_BINARY_DIR}/QrGenConfig.cmake"
INSTALL_DESTINATION "lib/cmake/QrGen"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/qrGenConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/QrGenConfigVersion.cmake"
VERSION "0.1.1.0"
COMPATIBILITY AnyNewerVersion
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/qrGenConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/qrGenConfigVersion.cmake
DESTINATION lib/cmake/qrGen
${CMAKE_CURRENT_BINARY_DIR}/QrGenConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/QrGenConfigVersion.cmake
DESTINATION lib/cmake/QrGen
)
export(EXPORT qrGenTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/qrGenTargets.cmake"
export(EXPORT QrGenTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/QrGenTargets.cmake"
)
3 changes: 3 additions & 0 deletions QrGen/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include ( "${CMAKE_CURRENT_LIST_DIR}/QrGenTargets.cmake" )
14 changes: 14 additions & 0 deletions QrGen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# QrGen

The code is based on [QR Code generator library](https://github.com/nayuki/QR-Code-generator) and produce a library for the generation of a QR code of certain data.
CMake produce the target 'QrGen' so one can link to this library like
```
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> QrGen)
```







File renamed without changes.
1 change: 1 addition & 0 deletions qrGen/src/qrcodegen.cpp → QrGen/src/qrcodegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ int QrCode::getFormatBits(Ecc ecl) {


QrCode QrCode::encodeText(const char *text, Ecc ecl) {

vector<QrSegment> segs = QrSegment::makeSegments(text);
return encodeSegments(segs, ecl);
}
Expand Down
File renamed without changes.
58 changes: 58 additions & 0 deletions QtQrGen/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cmake_minimum_required(VERSION 3.16)
project(qmlqr LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
include(local_conf.cmake OPTIONAL)

find_package(Qt6 REQUIRED COMPONENTS Core Gui Qml Quick Core5Compat Svg)

qt_add_library(QtQrGen Qrimageprovider.cpp include/Qrimageprovider.hpp)

set_target_properties(QtQrGen PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)
target_include_directories(QtQrGen PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
"$<INSTALL_INTERFACE:include>")
target_link_libraries(QtQrGen PUBLIC
Qt6::Quick
)
target_link_libraries(QtQrGen PRIVATE
QrGen
Qt6::Core
Qt6::Gui
Qt6::Qml
Qt6::Quick
Qt6::Core5Compat
Qt6::Svg
)

install(TARGETS QtQrGen EXPORT QtQrGenTargets LIBRARY DESTINATION lib)
install(DIRECTORY include/ DESTINATION include/)

install(EXPORT QtQrGenTargets
FILE QtQrGenTargets.cmake
NAMESPACE qr::
DESTINATION lib/cmake/QtQrGen
)
include(CMakePackageConfigHelpers)
# generate the config file that is includes the exports
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
"${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfig.cmake"
INSTALL_DESTINATION "lib/cmake/QtQrGen"
NO_SET_AND_CHECK_MACRO
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfigVersion.cmake"
VERSION "0.1.1.0"
COMPATIBILITY AnyNewerVersion
)
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/QtQrGenConfigVersion.cmake
DESTINATION lib/cmake/QtQrGen
)
export(EXPORT QtQrGenTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/QtQrGenTargets.cmake"
)
3 changes: 3 additions & 0 deletions QtQrGen/Config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@PACKAGE_INIT@

include ( "${CMAKE_CURRENT_LIST_DIR}/QtQrGenTargets.cmake" )
30 changes: 30 additions & 0 deletions QtQrGen/Qrimageprovider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <QPainter>
#include <QStringRef>
#include <QSvgRenderer>
#include "qrcodegen.hpp"
#include "Qrimageprovider.hpp"
#include <QDebug>
using namespace qrcodegen;

QPixmap QRImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
{
const int width = 100;

const auto max=(requestedSize.width()>requestedSize.height())?requestedSize.width():requestedSize.height();

QPixmap pixmap(max > 0 ? max : width,
max> 0 ? max : width);

*size = pixmap.size();
const QrCode qr = QrCode::encodeText(id.toStdString().c_str(), static_cast<QrCode::Ecc>(errC));
const auto qrSVGstr=toSvgString(qr, color.name().toStdString());

auto qrImage=QSvgRenderer(QByteArray::fromStdString(qrSVGstr));
QPainter Painter;

Painter.begin(&pixmap);
qrImage.render(&Painter);
Painter.end();
return pixmap;
}

43 changes: 43 additions & 0 deletions QtQrGen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#QtQrGen

This code produce a library with a custom ImageProvider of Qt. The image provider print a QRCODE from a string.
CMake produce the target 'QtQrGen' so one can link to this library like
```
target_link_libraries(<target> <PRIVATE|PUBLIC|INTERFACE> QtQrGen)
```


## Showing the QRCODE on QML aplications will be simple as

```
Image {
sourceSize.width: 300
source: "image://qrcode/https://eddytheco.github.io/"
}
```

For this to work one has to add the custom ImageProvider to the QML engine like in the following main.cpp
```
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include "Qrimageprovider.hpp"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.addImageProvider(QLatin1String("qrCode"), new QRImageProvider("blue",1));
const QUrl url(u"qrc:/app/main.qml"_qs);
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
```


22 changes: 22 additions & 0 deletions QtQrGen/include/Qrimageprovider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <qquickimageprovider.h>




class QRImageProvider : public QQuickImageProvider
{
public:
QRImageProvider(QColor col="black",int erc=0)
: QQuickImageProvider(QQuickImageProvider::Pixmap),color(col),errC(erc)
{

}

QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override;
private:
const QColor color;
const int errC;
};



3 changes: 0 additions & 3 deletions qrGen/Config.cmake.in

This file was deleted.

0 comments on commit 97ed122

Please sign in to comment.