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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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.
**
**************************************************************************/
#include "itemlibrarymodel.h"
#include "itemlibraryinfo.h"
#include <model.h>
#include <nodemetainfo.h>
#include <QVariant>
#include <QMimeData>
#include <QPainter>
#include <QPen>
#include <qdebug.h>
namespace QmlDesigner {
namespace Internal {
template <class T>
ItemLibrarySortedModel<T>::ItemLibrarySortedModel(QObject *parent) :
QDeclarativeListModel(parent)
{
}
template <class T>
ItemLibrarySortedModel<T>::~ItemLibrarySortedModel()
{
clearElements();
}
template <class T>
void ItemLibrarySortedModel<T>::clearElements()
{
while (m_elementOrder.count() > 0)
removeElement(m_elementOrder.at(0).libId);
}
template <class T>
void ItemLibrarySortedModel<T>::addElement(T *element, int libId)
{
struct order_struct orderEntry;
orderEntry.libId = libId;
orderEntry.visible = false;
int pos = 0;
while ((pos < m_elementOrder.count()) &&
(*(m_elementModels.value(m_elementOrder.at(pos).libId)) < *element))
++pos;
m_elementModels.insert(libId, element);
m_elementOrder.insert(pos, orderEntry);
setElementVisible(libId, true);
}
template <class T>
void ItemLibrarySortedModel<T>::removeElement(int libId)
{
T *element = m_elementModels.value(libId);
int pos = findElement(libId);
setElementVisible(libId, false);
m_elementModels.remove(libId);
m_elementOrder.removeAt(pos);
delete element;
}
template <class T>
bool ItemLibrarySortedModel<T>::elementVisible(int libId) const
{
int pos = findElement(libId);
return m_elementOrder.at(pos).visible;
}
template <class T>
bool ItemLibrarySortedModel<T>::setElementVisible(int libId, bool visible)
{
int pos = findElement(libId);
if (m_elementOrder.at(pos).visible == visible)
return false;
int visiblePos = visibleElementPosition(libId);
if (visible)
insert(visiblePos, *(m_elementModels.value(libId)));
else
remove(visiblePos);
m_elementOrder[pos].visible = visible;
return true;
}
template <class T>
const QMap<int, T *> &ItemLibrarySortedModel<T>::elements() const
{
return m_elementModels;
}
template <class T>
T *ItemLibrarySortedModel<T>::elementModel(int libId)
{
return m_elementModels.value(libId);
}
template <class T>
int ItemLibrarySortedModel<T>::findElement(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_elementOrder);
while (it.hasNext()) {
if (it.next().libId == libId)
return i;
++i;
}
return -1;
}
template <class T>
int ItemLibrarySortedModel<T>::visibleElementPosition(int libId) const
{
int i = 0;
QListIterator<struct order_struct> it(m_elementOrder);
while (it.hasNext()) {
struct order_struct order = it.next();
if (order.libId == libId)
return i;
if (order.visible)
++i;
}
return -1;
}
ItemLibraryItemModel::ItemLibraryItemModel(QScriptEngine *scriptEngine, int itemLibId, const QString &itemName)
: QScriptValue(scriptEngine->newObject()),
m_scriptEngine(scriptEngine),
m_libId(itemLibId),
m_name(itemName),
m_iconSize(64, 64)
{
QScriptValue pixmapScriptValue(m_scriptEngine->newVariant(QPixmap()));
setProperty(QLatin1String("itemLibId"), itemLibId);
setProperty(QLatin1String("itemName"), itemName);
setProperty(QLatin1String("itemPixmap"), pixmapScriptValue);
}
ItemLibraryItemModel::~ItemLibraryItemModel()
{
setProperty(QLatin1String("itemPixmap"), QVariant::Invalid);
}
int ItemLibraryItemModel::itemLibId() const
{
return m_libId;
}
QString ItemLibraryItemModel::itemName() const
{
return m_name;
}
void ItemLibraryItemModel::setItemIconPath(const QString &iconPath)
{
m_iconPath = iconPath;
setProperty(QLatin1String("itemLibraryIconPath"),
QString(QLatin1String("image://qmldesigner_itemlibrary/") + iconPath));
}
void ItemLibraryItemModel::setItemIconSize(const QSize &itemIconSize)
{
m_iconSize = itemIconSize;
// qDebug() << "set icon size" << itemIconSize;
setItemIconPath(m_iconPath);
}
bool ItemLibraryItemModel::operator<(const ItemLibraryItemModel &other) const
{
return itemName() < other.itemName();
}
ItemLibrarySectionModel::ItemLibrarySectionModel(QScriptEngine *scriptEngine, int sectionLibId, const QString §ionName, QObject *parent)
: QScriptValue(scriptEngine->newObject()),
m_name(sectionName),
m_sectionEntries(parent)
{
QScriptValue::setProperty(QLatin1String("sectionLibId"), sectionLibId);
QScriptValue::setProperty(QLatin1String("sectionName"), sectionName);
QScriptValue::setProperty(QLatin1String("sectionEntries"),
scriptEngine->newVariant(QVariant::fromValue(static_cast<QDeclarativeListModel *>(&m_sectionEntries))));
}
QString ItemLibrarySectionModel::sectionName() const
{
return m_name;
}
void ItemLibrarySectionModel::addSectionEntry(ItemLibraryItemModel *sectionEntry)
{
m_sectionEntries.addElement(sectionEntry, sectionEntry->itemLibId());
}
void ItemLibrarySectionModel::removeSectionEntry(int itemLibId)
{
m_sectionEntries.removeElement(itemLibId);
}
int ItemLibrarySectionModel::visibleItemIndex(int itemLibId)
{
return m_sectionEntries.visibleElementPosition(itemLibId);
}
bool ItemLibrarySectionModel::isItemVisible(int itemLibId)
{
return m_sectionEntries.elementVisible(itemLibId);
}
bool ItemLibrarySectionModel::updateSectionVisibility(const QString &searchText, bool *changed)
{
bool haveVisibleItems = false;
*changed = false;
QMap<int, ItemLibraryItemModel *>::const_iterator itemIt = m_sectionEntries.elements().constBegin();
while (itemIt != m_sectionEntries.elements().constEnd()) {
bool itemVisible = itemIt.value()->itemName().toLower().contains(searchText),
itemChanged = false;
itemChanged = m_sectionEntries.setElementVisible(itemIt.key(), itemVisible);
*changed |= itemChanged;
if (itemVisible)
haveVisibleItems = true;
++itemIt;
}
return haveVisibleItems;
}
void ItemLibrarySectionModel::updateItemIconSize(const QSize &itemIconSize)
{
foreach (ItemLibraryItemModel *item, m_sectionEntries.elements().values()) {
item->setItemIconSize(itemIconSize);
}
}
bool ItemLibrarySectionModel::operator<(const ItemLibrarySectionModel &other) const
{
if (sectionName() == QLatin1String("QML Components")) //Qml Components always come first
return true;
return sectionName() < other.sectionName();
}
ItemLibraryModel::ItemLibraryModel(QScriptEngine *scriptEngine, QObject *parent)
: ItemLibrarySortedModel<ItemLibrarySectionModel>(parent),
m_scriptEngine(scriptEngine),
m_searchText(""),
m_itemIconSize(64, 64),
m_nextLibId(0)
{
}
ItemLibraryModel::~ItemLibraryModel()
{
}
QString ItemLibraryModel::searchText() const
{
return m_searchText;
}
void ItemLibraryModel::setSearchText(const QString &searchText)
{
QString lowerSearchText = searchText.toLower();
if (m_searchText != lowerSearchText) {
m_searchText = lowerSearchText;
emit searchTextChanged();
updateVisibility();
}
}
void ItemLibraryModel::setItemIconSize(const QSize &itemIconSize)
{
m_itemIconSize = itemIconSize;
foreach (ItemLibrarySectionModel *section, elements().values())
section->updateItemIconSize(itemIconSize);
}
int ItemLibraryModel::getItemSectionIndex(int itemLibId)
{
if (m_sections.contains(itemLibId))
return elementModel(m_sections.value(itemLibId))->visibleItemIndex(itemLibId);
else
return -1;
}
int ItemLibraryModel::getSectionLibId(int itemLibId)
{
return m_sections.value(itemLibId);
}
bool ItemLibraryModel::isItemVisible(int itemLibId)
{
if (!m_sections.contains(itemLibId))
return false;
int sectionLibId = m_sections.value(itemLibId);
if (!elementVisible(sectionLibId))
return false;
return elementModel(sectionLibId)->isItemVisible(itemLibId);
}
QString entryToImport(const ItemLibraryEntry &entry)
{
return entry.requiredImport() + QLatin1Char(' ') + QString::number(entry.majorVersion())
+ QLatin1Char('.') + QString::number(entry.minorVersion());
}
void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
{
QMap<QString, int> sections;
clearElements();
m_itemInfos.clear();
m_sections.clear();
m_nextLibId = 0;
QStringList imports;
foreach (const Import &import, model->imports())
if (import.isLibraryImport())
imports << import.url() + QLatin1Char(' ') + import.version();
foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
bool valid = model->metaInfo(entry.typeName(), entry.majorVersion(), entry.minorVersion()).isValid();
if (valid && (entry.requiredImport().isEmpty() || imports.contains(entryToImport(entry), Qt::CaseInsensitive))) {
QString itemSectionName = entry.category();
ItemLibrarySectionModel *sectionModel;
ItemLibraryItemModel *itemModel;
int itemId = m_nextLibId++, sectionId;
if (sections.contains(itemSectionName)) {
sectionId = sections.value(itemSectionName);
sectionModel = elementModel(sectionId);
} else {
sectionId = m_nextLibId++;
sectionModel = new ItemLibrarySectionModel(m_scriptEngine.data(), sectionId, itemSectionName, this);
addElement(sectionModel, sectionId);
sections.insert(itemSectionName, sectionId);
}
m_itemInfos.insert(itemId, entry);
itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, entry.name());
// delayed creation of (default) icons
if (entry.iconPath().isEmpty())
entry.setIconPath(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
if (entry.dragIcon().isNull())
entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));
itemModel->setItemIconPath(entry.iconPath());
itemModel->setItemIconSize(m_itemIconSize);
sectionModel->addSectionEntry(itemModel);
m_sections.insert(itemId, sectionId);
}
}
updateVisibility();
}
QString ItemLibraryModel::getTypeName(int libId)
{
return m_itemInfos.value(libId).typeName();
}
QMimeData *ItemLibraryModel::getMimeData(int libId)
{
QMimeData *mimeData = new QMimeData();
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << m_itemInfos.value(libId);
mimeData->setData(QLatin1String("application/vnd.bauhaus.itemlibraryinfo"), data);
const QIcon icon = m_itemInfos.value(libId).dragIcon();
if (!icon.isNull()) {
const QList<QSize> sizes = icon.availableSizes();
if (!sizes.isEmpty())
mimeData->setImageData(icon.pixmap(sizes.front()).toImage());
}
mimeData->removeFormat(QLatin1String("text/plain"));
return mimeData;
}
QIcon ItemLibraryModel::getIcon(int libId)
{
return m_itemInfos.value(libId).icon();
}
void ItemLibraryModel::updateVisibility()
{
bool changed = false;
QMap<int, ItemLibrarySectionModel *>::const_iterator sectionIt = elements().constBegin();
while (sectionIt != elements().constEnd()) {
ItemLibrarySectionModel *sectionModel = sectionIt.value();
QString sectionSearchText = m_searchText;
if (sectionModel->sectionName().toLower().contains(m_searchText))
sectionSearchText = "";
bool sectionChanged = false,
sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
§ionChanged);
if (sectionChanged) {
changed = true;
if (sectionVisibility)
emit sectionVisibilityChanged(sectionIt.key());
}
changed |= setElementVisible(sectionIt.key(), sectionVisibility);
++sectionIt;
}
if (changed)
emit visibilityChanged();
}
int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
{
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
{
if (property.name() == QLatin1String("width"))
return property.value().toInt();
}
return 64;
}
int ItemLibraryModel::getHeight(const ItemLibraryEntry &itemLibraryEntry)
{
foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
{
if (property.name() == QLatin1String("height"))
return property.value().toInt();
}
return 64;
}
QPixmap ItemLibraryModel::createDragPixmap(int width, int height)
{
QImage dragImage(width, height, QImage::Format_RGB32); // TODO: draw item drag icon
dragImage.fill(0xffffffff);
QPainter p(&dragImage);
QPen pen(Qt::gray);
pen.setWidth(2);
p.setPen(pen);
p.drawRect(1, 1, dragImage.width() - 2, dragImage.height() - 2);
return QPixmap::fromImage(dragImage);
}
} // namespace Internal
} // namespace QmlDesigner
|