Skip to content

Commit

Permalink
feat(theme plugin): read settings from config file
Browse files Browse the repository at this point in the history
Change-Id: I25c89bc5466516632be17a9314c5faa597b2c6ea
  • Loading branch information
zccrs committed Oct 31, 2017
1 parent 6e9c827 commit aacc299
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 2 deletions.
151 changes: 151 additions & 0 deletions platformthemeplugin/dthemesettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
*
* Author: zccrs <[email protected]>
*
* Maintainer: zccrs <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dthemesettings.h"

#include <QFileSystemWatcher>
#include <QFile>
#include <QTimer>
#include <QCoreApplication>
#include <QDebug>

#define ICON_THEME_NAME QStringLiteral("IconThemeName")
#define F_ICON_THEME_NAME QStringLiteral("FallBackIconThemeName")
#define STYLE_NAMES QStringLiteral("StyleNames")
#define SYSTEM_FONT QStringLiteral("SystemFont")
#define SYSTEM_FONT_PIXEL_SIZE QStringLiteral("SystemFontPixelSize")

DThemeSettings::DThemeSettings(QObject *parent)
: QObject(parent)
, settings(QSettings::IniFormat,
QSettings::UserScope,
"deepin", "qt-theme")
{
QStringList list;

list << settings.fileName();
list << QSettings(QSettings::IniFormat,
QSettings::SystemScope,
"deepin", "qt-theme").fileName();

for (const QString &path : list) {
QFile file(path);

if (!file.exists()) {
file.open(QFile::WriteOnly);
file.close();
}
}

QFileSystemWatcher *watcher = new QFileSystemWatcher(this);

connect(watcher, &QFileSystemWatcher::fileChanged, this, &DThemeSettings::onConfigChanged);
}

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

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

bool DThemeSettings::isSetIconThemeName() const
{
return contains(ICON_THEME_NAME);
}

QString DThemeSettings::iconThemeName() const
{
return value(ICON_THEME_NAME).toString();
}

bool DThemeSettings::isSetFallbackIconThemeName() const
{
return contains(F_ICON_THEME_NAME);
}

QString DThemeSettings::fallbackIconThemeName() const
{
return value(F_ICON_THEME_NAME).toString();
}

bool DThemeSettings::isSetSystemFont() const
{
return contains(SYSTEM_FONT);
}

QString DThemeSettings::systemFont() const
{
return value(SYSTEM_FONT).toString();
}

bool DThemeSettings::isSetStyleNames() const
{
return contains(STYLE_NAMES);
}

QStringList DThemeSettings::styleNames() const
{
return value(STYLE_NAMES).toStringList();
}

bool DThemeSettings::isSetSystemFontPixelSize() const
{
return contains(SYSTEM_FONT_PIXEL_SIZE);
}

int DThemeSettings::systemFontPixelSize() const
{
return value(SYSTEM_FONT_PIXEL_SIZE, 12).toInt();
}

void DThemeSettings::onConfigChanged()
{
QVariantMap config;

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

settings.sync();

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

if (old_value != new_value) {
if (v == ICON_THEME_NAME)
emit iconThemeNameChanged(new_value.toString());
else if (v == F_ICON_THEME_NAME)
emit fallbackIconThemeNameChanged(new_value.toString());
else if (v == SYSTEM_FONT)
emit systemFontChanged(new_value.toString());
else if (v == STYLE_NAMES)
emit styleNamesChanged(new_value.toStringList());
else if (v == SYSTEM_FONT_PIXEL_SIZE)
emit systemFontPixelSizeChanged(new_value.toInt());

emit valueChanged(v, old_value, new_value);
}
}
}
68 changes: 68 additions & 0 deletions platformthemeplugin/dthemesettings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2017 ~ 2017 Deepin Technology Co., Ltd.
*
* Author: zccrs <[email protected]>
*
* Maintainer: zccrs <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DTHEMESETTINGS_H
#define DTHEMESETTINGS_H

#include <QObject>
#include <QSettings>

class DThemeSettings : public QObject
{
Q_OBJECT

Q_PROPERTY(QString iconThemeName READ iconThemeName NOTIFY iconThemeNameChanged)
Q_PROPERTY(QString fallbackIconThemeName READ fallbackIconThemeName NOTIFY fallbackIconThemeNameChanged)
Q_PROPERTY(QString systemFont READ systemFont NOTIFY systemFontChanged)
Q_PROPERTY(int systemFontPixelSize READ systemFontPixelSize NOTIFY systemFontPixelSizeChanged)
Q_PROPERTY(QStringList styleNames READ styleNames NOTIFY styleNamesChanged)

public:
explicit DThemeSettings(QObject *parent = 0);

bool contains(const QString &key) const;
QVariant value(const QString &key, const QVariant &defaultValue = QVariant()) const;

bool isSetIconThemeName() const;
QString iconThemeName() const;
bool isSetFallbackIconThemeName() const;
QString fallbackIconThemeName() const;
bool isSetSystemFont() const;
QString systemFont() const;
bool isSetStyleNames() const;
QStringList styleNames() const;
bool isSetSystemFontPixelSize() const;
int systemFontPixelSize() const;

signals:
void valueChanged(const QString &key, const QVariant &oldValue, const QVariant &newValue);
void iconThemeNameChanged(QString iconThemeName);
void fallbackIconThemeNameChanged(QString fallbackIconThemeName);
void systemFontChanged(QString systemFont);
void styleNamesChanged(QStringList styleNames);
void systemFontPixelSizeChanged(int systemFontPixelSize);

private:
QSettings settings;

void onConfigChanged();
};

#endif // DTHEMESETTINGS_H
58 changes: 58 additions & 0 deletions platformthemeplugin/qdeepintheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
#include "qdeepinfiledialoghelper.h"
#include "diconproxyengine.h"
#include "filedialogmanager_interface.h"
#include "dthemesettings.h"

#include <QVariant>
#include <QDebug>
#include <QGuiApplication>

#include <XdgIcon>

Expand All @@ -38,6 +40,7 @@ QT_BEGIN_NAMESPACE
const char *QDeepinTheme::name = "deepin";
bool QDeepinTheme::m_usePlatformNativeDialog = true;
QMimeDatabase QDeepinTheme::m_mimeDatabase;
DThemeSettings *QDeepinTheme::m_settings = 0;

static QString gtkSetting(const gchar *propertyName)
{
Expand Down Expand Up @@ -161,6 +164,9 @@ QVariant QDeepinTheme::themeHint(QPlatformTheme::ThemeHint hint) const
{
switch (hint) {
case QPlatformTheme::StyleNames: {
if (settings()->isSetStyleNames() && !settings()->styleNames().isEmpty())
return settings()->styleNames();

QStringList styleNames;
// TODO(hualet): Make ddark&dlight styles ready!
// styleNames << QStringLiteral("dlight");
Expand All @@ -172,8 +178,14 @@ QVariant QDeepinTheme::themeHint(QPlatformTheme::ThemeHint hint) const
return QVariant(styleNames);
}
case QPlatformTheme::SystemIconThemeName:
if (settings()->isSetIconThemeName())
return settings()->iconThemeName();

return QVariant(gtkSetting("gtk-icon-theme-name"));
case QPlatformTheme::SystemIconFallbackThemeName:
if (settings()->isSetFallbackIconThemeName())
return settings()->fallbackIconThemeName();

return QVariant(gtkSetting("gtk-fallback-icon-theme"));
case QPlatformTheme::IconThemeSearchPaths:
return QVariant(QGenericUnixTheme::xdgIconThemePaths() << QDir::homePath() + "/.local/share/icons");
Expand All @@ -186,4 +198,50 @@ QVariant QDeepinTheme::themeHint(QPlatformTheme::ThemeHint hint) const
return QGenericUnixTheme::themeHint(hint);
}

const QFont *QDeepinTheme::font(QPlatformTheme::Font type) const
{
switch (type) {
case SystemFont:
if (qApp->desktopSettingsAware() && settings()->isSetSystemFont()) {
static QFont *system_font = new QFont("");

if (!settings()->systemFont().isEmpty()) {
system_font->setFamily(settings()->systemFont());
system_font->setPixelSize(settings()->systemFontPixelSize());
}

return system_font;
}

break;
default:
break;
}

return QGenericUnixTheme::font(type);
}

DThemeSettings *QDeepinTheme::settings() const
{
if (!m_settings) {
m_settings = new DThemeSettings();

if (qApp->inherits("Dtk::Widget::DApplication")) {
QObject::connect(m_settings, SIGNAL(iconThemeNameChanged(QString)),
qApp, SLOT(iconThemeChanged()), Qt::UniqueConnection);
}

auto updateSystemFont = [this] {
qApp->setFont(*font(QPlatformTheme::SystemFont));
QEvent event(QEvent::ApplicationFontChange);
qApp->sendEvent(qApp, &event);
};

QObject::connect(m_settings, &DThemeSettings::systemFontChanged, m_settings, updateSystemFont, Qt::UniqueConnection);
QObject::connect(m_settings, &DThemeSettings::systemFontPixelSizeChanged, m_settings, updateSystemFont, Qt::UniqueConnection);
}

return m_settings;
}

QT_END_NAMESPACE
4 changes: 4 additions & 0 deletions platformthemeplugin/qdeepintheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <qpa/qplatformwindow.h>
#include <qpa/qplatformnativeinterface.h>

class DThemeSettings;
class QDeepinTheme : public QGenericUnixTheme
{
public:
Expand All @@ -44,12 +45,15 @@ class QDeepinTheme : public QGenericUnixTheme
#endif

QVariant themeHint(ThemeHint hint) const Q_DECL_OVERRIDE;
const QFont *font(Font type) const Q_DECL_OVERRIDE;
DThemeSettings *settings() const;

static const char *name;

private:
static bool m_usePlatformNativeDialog;
static QMimeDatabase m_mimeDatabase;
static DThemeSettings *m_settings;

friend class QDeepinFileDialogHelper;
friend class QDeepinPlatformMenu;
Expand Down
6 changes: 4 additions & 2 deletions platformthemeplugin/qt5deepintheme-plugin.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ DESTDIR = $$_PRO_FILE_PWD_/../bin/plugins/platformthemes
SOURCES += qdeepintheme.cpp \
main.cpp \
qdeepinfiledialoghelper.cpp \
diconproxyengine.cpp
diconproxyengine.cpp \
dthemesettings.cpp

HEADERS += qdeepintheme.h \
qdeepinfiledialoghelper.h \
diconproxyengine.h
diconproxyengine.h \
dthemesettings.h

DISTFILES += \
deepin.json
Expand Down

0 comments on commit aacc299

Please sign in to comment.