OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / designercore / metainfo / itemlibraryinfo.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 "itemlibraryinfo.h"
35 #include "nodemetainfo.h"
36
37 #include <QSharedData>
38
39 namespace QmlDesigner {
40
41 namespace Internal {
42
43 class ItemLibraryEntryData : public QSharedData
44 {
45 public:
46     ItemLibraryEntryData() : majorVersion(-1), minorVersion(-1)
47     { }
48     QString name;
49     QString typeName;
50     QString category;
51     int majorVersion;
52     int minorVersion;
53     QString iconPath;
54     QIcon icon;
55     QIcon dragIcon;
56     QList<PropertyContainer> properties;
57     QString qml;
58     QString requiredImport;
59 };
60
61 class ItemLibraryInfoPrivate
62 {
63 public:
64     QHash<QString, ItemLibraryEntry> nameToEntryHash;
65
66     QWeakPointer<ItemLibraryInfo> baseInfo;
67 };
68
69 } // namespace Internal
70
71 //
72 // ItemLibraryEntry
73 //
74
75 ItemLibraryEntry::ItemLibraryEntry(const ItemLibraryEntry &other)
76     : m_data(other.m_data)
77 {
78 }
79
80 ItemLibraryEntry& ItemLibraryEntry::operator=(const ItemLibraryEntry &other)
81 {
82     if (this !=&other)
83         m_data = other.m_data;
84
85     return *this;
86 }
87
88 void ItemLibraryEntry::setDragIcon(const QIcon &icon)
89 {
90     m_data->dragIcon = icon;
91 }
92
93 void ItemLibraryEntry::setIcon(const QIcon &icon)
94 {
95     m_data->icon = icon;
96 }
97
98 QIcon ItemLibraryEntry::dragIcon() const
99 {
100     return m_data->dragIcon;
101 }
102
103 void ItemLibraryEntry::addProperty(const Property &property)
104 {
105     m_data->properties.append(property);
106 }
107
108 QList<ItemLibraryEntry::Property> ItemLibraryEntry::properties() const
109 {
110     return m_data->properties;
111 }
112
113 ItemLibraryEntry::ItemLibraryEntry() : m_data(new Internal::ItemLibraryEntryData)
114 {
115     m_data->name.clear();
116 }
117
118 ItemLibraryEntry::~ItemLibraryEntry()
119 {
120 }
121
122 QString ItemLibraryEntry::name() const
123 {
124     return m_data->name;
125 }
126
127 QString ItemLibraryEntry::typeName() const
128 {
129     return m_data->typeName;
130 }
131
132 QString ItemLibraryEntry::qml() const
133 {
134     return m_data->qml;
135 }
136
137 QString ItemLibraryEntry::requiredImport() const
138 {
139     return m_data->requiredImport;
140 }
141
142 int ItemLibraryEntry::majorVersion() const
143 {
144     return m_data->majorVersion;
145 }
146
147 int ItemLibraryEntry::minorVersion() const
148 {
149     return m_data->minorVersion;
150 }
151
152 QString ItemLibraryEntry::category() const
153 {
154     return m_data->category;
155 }
156
157 void ItemLibraryEntry::setCategory(const QString &category)
158 {
159     m_data->category = category;
160 }
161
162 QIcon ItemLibraryEntry::icon() const
163 {
164     return m_data->icon;
165 }
166
167 QString ItemLibraryEntry::iconPath() const
168 {
169     return m_data->iconPath;
170 }
171
172 void ItemLibraryEntry::setName(const QString &name)
173 {
174      m_data->name = name;
175 }
176
177 void ItemLibraryEntry::setType(const QString &typeName, int majorVersion, int minorVersion)
178 {
179     m_data->typeName = typeName;
180     m_data->majorVersion = majorVersion;
181     m_data->minorVersion = minorVersion;
182 }
183
184 void ItemLibraryEntry::setIconPath(const QString &iconPath)
185 {
186     m_data->iconPath = iconPath;
187 }
188
189 void ItemLibraryEntry::setQml(const QString &qml)
190 {
191     m_data->qml = qml;
192 }
193
194 void ItemLibraryEntry::setRequiredImport(const QString &requiredImport)
195 {
196     m_data->requiredImport = requiredImport;
197 }
198
199 void ItemLibraryEntry::addProperty(QString &name, QString &type, QString &value)
200 {
201     Property property;
202     property.set(name, type, value);
203     addProperty(property);
204 }
205
206 QDataStream& operator<<(QDataStream& stream, const ItemLibraryEntry &itemLibraryEntry)
207 {
208     stream << itemLibraryEntry.name();
209     stream << itemLibraryEntry.typeName();
210     stream << itemLibraryEntry.majorVersion();
211     stream << itemLibraryEntry.minorVersion();
212     stream << itemLibraryEntry.icon();
213     stream << itemLibraryEntry.iconPath();
214     stream << itemLibraryEntry.category();
215     stream << itemLibraryEntry.dragIcon();
216     stream << itemLibraryEntry.requiredImport();
217
218     stream << itemLibraryEntry.m_data->properties;
219
220     return stream;
221 }
222
223 QDataStream& operator>>(QDataStream& stream, ItemLibraryEntry &itemLibraryEntry)
224 {
225     stream >> itemLibraryEntry.m_data->name;
226     stream >> itemLibraryEntry.m_data->typeName;
227     stream >> itemLibraryEntry.m_data->majorVersion;
228     stream >> itemLibraryEntry.m_data->minorVersion;
229     stream >> itemLibraryEntry.m_data->icon;
230     stream >> itemLibraryEntry.m_data->iconPath;
231     stream >> itemLibraryEntry.m_data->category;
232     stream >> itemLibraryEntry.m_data->dragIcon;
233     stream >> itemLibraryEntry.m_data->requiredImport;
234
235     stream >> itemLibraryEntry.m_data->properties;
236
237     return stream;
238 }
239
240 //
241 // ItemLibraryInfo
242 //
243
244 ItemLibraryInfo::ItemLibraryInfo(QObject *parent) :
245         QObject(parent),
246         m_d(new Internal::ItemLibraryInfoPrivate())
247 {
248 }
249
250 ItemLibraryInfo::~ItemLibraryInfo()
251 {
252 }
253
254 QList<ItemLibraryEntry> ItemLibraryInfo::entriesForType(const QString &typeName, int majorVersion, int minorVersion) const
255 {
256     QList<ItemLibraryEntry> entries;
257
258     foreach (const ItemLibraryEntry &entry, m_d->nameToEntryHash.values()) {
259         if (entry.typeName() == typeName
260             && entry.majorVersion() >= majorVersion
261             && entry.minorVersion() >= minorVersion)
262             entries += entry;
263     }
264
265     if (m_d->baseInfo)
266         entries += m_d->baseInfo->entriesForType(typeName, majorVersion, minorVersion);
267
268     return entries;
269 }
270
271 ItemLibraryEntry ItemLibraryInfo::entry(const QString &name) const
272 {
273     if (m_d->nameToEntryHash.contains(name))
274         return m_d->nameToEntryHash.value(name);
275
276     if (m_d->baseInfo)
277         return m_d->baseInfo->entry(name);
278
279     return ItemLibraryEntry();
280 }
281
282 QList<ItemLibraryEntry> ItemLibraryInfo::entries() const
283 {
284     QList<ItemLibraryEntry> list = m_d->nameToEntryHash.values();
285     if (m_d->baseInfo)
286         list += m_d->baseInfo->entries();
287     return list;
288 }
289
290 static inline QString keyForEntry(const ItemLibraryEntry &entry)
291 {
292     return entry.name() + entry.category();
293 }
294
295 void ItemLibraryInfo::addEntry(const ItemLibraryEntry &entry)
296 {
297     const QString key = keyForEntry(entry);
298     if (m_d->nameToEntryHash.contains(key))
299         throw InvalidMetaInfoException(__LINE__, __FUNCTION__, __FILE__);
300     m_d->nameToEntryHash.insert(key, entry);
301
302     emit entriesChanged();
303 }
304
305 bool ItemLibraryInfo::containsEntry(const ItemLibraryEntry &entry)
306 {
307     const QString key = keyForEntry(entry);
308     return m_d->nameToEntryHash.contains(key);
309 }
310
311 void ItemLibraryInfo::clearEntries()
312 {
313     m_d->nameToEntryHash.clear();
314     emit entriesChanged();
315 }
316
317 void ItemLibraryInfo::setBaseInfo(ItemLibraryInfo *baseInfo)
318 {
319     m_d->baseInfo = baseInfo;
320 }
321
322 } // namespace QmlDesigner