OSDN Git Service

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