#include "def.h" #include "NameCreater.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "FiveToSingle.h" #include "BirthdayCreater.h" #include "TowelCreater.h" #include "NewFiveCreater.h" #include "HKHolderCreater.h" #include "customlogo.h" #include "customlogo2.h" #define TOWEL_FONT_PATH ":/NameCreater/_exp/W1.ttc" #define ENGLISH_FONT_PATH ":/NameCreater/_exp/FontsFree-Net-Acumin-Pro-Semibold.ttf" #define FONT_SIZE 120 //#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" NameCreater::NameCreater(QWidget* parent) : QMainWindow(parent) { ui.setupUi(this); ui.scrollAreaWidgetContents->setMinimumSize(ui.frame->size()); connect(ui.fivesingle_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); //connect(ui.fiveold_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.birthday_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.towel_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.fivenew_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.birthday_zh_eng_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.birthday_eng_eng_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.handkerchief_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); connect(ui.fivesingle_zh_eng_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); 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, int type) { QNetworkRequest req(url); req.setAttribute(QNetworkRequest::User, QVariant(type)); network_mgr.get(req); } void NameCreater::loadFont(const QByteArray& fontdata) { int id = QFontDatabase::addApplicationFontFromData(fontdata); QStringList family_list = QFontDatabase::applicationFontFamilies(id); qDebug() << "font family list:" << family_list; QString cht_fontname = family_list.at(0); font = QFont(cht_fontname); font.setPixelSize(FONT_SIZE); QString font_path = QString(TOWEL_FONT_PATH); QFile font_res_towel(font_path); if (!font_res_towel.open(QIODevice::ReadOnly)) { QMessageBox msgBox; msgBox.setText("Can not load font file!" + font_path); msgBox.exec(); font_res_towel.close(); } id = QFontDatabase::addApplicationFontFromData(font_res_towel.readAll()); font_towel = QFont(QFontDatabase::applicationFontFamilies(id).at(0)); font_towel.setPixelSize(240); font_towel.setLetterSpacing(QFont::PercentageSpacing, 42); font_path = QString(ENGLISH_FONT_PATH); QFile font_res_eng(font_path); if (!font_res_eng.open(QIODevice::ReadOnly)) { QMessageBox msgBox; msgBox.setText("Can not load font file!" + font_path); msgBox.exec(); font_res_eng.close(); } id = QFontDatabase::addApplicationFontFromData(font_res_eng.readAll()); font_english = QFont(QFontDatabase::applicationFontFamilies(id).at(0)); font_color = QColor(0, 0, 0); QFont tital_font = QFont(font); tital_font.setPixelSize(20); QFont version_font = QFont(font); version_font.setPixelSize(12); //ui.tital_label->setFont(tital_font); char c_font_name[64] = { 0 }; sprintf(c_font_name, "%s", cht_fontname.toStdString().c_str()); ui.font_name_label->setFont(version_font); ui.font_name_label->setText(QString::fromLocal8Bit(c_font_name)); ui.version_label->setFont(version_font); ui.version_label->setText(VERSION); ui.font_comboBox->setFont(version_font); QList titals = findChildren(QRegularExpression("tital_label")); foreach(auto tital, titals) { tital->setFont(tital_font); } qDebug() << "Load Font Succeed!"; } void NameCreater::OnClickedReadBtn() { auto sender = (QPushButton*)QObject::sender(); #ifdef Q_OS_WASM auto fileContentReady = [this, sender](const QString& fileName, const QByteArray& fileContent) { if (!fileName.isEmpty()) { this->execCreater(sender, fileName, fileContent); } }; QFileDialog::getOpenFileContent("CSV (*.csv)", fileContentReady); #else QString filename = QFileDialog::getOpenFileName(nullptr, "Open Name File", QDir::currentPath(), "CSV file(*.csv)"); if (!filename.isEmpty()) { QFile _f(filename); if (_f.open(QIODevice::ReadOnly)) { execCreater(sender, filename, _f.readAll()); } } #endif } 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); if (sender == ui.birthday_btn) BirthdayCreater().generaImageFromCSV(Name_Type_e::Name_zh, fileName, fileContent, this->font, this->font_color); if (sender == ui.fivenew_btn) NewFiveCreater().generaImageFromCSV(Name_Type_e::Name_zh, fileName, fileContent, this->font, this->font_color); if (sender == ui.towel_btn) TowelCreater().generaImageFromCSV(Name_Type_e::Name_zh, fileName, fileContent, this->font_towel, this->font_color); if (sender == ui.birthday_zh_eng_btn) { BirthdayCreater creater; creater.setEnglishFont(font_english); creater.generaImageFromCSV(Name_Type_e::Name_zh_eng, fileName, fileContent, this->font, this->font_color); } if (sender == ui.birthday_eng_eng_btn) { BirthdayCreater b; b.setEnglishFont(font_english); b.generaImageFromCSV(Name_Type_e::Name_eng, fileName, fileContent, this->font, this->font_color); } if (sender == ui.handkerchief_btn) { QFont newFont(font); newFont.setPixelSize(78); QColor newColor(0, 0, 0); HKHolderCreater().generaImageFromCSV(Name_Type_e::Name_zh, fileName, fileContent, newFont, newColor); } if (sender == ui.fivesingle_zh_eng_btn) { FiveToSingle f; f.setEnglishFont(this->font_english); f.generaImageFromCSV(Name_Type_e::Name_zh_eng, fileName, fileContent, this->font, this->font_color); return; } } void NameCreater::OnClickedCustomLogoBtn() { QEventLoop loop; CustomLogo widget(&this->font); widget.show(); connect(&widget, &CustomLogo::finished, &loop, &QEventLoop::quit); loop.exec(); qDebug() << "exit event loop"; } void NameCreater::OnClickedCustomLogo2Btn() { QEventLoop loop; customlogo2 widget(&this->font); connect(&widget, &CustomLogo::finished, &loop, &QEventLoop::quit); widget.show(); loop.exec(); } void NameCreater::onFontDownloadFinished(QNetworkReply* reply) { if (reply->error() == QNetworkReply::NoError) { QString url = reply->request().url().toString(); int attri = reply->request().attribute(QNetworkRequest::User).toInt(); qDebug() << "req url:" << url << "attribi:" << QString::number(attri); if (url.contains("fonts.json") || attri == 10 || query_type == 10) { qDebug() << "query fonts json succeed"; parseFontJson(reply->readAll()); }else if (url.contains(".ttf") || url.contains(".otf") || attri == 20 || query_type == 20) { qDebug() << "query font family succeed"; loadFont(reply->readAll()); } } else { QMessageBox msgBox; msgBox.setText("Can not load font file!" + reply->errorString()); msgBox.exec(); reply->deleteLater(); return; } } void NameCreater::OnFontComboBoxTextChanged(const QString& str) { QComboBox* sender = (QComboBox*)QObject::sender(); QString u = sender->currentData().toString(); query_type = 20; query(QUrl(u),20); } void NameCreater::OnFontComboBoxIndexChanged(int idx) { QComboBox* sender = (QComboBox*)QObject::sender(); QString u = sender->currentData().toString(); query_type = 20; query(QUrl(u),20); }