#include "appinit.h" #include "quihelper.h" QScopedPointer AppInit::self; AppInit *AppInit::Instance() { if (self.isNull()) { static QMutex mutex; QMutexLocker locker(&mutex); if (self.isNull()) { self.reset(new AppInit); } } return self.data(); } AppInit::AppInit(QObject *parent) : QObject(parent) { } bool AppInit::eventFilter(QObject *watched, QEvent *event) { QWidget *w = (QWidget *)watched; if (!w->property("canMove").toBool()) { return QObject::eventFilter(watched, event); } static QPoint mousePoint; static bool mousePressed = false; QMouseEvent *mouseEvent = static_cast(event); if (mouseEvent->type() == QEvent::MouseButtonPress) { if (mouseEvent->button() == Qt::LeftButton) { mousePressed = true; mousePoint = mouseEvent->globalPos() - w->pos(); } } else if (mouseEvent->type() == QEvent::MouseButtonRelease) { mousePressed = false; } else if (mouseEvent->type() == QEvent::MouseMove) { if (mousePressed) { w->move(mouseEvent->globalPos() - mousePoint); return true; } } return QObject::eventFilter(watched, event); } void AppInit::start() { //绑定全局事件过滤器,整体无边框拖动 qApp->installEventFilter(this); //设置全局字体 QUIHelper::setFont(); //设置中文编码 QUIHelper::setCode(); //设置翻译文件 QUIHelper::setTranslator(); //读取配置文件 AppConfig::ConfigFile = QString("%1/%2.ini").arg(QUIHelper::appPath()).arg(QUIHelper::appName()); AppConfig::readConfig(); //加载样式表 this->initStyle(AppConfig::StyleName); } void AppInit::initStyle(const QString &qssFile) { QFile file(qssFile); if (file.open(QFile::ReadOnly)) { QString qss = QLatin1String(file.readAll()); file.close(); QUIStyle::getQssColor(qss, QUIConfig::TextColor, QUIConfig::PanelColor, QUIConfig::BorderColor, QUIConfig::NormalColorStart, QUIConfig::NormalColorEnd, QUIConfig::DarkColorStart, QUIConfig::DarkColorEnd, QUIConfig::HighColor); //追加额外的样式 QStringList list; //菜单栏左侧线条为0,不然重叠很难看 list.append("QMenuBar{border-left-width:0px;}"); //树状菜单上边和下边线条去掉,顶部往下移动一点像素 list.append("QTreeWidget{border-width:0px 1px 0px 0px;padding-top:3px;}"); //边框为0 list.append("QTableWidget{border-width:1px 0px 0px 0px;}"); list.append("QHeaderView::section{border-width:0px 0px 1px 1px;}"); list.append("#labOpenGL{font:50px;}"); list.append(QString("#labLogo{background-color:%1;}").arg(QUIConfig::NormalColorStart)); list.append(QString("#widgetOpenGL{background-color:%1;}").arg(QUIConfig::NormalColorStart)); list.append(QString("NavTitle{qproperty-leftIcon:0;qproperty-bgColor:%1;qproperty-textColor:%2;" "qproperty-iconNormalColor:%2;qproperty-iconHoverColor:%2;qproperty-iconPressColor:%2;}") .arg(QUIConfig::BorderColor).arg(QUIConfig::TextColor)); qss += list.join(""); qApp->setPalette(QPalette(QUIConfig::PanelColor)); qApp->setStyleSheet(qss); emit changeStyle(qssFile); } }