-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
251 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "QCloudMusicApi/openssl-cmake"] | ||
path = QCloudMusicApi/openssl-cmake | ||
url = https://github.com/janbar/openssl-cmake.git | ||
[submodule "Test/libqrencode"] | ||
path = Test/libqrencode | ||
url = https://github.com/fukuchi/libqrencode.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef TABCOMMON_H | ||
#define TABCOMMON_H | ||
|
||
#include <QWidget> | ||
|
||
namespace Ui { | ||
class TabCommon; | ||
} | ||
|
||
class TabCommon : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit TabCommon(QWidget *parent = nullptr); | ||
~TabCommon(); | ||
|
||
private: | ||
Ui::TabCommon *ui; | ||
}; | ||
|
||
#endif // TABCOMMON_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include "tablogin_qr.h" | ||
#include "ui_tablogin_qr.h" | ||
|
||
#include "../../QCloudMusicApi/module.h" | ||
|
||
#include "../libqrencode/qrencode.h" | ||
|
||
#include <QJsonDocument> | ||
#include <QLabel> | ||
#include <QPainter> | ||
|
||
TabLogin_qr::TabLogin_qr(QWidget *parent) : | ||
QWidget(parent), | ||
ui(new Ui::TabLogin_qr) | ||
{ | ||
ui->setupUi(this); | ||
} | ||
|
||
TabLogin_qr::~TabLogin_qr() | ||
{ | ||
delete ui; | ||
} | ||
QPixmap TabLogin_qr::generateQRCode(QString strUrl, qint32 temp_width, qint32 temp_height, int offset) { | ||
QPixmap mainmap; | ||
if (strUrl.isEmpty()) { | ||
return mainmap; | ||
} | ||
// 调用 qrencode 的 API,生成 QR 码的位图数组 | ||
QRcode *qrcode = QRcode_encodeString(strUrl.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1); | ||
// 获取 QR 码的宽度 | ||
qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1; | ||
// 计算缩放比例 | ||
double scale_x = (double)temp_width / (double)qrcode_width; | ||
double scale_y = (double)temp_height / (double)qrcode_width; | ||
|
||
QImage mainimg = QImage(temp_width + offset * 2, temp_height + offset * 2, QImage::Format_ARGB32); | ||
QPainter painter(&mainimg); | ||
// 设置背景色为白色 | ||
QColor background(Qt::white); | ||
painter.setBrush(background); | ||
painter.setPen(Qt::NoPen); | ||
painter.drawRect(offset, offset, temp_width, temp_height); | ||
// 设置前景色为黑色 | ||
QColor foreground(Qt::black); | ||
painter.setBrush(foreground); | ||
// 遍历 QR 码的位图数组,绘制黑色的小方块 | ||
for (qint32 y = 0; y < qrcode_width; y++) { | ||
for (qint32 x = 0; x < qrcode_width; x++) { | ||
unsigned char b = qrcode->data[y * qrcode_width + x]; | ||
if (b & 0x01) { | ||
QRectF r(offset + x * scale_x, offset + y * scale_y, scale_x, scale_y); | ||
painter.drawRects(&r, 1); | ||
} | ||
} | ||
} | ||
mainmap = QPixmap::fromImage(mainimg); | ||
return mainmap; | ||
} | ||
|
||
QVariantMap TabLogin_qr::invoke(const QString funName, const QVariantMap arg) { | ||
NeteaseCloudMusicApi api; | ||
QVariantMap ret; | ||
QMetaObject::invokeMethod(&api, funName.toUtf8() | ||
, Qt::DirectConnection | ||
, Q_RETURN_ARG(QVariantMap, ret) | ||
, Q_ARG(QVariantMap, arg)); | ||
return ret; | ||
} | ||
|
||
void TabLogin_qr::on_pushButton_login_qr_create_clicked() | ||
{ | ||
QVariantMap ret = invoke("login_qr_key", {}); | ||
unikey = ret["body"].toMap()["data"].toMap()["unikey"].toString(); | ||
if (unikey.isEmpty()) { | ||
return; | ||
} | ||
ret = invoke("login_qr_create", | ||
{ | ||
{ "key", unikey } | ||
}); | ||
QString qrurl = ret["body"].toMap()["data"].toMap()["qrurl"].toString(); | ||
int size = qMin(ui->label->width(), ui->label->height()); | ||
ui->label->setPixmap(generateQRCode(qrurl, size, size, 0)); | ||
} | ||
|
||
|
||
void TabLogin_qr::on_pushButton_login_qr_check_clicked() | ||
{ | ||
QVariantMap ret = invoke("login_qr_check", | ||
{ | ||
{ "key", unikey } | ||
}); | ||
ui->textEdit_ret->setText(QJsonDocument::fromVariant(ret).toJson(QJsonDocument::Indented)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef TABLOGIN_QR_H | ||
#define TABLOGIN_QR_H | ||
|
||
#include <QWidget> | ||
|
||
QT_BEGIN_NAMESPACE | ||
namespace Ui { | ||
class TabLogin_qr; | ||
} | ||
QT_END_NAMESPACE | ||
|
||
class TabLogin_qr : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit TabLogin_qr(QWidget *parent = nullptr); | ||
~TabLogin_qr(); | ||
|
||
private: | ||
|
||
/** | ||
* @brief 反射调用API中的方法 | ||
* @param funName 函数名称 | ||
* @param arg 参数 | ||
* @return QVariantMap 返回的数据 | ||
*/ | ||
QVariantMap invoke(const QString funName, const QVariantMap arg); | ||
|
||
QPixmap generateQRCode(QString strUrl, qint32 temp_width, qint32 temp_height, int offset); | ||
|
||
private slots: | ||
void on_pushButton_login_qr_create_clicked(); | ||
|
||
void on_pushButton_login_qr_check_clicked(); | ||
|
||
private: | ||
Ui::TabLogin_qr *ui; | ||
QString unikey; | ||
}; | ||
|
||
#endif // TABLOGIN_QR_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>TabLogin_qr</class> | ||
<widget class="QWidget" name="TabLogin_qr"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>399</width> | ||
<height>365</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>Form</string> | ||
</property> | ||
<layout class="QHBoxLayout" name="horizontalLayout"> | ||
<item> | ||
<widget class="QGroupBox" name="groupBox_2"> | ||
<property name="title"> | ||
<string>Result</string> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout_3"> | ||
<item> | ||
<widget class="QTextEdit" name="textEdit_ret"/> | ||
</item> | ||
</layout> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QGroupBox" name="groupBox"> | ||
<property name="title"> | ||
<string>Arg</string> | ||
</property> | ||
<layout class="QVBoxLayout" name="verticalLayout_2"> | ||
<item> | ||
<layout class="QVBoxLayout" name="verticalLayout"> | ||
<item> | ||
<widget class="QLabel" name="label"> | ||
<property name="text"> | ||
<string>QR Code</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="pushButton_login_qr_check"> | ||
<property name="text"> | ||
<string>login_qr_check</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item> | ||
<widget class="QPushButton" name="pushButton_login_qr_create"> | ||
<property name="text"> | ||
<string>login_qr_key login_qr_create</string> | ||
</property> | ||
<property name="autoRepeat"> | ||
<bool>false</bool> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</item> | ||
</layout> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters