#include "NameCreater.h" #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_GIRL_TYPE_STR "girl" #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); 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); QFont tital_font = QFont(font); tital_font.setPixelSize(20); QFont version_font = QFont(font); version_font.setPixelSize(12); ui.tital_label->setFont(tital_font); ui.version_label->setFont(version_font); ui.version_label->setText(VERSION); font_res.close(); qDebug() << ui.label->font().family(); } void NameCreater::OnClickedReadBtn() { auto fileContentReady = [this](const QString &fileName, const QByteArray &fileContent) { if (fileName.isEmpty()) { // No file was selected } else { // Use fileName and fileContent processCSV(fileName, fileContent, this->font, this->font_color); } }; QFileDialog::getOpenFileContent("CSV (*.csv)", fileContentReady); }