OSDN Git Service

b6ebe4ed7c836ae52f1a3c8b8ca322d6c3942f0d
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / colorscheme.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "colorscheme.h"
35
36 #include "texteditorconstants.h"
37
38 #include <QtCore/QFile>
39 #include <QtCore/QCoreApplication>
40 #include <QtXml/QXmlStreamWriter>
41
42 using namespace TextEditor;
43
44 static const char *trueString = "true";
45
46 // Format
47
48 Format::Format() :
49     m_foreground(Qt::black),
50     m_background(Qt::white),
51     m_bold(false),
52     m_italic(false)
53 {
54 }
55
56 void Format::setForeground(const QColor &foreground)
57 {
58     m_foreground = foreground;
59 }
60
61 void Format::setBackground(const QColor &background)
62 {
63     m_background = background;
64 }
65
66 void Format::setBold(bool bold)
67 {
68     m_bold = bold;
69 }
70
71 void Format::setItalic(bool italic)
72 {
73     m_italic = italic;
74 }
75
76 static QColor stringToColor(const QString &string)
77 {
78     if (string == QLatin1String("invalid"))
79         return QColor();
80     return QColor(string);
81 }
82
83 bool Format::equals(const Format &f) const
84 {
85     return m_foreground ==  f.m_foreground && m_background == f.m_background &&
86            m_bold == f.m_bold && m_italic == f.m_italic;
87 }
88
89 bool Format::fromString(const QString &str)
90 {
91     *this = Format();
92
93     const QStringList lst = str.split(QLatin1Char(';'));
94     if (lst.count() != 4)
95         return false;
96
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);
101     return true;
102 }
103
104
105 // ColorScheme
106
107 ColorScheme::ColorScheme()
108 {
109 }
110
111 bool ColorScheme::contains(const QString &category) const
112 {
113     return m_formats.contains(category);
114 }
115
116 Format &ColorScheme::formatFor(const QString &category)
117 {
118     return m_formats[category];
119 }
120
121 Format ColorScheme::formatFor(const QString &category) const
122 {
123     return m_formats.value(category);
124 }
125
126 void ColorScheme::setFormatFor(const QString &category, const Format &format)
127 {
128     m_formats[category] = format;
129 }
130
131 void ColorScheme::clear()
132 {
133     m_formats.clear();
134 }
135
136 bool ColorScheme::save(const QString &fileName) const
137 {
138     QFile file(fileName);
139     if (!file.open(QIODevice::WriteOnly))
140         return false;
141
142     QXmlStreamWriter w(&file);
143     w.setAutoFormatting(true);
144     w.setAutoFormattingIndent(2);
145
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);
151
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());
161         if (format.bold())
162             w.writeAttribute(QLatin1String("bold"), QLatin1String(trueString));
163         if (format.italic())
164             w.writeAttribute(QLatin1String("italic"), QLatin1String(trueString));
165         w.writeEndElement();
166     }
167
168     w.writeEndElement();
169     w.writeEndDocument();
170
171     return true;
172 }
173
174 namespace {
175
176 class ColorSchemeReader : public QXmlStreamReader
177 {
178 public:
179     ColorSchemeReader() :
180         m_scheme(0)
181     {}
182
183     bool read(const QString &fileName, ColorScheme *scheme);
184     QString readName(const QString &fileName);
185
186 private:
187     bool readNextStartElement();
188     void skipCurrentElement();
189     void readStyleScheme();
190     void readStyle();
191
192     ColorScheme *m_scheme;
193     QString m_name;
194 };
195
196 bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme)
197 {
198     m_scheme = scheme;
199
200     if (m_scheme)
201         m_scheme->clear();
202
203     QFile file(fileName);
204     if (!file.open(QFile::ReadOnly | QFile::Text))
205         return false;
206
207     setDevice(&file);
208
209     if (readNextStartElement() && name() == QLatin1String("style-scheme"))
210         readStyleScheme();
211     else
212         raiseError(QCoreApplication::translate("TextEditor::Internal::ColorScheme", "Not a color scheme file."));
213
214     return true;
215 }
216
217 QString ColorSchemeReader::readName(const QString &fileName)
218 {
219     read(fileName, 0);
220     return m_name;
221 }
222
223 bool ColorSchemeReader::readNextStartElement()
224 {
225     while (readNext() != Invalid) {
226         if (isStartElement())
227             return true;
228         else if (isEndElement())
229             return false;
230     }
231     return false;
232 }
233
234 void ColorSchemeReader::skipCurrentElement()
235 {
236     while (readNextStartElement())
237         skipCurrentElement();
238 }
239
240 void ColorSchemeReader::readStyleScheme()
241 {
242     Q_ASSERT(isStartElement() && name() == QLatin1String("style-scheme"));
243
244     const QXmlStreamAttributes attr = attributes();
245     m_name = attr.value(QLatin1String("name")).toString();
246     if (!m_scheme)
247         // We're done
248         raiseError(QLatin1String("name loaded"));
249     else
250         m_scheme->setDisplayName(m_name);
251
252     while (readNextStartElement()) {
253         if (name() == QLatin1String("style"))
254             readStyle();
255         else
256             skipCurrentElement();
257     }
258 }
259
260 void ColorSchemeReader::readStyle()
261 {
262     Q_ASSERT(isStartElement() && name() == QLatin1String("style"));
263
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);
270
271     Format format;
272
273     if (QColor::isValidColor(foreground))
274         format.setForeground(QColor(foreground));
275     else
276         format.setForeground(QColor());
277
278     if (QColor::isValidColor(background))
279         format.setBackground(QColor(background));
280     else
281         format.setBackground(QColor());
282
283     format.setBold(bold);
284     format.setItalic(italic);
285
286     m_scheme->setFormatFor(name, format);
287
288     skipCurrentElement();
289 }
290
291 } // anonymous namespace
292
293
294 bool ColorScheme::load(const QString &fileName)
295 {
296     ColorSchemeReader reader;
297     return reader.read(fileName, this) && !reader.hasError();
298 }
299
300 QString ColorScheme::readNameOfScheme(const QString &fileName)
301 {
302     return ColorSchemeReader().readName(fileName);
303 }