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
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "collectiondatatypemodel.h"
#include <QHash>
#include <QtQml/QmlTypeAndRevisionsRegistration>
namespace QmlDesigner {
struct CollectionDataTypeModel::Details
{
CollectionDetails::DataType type;
QString name;
QString description;
};
const QList<CollectionDataTypeModel::Details> CollectionDataTypeModel::m_orderedDetails{
{DataType::String, "String", "Text"},
{DataType::Integer, "Integer", "Whole number that can be positive, negative, or zero"},
{DataType::Real, "Real", "Number with a decimal"},
{DataType::Image, "Image", "Image resource"},
{DataType::Color, "Color", "HEX value"},
{DataType::Url, "Url", "Resource locator"},
{DataType::Boolean, "Boolean", "True/false"},
};
CollectionDataTypeModel::CollectionDataTypeModel(QObject *parent)
: QAbstractListModel(parent)
{
}
int CollectionDataTypeModel::rowCount([[maybe_unused]] const QModelIndex &parent) const
{
return m_orderedDetails.size();
}
QVariant CollectionDataTypeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return {};
if (role == Qt::DisplayRole)
return m_orderedDetails.at(index.row()).name;
if (role == Qt::ToolTipRole)
return m_orderedDetails.at(index.row()).description;
return {};
}
QString CollectionDataTypeModel::dataTypeToString(DataType dataType)
{
static const QHash<DataType, QString> dataTypeHash = []() -> QHash<DataType, QString> {
QHash<DataType, QString> result;
for (const Details &details : m_orderedDetails)
result.insert(details.type, details.name);
return result;
}();
if (dataTypeHash.contains(dataType))
return dataTypeHash.value(dataType);
return "Unknown";
}
CollectionDetails::DataType CollectionDataTypeModel::dataTypeFromString(const QString &dataType)
{
static const QHash<QString, DataType> stringTypeHash = []() -> QHash<QString, DataType> {
QHash<QString, DataType> result;
for (const Details &details : m_orderedDetails)
result.insert(details.name, details.type);
return result;
}();
if (stringTypeHash.contains(dataType))
return stringTypeHash.value(dataType);
return DataType::String;
}
void CollectionDataTypeModel::registerDeclarativeType()
{
qmlRegisterType<CollectionDataTypeModel>("CollectionDetails", 1, 0, "CollectionDataTypeModel");
}
} // namespace QmlDesigner
|