File: fontsconfig.cpp

package info (click to toggle)
lxqt-config 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 6,676 kB
  • sloc: cpp: 12,832; makefile: 20; sh: 4
file content (187 lines) | stat: -rw-r--r-- 6,260 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* BEGIN_COMMON_COPYRIGHT_HEADER
 * (c)LGPL2+
 *
 * LXQt - a lightweight, Qt based, desktop toolset
 * https://lxqt-project.org/
 *
 * Copyright: 2014 LXQt team
 * Authors:
 *   Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
 *
 * This program or library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA
 *
 * END_COMMON_COPYRIGHT_HEADER */

#include "fontsconfig.h"
#include "ui_fontsconfig.h"
#include <QTreeWidget>
#include <QDebug>
#include <QSettings>
#include <QFont>
#include <QFile>
#include <QDir>
#include <QDesktopServices>
#include <QTextStream>
#include <QStringBuilder>
#include <QDomDocument>

static const char* subpixelNames[] = {"none", "rgb", "bgr", "vrgb", "vbgr"};
static const char* hintStyleNames[] = {"hintnone", "hintslight", "hintmedium", "hintfull"};

FontsConfig::FontsConfig(LXQt::Settings* settings, QSettings* qtSettings, QWidget* parent) :
    QWidget(parent),
    ui(new Ui::FontsConfig),
    mQtSettings(qtSettings),
    mSettings(settings),
    mFontConfigFile()
{
    ui->setupUi(this);

    initControls();

    connect(ui->fontName, QOverload<int>::of(&QComboBox::activated), this, &FontsConfig::settingsChanged);
    connect(ui->fontStyle, QOverload<int>::of(&QComboBox::activated), this, &FontsConfig::settingsChanged);
    connect(ui->fontSize, QOverload<int>::of(&QSpinBox::valueChanged), this, &FontsConfig::settingsChanged);
    connect(ui->antialias, &QAbstractButton::clicked, this, &FontsConfig::settingsChanged);
    connect(ui->subpixel, QOverload<int>::of(&QComboBox::activated), this, &FontsConfig::settingsChanged);
    connect(ui->hinting, &QAbstractButton::clicked, this, &FontsConfig::settingsChanged);
    connect(ui->hintStyle, QOverload<int>::of(&QComboBox::activated), this, &FontsConfig::settingsChanged);
    connect(ui->dpi, QOverload<int>::of(&QSpinBox::valueChanged), this, &FontsConfig::settingsChanged);
    connect(ui->autohint, &QAbstractButton::clicked, this, &FontsConfig::settingsChanged);
}


FontsConfig::~FontsConfig()
{
    delete ui;
}


void FontsConfig::initControls()
{
    // read Qt style settings from Qt Trolltech.conf config
    mQtSettings->beginGroup(QLatin1String("Qt"));

    QString fontName = mQtSettings->value(QStringLiteral("font")).toString();
    QFont font;
    font.fromString(fontName);
    ui->fontName->setCurrentFont(font);

    ui->fontSize->blockSignals(true);
    ui->fontSize->setValue(font.pointSize());
    ui->fontSize->blockSignals(false);

    int fontStyle = 0;
    if(font.bold())
      fontStyle = font.italic() ? 3 : 1;
    else if(font.italic())
      fontStyle = 2;
    ui->fontStyle->setCurrentIndex(fontStyle);

    mQtSettings->endGroup();

    // load fontconfig config
    ui->antialias->setChecked(mFontConfigFile.antialias());
    ui->autohint->setChecked(mFontConfigFile.autohint());

    QByteArray subpixelStr = mFontConfigFile.subpixel();
    int subpixel;
    for(subpixel = 0; subpixel < 5; ++subpixel)
    {
        if(subpixelStr == subpixelNames[subpixel])
            break;
    }
    if(subpixel < 5)
        ui->subpixel->setCurrentIndex(subpixel);

    ui->hinting->setChecked(mFontConfigFile.hinting());

    QByteArray hintStyleStr = mFontConfigFile.hintStyle();
    int hintStyle;
    for(hintStyle = 0; hintStyle < 4; ++hintStyle)
    {
        if(hintStyleStr == hintStyleNames[hintStyle])
            break;
    }
    if(hintStyle < 4)
        ui->hintStyle->setCurrentIndex(hintStyle);

    int dpi = mFontConfigFile.dpi();
    ui->dpi->blockSignals(true);
    ui->dpi->setValue(dpi);
    ui->dpi->blockSignals(false);

    update();
}

void FontsConfig::updateQtFont()
{
    if(mFontConfigFile.antialias() != ui->antialias->isChecked())
        mFontConfigFile.setAntialias(ui->antialias->isChecked());

    if(mFontConfigFile.dpi() != ui->dpi->value())
        mFontConfigFile.setDpi(ui->dpi->value());

    if(mFontConfigFile.hinting() != ui->hinting->isChecked())
        mFontConfigFile.setHinting(ui->hinting->isChecked());

    int index = ui->subpixel->currentIndex();
    if(index >= 0 && index <= 4 && mFontConfigFile.subpixel() != subpixelNames[index])
        mFontConfigFile.setSubpixel(subpixelNames[index]);

    index = ui->hintStyle->currentIndex();
    if(index >= 0 && index <= 3 && mFontConfigFile.hintStyle() != hintStyleNames[index])
        mFontConfigFile.setHintStyle(hintStyleNames[index]);

    if(mFontConfigFile.autohint() != ui->autohint->isChecked())
        mFontConfigFile.setAutohint(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();
    int size = ui->fontSize->value();
    bool bold = false;
    bool italic = false;
    switch(ui->fontStyle->currentIndex())
    {
        case 1:
            bold = true;
            break;
        case 2:
            italic = true;
            break;
        case 3:
          bold = italic = true;
    }

    font.setPointSize(size);
    font.setBold(bold);
    font.setItalic(italic);

    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();
    }
}