aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/texteditor/fontsettings.cpp
blob: 89ae41fb4bef9650f96dd9dfffdb87388fc3474e (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
**************************************************************************/

#include "fontsettings.h"
#include "fontsettingspage.h"

#include <utils/qtcassert.h>

#include <QtCore/QSettings>
#include <QtGui/QTextCharFormat>

static const char *fontFamilyKey = "FontFamily";
static const char *fontSizeKey = "FontSize";
static const char *trueString = "true";
static const char *falseString = "false";

namespace {
#ifdef Q_WS_MAC
    enum { DEFAULT_FONT_SIZE = 12 };
    static const char *DEFAULT_FONT_FAMILY = "Monaco";
#else
#ifdef Q_OS_UNIX
    enum { DEFAULT_FONT_SIZE = 9 };
    static const char *DEFAULT_FONT_FAMILY = "Monospace";
#else
    enum { DEFAULT_FONT_SIZE = 10 };
    static const char *DEFAULT_FONT_FAMILY = "Courier";
#endif
#endif
} // anonymous namespace

namespace TextEditor {

// Format --
Format::Format() :
    m_foreground(Qt::black),
    m_background(Qt::white),
    m_bold(false),
    m_italic(false)
{
}

void Format::setForeground(const QColor &foreground)
{
    m_foreground = foreground;
}

void Format::setBackground(const QColor &background)
{
    m_background = background;
}

void Format::setBold(bool bold)
{
    m_bold = bold;
}

void Format::setItalic(bool italic)
{
    m_italic = italic;
}

static QString colorToString(const QColor &color) {
    if (color.isValid())
        return color.name();
    return QLatin1String("invalid");
}

static QColor stringToColor(const QString &string) {
    if (string == QLatin1String("invalid"))
        return QColor();
    return QColor(string);
}

QString Format::toString() const
{
    const QChar delimiter = QLatin1Char(';');
    QString s =  colorToString(m_foreground);
    s += delimiter;
    s += colorToString(m_background);
    s += delimiter;
    s += m_bold   ? QLatin1String(trueString) : QLatin1String(falseString);
    s += delimiter;
    s += m_italic ? QLatin1String(trueString) : QLatin1String(falseString);
    return s;
}

bool Format::fromString(const QString &str)
{
    *this = Format();

    const QStringList lst = str.split(QLatin1Char(';'));
    if (lst.count() != 4)
        return false;

    m_foreground = stringToColor(lst.at(0));
    m_background = stringToColor(lst.at(1));
    m_bold = lst.at(2) == QLatin1String(trueString);
    m_italic = lst.at(3) == QLatin1String(trueString);
    return true;
}

bool Format::equals(const Format &f) const
{
    return m_foreground ==  f.m_foreground && m_background == f.m_background &&
           m_bold == f.m_bold && m_italic == f.m_italic;
}

// -- FontSettings
FontSettings::FontSettings(const FormatDescriptions &fd) :
    m_family(defaultFixedFontFamily()),
    m_fontSize(DEFAULT_FONT_SIZE)
{
    Q_UNUSED(fd);
}

void FontSettings::clear()
{
    m_family = defaultFixedFontFamily();
    m_fontSize = DEFAULT_FONT_SIZE;
    qFill(m_formats.begin(), m_formats.end(), Format());
}

void FontSettings::toSettings(const QString &category,
                              const FormatDescriptions &descriptions,
                              QSettings *s) const
{
    const int numFormats = m_formats.size();
    QTC_ASSERT(descriptions.size() == numFormats, /**/);
    s->beginGroup(category);
    if (m_family != defaultFixedFontFamily() || s->contains(QLatin1String(fontFamilyKey)))
        s->setValue(QLatin1String(fontFamilyKey), m_family);

    if (m_fontSize != DEFAULT_FONT_SIZE || s->contains(QLatin1String(fontSizeKey)))
        s->setValue(QLatin1String(fontSizeKey), m_fontSize);

    const Format defaultFormat;

    foreach (const FormatDescription &desc, descriptions) {
        QMap<QString, Format>::const_iterator i = m_formats.find(desc.name());
        if (i != m_formats.end() && ((*i) != defaultFormat || s->contains(desc.name()))) {
            s->setValue(desc.name(), (*i).toString());
        }
    }
    s->endGroup();
}

bool FontSettings::fromSettings(const QString &category,
                                const FormatDescriptions &descriptions,
                                const QSettings *s)
{
    clear();

    if (!s->childGroups().contains(category))
        return false;

    QString group = category;
    group += QLatin1Char('/');

    m_family = s->value(group + QLatin1String(fontFamilyKey), defaultFixedFontFamily()).toString();
    m_fontSize = s->value(group + QLatin1String(QLatin1String(fontSizeKey)), m_fontSize).toInt();

    foreach (const FormatDescription &desc, descriptions) {
        const QString name = desc.name();
        const QString fmt = s->value(group + name, QString()).toString();
        if (fmt.isEmpty()) {
            m_formats[name].setForeground(desc.foreground());
            m_formats[name].setBackground(desc.background());
        } else {
            m_formats[name].fromString(fmt);
        }
    }
    return true;
}

bool FontSettings::equals(const FontSettings &f) const
{
    return m_family == f.m_family
            && m_fontSize == f.m_fontSize
            && m_formats == f.m_formats;
}

QTextCharFormat FontSettings::toTextCharFormat(const QString &category) const
{
    const Format f = m_formats.value(category);
    QTextCharFormat tf;
    if (category == QLatin1String("Text")) {
        tf.setFontFamily(m_family);
        tf.setFontPointSize(m_fontSize);
    }

    if (f.foreground().isValid())
        tf.setForeground(f.foreground());
    if (f.background().isValid() && (category == QLatin1String("Text") || f.background() != m_formats.value(QLatin1String("Text")).background()))
        tf.setBackground(f.background());
    tf.setFontWeight(f.bold() ? QFont::Bold : QFont::Normal);
    tf.setFontItalic(f.italic());
    return tf;
}

QVector<QTextCharFormat> FontSettings::toTextCharFormats(const QVector<QString> &categories) const
{
    QVector<QTextCharFormat> rc;
    const int size = categories.size();
    rc.reserve(size);
    for (int i = 0; i < size; i++)
         rc.push_back(toTextCharFormat(categories.at(i)));
    return rc;
}

QString FontSettings::family() const
{
    return m_family;
}

void FontSettings::setFamily(const QString &family)
{
    m_family = family;
}

int FontSettings::fontSize() const
{
    return m_fontSize;
}

void FontSettings::setFontSize(int size)
{
    m_fontSize = size;
}

Format &FontSettings::formatFor(const QString &category)
{
    return m_formats[category];
}

QString FontSettings::defaultFixedFontFamily()
{
    static QString rc;
    if (rc.isEmpty()) {
        QFont f(DEFAULT_FONT_FAMILY);
        f.setStyleHint(QFont::TypeWriter);
        rc = f.family();
    }
    return rc;
}

int FontSettings::defaultFontSize()
{
    return DEFAULT_FONT_SIZE;
}

} // namespace TextEditor