namecreater/NameCreater/INameCreater.cpp

171 lines
4.6 KiB
C++

#include "INameCreater.h"
#include <QDebug>
#include <QApplication>
#include <QPainter>
#include <QDateTime>
#include <QBuffer>
#include <QFileDialog>
#include <QTextCodec>
#include <QTextStream>
#include <QMessageBox>
#include <QRegularExpression>
#define BG_PATH_FORMAT ":/NameCreater/resource/background_%1.jpg"
INameCreater::INameCreater()
{
}
INameCreater::~INameCreater()
{
}
void INameCreater::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");
#else
QString folder_name = QCoreApplication::applicationDirPath() + "/export_" + QDateTime::currentDateTime().toString("yyyyMMdd");
#endif
image.save(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);
}
QList<NameInfo_t> INameCreater::getNameByFile(Name_Type_e type, const QString& filename)
{
QList<NameInfo_t> nameList;
#ifdef Q_OS_MAC
QString root_path = QCoreApplication::applicationDirPath() + "/../../../";
#else
QString root_path = "";//QCoreApplication::applicationDirPath() + "/";
#endif
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox msgBox;
msgBox.setText(QString("Load file failed!").append(filename));
msgBox.exec();
file.close();
return nameList;
}
int row = 0;
QTextCodec* tc = QTextCodec::codecForName("Big5");
while (!file.atEnd())
{
NameInfo_t info;
QByteArray line = file.readLine();
QString name_tc = tc->toUnicode(line);
QStringList csvList = name_tc.split(',');
QString name1 = csvList.at(0);
QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last());
qDebug() << "row :" << row << " " << name1;
type_str = type_str.remove(QRegularExpression("\r"));
type_str = type_str.remove(QRegularExpression("\n"));
if (name1.compare("name_1") == 0) {
continue;
}
info.name1 = name1;
info.bg_path = QString(BG_PATH_FORMAT).arg(type_str);
info.is_number_bg = type_str[0].isDigit();
nameList.append(info);
row++;
}
file.close();
return nameList;
}
QList<NameInfo_t> INameCreater::getNameByFileContent(Name_Type_e type, const QByteArray& conetent)
{
(void)type;
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 name1 = csvList.at(0);
QString type_str = csvList.last();//QString::fromLocal8Bit(csvList.last());
qDebug() << "row :" << row << " " << name1;
type_str = type_str.remove(QRegularExpression("\r"));
type_str = type_str.remove(QRegularExpression("\n"));
if (name1.compare("name_1") == 0) {
continue;
}
info.name1 = name1;
info.bg_path = QString(BG_PATH_FORMAT).arg(type_str);
info.is_number_bg = type_str[0].isDigit();
nameList.append(info);
row++;
}
return nameList;
}
void INameCreater::generaImageFromCSV(Name_Type_e type, const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color)
{
}
int INameCreater::getFontSizebyStr(const QFont& font, int max_font_size,int max_label_width, const QString& str)
{
QFont _font(font);
int font_size = max_font_size;
while (font_size > 10) {
_font.setPixelSize(font_size);
QFontMetrics fm(_font);
if (fm.horizontalAdvance(str) <= max_label_width) return font_size;
font_size--;
}
return font_size;
}