1 | // Copyright (C) 2019 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qqmltablemodelcolumn_p.h" |
5 | |
6 | #include <QtQml/qqmlinfo.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | /*! |
11 | \qmltype TableModelColumn |
12 | //! \instantiates QQmlTableModelColumn |
13 | \inqmlmodule Qt.labs.qmlmodels |
14 | \brief Represents a column in a model. |
15 | \since 5.14 |
16 | |
17 | \section1 Supported Roles |
18 | |
19 | TableModelColumn supports all of \l {Qt::ItemDataRole}{Qt's roles}, |
20 | with the exception of \c Qt::InitialSortOrderRole. |
21 | |
22 | \sa TableModel, TableView |
23 | */ |
24 | |
25 | static const QString displayRoleName = QStringLiteral("display" ); |
26 | static const QString decorationRoleName = QStringLiteral("decoration" ); |
27 | static const QString editRoleName = QStringLiteral("edit" ); |
28 | static const QString toolTipRoleName = QStringLiteral("toolTip" ); |
29 | static const QString statusTipRoleName = QStringLiteral("statusTip" ); |
30 | static const QString whatsThisRoleName = QStringLiteral("whatsThis" ); |
31 | |
32 | static const QString fontRoleName = QStringLiteral("font" ); |
33 | static const QString textAlignmentRoleName = QStringLiteral("textAlignment" ); |
34 | static const QString backgroundRoleName = QStringLiteral("background" ); |
35 | static const QString foregroundRoleName = QStringLiteral("foreground" ); |
36 | static const QString checkStateRoleName = QStringLiteral("checkState" ); |
37 | |
38 | static const QString accessibleTextRoleName = QStringLiteral("accessibleText" ); |
39 | static const QString accessibleDescriptionRoleName = QStringLiteral("accessibleDescription" ); |
40 | |
41 | static const QString sizeHintRoleName = QStringLiteral("sizeHint" ); |
42 | |
43 | |
44 | QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent) |
45 | : QObject(parent) |
46 | { |
47 | } |
48 | |
49 | QQmlTableModelColumn::~QQmlTableModelColumn() |
50 | { |
51 | } |
52 | |
53 | #define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, setterGetterName, setterSetterName, setterSignal, roleName) \ |
54 | QJSValue QQmlTableModelColumn::getterGetterName() const \ |
55 | { \ |
56 | return mGetters.value(roleName); \ |
57 | } \ |
58 | \ |
59 | void QQmlTableModelColumn::getterSetterName(const QJSValue &stringOrFunction) \ |
60 | { \ |
61 | if (!stringOrFunction.isString() && !stringOrFunction.isCallable()) { \ |
62 | qmlWarning(this).quote() << "getter for " << roleName << " must be a function"; \ |
63 | return; \ |
64 | } \ |
65 | if (stringOrFunction.strictlyEquals(decoration())) \ |
66 | return; \ |
67 | \ |
68 | mGetters[roleName] = stringOrFunction; \ |
69 | emit decorationChanged(); \ |
70 | } \ |
71 | \ |
72 | QJSValue QQmlTableModelColumn::setterGetterName() const \ |
73 | { \ |
74 | return mSetters.value(roleName); \ |
75 | } \ |
76 | \ |
77 | void QQmlTableModelColumn::setterSetterName(const QJSValue &function) \ |
78 | { \ |
79 | if (!function.isCallable()) { \ |
80 | qmlWarning(this).quote() << "setter for " << roleName << " must be a function"; \ |
81 | return; \ |
82 | } \ |
83 | \ |
84 | if (function.strictlyEquals(getSetDisplay())) \ |
85 | return; \ |
86 | \ |
87 | mSetters[roleName] = function; \ |
88 | emit setDisplayChanged(); \ |
89 | } |
90 | |
91 | DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged, |
92 | getSetDisplay, setSetDisplay, setDisplayChanged, displayRoleName) |
93 | DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged, |
94 | getSetDecoration, setSetDecoration, setDecorationChanged, decorationRoleName) |
95 | DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged, |
96 | getSetEdit, setSetEdit, setEditChanged, editRoleName) |
97 | DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged, |
98 | getSetToolTip, setSetToolTip, setToolTipChanged, toolTipRoleName) |
99 | DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged, |
100 | getSetStatusTip, setSetStatusTip, setStatusTipChanged, statusTipRoleName) |
101 | DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged, |
102 | getSetWhatsThis, setSetWhatsThis, setWhatsThisChanged, whatsThisRoleName) |
103 | |
104 | DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged, |
105 | getSetFont, setSetFont, setFontChanged, fontRoleName) |
106 | DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged, |
107 | getSetTextAlignment, setSetTextAlignment, setTextAlignmentChanged, textAlignmentRoleName) |
108 | DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged, |
109 | getSetBackground, setSetBackground, setBackgroundChanged, backgroundRoleName) |
110 | DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged, |
111 | getSetForeground, setSetForeground, setForegroundChanged, foregroundRoleName) |
112 | DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged, |
113 | getSetCheckState, setSetCheckState, setCheckStateChanged, checkStateRoleName) |
114 | |
115 | DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged, |
116 | getSetAccessibleText, setSetAccessibleText, setAccessibleTextChanged, accessibleTextRoleName) |
117 | DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged, |
118 | getSetAccessibleDescription, setSetAccessibleDescription, setAccessibleDescriptionChanged, accessibleDescriptionRoleName) |
119 | |
120 | DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged, |
121 | getSetSizeHint, setSetSizeHint, setSizeHintChanged, sizeHintRoleName) |
122 | |
123 | QJSValue QQmlTableModelColumn::getterAtRole(const QString &roleName) |
124 | { |
125 | auto it = mGetters.find(key: roleName); |
126 | if (it == mGetters.end()) |
127 | return QJSValue(); |
128 | return *it; |
129 | } |
130 | |
131 | QJSValue QQmlTableModelColumn::setterAtRole(const QString &roleName) |
132 | { |
133 | auto it = mSetters.find(key: roleName); |
134 | if (it == mSetters.end()) |
135 | return QJSValue(); |
136 | return *it; |
137 | } |
138 | |
139 | const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const |
140 | { |
141 | return mGetters; |
142 | } |
143 | |
144 | const QHash<int, QString> QQmlTableModelColumn::supportedRoleNames() |
145 | { |
146 | QHash<int, QString> names; |
147 | names[Qt::DisplayRole] = QLatin1String("display" ); |
148 | names[Qt::DecorationRole] = QLatin1String("decoration" ); |
149 | names[Qt::EditRole] = QLatin1String("edit" ); |
150 | names[Qt::ToolTipRole] = QLatin1String("toolTip" ); |
151 | names[Qt::StatusTipRole] = QLatin1String("statusTip" ); |
152 | names[Qt::WhatsThisRole] = QLatin1String("whatsThis" ); |
153 | names[Qt::FontRole] = QLatin1String("font" ); |
154 | names[Qt::TextAlignmentRole] = QLatin1String("textAlignment" ); |
155 | names[Qt::BackgroundRole] = QLatin1String("background" ); |
156 | names[Qt::ForegroundRole] = QLatin1String("foreground" ); |
157 | names[Qt::CheckStateRole] = QLatin1String("checkState" ); |
158 | names[Qt::AccessibleTextRole] = QLatin1String("accessibleText" ); |
159 | names[Qt::AccessibleDescriptionRole] = QLatin1String("accessibleDescription" ); |
160 | names[Qt::SizeHintRole] = QLatin1String("sizeHint" ); |
161 | return names; |
162 | } |
163 | |
164 | QT_END_NAMESPACE |
165 | |
166 | #include "moc_qqmltablemodelcolumn_p.cpp" |
167 | |