2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** Commercial Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** 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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-06-17 00:01:27 +10:00
|
|
|
** contact the sales department at http://www.qtsoftware.com/contact.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "fontsettings.h"
|
|
|
|
#include "fontsettingspage.h"
|
|
|
|
|
2008-12-09 15:25:01 +01:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QtCore/QSettings>
|
|
|
|
#include <QtGui/QTextCharFormat>
|
|
|
|
|
|
|
|
static const char *fontFamilyKey = "FontFamily";
|
|
|
|
static const char *fontSizeKey = "FontSize";
|
2009-05-27 15:33:37 +02:00
|
|
|
static const char *antialiasKey = "FontAntialias";
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
namespace {
|
2009-05-27 15:33:37 +02:00
|
|
|
static const bool DEFAULT_ANTIALIAS = true;
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#ifdef Q_WS_MAC
|
|
|
|
enum { DEFAULT_FONT_SIZE = 12 };
|
|
|
|
static const char *DEFAULT_FONT_FAMILY = "Monaco";
|
|
|
|
#else
|
2009-06-03 20:45:49 +02:00
|
|
|
#ifdef Q_WS_X11
|
2008-12-02 12:01:29 +01:00
|
|
|
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
|
2009-06-17 19:12:19 +02:00
|
|
|
FontSettings::FontSettings() :
|
2008-12-02 12:01:29 +01:00
|
|
|
m_family(defaultFixedFontFamily()),
|
2009-05-27 15:33:37 +02:00
|
|
|
m_fontSize(DEFAULT_FONT_SIZE),
|
|
|
|
m_antialias(DEFAULT_ANTIALIAS)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontSettings::clear()
|
|
|
|
{
|
|
|
|
m_family = defaultFixedFontFamily();
|
|
|
|
m_fontSize = DEFAULT_FONT_SIZE;
|
2009-05-27 15:33:37 +02:00
|
|
|
m_antialias = DEFAULT_ANTIALIAS;
|
2009-06-18 12:28:40 +02:00
|
|
|
m_scheme.clear();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FontSettings::toSettings(const QString &category,
|
|
|
|
const FormatDescriptions &descriptions,
|
|
|
|
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);
|
|
|
|
|
2009-05-27 15:33:37 +02:00
|
|
|
if (m_antialias != DEFAULT_ANTIALIAS || s->contains(QLatin1String(antialiasKey)))
|
|
|
|
s->setValue(QLatin1String(antialiasKey), m_antialias);
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
const Format defaultFormat;
|
|
|
|
|
|
|
|
foreach (const FormatDescription &desc, descriptions) {
|
2009-06-18 12:28:40 +02:00
|
|
|
const QString name = desc.name();
|
|
|
|
if (m_scheme.contains(name)) {
|
|
|
|
const Format &f = m_scheme.formatFor(name);
|
|
|
|
if (f != defaultFormat || s->contains(name))
|
|
|
|
s->setValue(name, f.toString());
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
2009-05-27 15:33:37 +02:00
|
|
|
m_fontSize = s->value(group + QLatin1String(fontSizeKey), m_fontSize).toInt();
|
|
|
|
m_antialias = s->value(group + QLatin1String(antialiasKey), DEFAULT_ANTIALIAS).toBool();
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
foreach (const FormatDescription &desc, descriptions) {
|
|
|
|
const QString name = desc.name();
|
|
|
|
const QString fmt = s->value(group + name, QString()).toString();
|
2009-06-18 12:28:40 +02:00
|
|
|
Format format;
|
2008-12-02 12:01:29 +01:00
|
|
|
if (fmt.isEmpty()) {
|
2009-06-18 12:28:40 +02:00
|
|
|
format.setForeground(desc.foreground());
|
|
|
|
format.setBackground(desc.background());
|
|
|
|
format.setBold(desc.format().bold());
|
|
|
|
format.setItalic(desc.format().italic());
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
2009-06-18 12:28:40 +02:00
|
|
|
format.fromString(fmt);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2009-06-18 12:28:40 +02:00
|
|
|
m_scheme.setFormatFor(name, format);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FontSettings::equals(const FontSettings &f) const
|
|
|
|
{
|
|
|
|
return m_family == f.m_family
|
|
|
|
&& m_fontSize == f.m_fontSize
|
2009-05-27 15:33:37 +02:00
|
|
|
&& m_antialias == f.m_antialias
|
2009-06-18 12:28:40 +02:00
|
|
|
&& m_scheme == f.m_scheme;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat FontSettings::toTextCharFormat(const QString &category) const
|
|
|
|
{
|
2009-06-18 12:28:40 +02:00
|
|
|
const Format &f = m_scheme.formatFor(category);
|
|
|
|
const QLatin1String textCategory("Text");
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QTextCharFormat tf;
|
2009-06-18 12:28:40 +02:00
|
|
|
|
|
|
|
if (category == textCategory) {
|
2008-12-02 12:01:29 +01:00
|
|
|
tf.setFontFamily(m_family);
|
|
|
|
tf.setFontPointSize(m_fontSize);
|
2009-05-27 15:33:37 +02:00
|
|
|
tf.setFontStyleStrategy(m_antialias ? QFont::PreferAntialias : QFont::NoAntialias);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (f.foreground().isValid())
|
|
|
|
tf.setForeground(f.foreground());
|
2009-06-18 12:28:40 +02:00
|
|
|
if (f.background().isValid() && (category == textCategory || f.background() != m_scheme.formatFor(textCategory).background()))
|
2008-12-02 12:01:29 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-05-27 15:33:37 +02:00
|
|
|
bool FontSettings::antialias() const
|
|
|
|
{
|
|
|
|
return m_antialias;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FontSettings::setAntialias(bool antialias)
|
|
|
|
{
|
|
|
|
m_antialias = antialias;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Format &FontSettings::formatFor(const QString &category)
|
|
|
|
{
|
2009-06-18 12:28:40 +02:00
|
|
|
return m_scheme.formatFor(category);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|