itemlibrarymodel.cpp 7.53 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
12 13 14
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16 17
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
18 19 20 21 22
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
con's avatar
con committed
23
**
hjk's avatar
hjk committed
24
****************************************************************************/
25

26
#include "itemlibrarymodel.h"
27
#include "itemlibraryinfo.h"
28
#include "itemlibrarysection.h"
29
#include "itemlibraryitem.h"
30
#include "itemlibrarysection.h"
31

32 33
#include <model.h>
#include <nodemetainfo.h>
34

35
#include <QVariant>
36
#include <QMetaProperty>
37
#include <QMimeData>
38
#include <QPainter>
39 40
#include <QPen>
#include <qdebug.h>
41

42
static bool inline registerItemLibrarySortedModel() {
43
    qmlRegisterType<QmlDesigner::ItemLibrarySectionModel>();
44 45
    return true;
}
46

47 48
namespace QmlDesigner {

49 50
static QHash<QString, bool> collapsedStateHash;

51

52 53 54 55 56 57 58 59 60
void ItemLibraryModel::setExpanded(bool expanded, const QString &section)
{
    if (collapsedStateHash.contains(section))
        collapsedStateHash.remove(section);

    if (!expanded) //default is true
        collapsedStateHash.insert(section, expanded);
}

61
ItemLibraryModel::ItemLibraryModel(QObject *parent)
62
    : QAbstractListModel(parent)
63
{
64
    addRoleNames();
65 66 67 68
}

ItemLibraryModel::~ItemLibraryModel()
{
69
    clearSections();
70 71
}

72 73
int ItemLibraryModel::rowCount(const QModelIndex & /*parent*/) const
{
74
    return m_sections.count();
75 76 77 78
}

QVariant ItemLibraryModel::data(const QModelIndex &index, int role) const
{
79
    if (!index.isValid() || index.row() +1 > m_sections.count())
80 81 82 83
        return QVariant();


    if (m_roleNames.contains(role)) {
84
        QVariant value = m_sections.at(index.row())->property(m_roleNames.value(role));
85

86
        ItemLibrarySectionModel* model = qobject_cast<ItemLibrarySectionModel *>(value.value<QObject*>());
87 88 89 90 91 92 93 94 95 96 97 98 99 100
        if (model)
            return QVariant::fromValue(model);

        ItemLibraryModel* model2 = qobject_cast<ItemLibraryModel *>(value.value<QObject*>());
        if (model2)
            return QVariant::fromValue(model2);

        return value;
    }

    qWarning() << Q_FUNC_INFO << "invalid role requested";

    return QVariant();
}
101

102 103 104 105 106
QHash<int, QByteArray> ItemLibraryModel::roleNames() const
{
    return m_roleNames;
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120
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();

121 122 123 124
        bool changed = false;
        updateVisibility(&changed);
        if (changed)
            dataChanged(QModelIndex(), QModelIndex());
125 126 127
    }
}

Thomas Hartmann's avatar
Thomas Hartmann committed
128
Import entryToImport(const ItemLibraryEntry &entry)
129
{
130 131 132
    if (entry.majorVersion() == -1 && entry.minorVersion() == -1)
        return Import::createFileImport(entry.requiredImport());

Thomas Hartmann's avatar
Thomas Hartmann committed
133 134 135
    return Import::createLibraryImport(entry.requiredImport(), QString::number(entry.majorVersion()) + QLatin1Char('.') +
                                                               QString::number(entry.minorVersion()));

136 137 138
}

void ItemLibraryModel::update(ItemLibraryInfo *itemLibraryInfo, Model *model)
139
{
140 141 142
    if (!model)
        return;

143
    beginResetModel();
144
    clearSections();
145

146 147 148
    QStringList imports;
    foreach (const Import &import, model->imports())
        if (import.isLibraryImport())
149
            imports << import.url() + QLatin1Char(' ') + import.version();
150

151
    foreach (ItemLibraryEntry entry, itemLibraryInfo->entries()) {
152

153
         NodeMetaInfo metaInfo = model->metaInfo(entry.typeName());
154
         bool valid = metaInfo.isValid() && metaInfo.majorVersion() == entry.majorVersion();
155

156
         if (valid
157
                 && (entry.requiredImport().isEmpty()
158
                     || model->hasImport(entryToImport(entry), true, true))) {
159
            QString itemSectionName = entry.category();
160 161 162 163
            ItemLibrarySection *sectionModel = sectionByName(itemSectionName);

            if (sectionModel == 0) {
                sectionModel = new ItemLibrarySection(itemSectionName, this);
164
                m_sections.append(sectionModel);
165 166
            }

167
            ItemLibraryItem *item = new ItemLibraryItem(sectionModel);
168 169
            item->setItemLibraryEntry(entry);
            sectionModel->addSectionEntry(item);
170
        }
171
    }
172

173
    sortSections();
174 175 176
    bool changed = false;
    updateVisibility(&changed);
    endResetModel();
177 178
}

179
QMimeData *ItemLibraryModel::getMimeData(const ItemLibraryEntry &itemLibraryEntry)
180
{
181 182 183 184
    QMimeData *mimeData = new QMimeData();

    QByteArray data;
    QDataStream stream(&data, QIODevice::WriteOnly);
185
    stream << itemLibraryEntry;
186
    mimeData->setData(QStringLiteral("application/vnd.bauhaus.itemlibraryinfo"), data);
187

188
    mimeData->removeFormat(QStringLiteral("text/plain"));
189

190
    return mimeData;
191 192
}

193
QList<ItemLibrarySection *> ItemLibraryModel::sections() const
194
{
195
    return m_sections;
196 197 198 199
}

void ItemLibraryModel::clearSections()
{
200 201
    qDeleteAll(m_sections);
    m_sections.clear();
202 203 204 205
}

void ItemLibraryModel::registerQmlTypes()
{
206
    qmlRegisterType<QmlDesigner::ItemLibrarySectionModel>();
207 208 209
    qmlRegisterType<QmlDesigner::ItemLibraryModel>();
}

210 211
ItemLibrarySection *ItemLibraryModel::sectionByName(const QString &sectionName)
{
212
    foreach (ItemLibrarySection *itemLibrarySection, m_sections) {
213 214 215 216 217 218 219
        if (itemLibrarySection->sectionName() == sectionName)
            return itemLibrarySection;
    }

    return 0;
}

220
void ItemLibraryModel::updateVisibility(bool *changed)
221
{
222
    foreach (ItemLibrarySection *itemLibrarySection, m_sections) {
223
        QString sectionSearchText = m_searchText;
224

225 226 227
        bool sectionChanged = false;
        bool sectionVisibility = itemLibrarySection->updateSectionVisibility(sectionSearchText,
                                                                             &sectionChanged);
228 229
        *changed |= sectionChanged;
        *changed |= itemLibrarySection->setVisible(sectionVisibility);
230 231 232
    }
}

233 234 235
void ItemLibraryModel::addRoleNames()
{
    int role = 0;
236 237
    for (int propertyIndex = 0; propertyIndex < ItemLibrarySection::staticMetaObject.propertyCount(); ++propertyIndex) {
        QMetaProperty property = ItemLibrarySection::staticMetaObject.property(propertyIndex);
238 239 240 241 242
        m_roleNames.insert(role, property.name());
        ++role;
    }
}

243 244 245 246 247 248 249 250 251 252 253 254
void ItemLibraryModel::sortSections()
{
    auto sectionSort = [](ItemLibrarySection *first, ItemLibrarySection *second) {
        return QString::localeAwareCompare(first->sortingName(), second->sortingName()) < 1;
    };

    std::sort(m_sections.begin(), m_sections.end(), sectionSort);

    foreach (ItemLibrarySection *itemLibrarySection, m_sections)
        itemLibrarySection->sortItems();
}

255 256 257 258 259
void registerQmlTypes()
{
    registerItemLibrarySortedModel();
}

260
} // namespace QmlDesigner
261