1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "colorscheme.h"
36 #include "texteditorconstants.h"
38 #include <QtCore/QFile>
39 #include <QtCore/QCoreApplication>
40 #include <QtXml/QXmlStreamWriter>
42 using namespace TextEditor;
44 static const char *trueString = "true";
49 m_foreground(Qt::black),
50 m_background(Qt::white),
56 void Format::setForeground(const QColor &foreground)
58 m_foreground = foreground;
61 void Format::setBackground(const QColor &background)
63 m_background = background;
66 void Format::setBold(bool bold)
71 void Format::setItalic(bool italic)
76 static QColor stringToColor(const QString &string)
78 if (string == QLatin1String("invalid"))
80 return QColor(string);
83 bool Format::equals(const Format &f) const
85 return m_foreground == f.m_foreground && m_background == f.m_background &&
86 m_bold == f.m_bold && m_italic == f.m_italic;
89 bool Format::fromString(const QString &str)
93 const QStringList lst = str.split(QLatin1Char(';'));
97 m_foreground = stringToColor(lst.at(0));
98 m_background = stringToColor(lst.at(1));
99 m_bold = lst.at(2) == QLatin1String(trueString);
100 m_italic = lst.at(3) == QLatin1String(trueString);
107 ColorScheme::ColorScheme()
111 bool ColorScheme::contains(const QString &category) const
113 return m_formats.contains(category);
116 Format &ColorScheme::formatFor(const QString &category)
118 return m_formats[category];
121 Format ColorScheme::formatFor(const QString &category) const
123 return m_formats.value(category);
126 void ColorScheme::setFormatFor(const QString &category, const Format &format)
128 m_formats[category] = format;
131 void ColorScheme::clear()
136 bool ColorScheme::save(const QString &fileName) const
138 QFile file(fileName);
139 if (!file.open(QIODevice::WriteOnly))
142 QXmlStreamWriter w(&file);
143 w.setAutoFormatting(true);
144 w.setAutoFormattingIndent(2);
146 w.writeStartDocument();
147 w.writeStartElement(QLatin1String("style-scheme"));
148 w.writeAttribute(QLatin1String("version"), QLatin1String("1.0"));
149 if (!m_displayName.isEmpty())
150 w.writeAttribute(QLatin1String("name"), m_displayName);
152 QMapIterator<QString, Format> i(m_formats);
153 while (i.hasNext()) {
154 const Format &format = i.next().value();
155 w.writeStartElement(QLatin1String("style"));
156 w.writeAttribute(QLatin1String("name"), i.key());
157 if (format.foreground().isValid())
158 w.writeAttribute(QLatin1String("foreground"), format.foreground().name().toLower());
159 if (format.background().isValid())
160 w.writeAttribute(QLatin1String("background"), format.background().name().toLower());
162 w.writeAttribute(QLatin1String("bold"), QLatin1String(trueString));
164 w.writeAttribute(QLatin1String("italic"), QLatin1String(trueString));
169 w.writeEndDocument();
176 class ColorSchemeReader : public QXmlStreamReader
179 ColorSchemeReader() :
183 bool read(const QString &fileName, ColorScheme *scheme);
184 QString readName(const QString &fileName);
187 bool readNextStartElement();
188 void skipCurrentElement();
189 void readStyleScheme();
192 ColorScheme *m_scheme;
196 bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme)
203 QFile file(fileName);
204 if (!file.open(QFile::ReadOnly | QFile::Text))
209 if (readNextStartElement() && name() == QLatin1String("style-scheme"))
212 raiseError(QCoreApplication::translate("TextEditor::Internal::ColorScheme", "Not a color scheme file."));
217 QString ColorSchemeReader::readName(const QString &fileName)
223 bool ColorSchemeReader::readNextStartElement()
225 while (readNext() != Invalid) {
226 if (isStartElement())
228 else if (isEndElement())
234 void ColorSchemeReader::skipCurrentElement()
236 while (readNextStartElement())
237 skipCurrentElement();
240 void ColorSchemeReader::readStyleScheme()
242 Q_ASSERT(isStartElement() && name() == QLatin1String("style-scheme"));
244 const QXmlStreamAttributes attr = attributes();
245 m_name = attr.value(QLatin1String("name")).toString();
248 raiseError(QLatin1String("name loaded"));
250 m_scheme->setDisplayName(m_name);
252 while (readNextStartElement()) {
253 if (name() == QLatin1String("style"))
256 skipCurrentElement();
260 void ColorSchemeReader::readStyle()
262 Q_ASSERT(isStartElement() && name() == QLatin1String("style"));
264 const QXmlStreamAttributes attr = attributes();
265 QString name = attr.value(QLatin1String("name")).toString();
266 QString foreground = attr.value(QLatin1String("foreground")).toString();
267 QString background = attr.value(QLatin1String("background")).toString();
268 bool bold = attr.value(QLatin1String("bold")) == QLatin1String(trueString);
269 bool italic = attr.value(QLatin1String("italic")) == QLatin1String(trueString);
273 if (QColor::isValidColor(foreground))
274 format.setForeground(QColor(foreground));
276 format.setForeground(QColor());
278 if (QColor::isValidColor(background))
279 format.setBackground(QColor(background));
281 format.setBackground(QColor());
283 format.setBold(bold);
284 format.setItalic(italic);
286 m_scheme->setFormatFor(name, format);
288 skipCurrentElement();
291 } // anonymous namespace
294 bool ColorScheme::load(const QString &fileName)
296 ColorSchemeReader reader;
297 return reader.read(fileName, this) && !reader.hasError();
300 QString ColorScheme::readNameOfScheme(const QString &fileName)
302 return ColorSchemeReader().readName(fileName);