1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "colorscheme.h"
35 #include "texteditorconstants.h"
37 #include <QtCore/QFile>
38 #include <QtCore/QCoreApplication>
39 #include <QtXml/QXmlStreamWriter>
41 using namespace TextEditor;
43 static const char *trueString = "true";
48 m_foreground(Qt::black),
49 m_background(Qt::white),
55 void Format::setForeground(const QColor &foreground)
57 m_foreground = foreground;
60 void Format::setBackground(const QColor &background)
62 m_background = background;
65 void Format::setBold(bool bold)
70 void Format::setItalic(bool italic)
75 static QColor stringToColor(const QString &string)
77 if (string == QLatin1String("invalid"))
79 return QColor(string);
82 bool Format::equals(const Format &f) const
84 return m_foreground == f.m_foreground && m_background == f.m_background &&
85 m_bold == f.m_bold && m_italic == f.m_italic;
88 bool Format::fromString(const QString &str)
92 const QStringList lst = str.split(QLatin1Char(';'));
96 m_foreground = stringToColor(lst.at(0));
97 m_background = stringToColor(lst.at(1));
98 m_bold = lst.at(2) == QLatin1String(trueString);
99 m_italic = lst.at(3) == QLatin1String(trueString);
106 ColorScheme::ColorScheme()
110 bool ColorScheme::contains(const QString &category) const
112 return m_formats.contains(category);
115 Format &ColorScheme::formatFor(const QString &category)
117 return m_formats[category];
120 Format ColorScheme::formatFor(const QString &category) const
122 return m_formats.value(category);
125 void ColorScheme::setFormatFor(const QString &category, const Format &format)
127 m_formats[category] = format;
130 void ColorScheme::clear()
135 bool ColorScheme::save(const QString &fileName) const
137 QFile file(fileName);
138 if (!file.open(QIODevice::WriteOnly))
141 QXmlStreamWriter w(&file);
142 w.setAutoFormatting(true);
143 w.setAutoFormattingIndent(2);
145 w.writeStartDocument();
146 w.writeStartElement(QLatin1String("style-scheme"));
147 w.writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
148 if (!m_displayName.isEmpty())
149 w.writeAttribute(QLatin1String("name"), m_displayName);
151 QMapIterator<QString, Format> i(m_formats);
152 while (i.hasNext()) {
153 const Format &format = i.next().value();
154 w.writeStartElement(QLatin1String("style"));
155 w.writeAttribute(QLatin1String("name"), i.key());
156 if (format.foreground().isValid())
157 w.writeAttribute(QLatin1String("foreground"), format.foreground().name().toLower());
158 if (format.background().isValid())
159 w.writeAttribute(QLatin1String("background"), format.background().name().toLower());
161 w.writeAttribute(QLatin1String("bold"), QLatin1String(trueString));
163 w.writeAttribute(QLatin1String("italic"), QLatin1String(trueString));
168 w.writeEndDocument();
175 class ColorSchemeReader : public QXmlStreamReader
178 ColorSchemeReader() :
182 bool read(const QString &fileName, ColorScheme *scheme);
183 QString readName(const QString &fileName);
186 bool readNextStartElement();
187 void skipCurrentElement();
188 void readStyleScheme();
191 ColorScheme *m_scheme;
195 bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme)
202 QFile file(fileName);
203 if (!file.open(QFile::ReadOnly | QFile::Text))
208 if (readNextStartElement() && name() == QLatin1String("style-scheme"))
211 raiseError(QCoreApplication::translate("TextEditor::Internal::ColorScheme", "Not a color scheme file."));
216 QString ColorSchemeReader::readName(const QString &fileName)
222 bool ColorSchemeReader::readNextStartElement()
224 while (readNext() != Invalid) {
225 if (isStartElement())
227 else if (isEndElement())
233 void ColorSchemeReader::skipCurrentElement()
235 while (readNextStartElement())
236 skipCurrentElement();
239 void ColorSchemeReader::readStyleScheme()
241 Q_ASSERT(isStartElement() && name() == QLatin1String("style-scheme"));
243 const QXmlStreamAttributes attr = attributes();
244 m_name = attr.value(QLatin1String("name")).toString();
247 raiseError(QLatin1String("name loaded"));
249 m_scheme->setDisplayName(m_name);
251 while (readNextStartElement()) {
252 if (name() == QLatin1String("style"))
255 skipCurrentElement();
259 void ColorSchemeReader::readStyle()
261 Q_ASSERT(isStartElement() && name() == QLatin1String("style"));
263 const QXmlStreamAttributes attr = attributes();
264 QString name = attr.value(QLatin1String("name")).toString();
265 QString foreground = attr.value(QLatin1String("foreground")).toString();
266 QString background = attr.value(QLatin1String("background")).toString();
267 bool bold = attr.value(QLatin1String("bold")) == QLatin1String(trueString);
268 bool italic = attr.value(QLatin1String("italic")) == QLatin1String(trueString);
272 if (QColor::isValidColor(foreground))
273 format.setForeground(QColor(foreground));
275 format.setForeground(QColor());
277 if (QColor::isValidColor(background))
278 format.setBackground(QColor(background));
280 format.setBackground(QColor());
282 format.setBold(bold);
283 format.setItalic(italic);
285 m_scheme->setFormatFor(name, format);
287 skipCurrentElement();
290 } // anonymous namespace
293 bool ColorScheme::load(const QString &fileName)
295 ColorSchemeReader reader;
296 return reader.read(fileName, this) && !reader.hasError();
299 QString ColorScheme::readNameOfScheme(const QString &fileName)
301 return ColorSchemeReader().readName(fileName);