1. 增加動態字形 2. fix color dialog issue in webassembly.

This commit is contained in:
shouchih_chen 2024-09-27 10:17:26 +08:00
parent 40d41b36e2
commit 7aa79287c8
6 changed files with 147 additions and 27 deletions

View File

@ -11,6 +11,11 @@
#include <QDateTime>
#include <QBuffer>
#include <QRegularExpression>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QComboBox>
#include "FiveToSingle.h"
#include "BirthdayCreater.h"
@ -26,7 +31,9 @@
#define FONT_SIZE 120
#define FONT_CHT_URL "https://bazaar1688.ddns.net/font/TaiwanPearl-SemiBold.ttf"
//#define FONT_CHT_URL "https://bazaar1688.ddns.net/font/TaiwanPearl-SemiBold.ttf"
#define FONT_JSON_URL "https://bazaar1688.ddns.net/font/fonts.json"
//#define FONT_CHT_URL "http://45.32.51.135/font/TaiwanPearl-SemiBold.ttf"
@ -46,15 +53,61 @@ NameCreater::NameCreater(QWidget *parent)
connect(ui.customlogo_btn, &QPushButton::released, this , &NameCreater::OnClickedCustomLogoBtn);
connect(ui.customlogo2_btn, &QPushButton::released, this, &NameCreater::OnClickedCustomLogo2Btn);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
connect(ui.font_comboBox, &QComboBox::currentTextChanged, this, &NameCreater::OnFontComboBoxTextChanged);
#else
connect(ui.font_comboBox, &QComboBox::currentIndexChanged, this, &NameCreater::OnFontComboBoxIndexChanged);
#endif
//qDebug()<<"QSslSocket="<<QSslSocket::sslLibraryBuildVersionString();
connect(&network_mgr, &QNetworkAccessManager::finished, this, &NameCreater::onFontDownloadFinished);
network_mgr.get(QNetworkRequest(QUrl(FONT_CHT_URL)));
;
query(QUrl(FONT_JSON_URL));
}
void NameCreater::parseFontJson(const QByteArray& data)
{
QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
if (jsonDoc.isNull()) {
qDebug() << "Failed to create JSON doc.";
return;
}
if (jsonDoc.isObject()) {
QJsonObject jsonObj = jsonDoc.object();
// Process the JSON object
qDebug() << "JSON Object:" << jsonObj;
}
else if (jsonDoc.isArray()) {
ui.font_comboBox->clear();
QJsonArray jsonArray = jsonDoc.array();
foreach(auto val, jsonArray) {
auto obj = val.toObject();
QString n = obj["font_name"].toString();
QString u = obj["font_url"].toString();
ui.font_comboBox->addItem(n, u);
}
// Process the JSON array
qDebug() << "JSON Array:" << jsonArray;
}
}
void NameCreater::query(const QUrl& url)
{
QNetworkRequest req(url);
network_mgr.get(req);
}
void NameCreater::loadFont(const QByteArray& fontdata){
int id = QFontDatabase::addApplicationFontFromData(fontdata);
@ -116,6 +169,8 @@ void NameCreater::loadFont(const QByteArray& fontdata){
tital->setFont(tital_font);
}
qDebug() << "Load Font Succeed!";
}
@ -145,6 +200,7 @@ void NameCreater::OnClickedReadBtn() {
void NameCreater::execCreater(QObject* sender, const QString& fileName, const QByteArray& fileContent) {
// Use fileName and fileContent
if(sender == ui.fivesingle_btn) FiveToSingle().generaImageFromCSV(Name_Type_e::Name_zh,fileName, fileContent, this->font, this->font_color);
@ -176,6 +232,8 @@ void NameCreater::execCreater(QObject* sender, const QString& fileName, const QB
}
void NameCreater::OnClickedCustomLogoBtn() {
QEventLoop loop;
CustomLogo widget(&this->font);
@ -197,9 +255,18 @@ void NameCreater::OnClickedCustomLogo2Btn(){
void NameCreater::onFontDownloadFinished(QNetworkReply *reply){
if(reply->error() == QNetworkReply::NoError){
//do somthine
loadFont(reply->readAll());
qDebug()<<"Load Font Succeed!";
QString url = reply->request().url().toString();
qDebug() << "req url:" << url;
if (url.contains("fonts.json")) {
qDebug() << "query fonts json succeed";
parseFontJson(reply->readAll());
}
else if(url.contains(".ttf") || url.contains(".otf")) {
qDebug() << "query font family succeed";
loadFont(reply->readAll());
}
}else{
@ -213,3 +280,21 @@ void NameCreater::onFontDownloadFinished(QNetworkReply *reply){
}
}
void NameCreater::OnFontComboBoxTextChanged(const QString& str)
{
QComboBox* sender = (QComboBox*)QObject::sender();
QString u = sender->currentData().toString();
query(QUrl(u));
}
void NameCreater::OnFontComboBoxIndexChanged(int idx)
{
QComboBox* sender = (QComboBox*)QObject::sender();
QString u = sender->currentData().toString();
query(QUrl(u));
}

View File

@ -8,6 +8,8 @@
#include <QNetworkReply>
class NameCreater : public QMainWindow
{
Q_OBJECT
@ -21,15 +23,20 @@ private:
QColor font_color;
QNetworkAccessManager network_mgr;
void query(const QUrl& url);
void loadFont(const QByteArray& fontdata);
void execCreater(QObject*sender, const QString&fileName, const QByteArray &fileContent);
void parseFontJson(const QByteArray& data);
private Q_SLOTS:
void OnClickedReadBtn();
void OnClickedCustomLogoBtn();
void OnClickedCustomLogo2Btn();
void onFontDownloadFinished(QNetworkReply *reply);
void OnFontComboBoxTextChanged(const QString& str);
void OnFontComboBoxIndexChanged(int idx);
};

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 14.0.1, 2024-09-20T13:23:45. -->
<!-- Written by QtCreator 14.0.1, 2024-09-27T09:47:08. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
@ -8,7 +8,7 @@
</data>
<data>
<variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="qlonglong">1</value>
<value type="qlonglong">0</value>
</data>
<data>
<variable>ProjectExplorer.Project.EditorSettings</variable>
@ -409,7 +409,6 @@
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/dev/namecreater/NameCreater/build/Desktop_Qt_5_15_2_MSVC2019_64bit-Debug</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>
@ -578,7 +577,6 @@
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseLibrarySearchPath">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
<value type="QString" key="RunConfiguration.WorkingDirectory.default">C:/dev/namecreater/NameCreater/build/Desktop_Qt_6_7_2_MSVC2019_64bit-Debug</value>
</valuemap>
<value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap>

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>946</width>
<height>694</height>
<width>916</width>
<height>816</height>
</rect>
</property>
<property name="font">
@ -474,14 +474,24 @@ color: rgb(255, 255, 255);
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
<widget class="QComboBox" name="font_comboBox">
<property name="geometry">
<rect>
<x>380</x>
<y>10</y>
<width>201</width>
<height>21</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>946</width>
<height>24</height>
<width>916</width>
<height>21</height>
</rect>
</property>
</widget>

View File

@ -22,6 +22,7 @@ static QPointF NAME_OFFSET_START = QPointF(79, 377);
static qreal EACH_WORD_OFFSET = 142;
static QColor FONT_COLOR = QColor(0, 0, 0);
static QColorDialog* s_colorDialog = nullptr;
class QGraphicsCloneTextItem : public QGraphicsTextItem {
@ -57,9 +58,18 @@ CustomLogo::CustomLogo(QFont* font, QDialog* parent) :
connect(ui->load_list_btn, &QPushButton::released, this, &CustomLogo::onClickedBtn);
connect(ui->color_btn, &QPushButton::released, this, &CustomLogo::onClickedBtn);
QFont _f(*font);
_f.setPixelSize(20);
QPalette palette = ui->color_label->palette();
palette.setColor(QPalette::WindowText, fontColor);
ui->color_label->setPalette(palette);
ui->color_label->setFont(_f);
ui->color_btn->setFont(_f);
ui->double_checkbox->setFont(_f);
}
CustomLogo::~CustomLogo()
@ -153,7 +163,7 @@ void CustomLogo::gerneraImageFromList(const QList<NameInfo_t>& nameList, Name_Ty
msgBox.setText(QString().asprintf("Name is Empty at %d", export_times + 1));
msgBox.exec();
}
QGraphicsRectItem* name_rectItem = new QGraphicsRectItem(QRectF(0, 0, name_bgItem->boundingRect().width(), name_bgItem->boundingRect().height()/2), name_bgItem);
QGraphicsRectItem* name_rectItem = new QGraphicsRectItem(QRectF(0, 0, name_bgItem->boundingRect().width(), name_bgItem->boundingRect().height() / 2), name_bgItem);
name_rectItem->setPen(QPen(QColor(0, 0, 0, 0)));
QString name = name_info.name1;
@ -161,7 +171,7 @@ void CustomLogo::gerneraImageFromList(const QList<NameInfo_t>& nameList, Name_Ty
QPointF pos = NAME_OFFSET_START;
qreal y_offset = 0;
if (name_len == 2) {
pos.setY(pos.y() + 82.0 );
pos.setY(pos.y() + 82.0);
y_offset = 25;
}
@ -169,7 +179,8 @@ void CustomLogo::gerneraImageFromList(const QList<NameInfo_t>& nameList, Name_Ty
QGraphicsCloneTextItem* textItem = new QGraphicsCloneTextItem(name.mid(k, 1), name_rectItem);
textItem->setFont(*customFont);
textItem->setDefaultTextColor(fontColor);
textItem->setPos(pos.x(), pos.y() + k * EACH_WORD_OFFSET + y_offset);
qreal _posX = name_rectItem->boundingRect().width() / 2 - textItem->boundingRect().width() / 2;
textItem->setPos(_posX, pos.y() + k * EACH_WORD_OFFSET + y_offset);
}
@ -253,6 +264,16 @@ QList<NameInfo_t> CustomLogo::getNameByFileContent(Name_Type_e type, const QByte
return nameList;
}
void CustomLogo::onColorSelected(const QColor& color)
{
qDebug() << "color accept: r:" << color.red() << ", g:" << color.green() <<", b:" << color.blue();
fontColor = QColor(color);
QPalette palette = ui->color_label->palette();
palette.setColor(QPalette::WindowText, fontColor);
ui->color_label->setPalette(palette);
}
void CustomLogo::onClickedBtn() {
auto sender = (QPushButton*)QObject::sender();
@ -265,14 +286,12 @@ void CustomLogo::onClickedBtn() {
}
else if (sender == ui->color_btn) {
QColor color = QColorDialog::getColor(Qt::red, this);
if (color.isValid()) {
fontColor = color;
QPalette palette = ui->color_label->palette();
palette.setColor(QPalette::WindowText, color);
ui->color_label->setPalette(palette);
if (s_colorDialog == nullptr) {
s_colorDialog = new QColorDialog(ui->color_label->palette().color(QPalette::WindowText), this);
connect(s_colorDialog, &QColorDialog::colorSelected, this, &CustomLogo::onColorSelected);
}
s_colorDialog->open();
}
}

View File

@ -31,6 +31,7 @@ protected:
private Q_SLOTS:
void onClickedBtn();
void onColorSelected(const QColor& color);