aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/materialbrowser/materialbrowserbundlemodel.cpp
blob: 1632fc14fab47e8d06274240d5ac2377d4221e4e (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
/****************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** 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
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** 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.
**
****************************************************************************/

#include "materialbrowserbundlemodel.h"

#include "bundleimporter.h"
#include "bundlematerial.h"
#include "bundlematerialcategory.h"
#include "utils/qtcassert.h"

#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonDocument>
#include <QUrl>

namespace QmlDesigner {

MaterialBrowserBundleModel::MaterialBrowserBundleModel(QObject *parent)
    : QAbstractListModel(parent)
{
    loadMaterialBundle();
}

int MaterialBrowserBundleModel::rowCount(const QModelIndex &) const
{
    return m_bundleCategories.size();
}

QVariant MaterialBrowserBundleModel::data(const QModelIndex &index, int role) const
{
    QTC_ASSERT(index.isValid() && index.row() < m_bundleCategories.count(), return {});
    QTC_ASSERT(roleNames().contains(role), return {});

    QByteArray roleName = roleNames().value(role);
    if (roleName == "bundleCategory")
        return m_bundleCategories.at(index.row())->name();

    if (roleName == "bundleCategoryVisible")
        return m_bundleCategories.at(index.row())->visible();

    if (roleName == "bundleMaterialsModel")
        return QVariant::fromValue(m_bundleCategories.at(index.row())->categoryMaterials());

    return {};
}

bool MaterialBrowserBundleModel::isValidIndex(int idx) const
{
    return idx > -1 && idx < rowCount();
}

QHash<int, QByteArray> MaterialBrowserBundleModel::roleNames() const
{
    static const QHash<int, QByteArray> roles {
        {Qt::UserRole + 1, "bundleCategory"},
        {Qt::UserRole + 2, "bundleCategoryVisible"},
        {Qt::UserRole + 3, "bundleMaterialsModel"}
    };
    return roles;
}

void MaterialBrowserBundleModel::loadMaterialBundle()
{
    if (m_matBundleExists || m_probeMatBundleDir)
        return;

    QDir matBundleDir(qEnvironmentVariable("MATERIAL_BUNDLE_PATH"));

    // search for matBundleDir from exec dir and up
    if (matBundleDir.dirName() == ".") {
        m_probeMatBundleDir = true; // probe only once

        matBundleDir.setPath(QCoreApplication::applicationDirPath());
        while (!matBundleDir.cd("material_bundle") && matBundleDir.cdUp())
            ; // do nothing

        if (matBundleDir.dirName() != "material_bundle") // bundlePathDir not found
            return;
    }

    QString matBundlePath = matBundleDir.filePath("material_bundle.json");

    if (m_matBundleObj.isEmpty()) {
        QFile matPropsFile(matBundlePath);

        if (!matPropsFile.open(QIODevice::ReadOnly)) {
            qWarning("Couldn't open material_bundle.json");
            return;
        }

        QJsonDocument matBundleJsonDoc = QJsonDocument::fromJson(matPropsFile.readAll());
        if (matBundleJsonDoc.isNull()) {
            qWarning("Invalid material_bundle.json file");
            return;
        } else {
            m_matBundleObj = matBundleJsonDoc.object();
        }
    }

    m_matBundleExists = true;

    const QJsonObject catsObj = m_matBundleObj.value("categories").toObject();
    const QStringList categories = catsObj.keys();
    for (const QString &cat : categories) {
        auto category = new BundleMaterialCategory(this, cat);

        const QJsonObject matsObj = catsObj.value(cat).toObject();
        const QStringList mats = matsObj.keys();
        for (const QString &mat : mats) {
            const QJsonObject matObj = matsObj.value(mat).toObject();

            QStringList files;
            const QJsonArray assetsArr = matObj.value("files").toArray();
            for (const QJsonValueRef &asset : assetsArr)
                files.append(asset.toString());

            auto bundleMat = new BundleMaterial(category, mat, matObj.value("qml").toString(),
                                                QUrl::fromLocalFile(matBundleDir.filePath(matObj.value("icon").toString())), files);

            category->addBundleMaterial(bundleMat);
        }
        m_bundleCategories.append(category);
    }

    QStringList sharedFiles;
    const QJsonArray sharedFilesArr = m_matBundleObj.value("sharedFiles").toArray();
    for (const QJsonValueRef &file : sharedFilesArr)
        sharedFiles.append(file.toString());

    m_importer = new Internal::BundleImporter(matBundleDir.path(), "MaterialBundle", sharedFiles);
    connect(m_importer, &Internal::BundleImporter::importFinished, this, [&](const QmlDesigner::NodeMetaInfo &metaInfo) {
        if (metaInfo.isValid())
            emit addBundleMaterialToProjectRequested(metaInfo);
    });
}

bool MaterialBrowserBundleModel::hasQuick3DImport() const
{
    return m_hasQuick3DImport;
}

void MaterialBrowserBundleModel::setHasQuick3DImport(bool b)
{
    if (b == m_hasQuick3DImport)
        return;

    m_hasQuick3DImport = b;
    emit hasQuick3DImportChanged();
}

bool MaterialBrowserBundleModel::hasMaterialRoot() const
{
    return m_hasMaterialRoot;
}

void MaterialBrowserBundleModel::setHasMaterialRoot(bool b)
{
    if (m_hasMaterialRoot == b)
        return;

    m_hasMaterialRoot = b;
    emit hasMaterialRootChanged();
}

void MaterialBrowserBundleModel::setSearchText(const QString &searchText)
{
    QString lowerSearchText = searchText.toLower();

    if (m_searchText == lowerSearchText)
        return;

    m_searchText = lowerSearchText;

    bool anyCatVisible = false;
    bool catVisibilityChanged = false;

    for (BundleMaterialCategory *cat : std::as_const(m_bundleCategories)) {
        catVisibilityChanged |= cat->filter(m_searchText);
        anyCatVisible |= cat->visible();
    }

    if (anyCatVisible == m_isEmpty) {
        m_isEmpty = !anyCatVisible;
        emit isEmptyChanged();
    }

    if (catVisibilityChanged)
        resetModel();
}

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

void MaterialBrowserBundleModel::applyToSelected(BundleMaterial *mat, bool add)
{
    emit applyToSelectedTriggered(mat, add);
}

void MaterialBrowserBundleModel::addMaterial(BundleMaterial *mat)
{
    m_importer->importComponent(mat->qml(), mat->files());
}

} // namespace QmlDesigner