File size: 8,755 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************
 *   Copyright (c) 2023 David Carter <dcarter@david.carter.ca>             *
 *                                                                         *
 *   This file is part of FreeCAD.                                         *
 *                                                                         *
 *   FreeCAD is free software: you can redistribute it and/or modify it    *
 *   under the terms of the GNU Lesser General Public License as           *
 *   published by the Free Software Foundation, either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   FreeCAD is distributed in the hope that it will be useful, but        *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with FreeCAD. If not, see                               *
 *   <https://www.gnu.org/licenses/>.                                      *
 *                                                                         *
 **************************************************************************/

#ifndef MATERIAL_MATERIALVALUE_H
#define MATERIAL_MATERIALVALUE_H

#include <memory>

#include <QList>
#include <QMetaType>
#include <QVariant>

#include <Gui/MetaTypes.h>

#include <Mod/Material/MaterialGlobal.h>

namespace Materials
{

class MaterialsExport MaterialValue: public Base::BaseClass
{
    TYPESYSTEM_HEADER();

public:
    enum ValueType
    {
        None = 0,
        String = 1,
        Boolean = 2,
        Integer = 3,
        Float = 4,
        Quantity = 5,
        Distribution = 6,
        List = 7,
        Array2D = 8,
        Array3D = 9,
        Color = 10,
        Image = 11,
        File = 12,
        URL = 13,
        MultiLineString = 14,
        FileList = 15,
        ImageList = 16,
        SVG
    };
    MaterialValue();
    explicit MaterialValue(const MaterialValue& other);
    explicit MaterialValue(ValueType type);
    virtual ~MaterialValue() = default;

    MaterialValue& operator=(const MaterialValue& other);
    virtual bool operator==(const MaterialValue& other) const;
    bool operator!=(const MaterialValue& other) const
    {
        return !operator==(other);
    }

    ValueType getType() const
    {
        return _valueType;
    }

    QVariant getValue() const
    {
        return _value;
    }
    QList<QVariant> getList()
    {
        return _value.value<QList<QVariant>>();
    }
    const QList<QVariant> getList() const
    {
        return _value.value<QList<QVariant>>();
    }
    virtual bool isNull() const;
    virtual bool isEmpty() const;

    virtual const QVariant getValueAt(const QVariant& value) const
    {
        Q_UNUSED(value);
        return _value;
    }
    void setValue(const QVariant& value)
    {
        _value = value;
    }
    void setList(const QList<QVariant>& value);

    virtual QString getYAMLString() const;
    static QString escapeString(const QString& source);
    static ValueType mapType(const QString& stringType);

    static const Base::QuantityFormat getQuantityFormat();

    // The precision is based on the value from the original materials editor
    static const int PRECISION = 6;

    void validate(const MaterialValue& other) const;

protected:
    MaterialValue(ValueType type, ValueType inherited);

    void setType(ValueType type)
    {
        _valueType = type;
    }
    void setInitialValue(ValueType inherited);

    QString getYAMLStringImage() const;
    QString getYAMLStringList() const;
    QString getYAMLStringImageList() const;
    QString getYAMLStringMultiLine() const;

    ValueType _valueType;
    QVariant _value;

private:
    static QMap<QString, ValueType> _typeMap;
};

class MaterialsExport Array2D: public MaterialValue
{
    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    Array2D();
    Array2D(const Array2D& other);
    ~Array2D() override = default;

    Array2D& operator=(const Array2D& other);

    bool isNull() const override;
    bool isEmpty() const override;

    const QList<std::shared_ptr<QList<QVariant>>>& getArray() const
    {
        return _rows;
    }

    void validateRow(int row) const;
    void validateColumn(int column) const;
    void validate(const Array2D& other) const;

    std::shared_ptr<QList<QVariant>> getRow(int row) const;
    std::shared_ptr<QList<QVariant>> getRow(int row);
    int rows() const
    {
        return _rows.size();
    }
    int columns() const
    {
        return _columns;
    }
    void setColumns(int size)
    {
        _columns = size;
    }
    void addRow(const std::shared_ptr<QList<QVariant>>& row);
    void insertRow(int index, const std::shared_ptr<QList<QVariant>>& row);
    void deleteRow(int row);
    void setRows(int rowCount);

    void setValue(int row, int column, const QVariant& value);
    QVariant getValue(int row, int column) const;

    QString getYAMLString() const override;

protected:
    void deepCopy(const Array2D& other);

    QList<std::shared_ptr<QList<QVariant>>> _rows;
    int _columns;

private:
    static void dumpRow(const std::shared_ptr<QList<QVariant>>& row);
    void dump() const;
};

class MaterialsExport Array3D: public MaterialValue
{
    TYPESYSTEM_HEADER_WITH_OVERRIDE();

public:
    Array3D();
    Array3D(const Array3D& other);
    ~Array3D() override = default;

    Array3D& operator=(const Array3D& other);

    bool isNull() const override;
    bool isEmpty() const override;

    const QList<
        std::pair<Base::Quantity, std::shared_ptr<QList<std::shared_ptr<QList<Base::Quantity>>>>>>&
    getArray() const
    {
        return _rowMap;
    }

    void validateDepth(int level) const;
    void validateColumn(int column) const;
    void validateRow(int level, int row) const;
    void validate(const Array3D& other) const;

    const std::shared_ptr<QList<std::shared_ptr<QList<Base::Quantity>>>>&
    getTable(const Base::Quantity& depth) const;
    const std::shared_ptr<QList<std::shared_ptr<QList<Base::Quantity>>>>&
    getTable(int depthIndex) const;
    std::shared_ptr<QList<Base::Quantity>> getRow(int depth, int row) const;
    std::shared_ptr<QList<Base::Quantity>> getRow(int row) const;
    std::shared_ptr<QList<Base::Quantity>> getRow(int depth, int row);
    std::shared_ptr<QList<Base::Quantity>> getRow(int row);
    void addRow(int depth, const std::shared_ptr<QList<Base::Quantity>>& row);
    void addRow(const std::shared_ptr<QList<Base::Quantity>>& row);
    int addDepth(int depth, const Base::Quantity& value);
    int addDepth(const Base::Quantity& value);
    void deleteDepth(int depth);
    void insertRow(int depth, int row, const std::shared_ptr<QList<Base::Quantity>>& rowData);
    void insertRow(int row, const std::shared_ptr<QList<Base::Quantity>>& rowData);
    void deleteRow(int depth, int row);
    void deleteRow(int row);
    void deleteRows(int depth);
    void deleteRows();
    int depth() const
    {
        return _rowMap.size();
    }
    int rows(int depth) const;
    int rows() const
    {
        return rows(_currentDepth);
    }
    int columns() const
    {
        return _columns;
    }
    void setColumns(int size)
    {
        _columns = size;
    }
    void setDepth(int depthCount);
    void setRows(int depth, int rowCount);

    void setValue(int depth, int row, int column, const Base::Quantity& value);
    void setValue(int row, int column, const Base::Quantity& value);
    void setDepthValue(int depth, const Base::Quantity& value);
    void setDepthValue(const Base::Quantity& value);
    Base::Quantity getValue(int depth, int row, int column) const;
    Base::Quantity getValue(int row, int column) const;
    Base::Quantity getDepthValue(int depth) const;

    int currentDepth() const;
    void setCurrentDepth(int depth);

    QString getYAMLString() const override;

protected:
    void deepCopy(const Array3D& other);

    QList<std::pair<Base::Quantity, std::shared_ptr<QList<std::shared_ptr<QList<Base::Quantity>>>>>>
        _rowMap;
    int _currentDepth;
    int _columns;
};

}  // namespace Materials

Q_DECLARE_METATYPE(Materials::MaterialValue)
Q_DECLARE_METATYPE(std::shared_ptr<Materials::Array2D>)
Q_DECLARE_METATYPE(std::shared_ptr<Materials::Array3D>)

#endif  // MATERIAL_MATERIALVALUE_H