itemlibrarymodel.cpp 14.7 KB
Newer Older
1
2
3
4
/**************************************************************************
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/

30
#include "itemlibrarymodel.h"
31
#include "metainfo.h"
32

33
34
#include <QVariant>
#include <QMimeData>
35
#include <QPainter>
36
37
#include <QPen>
#include <qdebug.h>
38
39


40
41
42
namespace QmlDesigner {

namespace Internal {
43
44


45
46
template <class T>
ItemLibrarySortedModel<T>::ItemLibrarySortedModel(QObject *parent) :
47
    QDeclarativeListModel(parent)
48
49
50
{
}

51
52
53

template <class T>
ItemLibrarySortedModel<T>::~ItemLibrarySortedModel()
54
{
55
    clearElements();
56
57
58
}


59
60
61
62
63
64
template <class T>
void ItemLibrarySortedModel<T>::clearElements()
{
    while (m_elementOrder.count() > 0)
        removeElement(m_elementOrder.at(0).libId);
}
65
66


67
68
template <class T>
void ItemLibrarySortedModel<T>::addElement(T *element, int libId)
69
{
70
71
72
73
74
75
76
77
78
79
80
81
82
    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);
83
84
}

85
86
template <class T>
void ItemLibrarySortedModel<T>::removeElement(int libId)
87
{
88
89
90
91
92
93
94
95
96
    T *element = m_elementModels.value(libId);
    int pos = findElement(libId);

    setElementVisible(libId, false);

    m_elementModels.remove(libId);
    m_elementOrder.removeAt(pos);

    delete element;
97
98
}

99
100
101

template <class T>
bool ItemLibrarySortedModel<T>::elementVisible(int libId) const
102
{
103
104
    int pos = findElement(libId);
    return m_elementOrder.at(pos).visible;
105
106
107
}


108
template <class T>
109
bool ItemLibrarySortedModel<T>::setElementVisible(int libId, bool visible)
110
{
111
    int pos = findElement(libId);
112
    if (m_elementOrder.at(pos).visible == visible)
113
        return false;
114

115
    int visiblePos = visibleElementPosition(libId);
116
    if (visible)
117
        insert(visiblePos, *(m_elementModels.value(libId)));
118
    else
119
        remove(visiblePos);
120
121

    m_elementOrder[pos].visible = visible;
122
    return true;
123
124
125
}


126
127
template <class T>
const QMap<int, T *> &ItemLibrarySortedModel<T>::elements() const
128
{
129
130
131
132
133
134
135
136
    return m_elementModels;
}


template <class T>
T *ItemLibrarySortedModel<T>::elementModel(int libId)
{
    return m_elementModels.value(libId);
137
138
}

139
140
141

template <class T>
int ItemLibrarySortedModel<T>::findElement(int libId) const
142
{
143
144
145
146
147
148
149
    int i = 0;
    QListIterator<struct order_struct> it(m_elementOrder);

    while (it.hasNext()) {
        if (it.next().libId == libId)
            return i;
        ++i;
150
    }
151
152
153
154

    return -1;
}

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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;
}
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186



ItemLibraryItemModel::ItemLibraryItemModel(QScriptEngine *scriptEngine, int itemLibId, const QString &itemName)
    : QScriptValue(scriptEngine->newObject()),
      m_scriptEngine(scriptEngine),
      m_libId(itemLibId),
      m_name(itemName),
      m_icon(),
      m_iconSize(64, 64)
{
    QScriptValue pixmapScriptValue(m_scriptEngine->newVariant(QPixmap()));

    setProperty(QLatin1String("itemLibId"), itemLibId);
    setProperty(QLatin1String("itemName"), itemName);
    setProperty(QLatin1String("itemPixmap"), pixmapScriptValue);
187
188
}

189
190

ItemLibraryItemModel::~ItemLibraryItemModel()
191
{
192
    setProperty(QLatin1String("itemPixmap"), QVariant::Invalid);
193
194
}

195
196

int ItemLibraryItemModel::itemLibId() const
197
{
198
    return m_libId;
199
200
}

201
202

QString ItemLibraryItemModel::itemName() const
203
{
204
    return m_name;
205
206
}

207
208

void ItemLibraryItemModel::setItemIcon(const QIcon &itemIcon)
209
{
210
211
212
213
214
    m_icon = itemIcon;

    QScriptValue pixmapScriptValue(m_scriptEngine->newVariant(m_icon.pixmap(m_iconSize)));
    setProperty(QLatin1String("itemPixmap"), pixmapScriptValue);
}
215
216


217
218
219
220
221
void ItemLibraryItemModel::setItemIconSize(const QSize &itemIconSize)
{
    m_iconSize = itemIconSize;
//    qDebug() << "set icon size" << itemIconSize;
    setItemIcon(m_icon);
222
223
}

224
225

bool ItemLibraryItemModel::operator<(const ItemLibraryItemModel &other) const
226
{
227
228
229
230
    return itemName() < other.itemName();
}


231
232


233
234
235
236
237
238
239
240
ItemLibrarySectionModel::ItemLibrarySectionModel(QScriptEngine *scriptEngine, int sectionLibId, const QString &sectionName, 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"),
241
        scriptEngine->newVariant(QVariant::fromValue(static_cast<QDeclarativeListModel *>(&m_sectionEntries))));
242
}
243
244


245
246
247
248
249
QString ItemLibrarySectionModel::sectionName() const
{
    return m_name;
}

250

251
252
253
void ItemLibrarySectionModel::addSectionEntry(ItemLibraryItemModel *sectionEntry)
{
    m_sectionEntries.addElement(sectionEntry, sectionEntry->itemLibId());
254
255
}

256
257

void ItemLibrarySectionModel::removeSectionEntry(int itemLibId)
258
{
259
    m_sectionEntries.removeElement(itemLibId);
260
261
}

262

263
264
265
266
267
268
269
270
271
272
273
274
275
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)
276
{
277
    bool haveVisibleItems = false;
278
279
280

    *changed = false;

281
282
    QMap<int, ItemLibraryItemModel *>::const_iterator itemIt = m_sectionEntries.elements().constBegin();
    while (itemIt != m_sectionEntries.elements().constEnd()) {
283

284
285
286
287
288
        bool itemVisible = itemIt.value()->itemName().toLower().contains(searchText),
            itemChanged = false;
        itemChanged = m_sectionEntries.setElementVisible(itemIt.key(), itemVisible);

        *changed |= itemChanged;
289

290
291
292
293
294
        if (itemVisible)
            haveVisibleItems = true;

        ++itemIt;
    }
295

296
297
    return haveVisibleItems;
}
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
void ItemLibrarySectionModel::updateItemIconSize(const QSize &itemIconSize)
{
    foreach (ItemLibraryItemModel *item, m_sectionEntries.elements().values()) {
        item->setItemIconSize(itemIconSize);
    }
}


bool ItemLibrarySectionModel::operator<(const ItemLibrarySectionModel &other) const
{
    return sectionName() < other.sectionName();
}





ItemLibraryModel::ItemLibraryModel(QScriptEngine *scriptEngine, QObject *parent)
    : ItemLibrarySortedModel<ItemLibrarySectionModel>(parent),
      m_scriptEngine(scriptEngine),
      m_metaInfo(0),
      m_searchText(""),
      m_itemIconSize(64, 64),
      m_nextLibId(0)
{
}


ItemLibraryModel::~ItemLibraryModel()
{
    if (m_metaInfo)
        delete m_metaInfo;
}


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


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


391
392
393
394
395
396
void ItemLibraryModel::update(const MetaInfo &metaInfo)
{
    QMap<QString, int> sections;

    clearElements();
    m_itemInfos.clear();
397
398
    m_sections.clear();
    m_nextLibId = 0;
399
400
401

    if (!m_metaInfo) {
        m_metaInfo = new MetaInfo(metaInfo);
402
    } else {
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
        *m_metaInfo = metaInfo;
    }

    foreach (const QString &type, metaInfo.itemLibraryItems()) {
        foreach (const ItemLibraryInfo &itemLibraryRepresentation, itemLibraryRepresentations(type)) {

            QString itemSectionName = itemLibraryRepresentation.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, itemLibraryRepresentation);

            itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, itemLibraryRepresentation.name());
            itemModel->setItemIcon(itemLibraryRepresentation.icon());
            itemModel->setItemIconSize(m_itemIconSize);
            sectionModel->addSectionEntry(itemModel);
430
            m_sections.insert(itemId, sectionId);
431
432
        }
    }
433
434
435
436
437
438
439
440

    updateVisibility();
}


QString ItemLibraryModel::getTypeName(int libId)
{
    return m_itemInfos.value(libId).typeName();
441
442
}

443
444

QMimeData *ItemLibraryModel::getMimeData(int libId)
445
{
446
447
448
449
450
451
452
453
454
455
456
457
458
    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());
    }
459

460
    mimeData->removeFormat(QLatin1String("text/plain"));
461

462
    return mimeData;
463
464
}

465
466

QIcon ItemLibraryModel::getIcon(int libId)
467
{
468
469
470
    return m_itemInfos.value(libId).icon();
}

471

472
473
void ItemLibraryModel::updateVisibility()
{
474
475
    bool changed = false;

476
477
    QMap<int, ItemLibrarySectionModel *>::const_iterator sectionIt = elements().constBegin();
    while (sectionIt != elements().constEnd()) {
478

479
480
        ItemLibrarySectionModel *sectionModel = sectionIt.value();
        QString sectionSearchText = m_searchText;
481

482
483
        if (sectionModel->sectionName().toLower().contains(m_searchText))
            sectionSearchText = "";
484

485
486
487
488
489
490
491
492
        bool sectionChanged = false,
            sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
                                                                      &sectionChanged);
        if (sectionChanged) {
            changed = true;
            if (sectionVisibility)
                emit sectionVisibilityChanged(sectionIt.key());
        }
493

494
        changed |= setElementVisible(sectionIt.key(), sectionVisibility);
495
496
497
        ++sectionIt;
    }

498
499
    if (changed)
        emit visibilityChanged();
500
501
502
503
504
}


QList<ItemLibraryInfo> ItemLibraryModel::itemLibraryRepresentations(const QString &type)
{
505
    QList<ItemLibraryInfo> itemLibraryRepresentationList;
506
507
    NodeMetaInfo nodeInfo = m_metaInfo->nodeMetaInfo(type);

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
547
    if (nodeInfo.isQmlGraphicsItem()) {
        itemLibraryRepresentationList = m_metaInfo->itemLibraryRepresentations(nodeInfo);

        QImage dragImage(64, 64, 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);
        QPixmap dragPixmap(QPixmap::fromImage(dragImage));

        if (!m_metaInfo->hasNodeMetaInfo(type))
            qWarning() << "ItemLibrary: type not declared: " << type;

        static QIcon defaultIcon(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));

        if (itemLibraryRepresentationList.isEmpty() || !m_metaInfo->hasNodeMetaInfo(type)) {
            QIcon icon = nodeInfo.icon();
            if (icon.isNull())
                icon = defaultIcon;

            ItemLibraryInfo itemLibraryInfo;
            itemLibraryInfo.setName(type);
            itemLibraryInfo.setTypeName(nodeInfo.typeName());
            itemLibraryInfo.setCategory(nodeInfo.category());
            itemLibraryInfo.setIcon(icon);
            itemLibraryInfo.setDragIcon(dragPixmap);
            itemLibraryInfo.setMajorVersion(nodeInfo.majorVersion());
            itemLibraryInfo.setMinorVersion(nodeInfo.minorVersion());
            itemLibraryRepresentationList.append(itemLibraryInfo);
        }
        else {
            foreach (ItemLibraryInfo itemLibraryRepresentation, itemLibraryRepresentationList) {
                QIcon icon = itemLibraryRepresentation.icon();
                if (itemLibraryRepresentation.icon().isNull())
                    itemLibraryRepresentation.setIcon(defaultIcon);

                if (itemLibraryRepresentation.dragIcon().isNull())
                    itemLibraryRepresentation.setDragIcon(dragPixmap);
548

549
550
551
                if (itemLibraryRepresentation.category().isEmpty())
                    itemLibraryRepresentation.setCategory(nodeInfo.category());
            }
552
553
        }
    }
554
555

    return itemLibraryRepresentationList;
556
557
558
559
}

} // namespace Internal
} // namespace QmlDesigner
560