Introduced a ColorScheme class that encapsulates a color scheme

This commit is contained in:
Thorbjørn Lindeijer
2009-06-18 12:28:40 +02:00
parent 5e48ed77b1
commit cc730442d6
7 changed files with 331 additions and 142 deletions

View File

@@ -0,0 +1,189 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact.
**
**************************************************************************/
#include "colorscheme.h"
#include <QtCore/QFile>
#include <QtXml/QXmlStreamWriter>
using namespace TextEditor;
static const char *trueString = "true";
static const char *falseString = "false";
// 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);
}
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;
}
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;
}
// ColorScheme
ColorScheme::ColorScheme()
{
}
bool ColorScheme::contains(const QString &category) const
{
return m_formats.contains(category);
}
Format &ColorScheme::formatFor(const QString &category)
{
return m_formats[category];
}
Format ColorScheme::formatFor(const QString &category) const
{
return m_formats.value(category);
}
void ColorScheme::setFormatFor(const QString &category, const Format &format)
{
m_formats[category] = format;
}
void ColorScheme::clear()
{
m_formats.clear();
}
bool ColorScheme::save(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return false;
QXmlStreamWriter w(&file);
w.setAutoFormatting(true);
w.setAutoFormattingIndent(2);
w.writeStartDocument();
w.writeStartElement(QLatin1String("style-scheme"));
w.writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
QMapIterator<QString, Format> i(m_formats);
while (i.hasNext()) {
const Format &format = i.next().value();
w.writeStartElement(QLatin1String("style"));
w.writeAttribute(QLatin1String("name"), i.key());
w.writeAttribute(QLatin1String("foreground"), format.foreground().name().toLower());
if (format.background().isValid())
w.writeAttribute(QLatin1String("background"), format.background().name().toLower());
if (format.bold())
w.writeAttribute(QLatin1String("bold"), QLatin1String(trueString));
if (format.italic())
w.writeAttribute(QLatin1String("italic"), QLatin1String(trueString));
w.writeEndElement();
}
w.writeEndElement();
w.writeEndDocument();
return true;
}
bool ColorScheme::load(const QString &fileName)
{
Q_UNUSED(fileName)
return false;
}

View File

@@ -0,0 +1,108 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact.
**
**************************************************************************/
#ifndef COLORSCHEME_H
#define COLORSCHEME_H
#include "texteditor_global.h"
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtGui/QColor>
#include <QtGui/QTextCharFormat>
namespace TextEditor {
/*! Format for a particular piece of text (text/comment, etc). */
class TEXTEDITOR_EXPORT Format
{
public:
Format();
QColor foreground() const { return m_foreground; }
void setForeground(const QColor &foreground);
QColor background() const { return m_background; }
void setBackground(const QColor &background);
bool bold() const { return m_bold; }
void setBold(bool bold);
bool italic() const { return m_italic; }
void setItalic(bool italic);
bool equals(const Format &f) const;
QString toString() const;
bool fromString(const QString &str);
private:
QColor m_foreground;
QColor m_background;
bool m_bold;
bool m_italic;
};
inline bool operator==(const Format &f1, const Format &f2) { return f1.equals(f2); }
inline bool operator!=(const Format &f1, const Format &f2) { return !f1.equals(f2); }
/*! A color scheme combines a set of formats for different highlighting
categories. It also provides saving and loading of the scheme to a file.
*/
class ColorScheme
{
public:
ColorScheme();
bool contains(const QString &category) const;
Format &formatFor(const QString &category);
Format formatFor(const QString &category) const;
void setFormatFor(const QString &category, const Format &format);
void clear();
bool save(const QString &fileName);
bool load(const QString &fileName);
inline bool equals(const ColorScheme &cs) const
{ return m_formats == cs.m_formats; }
private:
QMap<QString, Format> m_formats;
};
inline bool operator==(const ColorScheme &cs1, const ColorScheme &cs2) { return cs1.equals(cs2); }
inline bool operator!=(const ColorScheme &cs1, const ColorScheme &cs2) { return !cs1.equals(cs2); }
} // namespace TextEditor
#endif // COLORSCHEME_H

View File

@@ -38,8 +38,6 @@
static const char *fontFamilyKey = "FontFamily";
static const char *fontSizeKey = "FontSize";
static const char *antialiasKey = "FontAntialias";
static const char *trueString = "true";
static const char *falseString = "false";
namespace {
static const bool DEFAULT_ANTIALIAS = true;
@@ -60,81 +58,6 @@ static const bool DEFAULT_ANTIALIAS = true;
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() :
m_family(defaultFixedFontFamily()),
@@ -148,16 +71,13 @@ void FontSettings::clear()
m_family = defaultFixedFontFamily();
m_fontSize = DEFAULT_FONT_SIZE;
m_antialias = DEFAULT_ANTIALIAS;
m_formats.clear();
//qFill(m_formats.begin(), m_formats.end(), Format());
m_scheme.clear();
}
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);
@@ -171,9 +91,11 @@ void FontSettings::toSettings(const QString &category,
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());
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());
}
}
s->endGroup();
@@ -198,14 +120,16 @@ bool FontSettings::fromSettings(const QString &category,
foreach (const FormatDescription &desc, descriptions) {
const QString name = desc.name();
const QString fmt = s->value(group + name, QString()).toString();
Format format;
if (fmt.isEmpty()) {
m_formats[name].setForeground(desc.foreground());
m_formats[name].setBackground(desc.background());
m_formats[name].setBold(desc.format().bold());
m_formats[name].setItalic(desc.format().italic());
format.setForeground(desc.foreground());
format.setBackground(desc.background());
format.setBold(desc.format().bold());
format.setItalic(desc.format().italic());
} else {
m_formats[name].fromString(fmt);
format.fromString(fmt);
}
m_scheme.setFormatFor(name, format);
}
return true;
}
@@ -215,14 +139,17 @@ bool FontSettings::equals(const FontSettings &f) const
return m_family == f.m_family
&& m_fontSize == f.m_fontSize
&& m_antialias == f.m_antialias
&& m_formats == f.m_formats;
&& m_scheme == f.m_scheme;
}
QTextCharFormat FontSettings::toTextCharFormat(const QString &category) const
{
const Format f = m_formats.value(category);
const Format &f = m_scheme.formatFor(category);
const QLatin1String textCategory("Text");
QTextCharFormat tf;
if (category == QLatin1String("Text")) {
if (category == textCategory) {
tf.setFontFamily(m_family);
tf.setFontPointSize(m_fontSize);
tf.setFontStyleStrategy(m_antialias ? QFont::PreferAntialias : QFont::NoAntialias);
@@ -230,7 +157,7 @@ QTextCharFormat FontSettings::toTextCharFormat(const QString &category) const
if (f.foreground().isValid())
tf.setForeground(f.foreground());
if (f.background().isValid() && (category == QLatin1String("Text") || f.background() != m_formats.value(QLatin1String("Text")).background()))
if (f.background().isValid() && (category == textCategory || f.background() != m_scheme.formatFor(textCategory).background()))
tf.setBackground(f.background());
tf.setFontWeight(f.bold() ? QFont::Bold : QFont::Normal);
tf.setFontItalic(f.italic());
@@ -280,7 +207,7 @@ void FontSettings::setAntialias(bool antialias)
Format &FontSettings::formatFor(const QString &category)
{
return m_formats[category];
return m_scheme.formatFor(category);
}
QString FontSettings::defaultFixedFontFamily()

View File

@@ -32,6 +32,8 @@
#include "texteditor_global.h"
#include "colorscheme.h"
#include <QtCore/QString>
#include <QtCore/QList>
#include <QtCore/QMap>
@@ -47,39 +49,6 @@ namespace TextEditor {
class FormatDescription;
// Format for a particular piece of text (text/comment, etc).
class TEXTEDITOR_EXPORT Format
{
public:
Format();
QColor foreground() const { return m_foreground; }
void setForeground(const QColor &foreground);
QColor background() const { return m_background; }
void setBackground(const QColor &background);
bool bold() const { return m_bold; }
void setBold(bool bold);
bool italic() const { return m_italic; }
void setItalic(bool italic);
bool equals(const Format &f) const;
QString toString() const;
bool fromString(const QString &str);
private:
QColor m_foreground;
QColor m_background;
bool m_bold;
bool m_italic;
};
inline bool operator==(const Format &f1, const Format &f2) { return f1.equals(f2); }
inline bool operator!=(const Format &f1, const Format &f2) { return !f1.equals(f2); }
/**
* Font settings (default font and enumerated list of formats).
*/
@@ -143,7 +112,7 @@ private:
QString m_family;
int m_fontSize;
bool m_antialias;
QMap<QString, Format> m_formats;
ColorScheme m_scheme;
};
inline bool operator==(const FontSettings &f1, const FontSettings &f2) { return f1.equals(f2); }

View File

@@ -129,16 +129,6 @@ FormatDescription::FormatDescription(const QString &name, const QString &trName,
m_format.setForeground(color);
}
QString FormatDescription::name() const
{
return m_name;
}
QString FormatDescription::trName() const
{
return m_trName;
}
QColor FormatDescription::foreground() const
{
if (m_name == QLatin1String(Constants::C_LINE_NUMBER)) {

View File

@@ -59,8 +59,12 @@ public:
FormatDescription(const QString &name, const QString &trName,
const QColor &foreground = Qt::black);
QString name() const;
QString trName() const;
QString name() const
{ return m_name; }
QString trName() const
{ return m_trName; }
QColor foreground() const;
QColor background() const;

View File

@@ -26,7 +26,8 @@ SOURCES += texteditorplugin.cpp \
basefilefind.cpp \
texteditorsettings.cpp \
codecselector.cpp \
findincurrentfile.cpp
findincurrentfile.cpp \
colorscheme.cpp
HEADERS += texteditorplugin.h \
textfilewizard.h \
plaintexteditor.h \
@@ -56,7 +57,8 @@ HEADERS += texteditorplugin.h \
basefilefind.h \
texteditorsettings.h \
codecselector.h \
findincurrentfile.h
findincurrentfile.h \
colorscheme.h
FORMS += behaviorsettingspage.ui \
displaysettingspage.ui \
fontsettingspage.ui