namecreater/NameCreater/NameCreater.cpp

301 lines
9.8 KiB
C++

#include "def.h"
#include "NameCreater.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QFontDatabase>
#include <QTextCodec>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QDateTime>
#include <QBuffer>
#include <QRegularExpression>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QComboBox>
#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);
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="<<QSslSocket::sslLibraryBuildVersionString();
connect(&network_mgr, &QNetworkAccessManager::finished, this, &NameCreater::onFontDownloadFinished);
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);
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);
QList<QLabel*> titals = findChildren<QLabel*>(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();
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;
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(QUrl(u));
}
void NameCreater::OnFontComboBoxIndexChanged(int idx)
{
QComboBox* sender = (QComboBox*)QObject::sender();
QString u = sender->currentData().toString();
query(QUrl(u));
}