112 lines
4.1 KiB
C++
112 lines
4.1 KiB
C++
#include "tools.h"
|
|
#include <QBuffer>
|
|
#include <QFile>
|
|
#include <QPainter>
|
|
|
|
QString Tools::fileMd5(QString filePath) {
|
|
QFile file(filePath);
|
|
if(! file.open(QFile::ReadOnly)) return QString();
|
|
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
|
|
if(! cryptoHash.addData(&file)) {
|
|
file.close();
|
|
return QString();
|
|
}
|
|
file.close();
|
|
return QString::fromLatin1(cryptoHash.result().toHex());
|
|
}
|
|
QString Tools::saveImg(const QString& dir, const QFontMetrics& metric, const QFont& font, const QColor& color, const QString& str) {
|
|
if(str.isEmpty()) return QString();
|
|
QImage img(metric.horizontalAdvance(str), metric.lineSpacing(), QImage::Format_ARGB32);
|
|
img.fill(Qt::transparent);
|
|
{
|
|
QPainter painter(&img);
|
|
painter.setFont(font);
|
|
painter.setPen(color);
|
|
QTextOption opt(Qt::AlignCenter);
|
|
opt.setWrapMode(QTextOption::NoWrap);
|
|
painter.drawText(QRectF(0, 0, img.width(), img.height()), str, opt);
|
|
}
|
|
QByteArray data;
|
|
QBuffer buffer(&data);
|
|
buffer.open(QIODevice::WriteOnly);
|
|
if(! img.save(&buffer, "PNG")) return QString();
|
|
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
|
|
cryptoHash.addData(data);
|
|
auto md5 = QString::fromLatin1(cryptoHash.result().toHex());
|
|
QFile file(dir+"/"+md5);
|
|
if(! file.open(QFile::WriteOnly)) return QString();
|
|
file.write(data);
|
|
file.close();
|
|
return md5;
|
|
}
|
|
void Tools::saveImg(const QString& dir, const QFontMetrics& metric, const QFont& font, const QColor& color, JObj& imgs, const QString& str, const QString& name) {
|
|
if(str.isEmpty()) return;
|
|
QImage img(metric.horizontalAdvance(str), metric.lineSpacing(), QImage::Format_ARGB32);
|
|
img.fill(Qt::transparent);
|
|
{
|
|
QPainter painter(&img);
|
|
painter.setFont(font);
|
|
painter.setPen(color);
|
|
QTextOption opt(Qt::AlignCenter);
|
|
opt.setWrapMode(QTextOption::NoWrap);
|
|
painter.drawText(QRectF(0, 0, img.width(), img.height()), str, opt);
|
|
}
|
|
QByteArray data;
|
|
QBuffer buffer(&data);
|
|
buffer.open(QIODevice::WriteOnly);
|
|
if(! img.save(&buffer, "PNG")) return;
|
|
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
|
|
cryptoHash.addData(data);
|
|
auto md5 = QString::fromLatin1(cryptoHash.result().toHex());
|
|
QFile file(dir+"/"+md5);
|
|
if(! file.open(QFile::WriteOnly)) return;
|
|
file.write(data);
|
|
file.close();
|
|
imgs.insert(name, md5);
|
|
}
|
|
void Tools::saveImg2(const QString& dir, const QFontMetrics& metric, const QFont& font, const QColor& color, JArray& imgs, const QString& str, const QString& name, int height) {
|
|
JObj obj{
|
|
{"name", name}
|
|
};
|
|
if(! str.isEmpty()) {
|
|
QImage img(metric.horizontalAdvance(str), height ? height : metric.lineSpacing(), QImage::Format_ARGB32);
|
|
obj.insert("picWidth", img.width());
|
|
obj.insert("picHeight", img.height());
|
|
img.fill(Qt::transparent);
|
|
{
|
|
QPainter painter(&img);
|
|
painter.setFont(font);
|
|
painter.setPen(color);
|
|
QTextOption opt(Qt::AlignCenter);
|
|
opt.setWrapMode(QTextOption::NoWrap);
|
|
painter.drawText(QRectF(0, 0, img.width(), img.height()), str, opt);
|
|
}
|
|
QByteArray data;
|
|
QBuffer buffer(&data);
|
|
buffer.open(QIODevice::WriteOnly);
|
|
if(img.save(&buffer, "PNG")) {
|
|
QCryptographicHash cryptoHash(QCryptographicHash::Md5);
|
|
cryptoHash.addData(data);
|
|
auto md5 = QString::fromLatin1(cryptoHash.result().toHex());
|
|
obj.insert("id", md5);
|
|
QFile file(dir+"/"+md5);
|
|
if(file.open(QFile::WriteOnly)) {
|
|
file.write(data);
|
|
file.close();
|
|
}
|
|
}
|
|
}
|
|
imgs.append(obj);
|
|
}
|
|
|
|
QBrush Tools::getBrush(const QColor& color) {
|
|
return color.alpha()==0 ? Qt::NoBrush : QBrush(color);
|
|
}
|
|
QColor Tools::int2Color(int value) {
|
|
return QColor((value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff);
|
|
}
|
|
|
|
QString Tools::selectStr(bool f, const QString &s0, const QString &s1) {
|
|
return f ? s0 : s1;
|
|
}
|