finish layout adjustment.

This commit is contained in:
shouchih_chen 2022-02-19 08:44:26 +08:00
parent 4db00ecdfa
commit 615fa0e283
12 changed files with 197 additions and 141 deletions

View File

@ -1,8 +1,9 @@
<RCC> <RCC>
<qresource prefix="/NameCreater"> <qresource prefix="/NameCreater">
<file>resource/background.jpg</file>
<file>resource/background_sample.jpg</file> <file>resource/background_sample.jpg</file>
<file>resource/background_boy.jpg</file> <file>resource/background_b.jpg</file>
<file>resource/background_girl.jpg</file> <file>resource/background_g.jpg</file>
<file>resource/background_r.jpg</file>
<file>resource/background_y.jpg</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -1,4 +1,13 @@
name_1,name_2,bg_type name_1,bg_type
ÄŹ´éśŁ,ÄŹ¤_ŽŚ,mix <EFBFBD>,r
łŻĽKŽŚ,łŻŹfżÎ,boy <EFBFBD>,g
§őŤBŞä,§őŤBŞä,girl <EFBFBD>衱郝,b
<EFBFBD>,y
<EFBFBD>,r
<EFBFBD>,g
<EFBFBD>衱郝,b
<EFBFBD>,y
<EFBFBD>,r
<EFBFBD>,g
<EFBFBD>,y
<EFBFBD>,b

1 name_1 name_2 bg_type
2 ÄŹ´éśŁ ︺�诧 ÄŹ¤_ŽŚ mix r
3 łŻĽKŽŚ 甃�和 łŻŹfżÎ boy g
4 §őŤBŞä �衱郝 §őŤBŞä girl b
5 え�丹 y
6 ︺�诧 r
7 甃�和 g
8 �衱郝 b
9 え�丹 y
10 ︺�诧 r
11 甃�和 g
12 ︺�诧 y
13 甃�和 b

View File

@ -12,9 +12,9 @@
#include <QDateTime> #include <QDateTime>
#include <QFontDatabase> #include <QFontDatabase>
#define BG_MIX_PATH ":/NameCreater/resource/background.jpg"
#define BG_BOY_PATH ":/NameCreater/resource/background_boy.jpg" #define BG_SAMPLE_PATH ":/NameCreater/resource/background_sample.jpg"
#define BG_GIRL_PATH ":/NameCreater/resource/background_girl.jpg" #define BG_PATH_FORMAT ":/NameCreater/resource/background_%1.jpg"
#define FONT_NAME "DFYuanStd-W8.otf" #define FONT_NAME "DFYuanStd-W8.otf"
#define FONT_SIZE 120 #define FONT_SIZE 120
//#define FONT_SIZE 74 //#define FONT_SIZE 74
@ -24,134 +24,142 @@
#define BG_GIRL_TYPE_STR "girl" #define BG_GIRL_TYPE_STR "girl"
struct NameInfo_t { struct NameInfo_t {
QString name1; QString name1;
QString name2; QString name2;
int bg_type; QString bg_path;
}; };
static QPointF BOY_POS[] = { static QPointF BG_POS[] = {
QPointF(235, 387), QPointF(366, 1433), QPointF(159, 61), QPointF(482, 61), QPointF(802, 61),
QPointF(555, 387), QPointF(692, 1433), QPointF(1122, 61), QPointF(1449, 61), QPointF(1772, 61),
QPointF(878, 387), QPointF(1014, 1433), QPointF(2095, 61), QPointF(2414, 61), QPointF(2738, 61),
QPointF(1200, 387), QPointF(1337, 1433), QPointF(3060, 61), QPointF(900, 2094), QPointF(900, 2420),
QPointF(1527, 387), QPointF(1663, 1433),
}; };
static int BOY_POS_COUNT = sizeof(BOY_POS) / sizeof(BOY_POS[0]); static int BG_POS_COUNT = sizeof(BG_POS) / sizeof(BG_POS[0]);
static QPointF GIRL_POS[] = { static QPointF NAME_POS[] = {
QPointF(1854, 387), QPointF(1987, 1433), QPointF(240, 430), QPointF(372, 1402),
QPointF(2176, 387), QPointF(2314, 1433), QPointF(564, 430), QPointF(694, 1402),
QPointF(2498, 387), QPointF(2635, 1433), QPointF(883, 430), QPointF(1014, 1402),
QPointF(2821, 387), QPointF(2958, 1433), QPointF(1205, 430), QPointF(1337, 1402),
QPointF(3147, 387), QPointF(3285, 1433), 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 int GIRL_POS_COUNT = sizeof(GIRL_POS) / sizeof(GIRL_POS[0]); static int NAME_POS_COUNT = sizeof(NAME_POS) / sizeof(NAME_POS[0]);
void saveToImage(const QString& filename, QGraphicsScene* scene) { void saveToImage(const QString& filename, QGraphicsScene* scene) {
scene->clearSelection(); // Selections would also render to the file scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents 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 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 image.fill(Qt::transparent); // Start all pixels transparent
int dpm = 300 / 0.0254; // ~300 DPI int dpm = 300 / 0.0254; // ~300 DPI
image.setDotsPerMeterX(dpm); image.setDotsPerMeterX(dpm);
image.setDotsPerMeterY(dpm); image.setDotsPerMeterY(dpm);
QPainter painter(&image); QPainter painter(&image);
scene->render(&painter); scene->render(&painter);
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
QString folder_name = QCoreApplication::applicationDirPath()+"/../../../export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); QString folder_name = QCoreApplication::applicationDirPath() + "/../../../export_" + QDateTime::currentDateTime().toString("yyyyMMdd");
#else #else
QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd"); QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd");
#endif #endif
qDebug() << "folder_name :" << folder_name; qDebug() << "folder_name :" << folder_name;
if (!QDir(folder_name).exists()) { if (!QDir(folder_name).exists()) {
QDir().mkdir(folder_name); QDir().mkdir(folder_name);
} }
image.save(QString(folder_name).append("/").append(filename), "JPG", 100); image.save(QString(folder_name).append("/").append(filename), "JPG", 100);
} }
QList<NameInfo_t> getNameByFile(const QString& filename) { QList<NameInfo_t> getNameByFile(const QString& filename) {
QList<NameInfo_t> nameList; QList<NameInfo_t> nameList;
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
QString root_path = QCoreApplication::applicationDirPath() + "/../../../"; QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
#else #else
QString root_path = QCoreApplication::applicationDirPath() + "/"; QString root_path = QCoreApplication::applicationDirPath() + "/";
#endif #endif
QFile file(root_path.append(filename)); QFile file(root_path.append(filename));
if (!file.open(QIODevice::ReadOnly)) { if (!file.open(QIODevice::ReadOnly)) {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText("Load file failed!"); msgBox.setText("Load file failed!");
msgBox.exec(); msgBox.exec();
file.close(); file.close();
return nameList; return nameList;
} }
int row = 0; int row = 0;
QTextCodec *tc = QTextCodec::codecForName("Big5"); QTextCodec* tc = QTextCodec::codecForName("Big5");
while (!file.atEnd()) while (!file.atEnd())
{ {
NameInfo_t info; NameInfo_t info;
QByteArray line = file.readLine(); QByteArray line = file.readLine();
QString name_tc = tc->toUnicode(line); QString name_tc = tc->toUnicode(line);
QStringList csvList = name_tc.split(','); QStringList csvList = name_tc.split(',');
QString name1 = csvList.at(0); //QString::fromLocal8Bit(csvList.at(0)); QString name1 = csvList.at(0);
QString name2 = csvList.at(1);//QString::fromLocal8Bit(csvList.at(1)); QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last());
QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last()); qDebug() << "row :" << row << " " << name1;
qDebug() << "row :" << row << " " << name1 << " " << name2; type_str = type_str.remove(QRegExp("\r\n"));
if (name1.compare("name_1") == 0 || name2.compare("name_2") == 0) { if (name1.compare("name_1") == 0) {
continue; continue;
} }
info.name1 = name1; info.name1 = name1;
info.name2 = name2; info.bg_path = QString(BG_PATH_FORMAT).arg(type_str);
info.bg_type = type_str.contains(QRegExp("boy")) ? 1 : (type_str.contains(QRegExp("girl"))) ? 2 : 0;
nameList.append(info);
row++; nameList.append(info);
}
row++;
}
file.close(); file.close();
return nameList; return nameList;
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int export_times = 0; int export_times = 0;
QApplication a(argc, argv); QApplication a(argc, argv);
// NameCreater w; // NameCreater w;
// w.show(); // w.show();
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
QString root_path = QCoreApplication::applicationDirPath() + "/../../../"; QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
#else #else
QString root_path = QCoreApplication::applicationDirPath() + "/"; QString root_path = QCoreApplication::applicationDirPath() + "/";
#endif #endif
QString font_path = root_path.append(FONT_NAME); QString font_path = root_path.append(FONT_NAME);
QFile font_res(font_path); QFile font_res(font_path);
if (!font_res.open(QIODevice::ReadOnly)) { if (!font_res.open(QIODevice::ReadOnly)) {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText("Can not load font file!" + font_path); msgBox.setText("Can not load font file!" + font_path);
msgBox.exec(); msgBox.exec();
font_res.close(); font_res.close();
return 0; return 0;
@ -159,79 +167,117 @@ int main(int argc, char* argv[])
int id = QFontDatabase::addApplicationFontFromData(font_res.readAll()); int id = QFontDatabase::addApplicationFontFromData(font_res.readAll());
QStringList family_list = QFontDatabase::applicationFontFamilies(id); QStringList family_list = QFontDatabase::applicationFontFamilies(id);
QFont font = QFont(family_list.at(0)); QFont font = QFont(family_list.at(0));
font.setPixelSize(FONT_SIZE); font.setPixelSize(FONT_SIZE);
QColor font_color(0, 0, 0); QColor font_color(0, 0, 0);
QList<NameInfo_t> nameList = getNameByFile("name.csv"); QList<NameInfo_t> nameList = getNameByFile("name.csv");
if (nameList.isEmpty()) { if (nameList.isEmpty()) {
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText(("Name is empty !")); msgBox.setText(("Name is empty !"));
msgBox.exec(); msgBox.exec();
} }
QGraphicsScene scene; QGraphicsScene scene(0, 0, 3508, 2482);
QGraphicsView view(&scene); scene.setBackgroundBrush(QBrush(QColor(255, 255, 255)));
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); //QImage image(BG_SAMPLE_PATH);
//QGraphicsPixmapItem* bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(image));
scene.clear(); //scene.addItem(bgItem);
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++) { for (int n = 0; n < nameList.length(); n++) {
int flag = j % 2; if (n >= BG_POS_COUNT)break;
QString name = (flag == 0) ? name_info.name1 : name_info.name2; NameInfo_t name_info = nameList.at(n);
for (int i = 0; i < BOY_POS_COUNT; i++) { QImage name_image(name_info.bg_path);
int xscale, yscale; QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(QPixmap::fromImage(name_image));
xscale = yscale = (i % 2 == 1) ? -1 : 1; bool rot = (n == 10 || n == 11);
QPointF pos = (flag == 0) ? BOY_POS[i] : GIRL_POS[i]; name_bgItem->setRotation(rot ? -90 : 0);
name_bgItem->setPos(BG_POS[n]);
QGraphicsTextItem* textItem1 = new QGraphicsTextItem(name.mid(0, 1)); scene.addItem(name_bgItem);
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; if (name_info.name1.isEmpty()) {
msgBox.setText(QString().asprintf("Export Finshed. Count:%d",export_times)); QMessageBox msgBox;
msgBox.exec(); msgBox.setText(QString().asprintf("Name is Empty at %d", export_times + 1));
msgBox.exec();
}
QString name = name_info.name1;
for (int j = 0; j < 2; j++) {
int pos_idx = n * 2 + j;
int xscale, yscale;
xscale = yscale = (j == 1) ? -1 : 1;
QPointF pos = NAME_POS[pos_idx];
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 * xscale);
textItem2->setTransform(QTransform::fromScale(xscale, yscale));
}
else {
textItem2->setPos(pos.x() + 145 * xscale, pos.y());
textItem2->setRotation(-90);
textItem2->setTransform(QTransform::fromScale(xscale, yscale));
}
scene.addItem(textItem2);
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);
}
}
QString file_name = QString("%1_.jpg").arg(QString::number(export_times + 1));
saveToImage(file_name, &scene);
export_times++;
/*QMessageBox msgBox;
msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times));
msgBox.exec();*/
font_res.close(); font_res.close();
return 0; return a.exec();
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 225 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 85 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 225 KiB