145 lines
4.2 KiB
C++
145 lines
4.2 KiB
C++
#include "NewFiveCreater.h"
|
||
#include <QTextCodec>
|
||
#include <QTextStream>
|
||
#include <QDebug>
|
||
#include <QMessageBox>
|
||
#include <QDateTime>
|
||
|
||
|
||
static QString BG_SMAPLE_PATH = ":/NameCreater/resource/five/new_bg_sample.jpg";
|
||
static QString LABEL_BG_PATH_FORMAT = ":/NameCreater/resource/five/%1_%2.jpg";
|
||
|
||
static int LABEL_HIGHT_LIMIT = 350;
|
||
static QPoint LABEL_POS_START = QPoint(145, 55);
|
||
static int LABEL_POS_X_OFFSET = 323;
|
||
|
||
static QPoint NAME_OFFSET_START = QPoint(90, 335);
|
||
static QPoint NAME_OFFSET_REVERSE_START = QPoint(216, 1398);
|
||
static qreal NAME_TWO_WORD_OFFSET = 170;
|
||
static qreal NAME_THREE_WORD_OFFSET = 145;
|
||
|
||
static int MAX_LABEL_IN_PAPER = 10;
|
||
static int FONT_PIXEL_SIZE = 120;
|
||
|
||
|
||
NewFiveCreater::NewFiveCreater() : INameCreater()
|
||
{
|
||
}
|
||
|
||
NewFiveCreater::~NewFiveCreater()
|
||
{
|
||
}
|
||
|
||
QList<NameInfo_t> NewFiveCreater::getNameByFileContent(Name_Type_e type, const QByteArray& conetent)
|
||
{
|
||
QList<NameInfo_t> 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 name = csvList.at(0); //QString::fromLocal8Bit(csvList.at(0));
|
||
QString bg_type = csvList.at(1);//QString::fromLocal8Bit(csvList.at(1));
|
||
|
||
qDebug() << "row :" << row << " " << name << ", bg_type: " << bg_type;
|
||
|
||
if (name.compare("name") == 0) {
|
||
//pass first line
|
||
continue;
|
||
}
|
||
|
||
info.name1 = name;
|
||
info.bg_path = bg_type;
|
||
|
||
nameList.append(info);
|
||
|
||
row++;
|
||
}
|
||
return nameList;
|
||
|
||
}
|
||
|
||
void NewFiveCreater::generaImageFromCSV(Name_Type_e type, const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color)
|
||
{
|
||
|
||
int export_times = 0;
|
||
|
||
QList<NameInfo_t> nameList = getNameByFileContent(type, 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);
|
||
|
||
|
||
int idx = 0;
|
||
foreach(auto name, nameList) {
|
||
if ((idx % MAX_LABEL_IN_PAPER) == 0) {
|
||
QGraphicsRectItem* bg_item = new QGraphicsRectItem(0, 0, BG_WIDTH, BG_HEIGHT);
|
||
bg_item->setBrush(QBrush(QColor(255, 255, 255)));
|
||
//QGraphicsPixmapItem* bg_item = QGraphicsPixmapItem(QPixmap(BG_SMAPLE_PATH));
|
||
scene.addItem(bg_item);
|
||
}
|
||
|
||
for (int l = 0; l < 5; l++) {
|
||
int col_index = idx % MAX_LABEL_IN_PAPER;
|
||
QString label_rel_path = LABEL_BG_PATH_FORMAT.arg(name.bg_path, QString::number(l + 1));
|
||
auto label_item = new QGraphicsPixmapItem(QPixmap(label_rel_path));
|
||
label_item->setPos(LABEL_POS_START.x() + LABEL_POS_X_OFFSET * col_index, LABEL_POS_START.y());
|
||
label_item->setOpacity(1);
|
||
scene.addItem(label_item);
|
||
|
||
for (int i = 0; i < 2; i++) {
|
||
int name_word_len = name.name1.count();
|
||
qreal two_word_y = (name_word_len == 2) ? NAME_TWO_WORD_OFFSET / 2.0 : 0;
|
||
qreal offset_y = (name_word_len == 2) ? NAME_TWO_WORD_OFFSET : NAME_THREE_WORD_OFFSET;
|
||
|
||
for (int w = 0; w < name_word_len; w++) {
|
||
QGraphicsTextItem* word_item = new QGraphicsTextItem(name.name1.at(w));
|
||
word_item->setFont(font);
|
||
word_item->setDefaultTextColor(font_color);
|
||
qreal _x = (i == 0) ? label_item->x() + NAME_OFFSET_START.x() : label_item->x() + NAME_OFFSET_REVERSE_START.x();
|
||
qreal _y = (i == 0) ? (label_item->y() + NAME_OFFSET_START.y() + two_word_y + offset_y * w) : (label_item->y() + NAME_OFFSET_REVERSE_START.y() - two_word_y - offset_y * w);
|
||
if (i == 1) word_item->setTransform(QTransform::fromScale(-1, -1));
|
||
word_item->setPos(_x, _y);
|
||
scene.addItem(word_item);
|
||
|
||
}
|
||
}
|
||
|
||
if ((idx % MAX_LABEL_IN_PAPER == MAX_LABEL_IN_PAPER - 1) || idx == nameList.count() * 5 - 1) {
|
||
QString date_str = QDateTime::currentDateTime().toString("MMddhhmm");
|
||
QString file_name = QString(u8"<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD>_%2_%1.jpg").arg(QString::number(export_times + 1), date_str);
|
||
saveToImage(file_name, &scene);
|
||
export_times++;
|
||
scene.clear();
|
||
|
||
}
|
||
|
||
idx++;
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
QMessageBox msgBox;
|
||
msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times));
|
||
msgBox.exec();
|
||
|
||
}
|