272 lines
7.8 KiB
C++
272 lines
7.8 KiB
C++
#include "customlogo2.h"
|
|
#include "ui_customlogo2.h"
|
|
|
|
#include "QFileDialog"
|
|
|
|
#include <QMessageBox>
|
|
#include <QDateTime>
|
|
#include <QTextCodec>
|
|
#include <QTextStream>
|
|
#include <QBuffer>
|
|
#include <QDebug>
|
|
#include <QFontDatabase>
|
|
#include <QFileDialog>
|
|
|
|
|
|
//#define BG_DEMO_PATH ":/NameCreater/resource/custom_logo/demo_2.jpg"
|
|
#define BG_DEMO_PATH ":/NameCreater/resource/custom_logo/bg_2.jpg"
|
|
#define FONT_PATH ":/NameCreater/_exp/Ubuntu-Medium.ttf"
|
|
|
|
|
|
static QPoint BG_START_POS(20, 100);
|
|
static QPoint BG_ROT_START_POS(2080, 880);
|
|
static int BG_POS_COUNT = 12;
|
|
|
|
|
|
|
|
customlogo2::customlogo2(QFont *font, QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::customlogo2)
|
|
, zhFont(font)
|
|
{
|
|
ui->setupUi(this);
|
|
|
|
connect(ui->loadcsv_btn, &QPushButton::released, this,&customlogo2::OnClickedBtn);
|
|
|
|
|
|
// read ubuntu font
|
|
QFile font_file(FONT_PATH);
|
|
if (!font_file.open(QIODevice::ReadOnly)) {
|
|
QMessageBox msgBox;
|
|
msgBox.setText("Can not load font file!" + QString(FONT_PATH));
|
|
msgBox.exec();
|
|
font_file.close();
|
|
}
|
|
|
|
|
|
int id = QFontDatabase::addApplicationFontFromData(font_file.readAll());
|
|
QString font_families = QFontDatabase::applicationFontFamilies(id).at(0);
|
|
enFont = QFont(font_families);
|
|
enFont.setPixelSize(60);
|
|
|
|
// set zh font size
|
|
zhFont->setPixelSize(120);
|
|
|
|
|
|
}
|
|
|
|
customlogo2::~customlogo2()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
void customlogo2::OnClickedBtn(){
|
|
QPushButton *sender = (QPushButton*)QObject::sender();
|
|
|
|
if(sender == ui->loadcsv_btn){
|
|
loadListFromFile();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void customlogo2::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);
|
|
|
|
}
|
|
|
|
QList<NameInfo_t> customlogo2::getNameByFileContent(Name_Type_e type, const QByteArray& conetent)
|
|
{
|
|
QList<NameInfo_t> nameList;
|
|
|
|
QTextCodec* tc = QTextCodec::codecForName("Utf8");
|
|
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;
|
|
|
|
switch (type) {
|
|
case Name_zh:
|
|
{
|
|
int n = 1;
|
|
name = csvList.at(0);
|
|
|
|
while(n < csvList.count()){
|
|
if(n==2)eng_name.append(",");
|
|
eng_name.append(csvList.at(n));
|
|
n++;
|
|
}
|
|
eng_name.replace("\"","");
|
|
}
|
|
|
|
|
|
break;
|
|
default:
|
|
break;
|
|
|
|
}
|
|
|
|
qDebug() << "row :" << row << " " << name << " eng: "<<eng_name;
|
|
|
|
if (name.compare("name_1") == 0 || name.compare("name") == 0 ||
|
|
eng_name.compare("eng_name") == 0 ) {
|
|
//pass first line
|
|
continue;
|
|
}
|
|
|
|
info.name1 = name;
|
|
info.name_eng = eng_name;
|
|
|
|
nameList.append(info);
|
|
|
|
row++;
|
|
}
|
|
return nameList;
|
|
}
|
|
|
|
|
|
void customlogo2::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);
|
|
QRectF itemRect(0, 0, 880, 280);
|
|
QColor fontColor(33,142,188);
|
|
|
|
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_HEIGHT,BG_WIDTH);
|
|
bg_item->setBrush(QBrush(QColor(255, 255, 255)));
|
|
scene.addItem(bg_item);
|
|
}
|
|
|
|
|
|
QGraphicsPixmapItem* name_bgItem = new QGraphicsPixmapItem(QPixmap(BG_DEMO_PATH));
|
|
bool rot = (idx == 10 || idx == 11);
|
|
name_bgItem->setRotation(rot ? 90 : 0);
|
|
if (!rot)
|
|
name_bgItem->setPos(BG_START_POS.x(), BG_START_POS.y() + (name_bgItem->boundingRect().height()+30)*idx);
|
|
else
|
|
name_bgItem->setPos(BG_ROT_START_POS.x()+ (name_bgItem->boundingRect().height()+30)*(idx-10), BG_ROT_START_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();
|
|
}
|
|
|
|
QString name = name_info.name1;
|
|
QGraphicsTextItem* textItem = new QGraphicsTextItem(name, name_bgItem);
|
|
QFont _font = QFont(*zhFont);
|
|
_font.setLetterSpacing(QFont::PercentageSpacing, 110);
|
|
textItem->setFont(_font);
|
|
textItem->setDefaultTextColor(fontColor);
|
|
textItem->setPos(20+(itemRect.width() - textItem->boundingRect().width())/2,42);
|
|
|
|
|
|
scene.addItem(textItem);
|
|
|
|
QString name_en = name_info.name_eng;
|
|
QGraphicsTextItem* textItem_en = new QGraphicsTextItem(name_en, name_bgItem);
|
|
textItem_en->setFont(enFont);
|
|
textItem_en->setDefaultTextColor(fontColor);
|
|
textItem_en->setPos(20+(itemRect.width() - textItem_en->boundingRect().width())/2,170);
|
|
|
|
scene.addItem(textItem_en);
|
|
|
|
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);
|
|
export_times++;
|
|
|
|
scene.clear();
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
QMessageBox msgBox;
|
|
msgBox.setText(QString().asprintf("Export Finshed. Count:%d", export_times));
|
|
msgBox.exec();
|
|
|
|
}
|
|
|
|
void customlogo2::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");
|
|
qDebug() << "app director path" <<QCoreApplication::applicationDirPath();
|
|
#else
|
|
QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd");
|
|
#endif
|
|
|
|
image.save(folder_name.append(filename), "JPEG");
|
|
|
|
QByteArray imageData;
|
|
QBuffer buffer(&imageData);
|
|
buffer.open(QIODevice::WriteOnly);
|
|
QPixmap pixmap = QPixmap::fromImage(image);
|
|
pixmap.save(&buffer, "JPEG", 100); // You can also use other formats like "JPEG" or "BMP"
|
|
|
|
QFileDialog::saveFileContent(imageData, filename);
|
|
|
|
}
|
|
|