feat: move qr code glue in MSALoginDialog (#3676)

This commit is contained in:
Alexandru Ionut Tripon 2025-05-31 21:40:01 +03:00 committed by GitHub
commit 0a89f5cfaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 55 additions and 85 deletions

4
.gitmodules vendored
View File

@ -19,6 +19,6 @@
[submodule "flatpak/shared-modules"]
path = flatpak/shared-modules
url = https://github.com/flathub/shared-modules.git
[submodule "libraries/qt-qrcodegenerator/QR-Code-generator"]
path = libraries/qt-qrcodegenerator/QR-Code-generator
[submodule "libraries/qrcodegenerator"]
path = libraries/qrcodegenerator
url = https://github.com/nayuki/QR-Code-generator

View File

@ -475,7 +475,6 @@ add_subdirectory(libraries/libnbtplusplus)
add_subdirectory(libraries/systeminfo) # system information library
add_subdirectory(libraries/launcher) # java based launcher part for Minecraft
add_subdirectory(libraries/javacheck) # java compatibility checker
add_subdirectory(libraries/qt-qrcodegenerator) # qr code generator
if(FORCE_BUNDLED_ZLIB)
message(STATUS "Using bundled zlib")
@ -533,6 +532,15 @@ add_subdirectory(libraries/gamemode)
add_subdirectory(libraries/murmur2) # Hash for usage with the CurseForge API
add_subdirectory(libraries/qdcss) # css parser
# qr code generator
set(QRCODE_SOURCES
libraries/qrcodegenerator/cpp/qrcodegen.cpp
libraries/qrcodegenerator/cpp/qrcodegen.hpp
)
add_library(qrcodegenerator STATIC ${QRCODE_SOURCES})
target_include_directories(qrcodegenerator PUBLIC "libraries/qrcodegenerator/cpp/" )
generate_export_header(qrcodegenerator)
############################### Built Artifacts ###############################
add_subdirectory(buildconfig)

View File

@ -404,7 +404,7 @@
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
## qt-qrcodegenerator (`libraries/qt-qrcodegenerator`)
## QR-Code-generator (`libraries/qrcodegenerator`)
Copyright © 2024 Project Nayuki. (MIT License)
https://www.nayuki.io/page/qr-code-generator-library

4
flake.lock generated
View File

@ -32,7 +32,7 @@
"type": "github"
}
},
"qt-qrcodegenerator": {
"qrcodegenerator": {
"flake": false,
"locked": {
"lastModified": 1737616857,
@ -52,7 +52,7 @@
"inputs": {
"libnbtplusplus": "libnbtplusplus",
"nixpkgs": "nixpkgs",
"qt-qrcodegenerator": "qt-qrcodegenerator"
"qrcodegenerator": "qrcodegenerator"
}
}
},

View File

@ -16,7 +16,7 @@
flake = false;
};
qt-qrcodegenerator = {
qrcodegenerator = {
url = "github:nayuki/QR-Code-generator";
flake = false;
};
@ -27,7 +27,7 @@
self,
nixpkgs,
libnbtplusplus,
qt-qrcodegenerator,
qrcodegenerator,
}:
let
@ -175,7 +175,7 @@
prismlauncher-unwrapped = prev.callPackage ./nix/unwrapped.nix {
inherit
libnbtplusplus
qt-qrcodegenerator
qrcodegenerator
self
;
};

View File

@ -1311,7 +1311,7 @@ target_link_libraries(Launcher_logic
qdcss
BuildConfig
Qt${QT_VERSION_MAJOR}::Widgets
qrcode
qrcodegenerator
)
if (UNIX AND NOT CYGWIN AND NOT APPLE)

View File

@ -36,7 +36,6 @@
#include "MSALoginDialog.h"
#include "Application.h"
#include "qr.h"
#include "ui_MSALoginDialog.h"
#include "DesktopServices.h"
@ -44,10 +43,15 @@
#include <QApplication>
#include <QClipboard>
#include <QColor>
#include <QPainter>
#include <QPixmap>
#include <QSize>
#include <QUrl>
#include <QtWidgets/QPushButton>
#include "qrcodegen.hpp"
MSALoginDialog::MSALoginDialog(QWidget* parent) : QDialog(parent), ui(new Ui::MSALoginDialog)
{
ui->setupUi(this);
@ -139,6 +143,33 @@ void MSALoginDialog::authorizeWithBrowser(const QUrl& url)
m_url = url;
}
// https://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c
void paintQR(QPainter& painter, const QSize sz, const QString& data, QColor fg)
{
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.toUtf8().constData(), qrcodegen::QrCode::Ecc::LOW);
const int s = qr.getSize() > 0 ? qr.getSize() : 1;
const double w = sz.width();
const double h = sz.height();
const double aspect = w / h;
const double size = ((aspect > 1.0) ? h : w);
const double scale = size / (s + 2);
// NOTE: For performance reasons my implementation only draws the foreground parts in supplied color.
// It expects background to be prepared already (in white or whatever is preferred).
painter.setPen(Qt::NoPen);
painter.setBrush(fg);
for (int y = 0; y < s; y++) {
for (int x = 0; x < s; x++) {
const int color = qr.getModule(x, y); // 0 for white, 1 for black
if (0 != color) {
const double rx1 = (x + 1) * scale, ry1 = (y + 1) * scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r, 1);
}
}
}
}
void MSALoginDialog::authorizeWithBrowserWithExtra(QString url, QString code, [[maybe_unused]] int expiresIn)
{
ui->stackedWidget->setCurrentIndex(1);

View File

@ -99,7 +99,7 @@ Canonical implementation of the murmur2 hash, taken from [SMHasher](https://gith
Public domain (the author disclaimed the copyright).
## qt-qrcodegenerator
## QR-Code-generator
A simple library for generating QR codes

@ -0,0 +1 @@
Subproject commit 2c9044de6b049ca25cb3cd1649ed7e27aa055138

View File

@ -1,32 +0,0 @@
cmake_minimum_required(VERSION 3.6)
project(qrcode)
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_C_STANDARD_REQUIRED true)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 11)
if(QT_VERSION_MAJOR EQUAL 5)
find_package(Qt5 COMPONENTS Core Gui REQUIRED)
elseif(Launcher_QT_VERSION_MAJOR EQUAL 6)
find_package(Qt6 COMPONENTS Core Gui Core5Compat REQUIRED)
list(APPEND systeminfo_LIBS Qt${QT_VERSION_MAJOR}::Core5Compat)
endif()
add_library(qrcode STATIC qr.h qr.cpp QR-Code-generator/cpp/qrcodegen.cpp QR-Code-generator/cpp/qrcodegen.hpp )
target_link_libraries(qrcode Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui ${systeminfo_LIBS})
# needed for statically linked qrcode in shared libs on x86_64
set_target_properties(qrcode
PROPERTIES POSITION_INDEPENDENT_CODE TRUE
)
target_include_directories(qrcode PUBLIC ./ PRIVATE QR-Code-generator/cpp/)

@ -1 +0,0 @@
Subproject commit f40366c40d8d1956081f7ec643d240c02a81df52

View File

@ -1,29 +0,0 @@
#include "qr.h"
#include "qrcodegen.hpp"
void paintQR(QPainter& painter, const QSize sz, const QString& data, QColor fg)
{
// NOTE: At this point you will use the API to get the encoding and format you want, instead of my hardcoded stuff:
qrcodegen::QrCode qr = qrcodegen::QrCode::encodeText(data.toUtf8().constData(), qrcodegen::QrCode::Ecc::LOW);
const int s = qr.getSize() > 0 ? qr.getSize() : 1;
const double w = sz.width();
const double h = sz.height();
const double aspect = w / h;
const double size = ((aspect > 1.0) ? h : w);
const double scale = size / (s + 2);
// NOTE: For performance reasons my implementation only draws the foreground parts in supplied color.
// It expects background to be prepared already (in white or whatever is preferred).
painter.setPen(Qt::NoPen);
painter.setBrush(fg);
for (int y = 0; y < s; y++) {
for (int x = 0; x < s; x++) {
const int color = qr.getModule(x, y); // 0 for white, 1 for black
if (0 != color) {
const double rx1 = (x + 1) * scale, ry1 = (y + 1) * scale;
QRectF r(rx1, ry1, scale, scale);
painter.drawRects(&r, 1);
}
}
}
}

View File

@ -1,8 +0,0 @@
#pragma once
#include <QColor>
#include <QPainter>
#include <QSize>
// https://stackoverflow.com/questions/21400254/how-to-draw-a-qr-code-with-qt-in-native-c-c
void paintQR(QPainter& painter, const QSize sz, const QString& data, QColor fg);

View File

@ -9,7 +9,7 @@
jdk17,
kdePackages,
libnbtplusplus,
qt-qrcodegenerator,
qrcodegenerator,
ninja,
self,
stripJavaArchivesHook,
@ -64,8 +64,8 @@ stdenv.mkDerivation {
rm -rf source/libraries/libnbtplusplus
ln -s ${libnbtplusplus} source/libraries/libnbtplusplus
rm -rf source/libraries/qt-qrcodegenerator/QR-Code-generator
ln -s ${qt-qrcodegenerator} source/libraries/qt-qrcodegenerator/QR-Code-generator
rm -rf source/libraries/qrcodegenerator
ln -s ${qrcodegenerator} source/libraries/qrcodegenerator
'';
nativeBuildInputs = [