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
14 changed files with 55 additions and 85 deletions

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