238 lines
7.4 KiB
C++
238 lines
7.4 KiB
C++
#include "NameCreater.h"
|
|
#include <QtWidgets/QApplication>
|
|
#include <QTextCodec>
|
|
#include <QGraphicsScene>
|
|
#include <QGraphicsView>
|
|
#include <QGraphicsPixmapItem>
|
|
#include <QGraphicsTextItem>
|
|
#include <QFile>
|
|
#include <QDebug>
|
|
#include <QMessageBox>
|
|
#include <QDir>
|
|
#include <QDateTime>
|
|
#include <QFontDatabase>
|
|
|
|
#define BG_MIX_PATH ":/NameCreater/resource/background.jpg"
|
|
#define BG_BOY_PATH ":/NameCreater/resource/background_boy.jpg"
|
|
#define BG_GIRL_PATH ":/NameCreater/resource/background_girl.jpg"
|
|
#define FONT_NAME "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;
|
|
int bg_type;
|
|
};
|
|
|
|
static QPointF BOY_POS[] = {
|
|
QPointF(235, 387), QPointF(366, 1433),
|
|
QPointF(555, 387), QPointF(692, 1433),
|
|
QPointF(878, 387), QPointF(1014, 1433),
|
|
QPointF(1200, 387), QPointF(1337, 1433),
|
|
QPointF(1527, 387), QPointF(1663, 1433),
|
|
};
|
|
|
|
static int BOY_POS_COUNT = sizeof(BOY_POS) / sizeof(BOY_POS[0]);
|
|
|
|
|
|
static QPointF GIRL_POS[] = {
|
|
QPointF(1854, 387), QPointF(1987, 1433),
|
|
QPointF(2176, 387), QPointF(2314, 1433),
|
|
QPointF(2498, 387), QPointF(2635, 1433),
|
|
QPointF(2821, 387), QPointF(2958, 1433),
|
|
QPointF(3147, 387), QPointF(3285, 1433),
|
|
};
|
|
|
|
static int GIRL_POS_COUNT = sizeof(GIRL_POS) / sizeof(GIRL_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);
|
|
|
|
}
|
|
|
|
QList<NameInfo_t> getNameByFile(const QString& filename) {
|
|
QList<NameInfo_t> nameList;
|
|
|
|
#ifdef Q_OS_MAC
|
|
QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
|
|
#else
|
|
QString root_path = QCoreApplication::applicationDirPath() + "/";
|
|
#endif
|
|
|
|
|
|
QFile file(root_path.append(filename));
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
QMessageBox msgBox;
|
|
msgBox.setText("Load file failed!");
|
|
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::fromLocal8Bit(csvList.at(0));
|
|
QString name2 = csvList.at(1);//QString::fromLocal8Bit(csvList.at(1));
|
|
QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last());
|
|
qDebug() << "row :" << row << " " << name1 << " " << name2;
|
|
|
|
if (name1.compare("name_1") == 0 || name2.compare("name_2") == 0) {
|
|
continue;
|
|
}
|
|
|
|
info.name1 = name1;
|
|
info.name2 = name2;
|
|
info.bg_type = type_str.contains(QRegExp("boy")) ? 1 : (type_str.contains(QRegExp("girl"))) ? 2 : 0;
|
|
|
|
nameList.append(info);
|
|
|
|
row++;
|
|
}
|
|
file.close();
|
|
return nameList;
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
int export_times = 0;
|
|
|
|
|
|
|
|
QApplication a(argc, argv);
|
|
// NameCreater w;
|
|
// w.show();
|
|
#ifdef Q_OS_MAC
|
|
QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
|
|
#else
|
|
QString root_path = QCoreApplication::applicationDirPath() + "/";
|
|
#endif
|
|
|
|
QString 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();
|
|
return 0;
|
|
}
|
|
int id = QFontDatabase::addApplicationFontFromData(font_res.readAll());
|
|
QStringList family_list = QFontDatabase::applicationFontFamilies(id);
|
|
QFont font = QFont(family_list.at(0));
|
|
font.setPixelSize(FONT_SIZE);
|
|
QColor font_color(0, 0, 0);
|
|
|
|
|
|
QList<NameInfo_t> nameList = getNameByFile("name.csv");
|
|
if (nameList.isEmpty()) {
|
|
QMessageBox msgBox;
|
|
msgBox.setText(("Name is empty !"));
|
|
msgBox.exec();
|
|
}
|
|
|
|
QGraphicsScene scene;
|
|
QGraphicsView view(&scene);
|
|
|
|
|
|
for (int n = 0; n < nameList.length(); n++) {
|
|
//msgBox.setText(QString().sprintf("Export: %d / %d", export_times, nameList.length()));
|
|
|
|
NameInfo_t name_info = nameList.at(n);
|
|
|
|
scene.clear();
|
|
QImage image((name_info.bg_type == 1) ? BG_BOY_PATH : (name_info.bg_type == 2) ? BG_GIRL_PATH : BG_MIX_PATH);
|
|
QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
|
|
scene.addItem(bgItem);
|
|
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
int flag = j % 2;
|
|
QString name = (flag == 0) ? name_info.name1 : name_info.name2;
|
|
for (int i = 0; i < BOY_POS_COUNT; i++) {
|
|
int xscale, yscale;
|
|
xscale = yscale = (i % 2 == 1) ? -1 : 1;
|
|
QPointF pos = (flag == 0) ? BOY_POS[i] : GIRL_POS[i];
|
|
|
|
QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1));
|
|
textItem1->setFont(font);
|
|
textItem1->setDefaultTextColor(font_color);
|
|
textItem1->setPos(pos);
|
|
textItem1->setTransform(QTransform::fromScale(xscale, yscale));
|
|
scene.addItem(textItem1);
|
|
|
|
QGraphicsTextItem* textItem2 = new QGraphicsTextItem(name.mid(1, 1));
|
|
textItem2->setFont(font);
|
|
textItem2->setDefaultTextColor(font_color);
|
|
textItem2->setPos(pos.x(), pos.y() + 148 * xscale);
|
|
textItem2->setTransform(QTransform::fromScale(xscale, yscale));
|
|
scene.addItem(textItem2);
|
|
|
|
QGraphicsTextItem* textItem3 = new QGraphicsTextItem(name.mid(2, 1));
|
|
textItem3->setFont(font);
|
|
textItem3->setDefaultTextColor(font_color);
|
|
textItem3->setPos(pos.x(), pos.y() + 294 * xscale);
|
|
textItem3->setTransform(QTransform::fromScale(xscale, yscale));
|
|
scene.addItem(textItem3);
|
|
}
|
|
}
|
|
// if (name_info.bg_type == 0) {
|
|
// view.show();
|
|
// break;
|
|
// }
|
|
|
|
QString file_name = QString("%1_%2_%3.jpg").arg(QString::number(export_times+1),name_info.name1,name_info.name2);
|
|
saveToImage(file_name, &scene);
|
|
export_times++;
|
|
}
|
|
|
|
|
|
QMessageBox msgBox;
|
|
msgBox.setText(QString().asprintf("Export Finshed. Count:%d",export_times));
|
|
msgBox.exec();
|
|
|
|
|
|
|
|
font_res.close();
|
|
return 0;
|
|
}
|