namecreater/NameCreater/customlogo.cpp

302 lines
7.8 KiB
C++

#include "customlogo.h"
#include "ui_customlogo.h"
#include "QFileDialog"
#include <QMessageBox>
#include <QDateTime>
#include <QTextCodec>
#include <QTextStream>
#include <QBuffer>
#include <QDebug>
#include <QColorDialog>
static QPointF BG_POS_START = QPointF(159, 61);
static QPointF BG_POS_ROT_START = QPointF(900, 2094);
static qreal BG_WIDTH_OFFSET = 320;
static int NAME_COUNT_IN_PAGE = 12;
static QPointF NAME_OFFSET_START = QPointF(79, 365);
static qreal EACH_WORD_OFFSET = 142;
static QColor FONT_COLOR = QColor(0, 0, 0);
static QColorDialog* s_colorDialog = nullptr;
class QGraphicsCloneTextItem : public QGraphicsTextItem {
public:
QGraphicsCloneTextItem(const QString& text, QGraphicsItem* parent = nullptr) :
QGraphicsTextItem(text, parent) {
}
QGraphicsCloneTextItem* clone(QGraphicsItem* parent = nullptr) const {
QGraphicsCloneTextItem* newItem = new QGraphicsCloneTextItem(toPlainText(), parent);
newItem->setFont(font());
newItem->setPos(pos());
newItem->setDefaultTextColor(defaultTextColor());
return newItem;
}
};
CustomLogo::CustomLogo(QFont* font, QDialog* parent) :
QDialog(parent),
ui(new Ui::CustomLogo),
customFont(font),
fontColor(QColor(0, 0, 0))
{
ui->setupUi(this);
connect(ui->load_bg_btn, &QPushButton::released, this, &CustomLogo::onClickedBtn);
connect(ui->load_list_btn, &QPushButton::released, this, &CustomLogo::onClickedBtn);
connect(ui->color_btn, &QPushButton::released, this, &CustomLogo::onClickedBtn);
QFont _f(*font);
_f.setPixelSize(20);
QPalette palette = ui->color_label->palette();
palette.setColor(QPalette::WindowText, fontColor);
ui->color_label->setPalette(palette);
ui->color_label->setFont(_f);
ui->color_btn->setFont(_f);
ui->double_checkbox->setFont(_f);
}
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 % NAME_COUNT_IN_PAGE;
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);
qreal item_pos_x = rot ? BG_POS_ROT_START.x() : BG_POS_START.x() + idx * BG_WIDTH_OFFSET;
qreal item_pos_y = rot ? BG_POS_ROT_START.y() + (idx - 10) * BG_WIDTH_OFFSET : BG_POS_START.y();
name_bgItem->setPos(item_pos_x, item_pos_y);
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();
}
QGraphicsRectItem* name_rectItem = new QGraphicsRectItem(QRectF(0, 0, name_bgItem->boundingRect().width(), name_bgItem->boundingRect().height() / 2), name_bgItem);
name_rectItem->setPen(QPen(QColor(0, 0, 0, 0)));
QString name = name_info.name1;
int name_len = name.length();
QPointF pos = NAME_OFFSET_START;
qreal y_offset = 0;
if (name_len == 2) {
pos.setY(pos.y() + 65.0);
y_offset = 55;
}
for (int k = 0; k < name_len; k++) {
QGraphicsCloneTextItem* textItem = new QGraphicsCloneTextItem(name.mid(k, 1), name_rectItem);
textItem->setFont(*customFont);
textItem->setDefaultTextColor(fontColor);
qreal _posX = name_rectItem->boundingRect().width() / 2 - textItem->boundingRect().width() / 2;
qreal _posY = pos.y() + k * (EACH_WORD_OFFSET + y_offset);
qDebug() << "Name num:" << QString::number(k) << "PosY:" << QString::number(_posY);
textItem->setPos(_posX, _posY);
}
if (ui->double_checkbox->isChecked()) {
QGraphicsRectItem* name_reverse_rectItem = new QGraphicsRectItem(name_rectItem->rect(), name_bgItem);
name_reverse_rectItem->setPen(QPen(QColor(0, 0, 0, 0)));
name_reverse_rectItem->setPos(name_bgItem->boundingRect().width(), name_bgItem->boundingRect().height());
foreach(auto item, name_rectItem->childItems()) {
QGraphicsCloneTextItem* _ti = ((QGraphicsCloneTextItem*)item)->clone(name_reverse_rectItem);
}
name_reverse_rectItem->setRotation(180);
qDebug() << "name_rect item child count:" << name_rectItem->childItems().size();
}
bool is_export_page = ((idx + 1) % NAME_COUNT_IN_PAGE == 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::onColorSelected(const QColor& color)
{
qDebug() << "color accept: r:" << color.red() << ", g:" << color.green() <<", b:" << color.blue();
fontColor = QColor(color);
QPalette palette = ui->color_label->palette();
palette.setColor(QPalette::WindowText, fontColor);
ui->color_label->setPalette(palette);
}
void CustomLogo::onClickedBtn() {
auto sender = (QPushButton*)QObject::sender();
if (sender == ui->load_bg_btn) {
loadBGFromFile();
}
else if (sender == ui->load_list_btn) {
loadListFromFile();
}
else if (sender == ui->color_btn) {
if (s_colorDialog == nullptr) {
s_colorDialog = new QColorDialog(ui->color_label->palette().color(QPalette::WindowText), this);
connect(s_colorDialog, &QColorDialog::colorSelected, this, &CustomLogo::onColorSelected);
}
s_colorDialog->open();
}
}