Skip to content

Commit

Permalink
feat: 优化配置逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
mhduiy committed Dec 21, 2024
1 parent 830386f commit 06ab7e9
Show file tree
Hide file tree
Showing 34 changed files with 92 additions and 79 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ add_executable(${EXE_NAME}
)

add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/adb)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/appPageTools)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/appPageTool)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/components)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/controlPageTool)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/flashPageTools)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/flashPageTool)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/imagePageTool)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/infoPageTool)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/settingPageTools)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/settingPageTool)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/cpp/utils)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "../adb/connectmanager.h"
#include "../adb/adbinterface.h"
#include "../utils/utils.hpp"
#include "src/cpp/appPageTools/appHelper.h"
#include "src/cpp/appPageTool/appHelper.h"
#include <QVariant>
#include <QStandardPaths>
#include <QDir>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "flashPageTool.h"
#include "src/cpp/flashPageTools/flashtools.h"
#include "src/cpp/flashPageTool/flashtools.h"
#include <QtQml>

FlashPageTool::FlashPageTool(QObject *parent)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 9 additions & 10 deletions src/cpp/imagePageTool/scrcpy/ui/util/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ Config::Config(QObject *parent) : QObject(parent) {
if(!dir.exists(configDir)) {
dir.mkpath(configDir);
}
m_globalSetting = GlobalSetting::instance();
m_settings = new QSettings(configDir + "/config.ini", QSettings::IniFormat);
m_userData = new QSettings(configDir + "/userdata.ini", QSettings::IniFormat);

Expand All @@ -125,10 +124,10 @@ Config::Config(QObject *parent) : QObject(parent) {
void Config::initConfig()
{
// maxFps
m_globalSetting->checkConfig(GROUP_SCRCPY, COMMON_MAX_FPS_KEY, COMMON_MAX_FPS_DEF);
AppSettings->checkConfig(GROUP_SCRCPY, COMMON_MAX_FPS_KEY, COMMON_MAX_FPS_DEF);
// bitRate
m_globalSetting->checkConfig(GROUP_SCRCPY, COMMON_BITRATE_KEY, COMMON_BITRATE_DEF);
m_globalSetting->checkConfig(GROUP_SCRCPY, COMMON_RECORD_KEY, QDir::homePath());
AppSettings->checkConfig(GROUP_SCRCPY, COMMON_BITRATE_KEY, COMMON_BITRATE_DEF);
AppSettings->checkConfig(GROUP_SCRCPY, COMMON_RECORD_KEY, QDir::homePath());
}

Config &Config::getInstance() {
Expand Down Expand Up @@ -230,36 +229,36 @@ QString Config::getServerVersion() {

void Config::setMaxFps(int maxFps) {
maxFps = qBound(10, maxFps, 165);
m_globalSetting->writeConfig(GROUP_SCRCPY, COMMON_MAX_FPS_KEY, maxFps);
AppSettings->writeConfig(GROUP_SCRCPY, COMMON_MAX_FPS_KEY, maxFps);
emit maxFpsChanged(maxFps);
}

int Config::getMaxFps() const
{
int maxFps = m_globalSetting->readConfig(GROUP_SCRCPY, COMMON_MAX_FPS_KEY, COMMON_MAX_FPS_DEF).toUInt();
int maxFps = AppSettings->readConfig(GROUP_SCRCPY, COMMON_MAX_FPS_KEY, COMMON_MAX_FPS_DEF).toUInt();
return qBound(10, maxFps, 165);
}

void Config::setKBitRate(int kBitRate)
{
kBitRate = qBound(100, kBitRate, 100000); // 0.1Mbps ~ 100Mbps
m_globalSetting->writeConfig(GROUP_SCRCPY, COMMON_BITRATE_KEY, kBitRate);
AppSettings->writeConfig(GROUP_SCRCPY, COMMON_BITRATE_KEY, kBitRate);
emit kBitRateChanged(kBitRate);
}
int Config::getKBitRate() const
{
int kBitRate = m_globalSetting->readConfig(GROUP_SCRCPY, COMMON_BITRATE_KEY, COMMON_BITRATE_DEF).toUInt();
int kBitRate = AppSettings->readConfig(GROUP_SCRCPY, COMMON_BITRATE_KEY, COMMON_BITRATE_DEF).toUInt();
return qBound(100, kBitRate, 100000);
}

void Config::setRecordOutPath(const QString &recordOutPath)
{
m_globalSetting->writeConfig(GROUP_SCRCPY, COMMON_RECORD_KEY, recordOutPath);
AppSettings->writeConfig(GROUP_SCRCPY, COMMON_RECORD_KEY, recordOutPath);
emit recordOutPathChanged(recordOutPath);
}
QString Config::getRecordOutPath() const
{
return m_globalSetting->readConfig(GROUP_SCRCPY, COMMON_RECORD_KEY, QDir::homePath()).toString() + "/Documents";
return AppSettings->readConfig(GROUP_SCRCPY, COMMON_RECORD_KEY, QDir::homePath()).toString() + "/Documents";
}

int Config::getDesktopOpenGL() {
Expand Down
1 change: 0 additions & 1 deletion src/cpp/imagePageTool/scrcpy/ui/util/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,5 @@ class Config : public QObject
static QString s_configPath;
QPointer<QSettings> m_settings;
QPointer<QSettings> m_userData;
QPointer<GlobalSetting> m_globalSetting;
QString projectPath;
};
File renamed without changes.
34 changes: 34 additions & 0 deletions src/cpp/settingPageTool/othersettingshandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "othersettingshandler.h"
#include "../utils/globalsetting.h"

static auto constexpr Other_Section = "other";
static auto constexpr Wrapper_opacity_key = "wrapperOpacity";
static auto constexpr Use_OpenGL_Key = "useOpenGL";

OtherSettingsHandler::OtherSettingsHandler(QObject *parent)
: QObject(parent)
{
double configWrapperOpacity = AppSettings->readConfig(Other_Section, Wrapper_opacity_key, 0.3).toDouble();
setWrapperOpacity(configWrapperOpacity);

bool useOpenGL = AppSettings->readConfig(Other_Section, Use_OpenGL_Key, false).toBool();
setUseOpenGL(useOpenGL);
}

void OtherSettingsHandler::setWrapperOpacity(double value)
{
if (m_wrapperOpacity != value) {
m_wrapperOpacity = value;
AppSettings->writeConfig(Other_Section, Wrapper_opacity_key, value);
Q_EMIT wrapperOpacityChanged(value);
}
}

void OtherSettingsHandler::setUseOpenGL(bool value)
{
if (m_useOpenGL != value) {
m_useOpenGL = value;
AppSettings->writeConfig(Other_Section, Use_OpenGL_Key, value);
Q_EMIT useOpenGLChanged(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ class OtherSettingsHandler : public QObject
void wrapperOpacityChanged(double value);
void useOpenGLChanged(bool value);

private slots:
void syncConfig();

private:
double m_wrapperOpacity;
bool m_useOpenGL;
QTimer *m_triggerTimer;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <QJsonObject>
#include <QJsonDocument>

static auto constexpr Wallpaper_Section = "wallpaper";
static auto constexpr Url_Key = "url";
static auto constexpr Index_key = "index";

WallPaperModel::WallPaperModel(QObject *parent)
: QAbstractListModel(parent)
{
Expand Down Expand Up @@ -99,8 +103,8 @@ void WallPaperModel::setCurrentIndex(int index)
emit currentItemChanged(url);
emit currentIndexChanged(index);
WallpaperHelper::instance()->setWallPaper(url);
GlobalSetting::instance()->writeConfig("wallpaper", "url", url);
GlobalSetting::instance()->writeConfig("wallpaper", "index", QString::number(index));
AppSettings->writeConfig(Wallpaper_Section, Url_Key, url);
AppSettings->writeConfig(Wallpaper_Section, Index_key, QString::number(index));
}

QHash<int, QByteArray> WallPaperModel::roleNames() const
Expand Down Expand Up @@ -128,7 +132,7 @@ SettingPageTools::SettingPageTools(QObject *parent)
qmlRegisterSingletonInstance("WallpaperHelper", 1, 0, "WallpaperHelper", WallpaperHelper::instance(this));
qmlRegisterSingletonInstance("OtherSettingsHandler", 1, 0, "OtherSettingsHandler", OtherSettingsHandler::instance(this));

int configWallpaperIndex = GlobalSetting::instance()->readConfig("wallpaper", "index").toInt();
int configWallpaperIndex = AppSettings->readConfig(Wallpaper_Section, Index_key, 0).toInt();
m_wallpaperModel->setCurrentIndex(configWallpaperIndex);

// 默认壁纸和每日bing壁纸
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
45 changes: 0 additions & 45 deletions src/cpp/settingPageTools/othersettingshandler.cpp

This file was deleted.

28 changes: 22 additions & 6 deletions src/cpp/utils/globalsetting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>
#include <qapplication.h>
#include <qdir.h>
#include <qlogging.h>
#include <qvariant.h>
#include <QTimer>
#include <QApplication>

GlobalSetting* GlobalSetting::_instance = nullptr;

GlobalSetting::GlobalSetting(QObject *parent) : QObject(parent) {
QString cacheDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation);
m_timer = new QTimer();
m_timer->setInterval(1000);
m_timer->setSingleShot(true);
// 创建目录
cacheDir = cacheDir + QDir::separator() + QApplication::applicationName();
QDir dir(cacheDir);
Expand All @@ -22,14 +23,21 @@ GlobalSetting::GlobalSetting(QObject *parent) : QObject(parent) {

qDebug() << "config file:" << cacheDir + "/config.ini";
settings = new QSettings(cacheDir + "/config.ini", QSettings::IniFormat);

connect(m_timer, &QTimer::timeout, this, &GlobalSetting::syncConfig);
}

void GlobalSetting::writeConfig(const QString &title, const QString &key, const QVariant &value) {
qWarning() << title << key << value;
settings->setValue(QString("%1/%2").arg(title, key), value);
auto handleFunc = [title, key, value, this](){
qWarning() << title << key << value;
settings->setValue(QString("%1/%2").arg(title, key), value);
};
m_timer->start();
m_peddingTasks.enqueue(handleFunc);
}

QVariant GlobalSetting::readConfig(const QString &title, const QString &key, const QVariant &fallback) {
checkConfig(title, key, fallback);
return settings->value(QString("%1/%2").arg(title, key), fallback);
}

Expand All @@ -45,6 +53,14 @@ bool GlobalSetting::checkConfig(const QString &title, const QString &key, const
return true;
}

void GlobalSetting::syncConfig()
{
while(!m_peddingTasks.isEmpty()) {
auto handleFunc = m_peddingTasks.dequeue();
handleFunc();
}
}

GlobalSetting *GlobalSetting::instance(QObject *parent) {
static QMutex mutex;
QMutexLocker mutexLocker(&mutex);
Expand Down
10 changes: 10 additions & 0 deletions src/cpp/utils/globalsetting.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@

#include <QObject>
#include <QSettings>
#include <QQueue>
#include <QTimer>

#define AppSettings GlobalSetting::instance()

class GlobalSetting : public QObject{
Q_OBJECT
Expand All @@ -12,10 +16,16 @@ class GlobalSetting : public QObject{
// 检查键值是否存在,不存在则设置为给定的默认值
bool checkConfig(const QString &title, const QString &key, const QVariant &defaultValue);
static GlobalSetting* instance(QObject *parent = nullptr);

private:
void syncConfig();

private:
static GlobalSetting *_instance;
explicit GlobalSetting(QObject *parent = nullptr);
QSettings *settings = nullptr;
QQueue<std::function<void()>> m_peddingTasks;
QTimer *m_timer = nullptr;
};

#endif // GLOBALSETTING_H
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
#include "cpp/adb/connectmanager.h"
#include "cpp/infoPageTool/infopagetool.h"
#include "cpp/controlPageTool/controlPageTool.h"
#include "cpp/appPageTools/appPagetool.h"
#include "cpp/flashPageTools/flashPageTool.h"
#include "cpp/appPageTool/appPagetool.h"
#include "cpp/flashPageTool/flashPageTool.h"
#include "cpp/imagePageTool/imagePageTool.h"
#include "cpp/settingPageTools/settingPageTools.h"
#include "cpp/settingPageTool/settingPageTools.h"
#include "cpp/utils/notificationcontroller.h"
#include "cpp/components/fpsitem.h"
#include "cpp/utils/globalsetting.h"
Expand All @@ -42,7 +42,7 @@ bool checkADB() {
return false;
}

bool forceOpenGL()
void forceOpenGL()
{
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
QSurfaceFormat format;
Expand Down Expand Up @@ -109,7 +109,7 @@ int main(int argc, char *argv[])

qmlRegisterType<FpsItem>( "FpsItem", 1, 0, "FpsItem");

GlobalSetting::instance()->checkConfig("other", "useOpenGL", QVariant::fromValue(false));
AppSettings->checkConfig("other", "useOpenGL", QVariant::fromValue(false));
bool useOpenGL = GlobalSetting::instance()->readConfig("other", "useOpenGL").toBool();
if (useOpenGL) {
qInfo() << "force use OpenGL";
Expand Down

0 comments on commit 06ab7e9

Please sign in to comment.