fontsettings.cpp 10.2 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** 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.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
31
32
#include "fontsettings.h"
#include "fontsettingspage.h"

hjk's avatar
hjk committed
33
#include <utils/qtcassert.h>
34
#include <coreplugin/icore.h>
hjk's avatar
hjk committed
35

36
37
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
con's avatar
con committed
38
#include <QtCore/QSettings>
39
#include <QtCore/QCoreApplication>
con's avatar
con committed
40
41
42
43
#include <QtGui/QTextCharFormat>

static const char *fontFamilyKey = "FontFamily";
static const char *fontSizeKey = "FontSize";
mae's avatar
mae committed
44
static const char *fontZoomKey= "FontZoom";
45
static const char *antialiasKey = "FontAntialias";
46
static const char *schemeFileNameKey = "ColorScheme";
con's avatar
con committed
47
48

namespace {
49
50
static const bool DEFAULT_ANTIALIAS = true;

con's avatar
con committed
51
52
53
54
#ifdef Q_WS_MAC
    enum { DEFAULT_FONT_SIZE = 12 };
    static const char *DEFAULT_FONT_FAMILY = "Monaco";
#else
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
55
#ifdef Q_WS_X11
con's avatar
con committed
56
57
58
59
60
61
62
63
64
65
66
67
    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 {

// -- FontSettings
mae's avatar
mae committed
68
FontSettings::FontSettings() :
con's avatar
con committed
69
    m_family(defaultFixedFontFamily()),
70
    m_fontSize(DEFAULT_FONT_SIZE),
mae's avatar
mae committed
71
    m_fontZoom(100),
72
    m_antialias(DEFAULT_ANTIALIAS)
con's avatar
con committed
73
74
75
76
77
78
79
{
}

void FontSettings::clear()
{
    m_family = defaultFixedFontFamily();
    m_fontSize = DEFAULT_FONT_SIZE;
mae's avatar
mae committed
80
    m_fontZoom = 100;
81
    m_antialias = DEFAULT_ANTIALIAS;
82
    m_scheme.clear();
con's avatar
con committed
83
84
85
86
87
88
89
90
91
92
93
94
}

void FontSettings::toSettings(const QString &category,
                              QSettings *s) const
{
    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);

mae's avatar
mae committed
95
96
97
    if (m_fontZoom!= 100 || s->contains(QLatin1String(fontZoomKey)))
        s->setValue(QLatin1String(fontZoomKey), m_fontZoom);

98
99
100
    if (m_antialias != DEFAULT_ANTIALIAS || s->contains(QLatin1String(antialiasKey)))
        s->setValue(QLatin1String(antialiasKey), m_antialias);

101
102
    if (m_schemeFileName != defaultSchemeFileName() || s->contains(QLatin1String(schemeFileNameKey)))
        s->setValue(QLatin1String(schemeFileNameKey), m_schemeFileName);
con's avatar
con committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    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();
120
    m_fontSize = s->value(group + QLatin1String(fontSizeKey), m_fontSize).toInt();
mae's avatar
mae committed
121
    m_fontZoom= s->value(group + QLatin1String(fontZoomKey), m_fontZoom).toInt();
122
    m_antialias = s->value(group + QLatin1String(antialiasKey), DEFAULT_ANTIALIAS).toBool();
123
124
125

    if (s->contains(group + QLatin1String(schemeFileNameKey))) {
        // Load the selected color scheme
126
127
128
129
        QString scheme = s->value(group + QLatin1String(schemeFileNameKey)).toString();
        if (scheme.isEmpty() || !QFile::exists(scheme))
            scheme = defaultSchemeFileName(QFileInfo(scheme).fileName());
        loadColorScheme(scheme, descriptions);
130
131
132
    } else {
        // Load color scheme from ini file
        foreach (const FormatDescription &desc, descriptions) {
133
134
            const QString id = desc.id();
            const QString fmt = s->value(group + id, QString()).toString();
135
136
137
138
139
140
141
142
143
            Format format;
            if (fmt.isEmpty()) {
                format.setForeground(desc.foreground());
                format.setBackground(desc.background());
                format.setBold(desc.format().bold());
                format.setItalic(desc.format().italic());
            } else {
                format.fromString(fmt);
            }
144
            m_scheme.setFormatFor(id, format);
145
146
        }

147
        m_scheme.setDisplayName(QCoreApplication::translate("TextEditor::Internal::FontSettings", "Customized"));
148
    }
con's avatar
con committed
149
150
151
152
153
154
155

    return true;
}

bool FontSettings::equals(const FontSettings &f) const
{
    return m_family == f.m_family
156
            && m_schemeFileName == f.m_schemeFileName
con's avatar
con committed
157
            && m_fontSize == f.m_fontSize
mae's avatar
mae committed
158
            && m_fontZoom == f.m_fontZoom
159
            && m_antialias == f.m_antialias
160
            && m_scheme == f.m_scheme;
con's avatar
con committed
161
162
}

163
164
165
/**
 * Returns the QTextCharFormat of the given format category.
 */
con's avatar
con committed
166
167
QTextCharFormat FontSettings::toTextCharFormat(const QString &category) const
{
168
169
170
    const Format &f = m_scheme.formatFor(category);
    const QLatin1String textCategory("Text");

con's avatar
con committed
171
    QTextCharFormat tf;
172
173

    if (category == textCategory) {
con's avatar
con committed
174
        tf.setFontFamily(m_family);
175
        tf.setFontPointSize(m_fontSize * m_fontZoom / 100.);
176
        tf.setFontStyleStrategy(m_antialias ? QFont::PreferAntialias : QFont::NoAntialias);
con's avatar
con committed
177
178
179
180
    }

    if (f.foreground().isValid())
        tf.setForeground(f.foreground());
181
    if (f.background().isValid() && (category == textCategory || f.background() != m_scheme.formatFor(textCategory).background()))
con's avatar
con committed
182
183
184
185
186
187
        tf.setBackground(f.background());
    tf.setFontWeight(f.bold() ? QFont::Bold : QFont::Normal);
    tf.setFontItalic(f.italic());
    return tf;
}

188
189
190
191
/**
 * Returns the list of QTextCharFormats that corresponds to the list of
 * requested format categories.
 */
con's avatar
con committed
192
193
194
195
196
197
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++)
198
         rc.append(toTextCharFormat(categories.at(i)));
con's avatar
con committed
199
200
201
    return rc;
}

202
203
204
/**
 * Returns the configured font family.
 */
con's avatar
con committed
205
206
207
208
209
210
211
212
213
214
QString FontSettings::family() const
{
    return m_family;
}

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

215
216
217
/**
 * Returns the configured font size.
 */
con's avatar
con committed
218
219
220
221
222
223
224
225
226
227
int FontSettings::fontSize() const
{
    return m_fontSize;
}

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

mae's avatar
mae committed
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
 * Returns the configured font zoom factor in percent.
 */
int FontSettings::fontZoom() const
{
    return m_fontZoom;
}

void FontSettings::setFontZoom(int zoom)
{
    m_fontZoom = zoom;
}

241
242
243
/**
 * Returns the configured antialiasing behavior.
 */
244
245
246
247
248
249
250
251
252
253
bool FontSettings::antialias() const
{
    return m_antialias;
}

void FontSettings::setAntialias(bool antialias)
{
    m_antialias = antialias;
}

254
255
256
/**
 * Returns the format for the given font category.
 */
con's avatar
con committed
257
258
Format &FontSettings::formatFor(const QString &category)
{
259
    return m_scheme.formatFor(category);
con's avatar
con committed
260
261
}

262
263
264
265
266
267
268
269
/**
 * Returns the file name of the currently selected color scheme.
 */
QString FontSettings::colorSchemeFileName() const
{
    return m_schemeFileName;
}

270
271
272
273
274
275
276
277
278
279
/**
 * Sets the file name of the color scheme. Does not load the scheme from the
 * given file. If you want to load a scheme, use loadColorScheme() instead.
 */
void FontSettings::setColorSchemeFileName(const QString &fileName)
{
    m_schemeFileName = fileName;
}

bool FontSettings::loadColorScheme(const QString &fileName,
280
                                   const FormatDescriptions &descriptions)
281
{
282
    bool loaded = true;
283
    m_schemeFileName = fileName;
284
285
286

    if (!m_scheme.load(m_schemeFileName)) {
        loaded = false;
287
        m_schemeFileName.clear();
288
289
        qWarning() << "Failed to load color scheme:" << fileName;
    }
290
291
292

    // Apply default formats to undefined categories
    foreach (const FormatDescription &desc, descriptions) {
293
294
        const QString id = desc.id();
        if (!m_scheme.contains(id)) {
295
296
297
298
299
            Format format;
            format.setForeground(desc.foreground());
            format.setBackground(desc.background());
            format.setBold(desc.format().bold());
            format.setItalic(desc.format().italic());
300
            m_scheme.setFormatFor(id, format);
301
302
        }
    }
303
304

    return loaded;
305
306
}

307
308
309
310
311
312
313
314
bool FontSettings::saveColorScheme(const QString &fileName)
{
    const bool saved = m_scheme.save(fileName);
    if (saved)
        m_schemeFileName = fileName;
    return saved;
}

315
316
317
/**
 * Returns the currently active color scheme.
 */
318
const ColorScheme &FontSettings::colorScheme() const
319
320
321
322
323
324
325
326
327
{
    return m_scheme;
}

void FontSettings::setColorScheme(const ColorScheme &scheme)
{
    m_scheme = scheme;
}

con's avatar
con committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
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;
}

344
345
346
347
348
/**
 * Returns the default scheme file name, or the path to a shipped scheme when
 * one exists with the given \a fileName.
 */
QString FontSettings::defaultSchemeFileName(const QString &fileName)
349
{
350
351
352
353
354
355
356
357
358
    QString defaultScheme = Core::ICore::instance()->resourcePath();
    defaultScheme += QLatin1String("/styles/");

    if (!fileName.isEmpty() && QFile::exists(defaultScheme + fileName))
        defaultScheme += fileName;
    else
        defaultScheme += QLatin1String("default.xml");

    return defaultScheme;
359
360
}

con's avatar
con committed
361
} // namespace TextEditor