itemlibrarymodel.cpp 13.4 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 "itemlibraryinfo.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



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

188
189

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

194
195

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

200
201

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

206
void ItemLibraryItemModel::setItemIconPath(const QString &iconPath)
207
{
208
    m_iconPath = iconPath;
209

Kai Koehne's avatar
Kai Koehne committed
210
211
    setProperty(QLatin1String("itemLibraryIconPath"),
                QString(QLatin1String("image://qmldesigner_itemlibrary/") + iconPath));
212
}
213

214
215
216
217
void ItemLibraryItemModel::setItemIconSize(const QSize &itemIconSize)
{
    m_iconSize = itemIconSize;
//    qDebug() << "set icon size" << itemIconSize;
218
    setItemIconPath(m_iconPath);
219
220
}

221
222

bool ItemLibraryItemModel::operator<(const ItemLibraryItemModel &other) const
223
{
224
225
226
227
    return itemName() < other.itemName();
}


228
229


230
231
232
233
234
235
236
237
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"),
238
        scriptEngine->newVariant(QVariant::fromValue(static_cast<QDeclarativeListModel *>(&m_sectionEntries))));
239
}
240
241


242
243
244
245
246
QString ItemLibrarySectionModel::sectionName() const
{
    return m_name;
}

247

248
249
250
void ItemLibrarySectionModel::addSectionEntry(ItemLibraryItemModel *sectionEntry)
{
    m_sectionEntries.addElement(sectionEntry, sectionEntry->itemLibId());
251
252
}

253
254

void ItemLibrarySectionModel::removeSectionEntry(int itemLibId)
255
{
256
    m_sectionEntries.removeElement(itemLibId);
257
258
}

259

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

    *changed = false;

278
279
    QMap<int, ItemLibraryItemModel *>::const_iterator itemIt = m_sectionEntries.elements().constBegin();
    while (itemIt != m_sectionEntries.elements().constEnd()) {
280

281
282
283
284
285
        bool itemVisible = itemIt.value()->itemName().toLower().contains(searchText),
            itemChanged = false;
        itemChanged = m_sectionEntries.setElementVisible(itemIt.key(), itemVisible);

        *changed |= itemChanged;
286

287
288
289
290
291
        if (itemVisible)
            haveVisibleItems = true;

        ++itemIt;
    }
292

293
294
    return haveVisibleItems;
}
295
296


297
298
299
300
301
302
303
304
305
void ItemLibrarySectionModel::updateItemIconSize(const QSize &itemIconSize)
{
    foreach (ItemLibraryItemModel *item, m_sectionEntries.elements().values()) {
        item->setItemIconSize(itemIconSize);
    }
}

bool ItemLibrarySectionModel::operator<(const ItemLibrarySectionModel &other) const
{
306
307
    if (sectionName() == QLatin1String("QML Components")) //Qml Components always come first
        return true;
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
    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);
}


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

381
void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo)
382
383
384
385
386
{
    QMap<QString, int> sections;

    clearElements();
    m_itemInfos.clear();
387
388
    m_sections.clear();
    m_nextLibId = 0;
389

390
    foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
391
392
393
394
395
396
397
398
399
400
401
402
403
        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);
404
        }
405
406
407
408
409
410

        m_itemInfos.insert(itemId, entry);

        itemModel = new ItemLibraryItemModel(m_scriptEngine.data(), itemId, entry.name());

        // delayed creation of (default) icons
411
412
        if (entry.iconPath().isEmpty())
            entry.setIconPath(QLatin1String(":/ItemLibrary/images/item-default-icon.png"));
413
414
415
        if (entry.dragIcon().isNull())
            entry.setDragIcon(createDragPixmap(getWidth(entry), getHeight(entry)));

416
        itemModel->setItemIconPath(entry.iconPath());
417
418
419
        itemModel->setItemIconSize(m_itemIconSize);
        sectionModel->addSectionEntry(itemModel);
        m_sections.insert(itemId, sectionId);
420
    }
421
422
423
424
425
426
427
428

    updateVisibility();
}


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

431
432

QMimeData *ItemLibraryModel::getMimeData(int libId)
433
{
434
435
436
437
438
439
440
441
442
443
444
445
446
    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());
    }
447

448
    mimeData->removeFormat(QLatin1String("text/plain"));
449

450
    return mimeData;
451
452
}

453
454

QIcon ItemLibraryModel::getIcon(int libId)
455
{
456
457
458
    return m_itemInfos.value(libId).icon();
}

459

460
461
void ItemLibraryModel::updateVisibility()
{
462
463
    bool changed = false;

464
465
    QMap<int, ItemLibrarySectionModel *>::const_iterator sectionIt = elements().constBegin();
    while (sectionIt != elements().constEnd()) {
466

467
468
        ItemLibrarySectionModel *sectionModel = sectionIt.value();
        QString sectionSearchText = m_searchText;
469

470
471
        if (sectionModel->sectionName().toLower().contains(m_searchText))
            sectionSearchText = "";
472

473
474
475
476
477
478
479
480
        bool sectionChanged = false,
            sectionVisibility = sectionModel->updateSectionVisibility(sectionSearchText,
                                                                      &sectionChanged);
        if (sectionChanged) {
            changed = true;
            if (sectionVisibility)
                emit sectionVisibilityChanged(sectionIt.key());
        }
481

482
        changed |= setElementVisible(sectionIt.key(), sectionVisibility);
483
484
485
        ++sectionIt;
    }

486
487
    if (changed)
        emit visibilityChanged();
488
489
}

490
int ItemLibraryModel::getWidth(const ItemLibraryEntry &itemLibraryEntry)
491
{
492
    foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
493
494
495
496
497
498
499
    {
        if (property.name() == QLatin1String("width"))
            return property.value().toInt();
    }
    return 64;
}

500
int ItemLibraryModel::getHeight(const ItemLibraryEntry &itemLibraryEntry)
501
{
502
    foreach (const ItemLibraryEntry::Property &property, itemLibraryEntry.properties())
503
504
505
506
507
508
509
    {
        if (property.name() == QLatin1String("height"))
            return property.value().toInt();
    }
    return 64;
}

510
QPixmap ItemLibraryModel::createDragPixmap(int width, int height)
511
512
513
514
515
516
517
518
519
520
{
    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);
}
521

522
523
} // namespace Internal
} // namespace QmlDesigner
524