aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/collectioneditor/collectionsourcemodel.cpp
blob: 9378d6871c60c7ef436e788616af9bef2bc6b0eb (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "collectionsourcemodel.h"

#include "abstractview.h"
#include "collectioneditorconstants.h"
#include "collectioneditorutils.h"
#include "collectionlistmodel.h"
#include "variantproperty.h"

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

#include <QFile>
#include <QFileInfo>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>

namespace {

QSharedPointer<QmlDesigner::CollectionListModel> loadCollection(
    const QmlDesigner::ModelNode &sourceNode,
    QSharedPointer<QmlDesigner::CollectionListModel> initialCollection = {})
{
    using namespace QmlDesigner::CollectionEditorConstants;
    using namespace QmlDesigner::CollectionEditorUtils;
    QString sourceFileAddress = getSourceCollectionPath(sourceNode);

    QSharedPointer<QmlDesigner::CollectionListModel> collectionsList;
    auto setupCollectionList = [&sourceNode, &initialCollection, &collectionsList]() {
        if (initialCollection.isNull())
            collectionsList.reset(new QmlDesigner::CollectionListModel(sourceNode));
        else if (initialCollection->sourceNode() == sourceNode)
            collectionsList = initialCollection;
        else
            collectionsList.reset(new QmlDesigner::CollectionListModel(sourceNode));
    };

    if (sourceNode.type() == JSONCOLLECTIONMODEL_TYPENAME) {
        QFile sourceFile(sourceFileAddress);
        if (!sourceFile.open(QFile::ReadOnly))
            return {};

        QJsonParseError parseError;
        QJsonDocument document = QJsonDocument::fromJson(sourceFile.readAll(), &parseError);
        if (parseError.error != QJsonParseError::NoError)
            return {};

        setupCollectionList();

        if (document.isObject()) {
            const QJsonObject sourceObject = document.object();
            collectionsList->setStringList(sourceObject.toVariantMap().keys());
        }
    } else if (sourceNode.type() == CSVCOLLECTIONMODEL_TYPENAME) {
        QmlDesigner::VariantProperty collectionNameProperty = sourceNode.variantProperty(
            "objectName");
        setupCollectionList();
        collectionsList->setStringList({collectionNameProperty.value().toString()});
    }
    return collectionsList;
}

} // namespace

namespace QmlDesigner {

CollectionSourceModel::CollectionSourceModel(QObject *parent)
    : Super(parent)
{}

int CollectionSourceModel::rowCount(const QModelIndex &) const
{
    return m_collectionSources.size();
}

QVariant CollectionSourceModel::data(const QModelIndex &index, int role) const
{
    QTC_ASSERT(index.isValid(), return {});

    const ModelNode *collectionSource = &m_collectionSources.at(index.row());

    switch (role) {
    case NameRole: // Not used, to be removed
        return collectionSource->variantProperty("objectName").value().toString();
    case NodeRole:
        return QVariant::fromValue(*collectionSource);
    case CollectionTypeRole:
        return CollectionEditorUtils::getSourceCollectionType(*collectionSource);
    case SourceRole:
        return collectionSource->variantProperty(CollectionEditorConstants::SOURCEFILE_PROPERTY).value();
    case SelectedRole:
        return index.row() == m_selectedIndex;
    case CollectionsRole:
        return QVariant::fromValue(m_collectionList.at(index.row()).data());
    }

    return {};
}

bool CollectionSourceModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    ModelNode collectionSource = m_collectionSources.at(index.row());
    switch (role) {
    case Qt::DisplayRole:
    case NameRole: {
        auto collectionName = collectionSource.variantProperty("objectName");
        if (collectionName.value() == value)
            return false;

        collectionName.setValue(value.toString());
    } break;
    case SourceRole: {
        auto sourceAddress = collectionSource.variantProperty(
            CollectionEditorConstants::SOURCEFILE_PROPERTY);
        if (sourceAddress.value() == value)
            return false;

        sourceAddress.setValue(value.toString());
    } break;
    case SelectedRole: {
        if (value.toBool() != index.data(SelectedRole).toBool())
            setSelectedIndex(value.toBool() ? index.row() : -1);
        else
            return false;
    } break;
    default:
        return false;
    }

    return true;
}

bool CollectionSourceModel::removeRows(int row, int count, [[maybe_unused]] const QModelIndex &parent)
{
    const int rowMax = std::min(row + count, rowCount());

    if (row >= rowMax || row < 0)
        return false;

    AbstractView *view = m_collectionSources.at(row).view();
    if (!view)
        return false;

    count = rowMax - row;

    bool selectionUpdateNeeded = m_selectedIndex >= row && m_selectedIndex < rowMax;

    // It's better to remove the group of nodes here because of the performance issue for the list,
    // and update issue for the view
    beginRemoveRows({}, row, rowMax - 1);

    view->executeInTransaction(Q_FUNC_INFO, [row, count, this]() {
        for (ModelNode node : Utils::span<const ModelNode>(m_collectionSources).subspan(row, count)) {
            m_sourceIndexHash.remove(node.internalId());
            node.destroy();
        }
        m_collectionSources.remove(row, count);
        m_collectionList.remove(row, count);
    });

    int idx = row;
    for (const ModelNode &node : Utils::span<const ModelNode>(m_collectionSources).subspan(row))
        m_sourceIndexHash.insert(node.internalId(), ++idx);

    endRemoveRows();

    if (selectionUpdateNeeded)
        updateSelectedSource();

    updateEmpty();
    return true;
}

QHash<int, QByteArray> CollectionSourceModel::roleNames() const
{
    static QHash<int, QByteArray> roles;
    if (roles.isEmpty()) {
        roles.insert(Super::roleNames());
        roles.insert({{NameRole, "sourceName"},
                      {NodeRole, "sourceNode"},
                      {CollectionTypeRole, "sourceCollectionType"},
                      {SelectedRole, "sourceIsSelected"},
                      {SourceRole, "sourceAddress"},
                      {CollectionsRole, "internalModels"}});
    }
    return roles;
}

void CollectionSourceModel::setSources(const ModelNodes &sources)
{
    beginResetModel();
    m_collectionSources = sources;
    m_sourceIndexHash.clear();
    m_collectionList.clear();
    int i = -1;
    for (const ModelNode &collectionSource : sources) {
        m_sourceIndexHash.insert(collectionSource.internalId(), ++i);

        auto loadedCollection = loadCollection(collectionSource);
        m_collectionList.append(loadedCollection);

        registerCollection(loadedCollection);
    }

    updateEmpty();
    endResetModel();

    updateSelectedSource(true);
}

void CollectionSourceModel::removeSource(const ModelNode &node)
{
    int nodePlace = m_sourceIndexHash.value(node.internalId(), -1);
    if (nodePlace < 0)
        return;

    removeRow(nodePlace);
}

int CollectionSourceModel::sourceIndex(const ModelNode &node) const
{
    return m_sourceIndexHash.value(node.internalId(), -1);
}

void CollectionSourceModel::addSource(const ModelNode &node)
{
    int newRowId = m_collectionSources.count();
    beginInsertRows({}, newRowId, newRowId);
    m_collectionSources.append(node);
    m_sourceIndexHash.insert(node.internalId(), newRowId);

    auto loadedCollection = loadCollection(node);
    m_collectionList.append(loadedCollection);

    registerCollection(loadedCollection);

    updateEmpty();
    endInsertRows();
    updateSelectedSource(true);
}

void CollectionSourceModel::selectSource(const ModelNode &node)
{
    int nodePlace = m_sourceIndexHash.value(node.internalId(), -1);
    if (nodePlace < 0)
        return;

    selectSourceIndex(nodePlace, true);
}

bool CollectionSourceModel::collectionExists(const ModelNode &node, const QString &collectionName) const
{
    int idx = sourceIndex(node);
    if (idx < 0)
        return false;

    auto collections = m_collectionList.at(idx);
    if (collections.isNull())
        return false;

    return collections->contains(collectionName);
}

bool CollectionSourceModel::addCollectionToSource(const ModelNode &node,
                                                  const QString &collectionName,
                                                  const QJsonObject &newCollection,
                                                  QString *errorString)
{
    auto returnError = [errorString](const QString &msg) -> bool {
        if (errorString)
            *errorString = msg;
        return false;
    };

    int idx = sourceIndex(node);
    if (idx < 0)
        return returnError(tr("Node is not indexed in the models."));

    if (node.type() != CollectionEditorConstants::JSONCOLLECTIONMODEL_TYPENAME)
        return returnError(tr("Node should be a JSON model."));

    if (collectionExists(node, collectionName))
        return returnError(tr("A model with the identical name already exists."));

    QString sourceFileAddress = CollectionEditorUtils::getSourceCollectionPath(node);

    QFileInfo sourceFileInfo(sourceFileAddress);
    if (!sourceFileInfo.isFile())
        return returnError(tr("Selected node must have a valid source file address"));

    QFile jsonFile(sourceFileAddress);
    if (!jsonFile.open(QFile::ReadWrite))
        return returnError(tr("Can't read or write \"%1\".\n%2")
                               .arg(sourceFileInfo.absoluteFilePath(), jsonFile.errorString()));

    QJsonParseError parseError;
    QJsonDocument document = QJsonDocument::fromJson(jsonFile.readAll(), &parseError);
    if (parseError.error != QJsonParseError::NoError)
        return returnError(tr("\"%1\" is corrupted.\n%2")
                               .arg(sourceFileInfo.absoluteFilePath(), parseError.errorString()));

    if (document.isObject()) {
        QJsonObject sourceObject = document.object();
        sourceObject.insert(collectionName, newCollection);
        document.setObject(sourceObject);
        if (!jsonFile.resize(0))
            return returnError(tr("Can't clean \"%1\".").arg(sourceFileInfo.absoluteFilePath()));

        QByteArray jsonData = document.toJson();
        auto writtenBytes = jsonFile.write(jsonData);
        jsonFile.close();

        if (writtenBytes != jsonData.size())
            return returnError(tr("Can't write to \"%1\".").arg(sourceFileInfo.absoluteFilePath()));

        updateCollectionList(index(idx));

        auto collections = m_collectionList.at(idx);
        if (collections.isNull())
            return returnError(tr("No model is available for the JSON model group."));

        collections->selectCollectionName(collectionName);
        setSelectedCollectionName(collectionName);
        return true;
    } else {
        return returnError(tr("JSON document type should be an object containing models."));
    }
}

QmlDesigner::ModelNode CollectionSourceModel::sourceNodeAt(int idx)
{
    QModelIndex data = index(idx);
    if (!data.isValid())
        return {};

    return m_collectionSources.at(idx);
}

CollectionListModel *CollectionSourceModel::selectedCollectionList()
{
    QModelIndex idx = index(m_selectedIndex);
    if (!idx.isValid())
        return {};

    return idx.data(CollectionsRole).value<CollectionListModel *>();
}

void CollectionSourceModel::selectSourceIndex(int idx, bool selectAtLeastOne)
{
    int collectionCount = m_collectionSources.size();
    int preferredIndex = -1;
    if (collectionCount) {
        if (selectAtLeastOne)
            preferredIndex = std::max(0, std::min(idx, collectionCount - 1));
        else if (idx > -1 && idx < collectionCount)
            preferredIndex = idx;
    }

    setSelectedIndex(preferredIndex);
}

void CollectionSourceModel::deselect()
{
    setSelectedIndex(-1);
}

void CollectionSourceModel::updateSelectedSource(bool selectAtLeastOne)
{
    int idx = m_selectedIndex;
    m_selectedIndex = -1;
    selectSourceIndex(idx, selectAtLeastOne);
}

bool CollectionSourceModel::collectionExists(const QVariant &node, const QString &collectionName) const
{
    return collectionExists(node.value<ModelNode>(), collectionName);
}

void CollectionSourceModel::updateNodeName(const ModelNode &node)
{
    QModelIndex index = indexOfNode(node);
    emit dataChanged(index, index, {NameRole, Qt::DisplayRole});
    updateCollectionList(index);
}

void CollectionSourceModel::updateNodeSource(const ModelNode &node)
{
    QModelIndex index = indexOfNode(node);
    emit dataChanged(index, index, {SourceRole});
    updateCollectionList(index);
}

void CollectionSourceModel::onSelectedCollectionChanged(CollectionListModel *collectionList,
                                                        int collectionIndex)
{
    if (collectionIndex > -1) {
        if (m_previousSelectedList && m_previousSelectedList != collectionList)
            m_previousSelectedList->selectCollectionIndex(-1);

        m_previousSelectedList = collectionList;

        setSelectedCollectionName(collectionList->collectionNameAt(collectionIndex));

        selectSourceIndex(sourceIndex(collectionList->sourceNode()));
    } else {
        setSelectedCollectionName({});
    }
}

void CollectionSourceModel::onCollectionNameChanged(CollectionListModel *collectionList,
                                                    const QString &oldName, const QString &newName)
{
    auto emitRenameWarning = [this](const QString &msg) -> void {
        emit warning(tr("Rename Model"), msg);
    };

    const ModelNode node = collectionList->sourceNode();
    const QModelIndex nodeIndex = indexOfNode(node);

    if (!nodeIndex.isValid()) {
        emitRenameWarning(tr("Invalid node"));
        return;
    }

    if (node.type() == CollectionEditorConstants::CSVCOLLECTIONMODEL_TYPENAME) {
        if (!setData(nodeIndex, newName, NameRole))
            emitRenameWarning(tr("Can't rename the node"));
        return;
    } else if (node.type() != CollectionEditorConstants::JSONCOLLECTIONMODEL_TYPENAME) {
        emitRenameWarning(tr("Invalid node type"));
        return;
    }

    QString sourceFileAddress = CollectionEditorUtils::getSourceCollectionPath(node);

    QFileInfo sourceFileInfo(sourceFileAddress);
    if (!sourceFileInfo.isFile()) {
        emitRenameWarning(tr("Selected node must have a valid source file address"));
        return;
    }

    QFile jsonFile(sourceFileAddress);
    if (!jsonFile.open(QFile::ReadWrite)) {
        emitRenameWarning(tr("Can't read or write \"%1\".\n%2")
                              .arg(sourceFileInfo.absoluteFilePath(), jsonFile.errorString()));
        return;
    }

    QJsonParseError parseError;
    QJsonDocument document = QJsonDocument::fromJson(jsonFile.readAll(), &parseError);
    if (parseError.error != QJsonParseError::NoError) {
        emitRenameWarning(tr("\"%1\" is corrupted.\n%2")
                              .arg(sourceFileInfo.absoluteFilePath(), parseError.errorString()));
        return;
    }

    if (document.isObject()) {
        QJsonObject rootObject = document.object();

        bool collectionContainsOldName = rootObject.contains(oldName);
        bool collectionContainsNewName = rootObject.contains(newName);

        if (!collectionContainsOldName) {
            emitRenameWarning(
                tr("The model group doesn't contain the old model name (%1).").arg(oldName));
            return;
        }

        if (collectionContainsNewName) {
            emitRenameWarning(
                tr("The model name \"%1\" already exists in the model group.").arg(newName));
            return;
        }

        QJsonValue oldValue = rootObject.value(oldName);
        rootObject.insert(newName, oldValue);
        rootObject.remove(oldName);

        document.setObject(rootObject);
        if (!jsonFile.resize(0)) {
            emitRenameWarning(tr("Can't clean \"%1\".").arg(sourceFileInfo.absoluteFilePath()));
            return;
        }

        QByteArray jsonData = document.toJson();
        auto writtenBytes = jsonFile.write(jsonData);
        jsonFile.close();

        if (writtenBytes != jsonData.size()) {
            emitRenameWarning(tr("Can't write to \"%1\".").arg(sourceFileInfo.absoluteFilePath()));
            return;
        }

        CollectionListModel *list = m_collectionList.at(nodeIndex.row()).data();
        bool updateSelectedNames = list && list == m_previousSelectedList.data();
        emit collectionRenamed(oldName, newName);
        updateCollectionList(nodeIndex);

        if (updateSelectedNames) {
            list = m_collectionList.at(nodeIndex.row()).data();
            if (m_selectedCollectionName == oldName) {
                list->selectCollectionName(newName);
                setSelectedCollectionName(newName);
            } else {
                // reselect to update ID if it's changed due to renaming and order changes
                list->selectCollectionName(m_selectedCollectionName);
            }
        }
    }
}

void CollectionSourceModel::onCollectionsRemoved(CollectionListModel *collectionList,
                                                 const QStringList &removedCollections)
{
    auto emitDeleteWarning = [this](const QString &msg) -> void {
        emit warning(tr("Delete Model"), msg);
    };

    const ModelNode node = collectionList->sourceNode();
    const QModelIndex nodeIndex = indexOfNode(node);

    if (!nodeIndex.isValid()) {
        emitDeleteWarning(tr("Invalid node"));
        return;
    }

    if (node.type() == CollectionEditorConstants::CSVCOLLECTIONMODEL_TYPENAME) {
        removeSource(node);
        return;
    } else if (node.type() != CollectionEditorConstants::JSONCOLLECTIONMODEL_TYPENAME) {
        emitDeleteWarning(tr("Invalid node type"));
        return;
    }

    QString sourceFileAddress = CollectionEditorUtils::getSourceCollectionPath(node);

    QFileInfo sourceFileInfo(sourceFileAddress);
    if (!sourceFileInfo.isFile()) {
        emitDeleteWarning(tr("The selected node has an invalid source address"));
        return;
    }

    QFile jsonFile(sourceFileAddress);
    if (!jsonFile.open(QFile::ReadWrite)) {
        emitDeleteWarning(tr("Can't read or write \"%1\".\n%2")
                              .arg(sourceFileInfo.absoluteFilePath(), jsonFile.errorString()));
        return;
    }

    QJsonParseError parseError;
    QJsonDocument document = QJsonDocument::fromJson(jsonFile.readAll(), &parseError);
    if (parseError.error != QJsonParseError::NoError) {
        emitDeleteWarning(tr("\"%1\" is corrupted.\n%2")
                              .arg(sourceFileInfo.absoluteFilePath(), parseError.errorString()));
        return;
    }

    if (document.isObject()) {
        QJsonObject rootObject = document.object();

        QStringList collectionsRemovedFromDocument;
        for (const QString &collectionName : removedCollections) {
            bool sourceContainsCollection = rootObject.contains(collectionName);
            if (sourceContainsCollection) {
                rootObject.remove(collectionName);
                collectionsRemovedFromDocument << collectionName;
            } else {
                emitDeleteWarning(tr("The model group doesn't contain the model name (%1).")
                                      .arg(sourceContainsCollection));
            }
        }

        document.setObject(rootObject);
        if (!jsonFile.resize(0)) {
            emitDeleteWarning(tr("Can't clean \"%1\".").arg(sourceFileInfo.absoluteFilePath()));
            return;
        }

        QByteArray jsonData = document.toJson();
        auto writtenBytes = jsonFile.write(jsonData);
        jsonFile.close();

        if (writtenBytes != jsonData.size()) {
            emitDeleteWarning(tr("Can't write to \"%1\".").arg(sourceFileInfo.absoluteFilePath()));
            return;
        }

        for (const QString &collectionName : std::as_const(collectionsRemovedFromDocument))
            emit collectionRemoved(collectionName);

        updateCollectionList(nodeIndex);
        if (m_previousSelectedList == collectionList)
            onSelectedCollectionChanged(collectionList, collectionList->selectedIndex());
    }
}

void CollectionSourceModel::setSelectedIndex(int idx)
{
    idx = (idx > -1 && idx < m_collectionSources.count()) ? idx : -1;

    if (m_selectedIndex != idx) {
        QModelIndex previousIndex = index(m_selectedIndex);
        QModelIndex newIndex = index(idx);

        m_selectedIndex = idx;

        if (previousIndex.isValid())
            emit dataChanged(previousIndex, previousIndex, {SelectedRole});

        if (newIndex.isValid())
            emit dataChanged(newIndex, newIndex, {SelectedRole});

        emit selectedIndexChanged(idx);

        if (idx > -1) {
            QPointer<CollectionListModel> relatedCollectionList = m_collectionList.at(idx).data();
            if (relatedCollectionList) {
                if (relatedCollectionList->selectedIndex() < 0)
                    relatedCollectionList->selectCollectionIndex(0, true);
            } else if (m_previousSelectedList) {
                m_previousSelectedList->selectCollectionIndex(-1);
                m_previousSelectedList = {};
                setSelectedCollectionName({});
            }
        }
    }
}

void CollectionSourceModel::setSelectedCollectionName(const QString &collectionName)
{
    if (m_selectedCollectionName != collectionName) {
        m_selectedCollectionName = collectionName;
        emit collectionSelected(m_selectedCollectionName);
    }
}

void CollectionSourceModel::updateEmpty()
{
    bool isEmptyNow = m_collectionSources.isEmpty();
    if (m_isEmpty != isEmptyNow) {
        m_isEmpty = isEmptyNow;
        emit isEmptyChanged(m_isEmpty);

        if (m_isEmpty)
            setSelectedIndex(-1);
    }
}

void CollectionSourceModel::updateCollectionList(QModelIndex index)
{
    if (!index.isValid())
        return;

    ModelNode sourceNode = sourceNodeAt(index.row());
    QSharedPointer<CollectionListModel> oldList = m_collectionList.at(index.row());
    QSharedPointer<CollectionListModel> newList = loadCollection(sourceNode, oldList);
    if (oldList != newList) {
        m_collectionList.replace(index.row(), newList);
        emit dataChanged(index, index, {CollectionsRole});
        registerCollection(newList);
    }
}

void CollectionSourceModel::registerCollection(const QSharedPointer<CollectionListModel> &collection)
{
    CollectionListModel *collectionList = collection.data();
    if (collectionList == nullptr)
        return;

    if (!collectionList->property("_is_registered_in_sourceModel").toBool()) {
        collectionList->setProperty("_is_registered_in_sourceModel", true);

        connect(collectionList,
                &CollectionListModel::selectedIndexChanged,
                this,
                [this, collectionList](int idx) { onSelectedCollectionChanged(collectionList, idx); });

        connect(collectionList,
                &CollectionListModel::collectionNameChanged,
                this,
                [this, collectionList](const QString &oldName, const QString &newName) {
                    onCollectionNameChanged(collectionList, oldName, newName);
                });

        connect(collectionList,
                &CollectionListModel::collectionsRemoved,
                this,
                [this, collectionList](const QStringList &removedCollections) {
                    onCollectionsRemoved(collectionList, removedCollections);
                });
    }

    if (collectionList->sourceNode().isValid())
        emit collectionNamesInitialized(collection->stringList());
}

QModelIndex CollectionSourceModel::indexOfNode(const ModelNode &node) const
{
    return index(m_sourceIndexHash.value(node.internalId(), -1));
}

void CollectionJsonSourceFilterModel::registerDeclarativeType()
{
    qmlRegisterType<CollectionJsonSourceFilterModel>("CollectionEditor",
                                                     1,
                                                     0,
                                                     "CollectionJsonSourceFilterModel");
}

bool CollectionJsonSourceFilterModel::filterAcceptsRow(int source_row, const QModelIndex &) const
{
    if (!sourceModel())
        return false;
    QModelIndex sourceItem = sourceModel()->index(source_row, 0, {});
    return sourceItem.data(CollectionSourceModel::Roles::CollectionTypeRole).toString() == "json";
}

} // namespace QmlDesigner