namecreater/NameCreater/customlogo.cpp
2024-09-20 13:25:29 +08:00

238 lines
5.7 KiB
C++

#include "customlogo.h"
#include "ui_customlogo.h"
#include "QFileDialog"
#include <QMessageBox>
#include <QDateTime>
#include <QTextCodec>
#include <QTextStream>
#include <QBuffer>
#include <QDebug>
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_OFFSET_START = QPointF(79, 377);
static qreal EACH_WORD_OFFSET = 142;
static QColor FONT_COLOR = QColor(0, 0, 0);
CustomLogo::CustomLogo(QFont* font, QDialog* parent):
QDialog(parent),
ui(new Ui::CustomLogo),
customFont(font)
{
ui->setupUi(this);
connect(ui->load_bg_btn, &QPushButton::released,this, &CustomLogo::onClickedBtn);
connect(ui->load_list_btn, &QPushButton::released, this, &CustomLogo::onClickedBtn);
}
CustomLogo::~CustomLogo()
{
delete ui;
}
void CustomLogo::loadBGFromFile()
{
auto fileContentReady = [this](const QString& fileName, const QByteArray& fileContent) {
if (fileName.isEmpty()) {
// No file was selected
}else{
QPixmap pix;
if(pix.loadFromData(fileContent,"JPG")) {
ui->display_label->setPixmap(pix);
}else{
qDebug()<<"Data content not image format";
}
}
};
QFileDialog::getOpenFileContent("Image (*.jpg)", fileContentReady);
}
void CustomLogo::loadListFromFile()
{
auto fileContentReady = [this](const QString& fileName, const QByteArray& fileContent) {
if (fileName.isEmpty()) {
// No file was selected
}
else {
QList<NameInfo_t> nameList = getNameByFileContent(Name_zh, fileContent);
if (!nameList.isEmpty()) {
qDebug() << "List size: "<< QString::number(nameList.count());
gerneraImageFromList(nameList, Name_zh);
}
else {
qDebug() << "List is empty!!";
}
}
};
QFileDialog::getOpenFileContent("CSV file (*.csv)", fileContentReady);
}
void CustomLogo::gerneraImageFromList(const QList<NameInfo_t>& nameList, Name_Type_e type)
{
int export_times = 0;
QGraphicsScene scene;
scene.setBackgroundBrush(QBrush(QColor(255, 255, 255)));
QGraphicsView view(&scene);
switch (type) {
case Name_zh:
{
for (int n = 0; n < nameList.length(); n++) {
int idx = n % BG_POS_COUNT;
NameInfo_t name_info = nameList.at(n);
if (idx == 0) {
QGraphicsRectItem* bg_item = new QGraphicsRectItem(0, 0, BG_WIDTH, BG_HEIGHT);
bg_item->setBrush(QBrush(QColor(255, 255, 255)));
scene.addItem(bg_item);
}
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QPixmap _bg = ui->display_label->pixmap()->copy();
#else
QPixmap _bg = ui->display_label->pixmap().copy();
#endif //
QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(_bg);
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 < 1; j++) { // never reverse
qreal xscale, yscale;
xscale = yscale = (j == 1) ? -1 : 1;
QPointF pos = NAME_OFFSET_START;
qreal y_offset = 0;
if (name_len == 2) {
if (!rot) {
pos.setY(pos.y() + 82.0 * xscale);
}
else {
pos.setX(pos.x() + 82.0 * xscale);
}
y_offset = 25;
}
for (int k = 0; k < name_len; k++) {
QGraphicsTextItem* textItem = new QGraphicsTextItem(name.mid(k, 1), name_bgItem);
textItem->setFont(*customFont);
textItem->setDefaultTextColor(FONT_COLOR);
textItem->setPos(pos.x(),pos.y() + k* EACH_WORD_OFFSET + y_offset);
textItem->setTransform(QTransform::fromScale(xscale, yscale));
scene.addItem(textItem);
}
}
bool is_export_page = ((idx + 1) % BG_POS_COUNT == 0);
if (is_export_page || n == nameList.count() - 1) {
QString date_str = QDateTime::currentDateTime().toString("MMddhhmm");
QString file_name = QString("Signal_%2_%1.jpg").arg(QString::number(export_times + 1), date_str);
//saveToImage(file_name, &scene);
INameCreater().saveToImage(file_name, &scene);
export_times++;
scene.clear();
}
}
}
break;
default:
break;
}
QMessageBox msgBox;
msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times));
msgBox.exec();
}
QList<NameInfo_t> CustomLogo::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, eng_name, bg_type;
switch (type) {
case Name_zh:
name = csvList.at(0);
break;
default:
break;
}
qDebug() << "row :" << row << " " << name;
if (name.compare("name_1") == 0 || name.compare("name") == 0 ||
eng_name.compare("eng_name") == 0 ) {
//pass first line
continue;
}
info.name1 = name;
nameList.append(info);
row++;
}
return nameList;
}
void CustomLogo::onClickedBtn() {
auto sender = (QPushButton*)QObject::sender();
if (sender == ui->load_bg_btn) {
loadBGFromFile();
}
else if (sender == ui->load_list_btn) {
loadListFromFile();
}
}