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
26
27
28
29
30
31
32
33
34
35
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Designer of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtbrushmanager.h"
#include "brushmanagerproxy.h"
#include "qsimpleresource_p.h"
#include "qdesigner_utils_p.h"
#include "ui4_p.h"
#include <QtXml/QXmlStreamWriter>
QT_BEGIN_NAMESPACE
namespace qdesigner_internal {
class BrushManagerProxyPrivate
{
BrushManagerProxy *q_ptr;
Q_DECLARE_PUBLIC(BrushManagerProxy)
public:
BrushManagerProxyPrivate(BrushManagerProxy *bp, QDesignerFormEditorInterface *core);
void brushAdded(const QString &name, const QBrush &brush);
void brushRemoved(const QString &name);
QString uniqueBrushFileName(const QString &brushName) const;
QtBrushManager *m_Manager;
QString m_designerFolder;
const QString m_BrushFolder;
QString m_BrushPath;
QDesignerFormEditorInterface *m_Core;
QMap<QString, QString> m_FileToBrush;
QMap<QString, QString> m_BrushToFile;
};
BrushManagerProxyPrivate::BrushManagerProxyPrivate(BrushManagerProxy *bp, QDesignerFormEditorInterface *core) :
q_ptr(bp),
m_Manager(0),
m_BrushFolder(QLatin1String("brushes")),
m_Core(core)
{
m_designerFolder = QDir::homePath();
m_designerFolder += QDir::separator();
m_designerFolder += QLatin1String(".designer");
m_BrushPath = m_designerFolder;
m_BrushPath += QDir::separator();
m_BrushPath += m_BrushFolder;
}
} // namespace qdesigner_internal
using namespace qdesigner_internal;
void BrushManagerProxyPrivate::brushAdded(const QString &name, const QBrush &brush)
{
const QString filename = uniqueBrushFileName(name);
QDir designerDir(m_designerFolder);
if (!designerDir.exists(m_BrushFolder))
designerDir.mkdir(m_BrushFolder);
QFile file(m_BrushPath + QDir::separator() +filename);
if (file.open(QIODevice::WriteOnly)) {
QSimpleResource resource(m_Core);
DomBrush *dom = resource.saveBrush(brush);
QXmlStreamWriter writer(&file);
writer.setAutoFormatting(true);
writer.setAutoFormattingIndent(1);
writer.writeStartDocument();
writer.writeStartElement(QLatin1String("description"));
writer.writeAttribute(QLatin1String("name"), name);
dom->write(writer);
writer.writeEndElement();
writer.writeEndDocument();
delete dom;
file.close();
m_FileToBrush[filename] = name;
m_BrushToFile[name] = filename;
}
}
void BrushManagerProxyPrivate::brushRemoved(const QString &name)
{
QDir brushDir(m_BrushPath);
QString filename = m_BrushToFile[name];
brushDir.remove(filename);
m_BrushToFile.remove(name);
m_FileToBrush.remove(filename);
}
QString BrushManagerProxyPrivate::uniqueBrushFileName(const QString &brushName) const
{
const QString extension = QLatin1String(".br");
QString filename = brushName.toLower();
filename += extension;
int i = 0;
while (m_FileToBrush.contains(filename)) {
filename = brushName.toLower();
filename += QString::number(++i);
filename += extension;
}
return filename;
}
BrushManagerProxy::BrushManagerProxy(QDesignerFormEditorInterface *core, QObject *parent)
: QObject(parent), d_ptr(new BrushManagerProxyPrivate(this, core))
{
}
BrushManagerProxy::~BrushManagerProxy()
{
}
void BrushManagerProxy::setBrushManager(QtBrushManager *manager)
{
if (d_ptr->m_Manager == manager)
return;
if (d_ptr->m_Manager) {
disconnect(d_ptr->m_Manager, SIGNAL(brushAdded(const QString &, const QBrush &)),
this, SLOT(brushAdded(const QString &, const QBrush &)));
disconnect(d_ptr->m_Manager, SIGNAL(brushRemoved(const QString &)),
this, SLOT(brushRemoved(const QString &)));
}
d_ptr->m_Manager = manager;
if (!d_ptr->m_Manager)
return;
// clear the manager
QMap<QString, QBrush> brushes = d_ptr->m_Manager->brushes();
QMap<QString, QBrush>::ConstIterator it = brushes.constBegin();
while (it != brushes.constEnd()) {
QString name = it.key();
d_ptr->m_Manager->removeBrush(name);
it++;
}
// fill up the manager from compiled resources or from brush folder here
const QString nameAttribute = QLatin1String("name");
const QString brush = QLatin1String("brush");
const QString description = QLatin1String("description");
QDir brushDir(d_ptr->m_BrushPath);
bool customBrushesExist = brushDir.exists();
if (customBrushesExist) {
// load brushes from brush folder
QStringList nameFilters;
nameFilters.append(QLatin1String("*.br"));
QFileInfoList infos = brushDir.entryInfoList(nameFilters);
QListIterator<QFileInfo> it(infos);
while (it.hasNext()) {
const QFileInfo fi = it.next();
QString filename = fi.absoluteFilePath();
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
QXmlStreamReader reader(&file);
//<description name="black" >
// <brush brushstyle="SolidPattern" >
// <color alpha="255" .../>
// </brush>
//</description>
QString descname;
while (!reader.atEnd()) {
if (reader.readNext() == QXmlStreamReader::StartElement) {
const QString tag = reader.name().toString().toLower();
if (tag == description) {
if (!reader.attributes().hasAttribute(nameAttribute))
reader.raiseError(tr("The element '%1' is missing the required attribute '%2'.")
.arg(tag, nameAttribute));
else
descname = reader.attributes().value(nameAttribute).toString();
continue;
}
if (tag == brush) {
DomBrush brush;
brush.read(reader);
if (descname.isEmpty()) {
reader.raiseError(tr("Empty brush name encountered."));
} else {
QSimpleResource resource(d_ptr->m_Core);
QBrush br = resource.setupBrush(&brush);
d_ptr->m_Manager->addBrush(descname, br);
d_ptr->m_FileToBrush[filename] = descname;
d_ptr->m_BrushToFile[descname] = filename;
}
continue;
}
reader.raiseError(tr("An unexpected element '%1' was encountered.").arg(tag));
}
}
file.close();
if (reader.hasError()) {
qdesigner_internal::designerWarning(tr("An error occurred when reading the brush definition file '%1' at line line %2, column %3: %4")
.arg(fi.fileName())
.arg(reader.lineNumber())
.arg(reader.columnNumber())
.arg(reader.errorString()));
continue;
}
}
}
}
connect(d_ptr->m_Manager, SIGNAL(brushAdded(QString,QBrush)),
this, SLOT(brushAdded(QString, QBrush)));
connect(d_ptr->m_Manager, SIGNAL(brushRemoved(QString)),
this, SLOT(brushRemoved(QString)));
if (!customBrushesExist) {
// load brushes from resources
QFile qrcFile(QLatin1String(":trolltech/brushes/defaultbrushes.xml"));
if (!qrcFile.open(QIODevice::ReadOnly))
Q_ASSERT(0);
QXmlStreamReader reader(&qrcFile);
while (!reader.atEnd()) {
if (reader.readNext() == QXmlStreamReader::StartElement) {
if (reader.name().toString().toLower() == QLatin1String("description")) {
const QString name = reader.attributes().value(nameAttribute).toString();
do { // forward to <brush> element, which DomBrush expects
reader.readNext();
} while (!reader.atEnd() && reader.tokenType() != QXmlStreamReader::StartElement);
DomBrush brushDom;
brushDom.read(reader);
if (!reader.hasError()) {
QSimpleResource resource(d_ptr->m_Core);
QBrush br = resource.setupBrush(&brushDom);
d_ptr->m_Manager->addBrush(name, br);
}
}
}
}
if (reader.hasError()) {
// Should never happen
qdesigner_internal::designerWarning(tr("An error occurred when reading the resource file '%1' at line %2, column %3: %4")
.arg(qrcFile.fileName())
.arg(reader.lineNumber())
.arg(reader.columnNumber())
.arg(reader.errorString()));
}
qrcFile.close();
}
}
QT_END_NAMESPACE
#include "moc_brushmanagerproxy.cpp"
|