aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/itemlibrary/itemlibraryimport.cpp
blob: eb14408137fbee8cccab9ac2336789258086ca8f (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
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
// 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 "itemlibraryimport.h"
#include "itemlibrarycategory.h"

namespace QmlDesigner {

ItemLibraryImport::ItemLibraryImport(const Import &import, QObject *parent, SectionType sectionType)
    : QObject(parent),
      m_import(import),
      m_sectionType(sectionType)
{
    updateRemovable();
}

QString ItemLibraryImport::importName() const
{
    if (m_sectionType == SectionType::User)
        return userComponentsTitle();

    if (m_sectionType == SectionType::Quick3DAssets)
        return quick3DAssetsTitle();

    if (m_sectionType == SectionType::Unimported)
        return unimportedComponentsTitle();

    if (importUrl() == "QtQuick")
        return tr("Default Components");

    if (m_import.isFileImport())
        return m_import.toString(true, true);

    return importUrl().replace('.', ' ');
}

QString ItemLibraryImport::importUrl() const
{
    if (m_sectionType == SectionType::User)
        return userComponentsTitle();

    if (m_sectionType == SectionType::Quick3DAssets)
        return quick3DAssetsTitle();

    if (m_sectionType == SectionType::Unimported)
        return unimportedComponentsTitle();

    if (m_import.isFileImport())
        return m_import.file();

    return m_import.url();
}

bool ItemLibraryImport::importExpanded() const
{
    return m_importExpanded;
}

QString ItemLibraryImport::sortingName() const
{
    if (m_sectionType == SectionType::User)
        return "_"; // user components always come first

    if (m_sectionType == SectionType::Quick3DAssets)
        return "__";  // Quick3DAssets come second

    if (m_sectionType == SectionType::Unimported)
        return "zzzzzz"; // Unimported components come last

    if (!hasCategories()) // imports with no categories come before last
        return "zzzzz_" + importName();

    return importName();
}

void ItemLibraryImport::addCategory(ItemLibraryCategory *category)
{
    m_categoryModel.addCategory(category);
}

QObject *ItemLibraryImport::categoryModel()
{
    return &m_categoryModel;
}

void ItemLibraryImport::expandCategories(bool expand)
{
    m_categoryModel.expandCategories(expand);
}

bool ItemLibraryImport::updateCategoryVisibility(const QString &searchText, bool *changed)
{
    bool hasVisibleCategories = false;
    *changed = false;

    for (const auto &category : m_categoryModel.categorySections()) {
        bool categoryChanged = false;
        bool hasVisibleItems = category->updateItemVisibility(searchText, &categoryChanged);
        categoryChanged |= category->setVisible(hasVisibleItems);

        *changed |= categoryChanged;

        if (hasVisibleItems)
            hasVisibleCategories = true;

        if (searchText.isEmpty())
            category->setCategoryVisible(ItemLibraryModel::loadCategoryVisibleState(category->categoryName(),
                                                                                    importName()));
    }

    return hasVisibleCategories;
}

void ItemLibraryImport::showAllCategories()
{
    m_categoryModel.showAllCategories();
    setAllCategoriesVisible(true);
}

void ItemLibraryImport::hideCategory(const QString &categoryName)
{
    m_categoryModel.hideCategory(categoryName);
    setAllCategoriesVisible(false);
}

ItemLibraryCategory *ItemLibraryImport::selectCategory(int categoryIndex)
{
    return m_categoryModel.selectCategory(categoryIndex);
}

int ItemLibraryImport::selectFirstVisibleCategory()
{
    return m_categoryModel.selectFirstVisibleCategory();
}

void ItemLibraryImport::clearSelectedCategory(int categoryIndex)
{
    m_categoryModel.clearSelectedCategory(categoryIndex);
}

bool ItemLibraryImport::isAllCategoriesHidden() const
{
    if (!m_isVisible)
        return true;

    return m_categoryModel.isAllCategoriesHidden();
}

Import ItemLibraryImport::importEntry() const
{
    return m_import;
}

bool ItemLibraryImport::setVisible(bool isVisible)
{
    if (isVisible != m_isVisible) {
        m_isVisible = isVisible;
        emit importVisibleChanged();
        return true;
    }

    return false;
}

bool ItemLibraryImport::importVisible() const
{
    return m_isVisible;
}

void ItemLibraryImport::setImportUsed(bool importUsed)
{
    if (importUsed != m_importUsed) {
        m_importUsed = importUsed;
        updateRemovable();
        emit importUsedChanged();
    }
}

bool ItemLibraryImport::importUsed() const
{
    return m_importUsed;
}

bool ItemLibraryImport::importRemovable() const
{
    return m_importRemovable;
}

bool ItemLibraryImport::hasCategories() const
{
    return m_categoryModel.rowCount() > 0;
}

bool ItemLibraryImport::hasSingleCategory() const
{
    return m_categoryModel.rowCount() == 1;
}

void ItemLibraryImport::sortCategorySections()
{
    m_categoryModel.sortCategorySections();
}

void ItemLibraryImport::setImportExpanded(bool expanded)
{
    if (expanded != m_importExpanded) {
        m_importExpanded = expanded;
        emit importExpandChanged();
    }
}

ItemLibraryCategory *ItemLibraryImport::getCategoryByName(const QString &categoryName) const
{
    for (ItemLibraryCategory *catSec : std::as_const(m_categoryModel.categorySections())) {
        if (catSec->categoryName() == categoryName)
            return catSec;
    }

    return nullptr;
}

ItemLibraryCategory *ItemLibraryImport::getCategoryAt(int categoryIndex) const
{
    const QList<QPointer<ItemLibraryCategory>> categories = m_categoryModel.categorySections();

    if (categoryIndex != -1 && !categories.isEmpty())
        return categories.at(categoryIndex);

    return nullptr;
}

// static
QString ItemLibraryImport::userComponentsTitle()
{
    return tr("My Components");
}

// static
QString ItemLibraryImport::quick3DAssetsTitle()
{
    return tr("My 3D Components");
}

// static
QString ItemLibraryImport::unimportedComponentsTitle()
{
    return tr("All Other Components");
}

ItemLibraryImport::SectionType ItemLibraryImport::sectionType() const
{
    return m_sectionType;
}

void ItemLibraryImport::updateRemovable()
{
    bool importRemovable = !m_importUsed && m_sectionType == SectionType::Default
            && m_import.url() != "QtQuick";
    if (importRemovable != m_importRemovable) {
        m_importRemovable = importRemovable;
        emit importRemovableChanged();
    }
}

bool ItemLibraryImport::allCategoriesVisible() const
{
    return m_allCategoriesVisible;
}

void ItemLibraryImport::setAllCategoriesVisible(bool visible)
{
    m_allCategoriesVisible = visible;
}

} // namespace QmlDesigner