diff --git a/NameCreater/NameCreater.cpp b/NameCreater/NameCreater.cpp index 463bf02..1b7206b 100644 --- a/NameCreater/NameCreater.cpp +++ b/NameCreater/NameCreater.cpp @@ -11,6 +11,11 @@ #include #include #include +#include +#include +#include +#include + #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="<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,10 +255,19 @@ 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{ QMessageBox msgBox; @@ -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)); +} + + + + diff --git a/NameCreater/NameCreater.h b/NameCreater/NameCreater.h index d1157a4..68245a1 100644 --- a/NameCreater/NameCreater.h +++ b/NameCreater/NameCreater.h @@ -8,6 +8,8 @@ #include + + 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); + }; diff --git a/NameCreater/NameCreater.pro.user b/NameCreater/NameCreater.pro.user index 3192816..c3911f1 100644 --- a/NameCreater/NameCreater.pro.user +++ b/NameCreater/NameCreater.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -8,7 +8,7 @@ ProjectExplorer.Project.ActiveTarget - 1 + 0 ProjectExplorer.Project.EditorSettings @@ -409,7 +409,6 @@ true true true - C:/dev/namecreater/NameCreater/build/Desktop_Qt_5_15_2_MSVC2019_64bit-Debug 1 @@ -578,7 +577,6 @@ true true true - C:/dev/namecreater/NameCreater/build/Desktop_Qt_6_7_2_MSVC2019_64bit-Debug 1 diff --git a/NameCreater/NameCreater.ui b/NameCreater/NameCreater.ui index b3451fd..16e2b0b 100644 --- a/NameCreater/NameCreater.ui +++ b/NameCreater/NameCreater.ui @@ -6,8 +6,8 @@ 0 0 - 946 - 694 + 916 + 816 @@ -474,14 +474,24 @@ color: rgb(255, 255, 255); Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + 380 + 10 + 201 + 21 + + + 0 0 - 946 - 24 + 916 + 21 diff --git a/NameCreater/customlogo.cpp b/NameCreater/customlogo.cpp index b2741ae..d93bf7d 100644 --- a/NameCreater/customlogo.cpp +++ b/NameCreater/customlogo.cpp @@ -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,15 +163,15 @@ void CustomLogo::gerneraImageFromList(const QList& 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; int name_len = name.length(); QPointF pos = NAME_OFFSET_START; qreal y_offset = 0; - if (name_len == 2) { - pos.setY(pos.y() + 82.0 ); + if (name_len == 2) { + pos.setY(pos.y() + 82.0); y_offset = 25; } @@ -169,7 +179,8 @@ void CustomLogo::gerneraImageFromList(const QList& 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 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(); + } } diff --git a/NameCreater/customlogo.h b/NameCreater/customlogo.h index 21ef83d..be1a43a 100644 --- a/NameCreater/customlogo.h +++ b/NameCreater/customlogo.h @@ -31,6 +31,7 @@ protected: private Q_SLOTS: void onClickedBtn(); + void onColorSelected(const QColor& color);