colorscheme.cpp 7.26 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**************************************************************************
**
** 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
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
27
28
29
30
31
**
**************************************************************************/

#include "colorscheme.h"

32
33
#include "texteditorconstants.h"

34
#include <QtCore/QFile>
35
#include <QtCore/QCoreApplication>
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <QtXml/QXmlStreamWriter>

using namespace TextEditor;

static const char *trueString = "true";

// 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 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;
}

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();
}

132
bool ColorScheme::save(const QString &fileName) const
133
134
135
136
137
138
139
140
141
142
143
144
{
    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"));
145
146
    if (!m_name.isEmpty())
        w.writeAttribute(QLatin1String("name"), m_name);
147
148
149
150
151
152

    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());
153
        if (format.foreground().isValid())
154
            w.writeAttribute(QLatin1String("foreground"), format.foreground().name().toLower());
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
        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;
}

170
171
172
173
174
namespace {

class ColorSchemeReader : public QXmlStreamReader
{
public:
175
176
    ColorSchemeReader() :
        m_scheme(0)
177
178
    {}

179
180
    bool read(const QString &fileName, ColorScheme *scheme);
    QString readName(const QString &fileName);
181
182

private:
183
184
    bool readNextStartElement();
    void skipCurrentElement();
185
186
187
188
    void readStyleScheme();
    void readStyle();

    ColorScheme *m_scheme;
189
    QString m_name;
190
191
};

192
bool ColorSchemeReader::read(const QString &fileName, ColorScheme *scheme)
193
{
194
195
196
197
    m_scheme = scheme;

    if (m_scheme)
        m_scheme->clear();
198
199
200
201
202
203
204

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly | QFile::Text))
        return false;

    setDevice(&file);

205
206
207
    if (readNextStartElement() && name() == QLatin1String("style-scheme"))
        readStyleScheme();
    else
208
        raiseError(QCoreApplication::translate("TextEditor::Internal::ColorScheme", "Not a color scheme file."));
209
210
211
212

    return true;
}

213
214
215
216
217
218
QString ColorSchemeReader::readName(const QString &fileName)
{
    read(fileName, 0);
    return m_name;
}

219
bool ColorSchemeReader::readNextStartElement()
220
{
221
222
223
224
225
    while (readNext() != Invalid) {
        if (isStartElement())
            return true;
        else if (isEndElement())
            return false;
226
    }
227
228
229
230
231
232
233
    return false;
}

void ColorSchemeReader::skipCurrentElement()
{
    while (readNextStartElement())
        skipCurrentElement();
234
235
236
237
238
239
}

void ColorSchemeReader::readStyleScheme()
{
    Q_ASSERT(isStartElement() && name() == QLatin1String("style-scheme"));

240
241
242
243
244
245
246
247
    const QXmlStreamAttributes attr = attributes();
    m_name = attr.value(QLatin1String("name")).toString();
    if (!m_scheme)
        // We're done
        raiseError(QLatin1String("name loaded"));
    else
        m_scheme->setName(m_name);

248
249
250
251
252
    while (readNextStartElement()) {
        if (name() == QLatin1String("style"))
            readStyle();
        else
            skipCurrentElement();
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
    }
}

void ColorSchemeReader::readStyle()
{
    Q_ASSERT(isStartElement() && name() == QLatin1String("style"));

    const QXmlStreamAttributes attr = attributes();
    QString name = attr.value(QLatin1String("name")).toString();
    QString foreground = attr.value(QLatin1String("foreground")).toString();
    QString background = attr.value(QLatin1String("background")).toString();
    bool bold = attr.value(QLatin1String("bold")) == QLatin1String(trueString);
    bool italic = attr.value(QLatin1String("italic")) == QLatin1String(trueString);

    Format format;
268
269
    format.setForeground(QColor(foreground));
    format.setBackground(QColor(background));
270
271
272
273
274
    format.setBold(bold);
    format.setItalic(italic);

    m_scheme->setFormatFor(name, format);

275
    skipCurrentElement();
276
277
278
279
280
}

} // anonymous namespace


281
282
bool ColorScheme::load(const QString &fileName)
{
283
284
285
286
287
288
289
    ColorSchemeReader reader;
    return reader.read(fileName, this) && !reader.hasError();
}

QString ColorScheme::readNameOfScheme(const QString &fileName)
{
    return ColorSchemeReader().readName(fileName);
290
}