diff --git a/NameCreater/FiveToSingle.cpp b/NameCreater/FiveToSingle.cpp index 94c3701..d25f090 100644 --- a/NameCreater/FiveToSingle.cpp +++ b/NameCreater/FiveToSingle.cpp @@ -45,7 +45,7 @@ static QPointF NAME_POS_NUMBER[] = { static int NAME_POS_COUNT = sizeof(NAME_POS) / sizeof(NAME_POS[0]); -FiveToSingle::FiveToSingle() +FiveToSingle::FiveToSingle():INameCreater() { } @@ -183,8 +183,8 @@ void FiveToSingle::generaImageFromCSV(const QString& filename, const QByteArray& - QMessageBox msgBox; - msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times)); - msgBox.exec(); + //QMessageBox msgBox; + //msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times)); + //msgBox.exec(); } diff --git a/NameCreater/FiveToSingle.h b/NameCreater/FiveToSingle.h index 54393fd..9db8eac 100644 --- a/NameCreater/FiveToSingle.h +++ b/NameCreater/FiveToSingle.h @@ -6,7 +6,7 @@ public: FiveToSingle(); ~FiveToSingle(); -protected: +public: virtual void generaImageFromCSV(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color); }; diff --git a/NameCreater/NameCreater.cpp b/NameCreater/NameCreater.cpp index c2aabd3..132a7b3 100644 --- a/NameCreater/NameCreater.cpp +++ b/NameCreater/NameCreater.cpp @@ -10,6 +10,8 @@ #include #include +#include "FiveToSingle.h" + //#define BG_SAMPLE_PATH ":/NameCreater/resource/background_sample.jpg" #define BG_SAMPLE_PATH ":/NameCreater/resource/background_number_smaple.jpg" @@ -25,342 +27,14 @@ #define VERSION "v1.2.0" -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);*/ - - ; // obtained from e.g. QImage::save() - - - - 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 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(); -} - NameCreater::NameCreater(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); - connect(ui.openfile_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); + connect(ui.fivesingle_btn, &QPushButton::released, this, &NameCreater::OnClickedReadBtn); - QString font_path = FONT_PATH; //root_path.append(FONT_NAME); + QString font_path = FONT_PATH; QFile font_res(font_path); if (!font_res.open(QIODevice::ReadOnly)) { QMessageBox msgBox; @@ -399,7 +73,8 @@ void NameCreater::OnClickedReadBtn() { } else { // Use fileName and fileContent - processCSV(fileName, fileContent, this->font, this->font_color); + FiveToSingle().generaImageFromCSV(fileName, fileContent, this->font, this->font_color); + } }; QFileDialog::getOpenFileContent("CSV (*.csv)", fileContentReady); diff --git a/NameCreater/NameCreater.ui b/NameCreater/NameCreater.ui index efce780..719d7e8 100644 --- a/NameCreater/NameCreater.ui +++ b/NameCreater/NameCreater.ui @@ -6,8 +6,8 @@ 0 0 - 532 - 458 + 1013 + 651 @@ -44,55 +44,13 @@ background-color: rgb(0, 0, 0); :/NameCreater/resource/background.jpg - - - - 80 - 100 - 181 - 161 - - - - Open File - - - - - - 50 - 20 - 261 - 71 - - - - - Yu Gothic UI - 20 - 50 - false - false - - - - color: rgb(255, 255, 255); - - - - 五入組拆成單入產生器 - - - Qt::AlignCenter - - - 50 - 60 - 261 - 41 + 10 + 10 + 181 + 16 @@ -109,19 +67,217 @@ background-color: rgb(0, 0, 0); - 五入組拆成單入產生器 + v1.0.0 - Qt::AlignCenter + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + 10 + 40 + 851 + 441 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + + + Yu Gothic UI + 20 + 50 + false + false + + + + color: rgb(255, 255, 255); + + + + 五入組拆成單入產生器 + + + Qt::AlignCenter + + + + + + + + 0 + 76 + + + + Open File + + + + + + + + Yu Gothic UI + 20 + 50 + false + false + + + + color: rgb(255, 255, 255); + + + + 五入組產生器(舊) + + + Qt::AlignCenter + + + + + + + + Yu Gothic UI + 20 + 50 + false + false + + + + color: rgb(255, 255, 255); + + + + 生日禮產生器 + + + Qt::AlignCenter + + + + + + + + Yu Gothic UI + 20 + 50 + false + false + + + + color: rgb(255, 255, 255); + + + + 小方巾產生器 + + + Qt::AlignCenter + + + + + + + + 0 + 76 + + + + Open File + + + + + + + + 0 + 76 + + + + Open File + + + + + + + + Yu Gothic UI + 20 + 50 + false + false + + + + color: rgb(255, 255, 255); + + + + 五入組產生器(新) + + + Qt::AlignCenter + + + + + + + + 0 + 76 + + + + Open File + + + + + + + + 0 + 76 + + + + Open File + + + + + 0 0 - 532 + 1013 21