diff --git a/NameCreater/FiveToSingle.cpp b/NameCreater/FiveToSingle.cpp new file mode 100644 index 0000000..94c3701 --- /dev/null +++ b/NameCreater/FiveToSingle.cpp @@ -0,0 +1,190 @@ +#include "FiveToSingle.h" +#include + + + +static QPointF BG_POS[] = { + QPointF(159, 61), QPointF(482, 61), QPointF(802, 61), + QPointF(1122, 61), QPointF(1449, 61), QPointF(1772, 61), + QPointF(2095, 61), QPointF(2414, 61), QPointF(2738, 61), + QPointF(3060, 61), QPointF(900, 2094), QPointF(900, 2420), +}; + +static int BG_POS_COUNT = sizeof(BG_POS) / sizeof(BG_POS[0]); + + +static QPointF NAME_POS[] = { + QPointF(240, 430), QPointF(372, 1402), + QPointF(564, 430), QPointF(694, 1402), + QPointF(883, 430), QPointF(1014, 1402), + QPointF(1205, 430), QPointF(1337, 1402), + QPointF(1532, 430), QPointF(1663, 1402), + QPointF(1854, 430), QPointF(1987, 1402), + QPointF(2176, 430), QPointF(2309, 1402), + QPointF(2498, 430), QPointF(2629, 1402), + QPointF(2821, 430), QPointF(2952, 1402), + QPointF(3145, 430), QPointF(3276, 1402), + QPointF(1268, 2012), QPointF(2240, 1880), + QPointF(1268, 2334), QPointF(2240, 2202), +}; + +static QPointF NAME_POS_NUMBER[] = { + QPointF(240, 370), QPointF(372, 1442), + QPointF(564, 370), QPointF(694, 1442), + QPointF(883, 370), QPointF(1014, 1442), + QPointF(1205, 370), QPointF(1337, 1442), + QPointF(1532, 370), QPointF(1663, 1442), + QPointF(1854, 370), QPointF(1987, 1442), + QPointF(2176, 370), QPointF(2309, 1442), + QPointF(2498, 370), QPointF(2629, 1442), + QPointF(2821, 370), QPointF(2952, 1442), + QPointF(3145, 370), QPointF(3276, 1442), + QPointF(1228, 2012), QPointF(2280, 1880), + QPointF(1228, 2334), QPointF(2280, 2202), +}; + +static int NAME_POS_COUNT = sizeof(NAME_POS) / sizeof(NAME_POS[0]); + +FiveToSingle::FiveToSingle() +{ +} + +FiveToSingle::~FiveToSingle() +{ +} + +void FiveToSingle::generaImageFromCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color) +{ + int export_times = 0; + //QList nameList = getNameByFile(filename); + + QList nameList = getNameByFileContent(filecontent); + if (nameList.isEmpty()) { + QMessageBox msgBox; + msgBox.setText(QString("Name is empty ! ").append(filename)); + msgBox.exec(); + } + + QGraphicsScene scene; + scene.setBackgroundBrush(QBrush(QColor(255, 255, 255))); + QGraphicsView view(&scene); + + + + /*QImage image(BG_SAMPLE_PATH); + QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image)); + scene.addItem(bgItem);*/ + + + for (int n = 0; n < nameList.length(); n++) { + int idx = n % BG_POS_COUNT; + NameInfo_t name_info = nameList.at(n); + QImage name_image(name_info.bg_path); + if (idx == 0) { + QGraphicsRectItem* bg_item = new QGraphicsRectItem(0, 0, 3508, 2482); + bg_item->setBrush(QBrush(QColor(255, 255, 255))); + scene.addItem(bg_item); + } + + + QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(name_image)); + bool rot = (idx == 10 || idx == 11); + name_bgItem->setRotation(rot ? -90 : 0); + name_bgItem->setPos(BG_POS[idx]); + scene.addItem(name_bgItem); + + + if (name_info.name1.isEmpty()) { + QMessageBox msgBox; + msgBox.setText(QString().asprintf("Name is Empty at %d", export_times + 1)); + msgBox.exec(); + } + QString name = name_info.name1; + int name_len = name.length(); + for (int j = 0; j < 2; j++) { + int pos_idx = idx * 2 + j; + qreal xscale, yscale; + + xscale = yscale = (j == 1) ? -1 : 1; + QPointF pos = name_info.is_number_bg ? NAME_POS_NUMBER[pos_idx] : NAME_POS[pos_idx]; + qreal y_offset[2] = { 0 }; + if (name_len == 2) { + if (!rot) { + pos.setY(pos.y() + 82.0 * xscale); + } + else { + pos.setX(pos.x() + 82.0 * xscale); + + } + + y_offset[0] = 25; + } + + QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1)); + textItem1->setFont(font); + textItem1->setDefaultTextColor(font_color); + if (!rot) { + textItem1->setPos(pos); + textItem1->setTransform(QTransform::fromScale(xscale, yscale)); + } + else { + textItem1->setPos(pos); + textItem1->setRotation(-90); + textItem1->setTransform(QTransform::fromScale(xscale, yscale)); + } + + + scene.addItem(textItem1); + + QGraphicsTextItem* textItem2 = new QGraphicsTextItem(name.mid(1, 1)); + textItem2->setFont(font); + textItem2->setDefaultTextColor(font_color); + if (!rot) { + + textItem2->setPos(pos.x(), pos.y() + (145 + y_offset[0]) * xscale); + textItem2->setTransform(QTransform::fromScale(xscale, yscale)); + } + else { + textItem2->setPos(pos.x() + (145 + y_offset[0]) * xscale, pos.y()); + textItem2->setRotation(-90); + textItem2->setTransform(QTransform::fromScale(xscale, yscale)); + } + + scene.addItem(textItem2); + + if (name_len > 2) { + QGraphicsTextItem* textItem3 = new QGraphicsTextItem(name.mid(2, 1)); + textItem3->setFont(font); + textItem3->setDefaultTextColor(font_color); + if (!rot) { + textItem3->setPos(pos.x(), pos.y() + 290 * xscale); + textItem3->setTransform(QTransform::fromScale(xscale, yscale)); + } + else { + textItem3->setPos(pos.x() + 290 * xscale, pos.y()); + textItem3->setRotation(-90); + textItem3->setTransform(QTransform::fromScale(xscale, yscale)); + } + + scene.addItem(textItem3); + } + + + } + bool is_export_page = ((idx + 1) % BG_POS_COUNT == 0); + if (is_export_page || n == nameList.count() - 1) { + QString file_name = QString("%1_.jpg").arg(QString::number(export_times + 1)); + saveToImage(file_name, &scene); + export_times++; + + scene.clear(); + } + } + + + + QMessageBox msgBox; + msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times)); + msgBox.exec(); + +} diff --git a/NameCreater/FiveToSingle.h b/NameCreater/FiveToSingle.h new file mode 100644 index 0000000..54393fd --- /dev/null +++ b/NameCreater/FiveToSingle.h @@ -0,0 +1,13 @@ +#pragma once +#include "INameCreater.h" +class FiveToSingle : public INameCreater +{ +public: + FiveToSingle(); + ~FiveToSingle(); + +protected: + virtual void generaImageFromCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color); + +}; + diff --git a/NameCreater/INameCreater.cpp b/NameCreater/INameCreater.cpp new file mode 100644 index 0000000..50063e1 --- /dev/null +++ b/NameCreater/INameCreater.cpp @@ -0,0 +1,154 @@ +#include "INameCreater.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define BG_PATH_FORMAT ":/NameCreater/resource/background_%1.jpg" + +INameCreater::INameCreater() +{ +} + +INameCreater::~INameCreater() +{ +} + +void INameCreater::saveToImage(const QString& filename, QGraphicsScene* scene) +{ + scene->clearSelection(); // Selections would also render to the file + scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents + QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene + image.fill(Qt::transparent); // Start all pixels transparent + int dpm = 300 / 0.0254; // ~300 DPI + image.setDotsPerMeterX(dpm); + image.setDotsPerMeterY(dpm); + + + QPainter painter(&image); + scene->render(&painter); + +#ifdef Q_OS_MAC + QString folder_name = QCoreApplication::applicationDirPath() + "/../../../export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); +#else + QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); +#endif + + + QByteArray imageData; + QBuffer buffer(&imageData); + buffer.open(QIODevice::WriteOnly); + QPixmap pixmap = QPixmap::fromImage(image); + pixmap.save(&buffer, "JPEG", 100); // You can also use other formats like "JPEG" or "BMP" + + QFileDialog::saveFileContent(imageData, filename); + +} + +QList INameCreater::getNameByFile(const QString& filename) +{ + QList nameList; + +#ifdef Q_OS_MAC + QString root_path = QCoreApplication::applicationDirPath() + "/../../../"; +#else + QString root_path = "";//QCoreApplication::applicationDirPath() + "/"; +#endif + + + QFile file(filename); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox msgBox; + msgBox.setText(QString("Load file failed!").append(filename)); + msgBox.exec(); + file.close(); + return nameList; + } + + int row = 0; + QTextCodec* tc = QTextCodec::codecForName("Big5"); + while (!file.atEnd()) + { + NameInfo_t info; + QByteArray line = file.readLine(); + + QString name_tc = tc->toUnicode(line); + QStringList csvList = name_tc.split(','); + + + QString name1 = csvList.at(0); + QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); + qDebug() << "row :" << row << " " << name1; + + type_str = type_str.remove(QRegExp("\r")); + type_str = type_str.remove(QRegExp("\n")); + + + + if (name1.compare("name_1") == 0) { + continue; + } + + info.name1 = name1; + info.bg_path = QString(BG_PATH_FORMAT).arg(type_str); + info.is_number_bg = type_str[0].isDigit(); + + nameList.append(info); + + row++; + } + file.close(); + return nameList; +} + +QList INameCreater::getNameByFileContent(const QByteArray& conetent) +{ + QList nameList; + QTextCodec* tc = QTextCodec::codecForName("Big5"); + QString codec = tc->toUnicode(conetent.data()); + QTextStream stream(&codec); + + + int row = 0; + while (!stream.atEnd()) + { + NameInfo_t info; + QString line = stream.readLine(); + QString name_tc = line; + QStringList csvList = name_tc.split(','); + + QString name1 = csvList.at(0); + QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); + qDebug() << "row :" << row << " " << name1; + + type_str = type_str.remove(QRegExp("\r")); + type_str = type_str.remove(QRegExp("\n")); + + + + if (name1.compare("name_1") == 0) { + continue; + } + + info.name1 = name1; + info.bg_path = QString(BG_PATH_FORMAT).arg(type_str); + info.is_number_bg = type_str[0].isDigit(); + + nameList.append(info); + + row++; + } + return nameList; +} + +void INameCreater::generaImageFromCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color) +{ +} diff --git a/NameCreater/INameCreater.h b/NameCreater/INameCreater.h new file mode 100644 index 0000000..66b0cfa --- /dev/null +++ b/NameCreater/INameCreater.h @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include +#include + +struct NameInfo_t { + QString name1; + QString name2; + QString bg_path; + bool is_number_bg; +}; + + + +class INameCreater : public QObject { +public: + INameCreater(); + ~INameCreater(); + +protected: + void saveToImage(const QString& filename, QGraphicsScene* scene); + QList getNameByFile(const QString& filename); + QList getNameByFileContent(const QByteArray& conetent); + + virtual void generaImageFromCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color); + + +}; \ No newline at end of file diff --git a/NameCreater/NameCreater.cpp b/NameCreater/NameCreater.cpp index 0fbd33a..740d190 100644 --- a/NameCreater/NameCreater.cpp +++ b/NameCreater/NameCreater.cpp @@ -3,107 +3,107 @@ #include #include #include -#include -#include -#include +#include +#include +#include #include #include #include -//#define BG_SAMPLE_PATH ":/NameCreater/resource/background_sample.jpg" -#define BG_SAMPLE_PATH ":/NameCreater/resource/background_number_smaple.jpg" -#define BG_PATH_FORMAT ":/NameCreater/resource/background_%1.jpg" -#define FONT_NAME "DFYuanStd-W8.otf" -#define FONT_PATH ":/NameCreater/_exp/DFYuanStd-W8.otf" -#define FONT_SIZE 120 -//#define FONT_SIZE 74 - -#define BG_MIX_TYPE_STR "mix" -#define BG_BOY_TYPE_STR "boy" +//#define BG_SAMPLE_PATH ":/NameCreater/resource/background_sample.jpg" +#define BG_SAMPLE_PATH ":/NameCreater/resource/background_number_smaple.jpg" +#define BG_PATH_FORMAT ":/NameCreater/resource/background_%1.jpg" +#define FONT_NAME "DFYuanStd-W8.otf" +#define FONT_PATH ":/NameCreater/_exp/DFYuanStd-W8.otf" +#define FONT_SIZE 120 +//#define FONT_SIZE 74 + +#define BG_MIX_TYPE_STR "mix" +#define BG_BOY_TYPE_STR "boy" #define BG_GIRL_TYPE_STR "girl" -struct NameInfo_t { - QString name1; - QString name2; - QString bg_path; - bool is_number_bg; -}; - -static QPointF BG_POS[] = { - QPointF(159, 61), QPointF(482, 61), QPointF(802, 61), - QPointF(1122, 61), QPointF(1449, 61), QPointF(1772, 61), - QPointF(2095, 61), QPointF(2414, 61), QPointF(2738, 61), - QPointF(3060, 61), QPointF(900, 2094), QPointF(900, 2420), -}; - -static int BG_POS_COUNT = sizeof(BG_POS) / sizeof(BG_POS[0]); - - -static QPointF NAME_POS[] = { - QPointF(240, 430), QPointF(372, 1402), - QPointF(564, 430), QPointF(694, 1402), - QPointF(883, 430), QPointF(1014, 1402), - QPointF(1205, 430), QPointF(1337, 1402), - QPointF(1532, 430), QPointF(1663, 1402), - QPointF(1854, 430), QPointF(1987, 1402), - QPointF(2176, 430), QPointF(2309, 1402), - QPointF(2498, 430), QPointF(2629, 1402), - QPointF(2821, 430), QPointF(2952, 1402), - QPointF(3145, 430), QPointF(3276, 1402), - QPointF(1268, 2012), QPointF(2240, 1880), - QPointF(1268, 2334), QPointF(2240, 2202), -}; - -static QPointF NAME_POS_NUMBER[] = { - QPointF(240, 370), QPointF(372, 1442), - QPointF(564, 370), QPointF(694, 1442), - QPointF(883, 370), QPointF(1014, 1442), - QPointF(1205, 370), QPointF(1337, 1442), - QPointF(1532, 370), QPointF(1663, 1442), - QPointF(1854, 370), QPointF(1987, 1442), - QPointF(2176, 370), QPointF(2309, 1442), - QPointF(2498, 370), QPointF(2629, 1442), - QPointF(2821, 370), QPointF(2952, 1442), - QPointF(3145, 370), QPointF(3276, 1442), - QPointF(1228, 2012), QPointF(2280, 1880), - QPointF(1228, 2334), QPointF(2280, 2202), -}; - +struct NameInfo_t { + QString name1; + QString name2; + QString bg_path; + bool is_number_bg; +}; + +static QPointF BG_POS[] = { + QPointF(159, 61), QPointF(482, 61), QPointF(802, 61), + QPointF(1122, 61), QPointF(1449, 61), QPointF(1772, 61), + QPointF(2095, 61), QPointF(2414, 61), QPointF(2738, 61), + QPointF(3060, 61), QPointF(900, 2094), QPointF(900, 2420), +}; + +static int BG_POS_COUNT = sizeof(BG_POS) / sizeof(BG_POS[0]); + + +static QPointF NAME_POS[] = { + QPointF(240, 430), QPointF(372, 1402), + QPointF(564, 430), QPointF(694, 1402), + QPointF(883, 430), QPointF(1014, 1402), + QPointF(1205, 430), QPointF(1337, 1402), + QPointF(1532, 430), QPointF(1663, 1402), + QPointF(1854, 430), QPointF(1987, 1402), + QPointF(2176, 430), QPointF(2309, 1402), + QPointF(2498, 430), QPointF(2629, 1402), + QPointF(2821, 430), QPointF(2952, 1402), + QPointF(3145, 430), QPointF(3276, 1402), + QPointF(1268, 2012), QPointF(2240, 1880), + QPointF(1268, 2334), QPointF(2240, 2202), +}; + +static QPointF NAME_POS_NUMBER[] = { + QPointF(240, 370), QPointF(372, 1442), + QPointF(564, 370), QPointF(694, 1442), + QPointF(883, 370), QPointF(1014, 1442), + QPointF(1205, 370), QPointF(1337, 1442), + QPointF(1532, 370), QPointF(1663, 1442), + QPointF(1854, 370), QPointF(1987, 1442), + QPointF(2176, 370), QPointF(2309, 1442), + QPointF(2498, 370), QPointF(2629, 1442), + QPointF(2821, 370), QPointF(2952, 1442), + QPointF(3145, 370), QPointF(3276, 1442), + QPointF(1228, 2012), QPointF(2280, 1880), + QPointF(1228, 2334), QPointF(2280, 2202), +}; + static int NAME_POS_COUNT = sizeof(NAME_POS) / sizeof(NAME_POS[0]); - -void saveToImage(const QString& filename, QGraphicsScene* scene) { - - scene->clearSelection(); // Selections would also render to the file - scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents - QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene - image.fill(Qt::transparent); // Start all pixels transparent - int dpm = 300 / 0.0254; // ~300 DPI - image.setDotsPerMeterX(dpm); - image.setDotsPerMeterY(dpm); - - - QPainter painter(&image); - scene->render(&painter); - -#ifdef Q_OS_MAC - QString folder_name = QCoreApplication::applicationDirPath() + "/../../../export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); -#else - QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); -#endif - - /*qDebug() << "folder_name :" << folder_name; - if (!QDir(folder_name).exists()) { - QDir().mkdir(folder_name); - } - - image.save(QString(folder_name).append("/").append(filename), "JPG", 100);*/ - + +void saveToImage(const QString& filename, QGraphicsScene* scene) { + + scene->clearSelection(); // Selections would also render to the file + scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents + QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene + image.fill(Qt::transparent); // Start all pixels transparent + int dpm = 300 / 0.0254; // ~300 DPI + image.setDotsPerMeterX(dpm); + image.setDotsPerMeterY(dpm); + + + QPainter painter(&image); + scene->render(&painter); + +#ifdef Q_OS_MAC + QString folder_name = QCoreApplication::applicationDirPath() + "/../../../export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); +#else + QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); +#endif + + /*qDebug() << "folder_name :" << folder_name; + if (!QDir(folder_name).exists()) { + QDir().mkdir(folder_name); + } + + image.save(QString(folder_name).append("/").append(filename), "JPG", 100);*/ + ; // obtained from e.g. QImage::save() @@ -114,243 +114,243 @@ void saveToImage(const QString& filename, QGraphicsScene* scene) { QPixmap pixmap = QPixmap::fromImage(image); pixmap.save(&buffer, "JPEG",100); // You can also use other formats like "JPEG" or "BMP" - QFileDialog::saveFileContent(imageData, filename); - - -} - - - - -QList getNameByFile(const QString& filename) { - QList nameList; - -#ifdef Q_OS_MAC - QString root_path = QCoreApplication::applicationDirPath() + "/../../../"; -#else - QString root_path = "";//QCoreApplication::applicationDirPath() + "/"; -#endif - - - QFile file(filename); - - if (!file.open(QIODevice::ReadOnly)) { - QMessageBox msgBox; - msgBox.setText(QString("Load file failed!").append(filename)); - msgBox.exec(); - file.close(); - return nameList; - } - - int row = 0; - QTextCodec* tc = QTextCodec::codecForName("Big5"); - while (!file.atEnd()) - { - NameInfo_t info; - QByteArray line = file.readLine(); - - QString name_tc = tc->toUnicode(line); - QStringList csvList = name_tc.split(','); - - - QString name1 = csvList.at(0); - QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); - qDebug() << "row :" << row << " " << name1; - - type_str = type_str.remove(QRegExp("\r")); - type_str = type_str.remove(QRegExp("\n")); - - - - if (name1.compare("name_1") == 0) { - continue; - } - - info.name1 = name1; - info.bg_path = QString(BG_PATH_FORMAT).arg(type_str); - info.is_number_bg = type_str[0].isDigit(); - - nameList.append(info); - - row++; - } - file.close(); - return nameList; -} - -QList getNameByFileContent(const QByteArray& conetent) { - QList nameList; - QTextCodec* tc = QTextCodec::codecForName("Big5"); - QString codec = tc->toUnicode(conetent.data()); - QTextStream stream(&codec); - - - int row = 0; - while (!stream.atEnd()) - { - NameInfo_t info; - QString line = stream.readLine(); - QString name_tc = line; - QStringList csvList = name_tc.split(','); - - QString name1 = csvList.at(0); - QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); - qDebug() << "row :" << row << " " << name1; - - type_str = type_str.remove(QRegExp("\r")); - type_str = type_str.remove(QRegExp("\n")); - - - - if (name1.compare("name_1") == 0) { - continue; - } - - info.name1 = name1; - info.bg_path = QString(BG_PATH_FORMAT).arg(type_str); - info.is_number_bg = type_str[0].isDigit(); - - nameList.append(info); - - row++; - } - return nameList; -} - - - -void processCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color) { - int export_times = 0; - //QList nameList = getNameByFile(filename); - - QList nameList = getNameByFileContent(filecontent); - if (nameList.isEmpty()) { - QMessageBox msgBox; - msgBox.setText(QString("Name is empty ! ").append(filename)); - msgBox.exec(); - } - - QGraphicsScene scene; - scene.setBackgroundBrush(QBrush(QColor(255, 255, 255))); - QGraphicsView view(&scene); - - - - /*QImage image(BG_SAMPLE_PATH); - QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image)); - scene.addItem(bgItem);*/ - - - for (int n = 0; n < nameList.length(); n++) { - int idx = n % BG_POS_COUNT; - NameInfo_t name_info = nameList.at(n); - QImage name_image(name_info.bg_path); - if (idx == 0) { - QGraphicsRectItem* bg_item = new QGraphicsRectItem(0, 0, 3508, 2482); - bg_item->setBrush(QBrush(QColor(255, 255, 255))); - scene.addItem(bg_item); - } - - - QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(name_image)); - bool rot = (idx == 10 || idx == 11); - name_bgItem->setRotation(rot ? -90 : 0); - name_bgItem->setPos(BG_POS[idx]); - scene.addItem(name_bgItem); - - - if (name_info.name1.isEmpty()) { - QMessageBox msgBox; - msgBox.setText(QString().asprintf("Name is Empty at %d", export_times + 1)); - msgBox.exec(); - } - QString name = name_info.name1; - int name_len = name.length(); - for (int j = 0; j < 2; j++) { - int pos_idx = idx * 2 + j; - int xscale, yscale; - - xscale = yscale = (j == 1) ? -1 : 1; - QPointF pos = name_info.is_number_bg ? NAME_POS_NUMBER[pos_idx] : NAME_POS[pos_idx]; - int y_offset[2] = { 0 }; - if (name_len == 2) { - if (!rot) { - pos.setY(pos.y() + 82 * xscale); - } - else { - pos.setX(pos.x() + 82 * xscale); - - } - - y_offset[0] = 25; - } - - QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1)); - textItem1->setFont(font); - textItem1->setDefaultTextColor(font_color); - if (!rot) { - textItem1->setPos(pos); - textItem1->setTransform(QTransform::fromScale(xscale, yscale)); - } - else { - textItem1->setPos(pos); - textItem1->setRotation(-90); - textItem1->setTransform(QTransform::fromScale(xscale, yscale)); - } - - - scene.addItem(textItem1); - - QGraphicsTextItem* textItem2 = new QGraphicsTextItem(name.mid(1, 1)); - textItem2->setFont(font); - textItem2->setDefaultTextColor(font_color); - if (!rot) { - - textItem2->setPos(pos.x(), pos.y() + (145 + y_offset[0]) * xscale); - textItem2->setTransform(QTransform::fromScale(xscale, yscale)); - } - else { - textItem2->setPos(pos.x() + (145 + y_offset[0]) * xscale, pos.y()); - textItem2->setRotation(-90); - textItem2->setTransform(QTransform::fromScale(xscale, yscale)); - } - - scene.addItem(textItem2); - - if (name_len > 2) { - QGraphicsTextItem* textItem3 = new QGraphicsTextItem(name.mid(2, 1)); - textItem3->setFont(font); - textItem3->setDefaultTextColor(font_color); - if (!rot) { - textItem3->setPos(pos.x(), pos.y() + 290 * xscale); - textItem3->setTransform(QTransform::fromScale(xscale, yscale)); - } - else { - textItem3->setPos(pos.x() + 290 * xscale, pos.y()); - textItem3->setRotation(-90); - textItem3->setTransform(QTransform::fromScale(xscale, yscale)); - } - - scene.addItem(textItem3); - } - - - } - bool is_export_page = ((idx + 1) % BG_POS_COUNT == 0); - if (is_export_page || n == nameList.count() - 1) { - QString file_name = QString("%1_.jpg").arg(QString::number(export_times + 1)); - saveToImage(file_name, &scene); - export_times++; - - scene.clear(); - } - } - - - - QMessageBox msgBox; - msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times)); - msgBox.exec(); + QFileDialog::saveFileContent(imageData, filename); + + +} + + + + +QList getNameByFile(const QString& filename) { + QList nameList; + +#ifdef Q_OS_MAC + QString root_path = QCoreApplication::applicationDirPath() + "/../../../"; +#else + QString root_path = "";//QCoreApplication::applicationDirPath() + "/"; +#endif + + + QFile file(filename); + + if (!file.open(QIODevice::ReadOnly)) { + QMessageBox msgBox; + msgBox.setText(QString("Load file failed!").append(filename)); + msgBox.exec(); + file.close(); + return nameList; + } + + int row = 0; + QTextCodec* tc = QTextCodec::codecForName("Big5"); + while (!file.atEnd()) + { + NameInfo_t info; + QByteArray line = file.readLine(); + + QString name_tc = tc->toUnicode(line); + QStringList csvList = name_tc.split(','); + + + QString name1 = csvList.at(0); + QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); + qDebug() << "row :" << row << " " << name1; + + type_str = type_str.remove(QRegExp("\r")); + type_str = type_str.remove(QRegExp("\n")); + + + + if (name1.compare("name_1") == 0) { + continue; + } + + info.name1 = name1; + info.bg_path = QString(BG_PATH_FORMAT).arg(type_str); + info.is_number_bg = type_str[0].isDigit(); + + nameList.append(info); + + row++; + } + file.close(); + return nameList; +} + +QList getNameByFileContent(const QByteArray& conetent) { + QList nameList; + QTextCodec* tc = QTextCodec::codecForName("Big5"); + QString codec = tc->toUnicode(conetent.data()); + QTextStream stream(&codec); + + + int row = 0; + while (!stream.atEnd()) + { + NameInfo_t info; + QString line = stream.readLine(); + QString name_tc = line; + QStringList csvList = name_tc.split(','); + + QString name1 = csvList.at(0); + QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); + qDebug() << "row :" << row << " " << name1; + + type_str = type_str.remove(QRegExp("\r")); + type_str = type_str.remove(QRegExp("\n")); + + + + if (name1.compare("name_1") == 0) { + continue; + } + + info.name1 = name1; + info.bg_path = QString(BG_PATH_FORMAT).arg(type_str); + info.is_number_bg = type_str[0].isDigit(); + + nameList.append(info); + + row++; + } + return nameList; +} + + + +void processCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color) { + int export_times = 0; + //QList nameList = getNameByFile(filename); + + QList nameList = getNameByFileContent(filecontent); + if (nameList.isEmpty()) { + QMessageBox msgBox; + msgBox.setText(QString("Name is empty ! ").append(filename)); + msgBox.exec(); + } + + QGraphicsScene scene; + scene.setBackgroundBrush(QBrush(QColor(255, 255, 255))); + QGraphicsView view(&scene); + + + + /*QImage image(BG_SAMPLE_PATH); + QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image)); + scene.addItem(bgItem);*/ + + + for (int n = 0; n < nameList.length(); n++) { + int idx = n % BG_POS_COUNT; + NameInfo_t name_info = nameList.at(n); + QImage name_image(name_info.bg_path); + if (idx == 0) { + QGraphicsRectItem* bg_item = new QGraphicsRectItem(0, 0, 3508, 2482); + bg_item->setBrush(QBrush(QColor(255, 255, 255))); + scene.addItem(bg_item); + } + + + QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(name_image)); + bool rot = (idx == 10 || idx == 11); + name_bgItem->setRotation(rot ? -90 : 0); + name_bgItem->setPos(BG_POS[idx]); + scene.addItem(name_bgItem); + + + if (name_info.name1.isEmpty()) { + QMessageBox msgBox; + msgBox.setText(QString().asprintf("Name is Empty at %d", export_times + 1)); + msgBox.exec(); + } + QString name = name_info.name1; + int name_len = name.length(); + for (int j = 0; j < 2; j++) { + int pos_idx = idx * 2 + j; + int xscale, yscale; + + xscale = yscale = (j == 1) ? -1 : 1; + QPointF pos = name_info.is_number_bg ? NAME_POS_NUMBER[pos_idx] : NAME_POS[pos_idx]; + int y_offset[2] = { 0 }; + if (name_len == 2) { + if (!rot) { + pos.setY(pos.y() + 82 * xscale); + } + else { + pos.setX(pos.x() + 82 * xscale); + + } + + y_offset[0] = 25; + } + + QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1)); + textItem1->setFont(font); + textItem1->setDefaultTextColor(font_color); + if (!rot) { + textItem1->setPos(pos); + textItem1->setTransform(QTransform::fromScale(xscale, yscale)); + } + else { + textItem1->setPos(pos); + textItem1->setRotation(-90); + textItem1->setTransform(QTransform::fromScale(xscale, yscale)); + } + + + scene.addItem(textItem1); + + QGraphicsTextItem* textItem2 = new QGraphicsTextItem(name.mid(1, 1)); + textItem2->setFont(font); + textItem2->setDefaultTextColor(font_color); + if (!rot) { + + textItem2->setPos(pos.x(), pos.y() + (145 + y_offset[0]) * xscale); + textItem2->setTransform(QTransform::fromScale(xscale, yscale)); + } + else { + textItem2->setPos(pos.x() + (145 + y_offset[0]) * xscale, pos.y()); + textItem2->setRotation(-90); + textItem2->setTransform(QTransform::fromScale(xscale, yscale)); + } + + scene.addItem(textItem2); + + if (name_len > 2) { + QGraphicsTextItem* textItem3 = new QGraphicsTextItem(name.mid(2, 1)); + textItem3->setFont(font); + textItem3->setDefaultTextColor(font_color); + if (!rot) { + textItem3->setPos(pos.x(), pos.y() + 290 * xscale); + textItem3->setTransform(QTransform::fromScale(xscale, yscale)); + } + else { + textItem3->setPos(pos.x() + 290 * xscale, pos.y()); + textItem3->setRotation(-90); + textItem3->setTransform(QTransform::fromScale(xscale, yscale)); + } + + scene.addItem(textItem3); + } + + + } + bool is_export_page = ((idx + 1) % BG_POS_COUNT == 0); + if (is_export_page || n == nameList.count() - 1) { + QString file_name = QString("%1_.jpg").arg(QString::number(export_times + 1)); + saveToImage(file_name, &scene); + export_times++; + + scene.clear(); + } + } + + + + QMessageBox msgBox; + msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times)); + msgBox.exec(); } @@ -360,21 +360,21 @@ NameCreater::NameCreater(QWidget *parent) ui.setupUi(this); connect(ui.openfile_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); - QString font_path = FONT_PATH; //root_path.append(FONT_NAME); - QFile font_res(font_path); - if (!font_res.open(QIODevice::ReadOnly)) { - QMessageBox msgBox; - msgBox.setText("Can not load font file!" + font_path); - msgBox.exec(); - font_res.close(); - } - - int id = QFontDatabase::addApplicationFontFromData(font_res.readAll()); - QStringList family_list = QFontDatabase::applicationFontFamilies(id); - - - font = QFont(family_list.at(0)); - font.setPixelSize(FONT_SIZE); + QString font_path = FONT_PATH; //root_path.append(FONT_NAME); + QFile font_res(font_path); + if (!font_res.open(QIODevice::ReadOnly)) { + QMessageBox msgBox; + msgBox.setText("Can not load font file!" + font_path); + msgBox.exec(); + font_res.close(); + } + + int id = QFontDatabase::addApplicationFontFromData(font_res.readAll()); + QStringList family_list = QFontDatabase::applicationFontFamilies(id); + + + font = QFont(family_list.at(0)); + font.setPixelSize(FONT_SIZE); font_color = QColor(0, 0, 0); diff --git a/NameCreater/NameCreater.rc b/NameCreater/NameCreater.rc new file mode 100644 index 0000000..99d27de --- /dev/null +++ b/NameCreater/NameCreater.rc @@ -0,0 +1,60 @@ +// Microsoft Visual C++ generated resource script. +// + +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// ­^¤å (¬ü°ê) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE 4, 1 + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""winres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + +#endif // ­^¤å (¬ü°ê) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED diff --git a/NameCreater/NameCreater.vcxproj b/NameCreater/NameCreater.vcxproj index 8afb2c7..ab44369 100644 --- a/NameCreater/NameCreater.vcxproj +++ b/NameCreater/NameCreater.vcxproj @@ -22,7 +22,7 @@ {B66B108B-6DA8-408D-936D-988FF8B6B722} QtVS_v304 10.0.16299.0 - 10.0.19041.0 + 10.0 10.0.16299.0 10.0.19041.0 $(MSBuildProjectDirectory)\QtMsBuild @@ -49,12 +49,12 @@ - 5.8_msvc2015 + 5.15.2_msvc2019_64 core;gui;widgets debug - $(DefaultQtVersion) + 5.15.2_msvc2019_64 core;gui;widgets debug @@ -178,9 +178,19 @@ + + + + + + + + + + diff --git a/NameCreater/NameCreater.vcxproj.filters b/NameCreater/NameCreater.vcxproj.filters index 32317c8..9a907c1 100644 --- a/NameCreater/NameCreater.vcxproj.filters +++ b/NameCreater/NameCreater.vcxproj.filters @@ -1,44 +1,66 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - {99349809-55BA-4b9d-BF79-8FDBB0286EB3} - ui - - - {639EADAA-A684-42e4-A9AD-28FC9BCB8F7C} - ts - - - - - Resource Files - - - Form Files - - - Header Files - - - Source Files - - - - - Source Files - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {99349809-55BA-4b9d-BF79-8FDBB0286EB3} + ui + + + {639EADAA-A684-42e4-A9AD-28FC9BCB8F7C} + ts + + + + + Resource Files + + + Form Files + + + Header Files + + + Source Files + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + Header Files + + + Header Files + + + + + Resource Files + + \ No newline at end of file diff --git a/NameCreater/NameCreater.vcxproj.user b/NameCreater/NameCreater.vcxproj.user index e7ac749..e6de230 100644 --- a/NameCreater/NameCreater.vcxproj.user +++ b/NameCreater/NameCreater.vcxproj.user @@ -17,15 +17,15 @@ WindowsLocalDebugger - 2023-02-07T09:42:35.8095077Z + 2023-05-03T01:40:38.2815240Z - 2022-02-09T21:34:52.5655210Z + 2023-05-03T01:40:24.3278197Z - 2022-02-09T21:34:53.9897137Z + 2023-05-03T01:40:24.4724332Z - 2022-02-09T21:34:53.9897137Z + 2023-05-03T01:40:24.7925769Z \ No newline at end of file diff --git a/NameCreater/resource.h b/NameCreater/resource.h new file mode 100644 index 0000000..b709890 --- /dev/null +++ b/NameCreater/resource.h @@ -0,0 +1,14 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by NameCreater.rc + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 101 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif