diff --git a/lxqt-config-appearance/configothertoolkits.cpp b/lxqt-config-appearance/configothertoolkits.cpp index fd00d8064..95863c140 100644 --- a/lxqt-config-appearance/configothertoolkits.cpp +++ b/lxqt-config-appearance/configothertoolkits.cpp @@ -41,6 +41,8 @@ #include #include +#include "fontconfigfile.h" + static const char *GTK2_CONFIG = R"GTK2_CONFIG( # Created by lxqt-config-appearance (DO NOT EDIT!) gtk-theme-name = "%1" @@ -177,6 +179,8 @@ bool ConfigOtherToolKits::backupGTKSettings(QString version) void ConfigOtherToolKits::setConfig() { + setFontsConfig(); + if(!mConfigAppearanceSettings->contains(QStringLiteral("ControlGTKThemeEnabled"))) mConfigAppearanceSettings->setValue(QStringLiteral("ControlGTKThemeEnabled"), false); bool controlGTKThemeEnabled = mConfigAppearanceSettings->value(QStringLiteral("ControlGTKThemeEnabled")).toBool(); @@ -251,6 +255,12 @@ void ConfigOtherToolKits::setGTKConfig(QString version, QString theme) writeConfig(gtkrcPath, GTK3_CONFIG, ConfigOtherToolKits::GTK3); } +void ConfigOtherToolKits::setFontsConfig() +{ + FontConfigFile fontConfigFile(mSettings); + fontConfigFile.save(); +} + QString ConfigOtherToolKits::getConfig(const char *configString, ConfigOtherToolKits::Version version) { LXQt::Settings* sessionSettings = new LXQt::Settings(QStringLiteral("session")); @@ -441,4 +451,3 @@ void ConfigOtherToolKits::updateConfigFromSettings() } } } - diff --git a/lxqt-config-appearance/configothertoolkits.h b/lxqt-config-appearance/configothertoolkits.h index 4a91616b3..86f575645 100644 --- a/lxqt-config-appearance/configothertoolkits.h +++ b/lxqt-config-appearance/configothertoolkits.h @@ -49,6 +49,7 @@ public slots: void setXSettingsConfig(); void setGTKConfig(QString version, QString theme = QString()); void setGsettingsConfig(QString version, QString theme = QString()); + void setFontsConfig(); private: struct Config { diff --git a/lxqt-config-appearance/fontconfigfile.cpp b/lxqt-config-appearance/fontconfigfile.cpp index d2496be10..423ef1fe9 100644 --- a/lxqt-config-appearance/fontconfigfile.cpp +++ b/lxqt-config-appearance/fontconfigfile.cpp @@ -30,15 +30,12 @@ #include #include -FontConfigFile::FontConfigFile(QObject* parent): - QObject(parent), - mAntialias(true), - mHinting(true), - mSubpixel("rgb"), - mHintStyle("hintslight"), - mDpi(96), - mAutohint(false), - mSaveTimer(nullptr) +#include + +using namespace Qt::Literals::StringLiterals; + +FontConfigFile::FontConfigFile(LXQt::Settings *settings): + mSettings(settings) { mDirPath = QString::fromLocal8Bit(qgetenv("XDG_CONFIG_HOME")); QString homeDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation); @@ -46,191 +43,64 @@ FontConfigFile::FontConfigFile(QObject* parent): mDirPath = homeDir + QStringLiteral("/.config"); mDirPath += QLatin1String("/fontconfig"); mFilePath = mDirPath + QStringLiteral("/fonts.conf"); - - load(); -} - -FontConfigFile::~FontConfigFile() -{ - if(mSaveTimer) // has pending save request - { - delete mSaveTimer; - mSaveTimer = nullptr; - save(); - } -} - -void FontConfigFile::setAntialias(bool value) -{ - mAntialias = value; - queueSave(); -} - -void FontConfigFile::setSubpixel(QByteArray value) -{ - mSubpixel = value; - queueSave(); -} - -void FontConfigFile::setHinting(bool value) -{ - mHinting = value; - queueSave(); -} - -void FontConfigFile::setHintStyle(QByteArray value) -{ - mHintStyle = value; - queueSave(); -} - -void FontConfigFile::setDpi(int value) -{ - mDpi = value; - queueSave(); -} - -void FontConfigFile::setAutohint(bool value) -{ - mAutohint = value; - queueSave(); -} - -void FontConfigFile::load() -{ - QFile file(mFilePath); - if(file.open(QIODevice::ReadOnly)) - { - QByteArray buffer = file.readAll(); - file.close(); - if(buffer.contains("lxqt-config-appearance")) // the config file is created by us - { - // doing full xml parsing is over-kill. let's use some simpler brute-force methods. - QDomDocument doc; - doc.setContent(&file); - file.close(); - QDomElement docElem = doc.documentElement(); - QDomNodeList editNodes = docElem.elementsByTagName(QStringLiteral("edit")); - for(int i = 0; i < editNodes.count(); ++i) - { - QDomElement editElem = editNodes.at(i).toElement(); - QByteArray name = editElem.attribute(QStringLiteral("name")).toLatin1(); - if(name == "antialias") - { - QString value = editElem.firstChildElement(QStringLiteral("bool")).text(); - mAntialias = value[0] == QLatin1Char('t') ? true : false; - } - else if(name == "rgba") - { - QString value = editElem.firstChildElement(QStringLiteral("const")).text(); - mSubpixel = value.toLatin1(); - } - else if(name == "hinting") - { - QString value = editElem.firstChildElement(QStringLiteral("bool")).text(); - mHinting = value[0] == QLatin1Char('t') ? true : false; - } - else if(name == "hintstyle") - { - QString value = editElem.firstChildElement(QStringLiteral("const")).text(); - mHintStyle = value.toLatin1(); - } - else if(name == "dpi") - { - QString value = editElem.firstChildElement(QStringLiteral("double")).text(); - mDpi = value.toInt(); - } - else if(name == "autohint") - { - QString value = editElem.firstChildElement(QStringLiteral("bool")).text(); - mAutohint = value[0] == QLatin1Char('t') ? true : false; - } - } - } - else // the config file is created by others => make a backup and write our config - { - QFile backup(mFilePath + QStringLiteral(".bak")); - if(backup.open(QIODevice::WriteOnly)) - { - backup.write(buffer); - backup.close(); - } - queueSave(); // overwrite with our file - } - } } void FontConfigFile::save() { - if(mSaveTimer) - { - mSaveTimer->deleteLater(); - mSaveTimer = nullptr; - } - QFile file(mFilePath); QDir().mkdir(mDirPath); // References: https://wiki.archlinux.org/index.php/Font_Configuration - if(file.open(QIODevice::WriteOnly)) - { - QTextStream s(&file); - s << - "\n" - "\n" - "\n" - "\n" - " conf.d\n" - " \n" - " \n" - " " << (mAntialias ? "true" : "false") << "\n" - " \n" - " \n" - " \n" - " \n" - " " << mSubpixel << "\n" - " \n" - " \n" - " \n" - " \n" - " lcddefault\n" - " \n" - " \n" - " \n" - " \n" - " " << (mHinting ? "true" : "false") << "\n" - " \n" - " \n" - " \n" - " \n" - " " << mHintStyle << "\n" - " \n" - " \n" - " \n" - " \n" - " " << (mAutohint ? "true" : "false") << "\n" - " \n" - " \n" - " \n" - " \n" - " " << mDpi << "\n" - " \n" - " \n" - ""; - s.flush(); - file.close(); - } -} + if(!file.open(QIODevice::WriteOnly)) + return; -void FontConfigFile::queueSave() -{ - if(mSaveTimer) - mSaveTimer->start(1500); - else - { - mSaveTimer = new QTimer(); - mSaveTimer->setSingleShot(true); - connect(mSaveTimer, &QTimer::timeout, this, &FontConfigFile::save); - mSaveTimer->start(1500); - } -} + bool antialias = mSettings->value("Fonts/Antialias"_L1, true).toBool(); + QByteArray subpixel = mSettings->value("Fonts/Subpixel"_L1, "rgb"_ba).toByteArray(); + bool hinting = mSettings->value("Fonts/Hinting"_L1, true).toBool(); + QByteArray hintStyle = mSettings->value("Fonts/Hintstyle"_L1, "hintslight"_ba).toByteArray(); + bool autohint = mSettings->value("Fonts/Autohint"_L1, false).toBool(); + int dpi = mSettings->value("Fonts/DPI"_L1, 96).toInt(); + QTextStream s(&file); + s << + "\n" + "\n" + "\n" + "\n" + " conf.d\n" + " \n" + " \n" + " " << (antialias ? "true" : "false") << "\n" + " \n" + " \n" + " \n" + " \n" + " " << subpixel << "\n" + " \n" + " \n" + " \n" + " \n" + " lcddefault\n" + " \n" + " \n" + " \n" + " \n" + " " << (hinting ? "true" : "false") << "\n" + " \n" + " \n" + " \n" + " \n" + " " << hintStyle << "\n" + " \n" + " \n" + " \n" + " \n" + " " << (autohint ? "true" : "false") << "\n" + " \n" + " \n" + " \n" + " \n" + " " << dpi << "\n" + " \n" + " \n" + ""; +} diff --git a/lxqt-config-appearance/fontconfigfile.h b/lxqt-config-appearance/fontconfigfile.h index 51e0b3ee5..378294d5a 100644 --- a/lxqt-config-appearance/fontconfigfile.h +++ b/lxqt-config-appearance/fontconfigfile.h @@ -22,66 +22,23 @@ #define FONTCONFIGFILE_H #include -#include -#include -class QTimer; +namespace LXQt +{ + class Settings; +} -class FontConfigFile: public QObject +class FontConfigFile { - Q_OBJECT public: - explicit FontConfigFile(QObject* parent = nullptr); - virtual ~FontConfigFile(); - - bool antialias() const { - return mAntialias; - } - void setAntialias(bool value); - - bool hinting() const { - return mHinting; - } - void setHinting(bool value); - - QByteArray subpixel() const { - return mSubpixel; - } - void setSubpixel(QByteArray value); - - QByteArray hintStyle() const { - return mHintStyle; - } - void setHintStyle(QByteArray value); + explicit FontConfigFile(LXQt::Settings *settings); - int dpi() const { - return mDpi; - } - void setDpi(int value); - - bool autohint() const { - return mAutohint; - } - void setAutohint(bool value); - - -private Q_SLOTS: void save(); private: - void load(); - void queueSave(); - -private: - bool mAntialias; - bool mHinting; - QByteArray mSubpixel; - QByteArray mHintStyle; - int mDpi; - bool mAutohint; QString mDirPath; QString mFilePath; - QTimer* mSaveTimer; + LXQt::Settings *mSettings; }; #endif // FONTCONFIGFILE_H diff --git a/lxqt-config-appearance/fontsconfig.cpp b/lxqt-config-appearance/fontsconfig.cpp index 49818f800..04c451278 100644 --- a/lxqt-config-appearance/fontsconfig.cpp +++ b/lxqt-config-appearance/fontsconfig.cpp @@ -38,6 +38,8 @@ #include #include +using namespace Qt::Literals::StringLiterals; + static const char* subpixelNames[] = {"none", "rgb", "bgr", "vrgb", "vbgr"}; static const char* hintStyleNames[] = {"hintnone", "hintslight", "hintmedium", "hintfull"}; @@ -45,8 +47,7 @@ FontsConfig::FontsConfig(LXQt::Settings* settings, QSettings* qtSettings, QWidge QWidget(parent), ui(new Ui::FontsConfig), mQtSettings(qtSettings), - mSettings(settings), - mFontConfigFile() + mSettings(settings) { ui->setupUi(this); @@ -94,10 +95,10 @@ void FontsConfig::initControls() mQtSettings->endGroup(); // load fontconfig config - ui->antialias->setChecked(mFontConfigFile.antialias()); - ui->autohint->setChecked(mFontConfigFile.autohint()); + ui->antialias->setChecked(mQtSettings->value("Fonts/Antialias"_L1, true).toBool()); + ui->autohint->setChecked(mQtSettings->value("Fonts/Autohint"_L1, false).toBool()); - QByteArray subpixelStr = mFontConfigFile.subpixel(); + QByteArray subpixelStr = mQtSettings->value("Fonts/Subpixel"_L1, "rgb"_ba).toByteArray(); int subpixel; for(subpixel = 0; subpixel < 5; ++subpixel) { @@ -107,9 +108,9 @@ void FontsConfig::initControls() if(subpixel < 5) ui->subpixel->setCurrentIndex(subpixel); - ui->hinting->setChecked(mFontConfigFile.hinting()); + ui->hinting->setChecked(mQtSettings->value("Fonts/Hinting"_L1, true).toBool()); - QByteArray hintStyleStr = mFontConfigFile.hintStyle(); + QByteArray hintStyleStr = mQtSettings->value("Fonts/Hintstyle"_L1, "hintslight"_ba).toByteArray(); int hintStyle; for(hintStyle = 0; hintStyle < 4; ++hintStyle) { @@ -119,7 +120,7 @@ void FontsConfig::initControls() if(hintStyle < 4) ui->hintStyle->setCurrentIndex(hintStyle); - int dpi = mFontConfigFile.dpi(); + int dpi = mQtSettings->value("Fonts/DPI"_L1, 96).toInt(); ui->dpi->blockSignals(true); ui->dpi->setValue(dpi); ui->dpi->blockSignals(false); @@ -129,29 +130,28 @@ void FontsConfig::initControls() void FontsConfig::updateQtFont() { - if(mFontConfigFile.antialias() != ui->antialias->isChecked()) - mFontConfigFile.setAntialias(ui->antialias->isChecked()); + if(mQtSettings->value("Fonts/Antialias"_L1).toBool() != ui->antialias->isChecked()) + mQtSettings->setValue("Fonts/Antialias"_L1, ui->antialias->isChecked()); - if(mFontConfigFile.dpi() != ui->dpi->value()) - mFontConfigFile.setDpi(ui->dpi->value()); + if(mQtSettings->value("Fonts/DPI"_L1).toInt() != ui->dpi->value()) + mQtSettings->setValue("Fonts/DPI"_L1, ui->dpi->value()); - if(mFontConfigFile.hinting() != ui->hinting->isChecked()) - mFontConfigFile.setHinting(ui->hinting->isChecked()); + if(mQtSettings->value("Fonts/Hinting"_L1).toBool() != ui->hinting->isChecked()) + mQtSettings->setValue("Fonts/Hinting"_L1, ui->hinting->isChecked()); int index = ui->subpixel->currentIndex(); - if(index >= 0 && index <= 4 && mFontConfigFile.subpixel() != subpixelNames[index]) - mFontConfigFile.setSubpixel(subpixelNames[index]); + if(index >= 0 && index <= 4 && mQtSettings->value("Fonts/Subpixel"_L1).toByteArray() != subpixelNames[index]) + mQtSettings->setValue("Fonts/Subpixel"_L1, QByteArray(subpixelNames[index])); index = ui->hintStyle->currentIndex(); - if(index >= 0 && index <= 3 && mFontConfigFile.hintStyle() != hintStyleNames[index]) - mFontConfigFile.setHintStyle(hintStyleNames[index]); + if(index >= 0 && index <= 3 && mQtSettings->value("Fonts/Hintstyle"_L1).toByteArray() != hintStyleNames[index]) + mQtSettings->value("Fonts/Hintstyle"_L1, QByteArray(hintStyleNames[index])); - if(mFontConfigFile.autohint() != ui->autohint->isChecked()) - mFontConfigFile.setAutohint(ui->autohint->isChecked()); + if(mQtSettings->value("Fonts/Autohint"_L1).toBool() != ui->autohint->isChecked()) + mQtSettings->setValue("Fonts/Autohint"_L1, ui->autohint->isChecked()); // FIXME: the change does not apply to some currently running Qt programs. // FIXME: does not work with KDE apps - // TODO: also write the config values to GTK+ config files (gtk-2.0.rc and gtk3/settings.ini) // FIXME: the selected font does not apply to our own application. Why? QFont font = ui->fontName->currentFont(); @@ -174,14 +174,15 @@ void FontsConfig::updateQtFont() font.setBold(bold); font.setItalic(italic); + // FIXME: Migrate this setting under the 'Fonts' group instead of the 'Qt' group const QString fontStr = font.toString(); if(mQtSettings->value(QLatin1String("Qt/font")).toString() != fontStr) { mQtSettings->beginGroup(QLatin1String("Qt")); mQtSettings->setValue(QStringLiteral("font"), fontStr); mQtSettings->endGroup(); mQtSettings->sync(); - - emit updateOtherSettings(); - update(); } + + emit updateOtherSettings(); + update(); } diff --git a/lxqt-config-appearance/fontsconfig.h b/lxqt-config-appearance/fontsconfig.h index c43ab4c3a..6cf725b12 100644 --- a/lxqt-config-appearance/fontsconfig.h +++ b/lxqt-config-appearance/fontsconfig.h @@ -31,7 +31,6 @@ #include #include #include -#include "fontconfigfile.h" class QTreeWidgetItem; class QSettings; @@ -61,7 +60,6 @@ public Q_SLOTS: Ui::FontsConfig *ui; QSettings *mQtSettings; LXQt::Settings *mSettings; - FontConfigFile mFontConfigFile; }; #endif // FONTSCONFIG_H