91 lines
2.3 KiB
C++
91 lines
2.3 KiB
C++
|
#include "RecourceHelper.h"
|
||
|
|
||
|
#include <QApplication>
|
||
|
#include <QLocale>
|
||
|
|
||
|
#include <QFontDatabase>
|
||
|
#include <QLabel>
|
||
|
#include <QAbstractButton>
|
||
|
#include <QDebug>
|
||
|
#include <QFile>
|
||
|
#include <QAction>
|
||
|
|
||
|
#include "config.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();
|
||
|
}
|
||
|
|
||
|
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 {
|
||
|
qDebug() << "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 transName = QString("./translations/%1_%2.qm").arg(appName, QLocale().name());
|
||
|
#endif
|
||
|
qDebug() << transName;
|
||
|
bool success = trans_.load(transName);
|
||
|
if (!success) {
|
||
|
qDebug() << "load translations error";
|
||
|
}
|
||
|
QApplication::installTranslator(&trans_);
|
||
|
}
|