aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/itemlibrary/itemlibrarycategoriesmodel.cpp
blob: fc687278dc5d1ae86bdace166d9cbcf067d2d3dc (plain)
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
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "itemlibrarycategoriesmodel.h"
#include "itemlibrarycategory.h"
#include "itemlibrarymodel.h"

#include <utils/algorithm.h>
#include <utils/qtcassert.h>

#include <QDebug>
#include <QVariant>
#include <QMetaProperty>
#include <QMetaObject>

namespace QmlDesigner {

ItemLibraryCategoriesModel::ItemLibraryCategoriesModel(QObject *parent) :
    QAbstractListModel(parent)
{
    addRoleNames();
}

ItemLibraryCategoriesModel::~ItemLibraryCategoriesModel()
{
}

int ItemLibraryCategoriesModel::rowCount(const QModelIndex &) const
{
    return m_categoryList.count();
}

QVariant ItemLibraryCategoriesModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_categoryList.count()) {
        qWarning() << Q_FUNC_INFO << "invalid index requested";
        return {};
    }

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

        if (auto model = qobject_cast<ItemLibraryItemsModel *>(value.value<QObject *>()))
            return QVariant::fromValue(model);

        return value;
    }

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

    return {};
}

bool ItemLibraryCategoriesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    // currently only categoryExpanded and categoryVisible properties is updatable
    if (index.isValid() && m_roleNames.contains(role)) {
        QVariant currValue = m_categoryList.at(index.row())->property(m_roleNames.value(role));

        if (currValue != value) {
            m_categoryList[index.row()]->setProperty(m_roleNames.value(role), value);
            if (m_roleNames.value(role) == "categoryExpanded") {
                ItemLibraryModel::saveExpandedState(value.toBool(),
                                                    m_categoryList[index.row()]->categoryName());
            } else if (m_roleNames.value(role) == "categoryVisible") {
                const ItemLibraryCategory *category = m_categoryList[index.row()];
                ItemLibraryModel::saveCategoryVisibleState(value.toBool(), category->categoryName(),
                                                           category->ownerImport()->importName());
            }
            emit dataChanged(index, index, {role});
            return true;
        }
    }
    return false;
}

QHash<int, QByteArray> ItemLibraryCategoriesModel::roleNames() const
{
    return m_roleNames;
}

void ItemLibraryCategoriesModel::expandCategories(bool expand)
{
    int i = 0;
    for (const auto &category : std::as_const(m_categoryList)) {
        if (category->categoryExpanded() != expand) {
            category->setExpanded(expand);
            ItemLibraryModel::saveExpandedState(expand, category->categoryName());
            emit dataChanged(index(i), index(i), {m_roleNames.key("categoryExpanded")});
        }
        ++i;
    }
}

void ItemLibraryCategoriesModel::addCategory(ItemLibraryCategory *category)
{
    m_categoryList.append(category);

    category->setVisible(true);
}

const QList<QPointer<ItemLibraryCategory>> &ItemLibraryCategoriesModel::categorySections() const
{
    return m_categoryList;
}

void ItemLibraryCategoriesModel::sortCategorySections()
{
    auto categorySort = [](ItemLibraryCategory *first, ItemLibraryCategory *second) {
        return QString::localeAwareCompare(first->sortingName(), second->sortingName()) < 0;
    };

    std::sort(m_categoryList.begin(), m_categoryList.end(), categorySort);

    for (const auto &category : qAsConst(m_categoryList))
        category->sortItems();
}

void ItemLibraryCategoriesModel::resetModel()
{
    beginResetModel();
    endResetModel();
}

bool ItemLibraryCategoriesModel::isAllCategoriesHidden() const
{
    for (const auto &category : std::as_const(m_categoryList)) {
        if (category->isCategoryVisible())
            return false;
    }

    return true;
}

void ItemLibraryCategoriesModel::showAllCategories()
{
    for (const auto &category : std::as_const(m_categoryList)) {
        if (!category->isCategoryVisible()) {
            category->setCategoryVisible(true);
            ItemLibraryModel::saveCategoryVisibleState(true, category->categoryName(),
                                                       category->ownerImport()->importName());
        }
    }

    emit dataChanged(index(0), index(m_categoryList.size() - 1), {m_roleNames.key("categoryVisible")});
}

void ItemLibraryCategoriesModel::hideCategory(const QString &categoryName)
{
    for (int i = 0; i < m_categoryList.size(); ++i) {
        const auto category = m_categoryList.at(i);
        if (category->categoryName() == categoryName) {
            category->setCategoryVisible(false);
            ItemLibraryModel::saveCategoryVisibleState(false, category->categoryName(),
                                                       category->ownerImport()->importName());
            emit dataChanged(index(i), index(i), {m_roleNames.key("categoryVisible")});
            break;
        }
    }
}

int ItemLibraryCategoriesModel::selectFirstVisibleCategory()
{
    for (int i = 0; i < m_categoryList.length(); ++i) {
        const auto category = m_categoryList.at(i);

        if (category->isCategoryVisible()) {
            category->setCategorySelected(true);
            emit dataChanged(index(i), index(i), {m_roleNames.key("categorySelected")});
            return i;
        }
    }

    return -1;
}

void ItemLibraryCategoriesModel::clearSelectedCategory(int categoryIndex)
{
    if (categoryIndex == -1 || m_categoryList.isEmpty())
        return;

    m_categoryList.at(categoryIndex)->setCategorySelected(false);
    emit dataChanged(index(categoryIndex), index(categoryIndex), {m_roleNames.key("categorySelected")});
}

QPointer<ItemLibraryCategory> ItemLibraryCategoriesModel::selectCategory(int categoryIndex)
{
    if (m_categoryList.isEmpty() || categoryIndex < 0 || categoryIndex >= m_categoryList.size())
        return nullptr;

    const QPointer<ItemLibraryCategory> category = m_categoryList.at(categoryIndex);

    if (!category->categorySelected()) {
        category->setCategorySelected(true);
        emit dataChanged(index(categoryIndex),index(categoryIndex), {m_roleNames.key("categorySelected")});
    }

    return category;
}

void ItemLibraryCategoriesModel::addRoleNames()
{
    int role = 0;
    const QMetaObject meta = ItemLibraryCategory::staticMetaObject;
    for (int i = meta.propertyOffset(); i < meta.propertyCount(); ++i)
        m_roleNames.insert(role++, meta.property(i).name());
}

} // namespace QmlDesigner