aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/components/collectioneditor/collectiondetails.h
blob: a18c557c52bde91b7c4e3392909a4319e656e565 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#pragma once

#include "collectioneditorconstants.h"
#include "modelnode.h"

#include <QSharedPointer>

QT_BEGIN_NAMESPACE
class QJsonObject;
class QVariant;
QT_END_NAMESPACE

namespace QmlDesigner {

struct CollectionReference
{
    ModelNode node;
    QString name;

    friend auto qHash(const CollectionReference &collection)
    {
        return qHash(collection.node) ^ ::qHash(collection.name);
    }

    bool operator==(const CollectionReference &other) const
    {
        return node == other.node && name == other.name;
    }

    bool operator!=(const CollectionReference &other) const { return !(*this == other); }
};

struct CollectionProperty;

struct DataTypeWarning {
    Q_GADGET

public:
    enum Warning { None, CellDataTypeMismatch };
    Q_ENUM(Warning)

    Warning warning = None;
    DataTypeWarning(Warning warning)
        : warning(warning)
    {}

    static QString getDataTypeWarningString(Warning warning) {
        return dataTypeWarnings.value(warning);
    }

private:
    static const QMap<Warning, QString> dataTypeWarnings;
};

class CollectionDetails
{
    Q_GADGET

public:
    enum class DataType { Unknown, String, Url, Integer, Real, Boolean, Image, Color };
    Q_ENUM(DataType)

    explicit CollectionDetails();
    CollectionDetails(const CollectionReference &reference);
    CollectionDetails(const CollectionDetails &other);
    ~CollectionDetails();

    void resetDetails(const QStringList &propertyNames,
                      const QList<QJsonObject> &elements,
                      CollectionEditorConstants::SourceFormat format);
    void insertColumn(const QString &propertyName,
                      int colIdx = -1,
                      const QVariant &defaultValue = {},
                      DataType type = DataType::Unknown);
    bool removeColumns(int colIdx, int count = 1);

    void insertElementAt(std::optional<QJsonObject> object, int row = -1);
    void insertEmptyElements(int row = 0, int count = 1);
    bool removeElements(int row, int count = 1);
    bool setPropertyValue(int row, int column, const QVariant &value);

    bool setPropertyName(int column, const QString &value);
    bool setPropertyType(int column, DataType type);

    CollectionReference reference() const;
    CollectionEditorConstants::SourceFormat sourceFormat() const;
    QVariant data(int row, int column) const;
    QString propertyAt(int column) const;
    DataType typeAt(int column) const;
    DataType typeAt(int row, int column) const;
    DataTypeWarning::Warning cellWarningCheck(int row, int column) const;
    bool containsPropertyName(const QString &propertyName);

    bool isValid() const;
    bool isChanged() const;

    int columns() const;
    int rows() const;

    bool markSaved();

    void swap(CollectionDetails &other);
    void resetReference(const CollectionReference &reference);
    QString getCollectionAsJsonString() const;
    QString getCollectionAsCsvString() const;

    QJsonArray getCollectionAsJsonArray() const;

    static void registerDeclarativeType();

    CollectionDetails &operator=(const CollectionDetails &other);

private:
    void markChanged();
    void resetPropertyType(const QString &propertyName);
    void resetPropertyType(CollectionProperty &property);
    void resetPropertyTypes();

    // The private data is supposed to be shared between the copies
    class Private;
    QSharedPointer<Private> d;
};
} // namespace QmlDesigner