107 lines
2.9 KiB
C++
107 lines
2.9 KiB
C++
#include "RecourceHelper.h"
|
|
|
|
#include <QApplication>
|
|
#include <QLocale>
|
|
|
|
#include <QFontDatabase>
|
|
#include <QLabel>
|
|
#include <QAbstractButton>
|
|
#include <QDebug>
|
|
#include <QFile>
|
|
#include <QAction>
|
|
|
|
#include "config.h"
|
|
#include "common/SpdLogger.h"
|
|
|
|
template<> RecourceHelper* Singleton<RecourceHelper>::instance_ = nullptr;
|
|
RecourceHelper::RecourceHelper(QObject* parent)
|
|
: QObject(parent) {
|
|
|
|
Init();
|
|
}
|
|
|
|
RecourceHelper::~RecourceHelper() {
|
|
}
|
|
|
|
void RecourceHelper::OnDestory() {
|
|
|
|
}
|
|
|
|
void RecourceHelper::SetIcon(QLabel* lab, QChar c, quint32 size) {
|
|
iconFont_.setPointSize(size);
|
|
lab->setFont(iconFont_);
|
|
lab->setText(c);
|
|
}
|
|
|
|
void RecourceHelper::SetIcon(QAbstractButton* btn, QChar c, quint32 size) {
|
|
iconFont_.setPointSize(size);
|
|
btn->setFont(iconFont_);
|
|
btn->setText(c);
|
|
}
|
|
|
|
void RecourceHelper::OnChangeStyle() {
|
|
QAction* act = reinterpret_cast<QAction*>(sender());
|
|
QString skin = act->data().toString();
|
|
|
|
ChangeSkin(skin);
|
|
}
|
|
|
|
const QString RecourceHelper::GetBasePath() const {
|
|
return QApplication::applicationDirPath();
|
|
}
|
|
|
|
const QString RecourceHelper::GetResourcesPath() const {
|
|
return QApplication::applicationDirPath() + "/resources";
|
|
}
|
|
|
|
void RecourceHelper::ChangeSkin(const QString& skin) {
|
|
#if _DEBUG
|
|
const QString qssFile = QString("%1skin/%2.qss").arg(QString(CONFIG_PATH)).arg(skin);
|
|
#else
|
|
const QString appDirPath = QApplication::applicationDirPath();
|
|
const QString qssFile = QString("%1/config/skin/%2.qss").arg(appDirPath).arg(skin);
|
|
#endif
|
|
QFile file(qssFile);
|
|
if (file.open(QFile::ReadOnly | QFile::Text)) {
|
|
QTextStream stream(&file);
|
|
QString qss = stream.readAll();
|
|
qApp->setStyleSheet(qss);
|
|
file.close();
|
|
}
|
|
}
|
|
|
|
void RecourceHelper::Init() {
|
|
int fontId = QFontDatabase::addApplicationFont(":/fonts/res/fonts/fontawesome-webfont.ttf");
|
|
QStringList fontName = QFontDatabase::applicationFontFamilies(fontId);
|
|
|
|
if (fontName.count() > 0) {
|
|
iconFont_ = QFont(fontName.at(0));
|
|
} else {
|
|
LOG_WARN("fontName count <= 0 error");
|
|
}
|
|
|
|
const QString appName = QApplication::applicationDisplayName();
|
|
#ifndef NDEBUG
|
|
const QString transName = QString("./%1_%2.qm").arg(appName, QLocale().name());
|
|
#else
|
|
const QString appDirPath = QApplication::applicationDirPath();
|
|
const QString transName = QString("%1/translations/%2_%3.qm").arg(appDirPath, appName, QLocale().name());
|
|
#endif
|
|
qDebug() << transName;
|
|
bool success = trans_.load(transName);
|
|
if (success) {
|
|
QApplication::installTranslator(&trans_);
|
|
} else {
|
|
LOG_WARN("load translations error: {}", transName.toStdString());
|
|
}
|
|
|
|
const QString systemTransName = QString("./translations/qt_%1.qm").arg( QLocale::system().name());
|
|
success = systemTrans_.load(systemTransName);
|
|
if (success) {
|
|
QApplication::installTranslator(&systemTrans_);
|
|
} else {
|
|
LOG_WARN("load translations error: {}", systemTransName.toStdString());
|
|
}
|
|
|
|
}
|