#include "INameCreater.h" #include #include #include #include #include #include #include #include #include #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(folder_name + "/"+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 INameCreater::getNameByFile(const QString& filename) { QList 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(QRegExp("\r")); type_str = type_str.remove(QRegExp("\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 INameCreater::getNameByFileContent(const QByteArray& conetent) { QList 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(QRegExp("\r")); type_str = type_str.remove(QRegExp("\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(const QString& filename, const QByteArray& filecontent, const QFont& font, const QColor& font_color) { }