Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 108 additions & 14 deletions platformthemeplugin/dthemesettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@
// 只能在时间上控制让两个冲突的逻辑错开。此处定义此时间(单位:ms),表示从touch begin到touch move比较在
// 此时间段内完成,否则不应该认为这个一个触摸滚动操作
#define TOUCH_FLICK_BEGIN_MOVE_DELAY QStringLiteral("TouchFlickBeginMoveDelay")
#define SCREEN_SCALE_FACTORS QStringLiteral("ScreenScaleFactors")
#define SCALE_FACTOR QStringLiteral("ScaleFactor")
#define SCALE_LOGICAL_DPI QStringLiteral("ScaleLogicalDpi")

DCORE_USE_NAMESPACE

DThemeSettings::DThemeSettings(QObject *parent)
DThemeSettings::DThemeSettings(bool watchFile, QObject *parent)
: QObject(parent)
, settings(QSettings::IniFormat,
QSettings::UserScope,
"deepin", "qt-theme")
, settings(makeSettings())
{
settings.setIniCodec("utf-8");
settings.beginGroup("Theme");
if (!watchFile)
return;

QStringList list;

list << settings.fileName();
list << settings->fileName();
list << QSettings(QSettings::IniFormat,
QSettings::SystemScope,
"deepin", "qt-theme").fileName();
Expand All @@ -71,14 +72,64 @@ DThemeSettings::DThemeSettings(QObject *parent)
connect(watcher, &DFileWatcherManager::fileModified, this, &DThemeSettings::onConfigChanged);
}

QSettings * DThemeSettings::makeSettings()
{
QString saveConfigPath;

do {
// 需要自定义读取主题相关配置的存储路径,未定义时默认为 ~/.config
// 适用于这样的情况:一个使用pkexec使用root权限启动的UI应用,需要跟随启动时的普通
// 用户的字体、缩放等设置,可通过在QCoreApplication构造之前设置此环境变量指定使用
// 某用户的主题配置文件。
static QByteArray theme_config_path = qgetenv("D_QT_THEME_CONFIG_PATH");

if (theme_config_path.isEmpty()) {
break;
}

// 先创建一个对象,用于获取默认配置文件的路径
QSettings s(QSettings::IniFormat, QSettings::UserScope, "deepin", "qt-theme");
const QString suffix("/deepin/qt-theme.ini");
QString file_path = s.fileName();

// 必须以此路径结尾,去除此路径的剩余部分为配置文件路径
if (!file_path.endsWith(suffix)) {
break;
}

saveConfigPath = file_path.left(file_path.size() - suffix.size());

if (saveConfigPath.isEmpty()) {
break;
}

// 设置自定义的主题配置文件存储目录
QSettings::setPath(s.format(), s.scope(), QString::fromLocal8Bit(theme_config_path));
} while (false);

QSettings *s = new QSettings(QSettings::IniFormat,
QSettings::UserScope,
"deepin", "qt-theme");

// 恢复原本的配置目录
if (!saveConfigPath.isEmpty()) {
QSettings::setPath(s->format(), s->scope(), saveConfigPath);
}

s->setIniCodec("utf-8");
s->beginGroup("Theme");

return s;
}

bool DThemeSettings::contains(const QString &key) const
{
return settings.contains(key);
return settings->contains(key);
}

QVariant DThemeSettings::value(const QString &key, const QVariant &defaultValue) const
{
return settings.value(key, defaultValue);
return settings->value(key, defaultValue);
}

bool DThemeSettings::isSetIconThemeName() const
Expand Down Expand Up @@ -146,19 +197,56 @@ int DThemeSettings::touchFlickBeginMoveDelay() const
return value(TOUCH_FLICK_BEGIN_MOVE_DELAY, 300).toInt();
}

qreal DThemeSettings::scaleFactor() const
{
return value(SCALE_FACTOR).toReal();
}

QByteArray DThemeSettings::screenScaleFactors() const
{
return value(SCREEN_SCALE_FACTORS).toByteArray();
}

// 从配置文件中获取dpi相关数据,文件中存储的格式为 ScaleLogicalDpi=x,y
// 会被QSettings解析为QStringList,此处需要将其转换为QPair<qreal,qreal>
static QPair<qreal, qreal> takePair(const QVariant &value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加一点注释:例如使用的一种情况。

{
if (!value.isValid()) {
return qMakePair(0.0, 0.0);
}

const QStringList &l = value.toStringList();

if (l.count() < 2) {
return qMakePair(0.0, 0.0);
}

QPair<qreal, qreal> ret;

ret.first = l.first().toDouble();
ret.second = l.at(1).toDouble();

return ret;
}

QPair<qreal, qreal> DThemeSettings::scaleLogicalDpi() const
{
return takePair(value(SCALE_LOGICAL_DPI));
}

void DThemeSettings::onConfigChanged()
{
QVariantMap config;

for (const QString &v : settings.allKeys()) {
config[v] = settings.value(v);
for (const QString &v : settings->allKeys()) {
config[v] = settings->value(v);
}

settings.sync();
settings->sync();

for (const QString &v : settings.allKeys()) {
for (const QString &v : settings->allKeys()) {
const QVariant &old_value = config.value(v);
const QVariant &new_value = settings.value(v);
const QVariant &new_value = settings->value(v);

if (old_value != new_value) {
if (v == ICON_THEME_NAME)
Expand All @@ -175,6 +263,12 @@ void DThemeSettings::onConfigChanged()
emit systemFontPointSizeChanged(new_value.toInt());
else if (v == TOUCH_FLICK_BEGIN_MOVE_DELAY)
emit touchFlickBeginMoveDelayChanged(new_value.toInt());
else if (v == SCREEN_SCALE_FACTORS)
emit screenScaleFactorsChanged(new_value.toByteArray());
else if (v == SCALE_FACTOR)
emit scaleFactorChanged(new_value.toReal());
else if (v == SCALE_LOGICAL_DPI)
emit scaleLogicalDpiChanged(takePair(new_value));

emit valueChanged(v, old_value, new_value);
}
Expand Down
15 changes: 13 additions & 2 deletions platformthemeplugin/dthemesettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ class DThemeSettings : public QObject
Q_PROPERTY(qreal systemFontPointSize READ systemFontPointSize NOTIFY systemFontPointSizeChanged)
Q_PROPERTY(QStringList styleNames READ styleNames NOTIFY styleNamesChanged)
Q_PROPERTY(int touchFlickBeginMoveDelay READ touchFlickBeginMoveDelay NOTIFY touchFlickBeginMoveDelayChanged)
Q_PROPERTY(qreal scaleFactor READ scaleFactor NOTIFY scaleFactorChanged)
Q_PROPERTY(QByteArray screenScaleFactors READ screenScaleFactors NOTIFY screenScaleFactorsChanged)
Q_PROPERTY(QPair<qreal, qreal> scaleLogicalDpi READ scaleLogicalDpi NOTIFY scaleLogicalDpiChanged)

public:
explicit DThemeSettings(QObject *parent = 0);
explicit DThemeSettings(bool watchFile = true, QObject *parent = 0);

static QSettings *makeSettings();

bool contains(const QString &key) const;
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;
Expand All @@ -55,6 +60,9 @@ class DThemeSettings : public QObject
bool isSetSystemFixedFont() const;
QString systemFixedFont() const;
int touchFlickBeginMoveDelay() const;
qreal scaleFactor() const;
QByteArray screenScaleFactors() const;
QPair<qreal, qreal> scaleLogicalDpi() const;

signals:
void valueChanged(const QString &key, const QVariant &oldValue, const QVariant &newValue);
Expand All @@ -65,9 +73,12 @@ class DThemeSettings : public QObject
void systemFixedFontChanged(QString systemFixedFont);
void systemFontPointSizeChanged(qreal systemFontPointSize);
void touchFlickBeginMoveDelayChanged(int touchFlickBeginMoveDelay);
void scaleFactorChanged(const qreal &scaleFactor);
void screenScaleFactorsChanged(const QByteArray &screenScaleFactors);
void scaleLogicalDpiChanged(const QPair<qreal, qreal> scaleLogicalDpi);

private:
QSettings settings;
QSettings *settings;

void onConfigChanged();
};
Expand Down
Loading