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
8QT_BEGIN_NAMESPACE
9
10/*!
11 \qmltype TableModelColumn
12//! \nativetype 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 Roles can be accessed by as listed below, e.g.
22 \code
23 text: display
24
25 required property string display
26 \endcode
27
28 \table
29 \row \li Qt::DisplayRole \li display
30 \row \li Qt::DecorationRole \li decoration
31 \row \li Qt::EditRole \li edit
32 \row \li Qt::ToolTipRole \li toolTip
33 \row \li Qt::StatusTipRole \li statusTip
34 \row \li Qt::WhatsThisRole \li whatsThis
35 \row \li Qt::FontRole \li font
36 \row \li Qt::TextAlignmentRole \li textAlignment
37 \row \li Qt::BackgroundRole \li background
38 \row \li Qt::ForegroundRole \li foreground
39 \row \li Qt::CheckStateRole \li checkState
40 \row \li Qt::AccessibleTextRole \li accessibleText
41 \row \li Qt::AccessibleDescriptionRole \li accessibleDescription
42 \row \li Qt::SizeHintRole \li sizeHintRoleNam
43 \endtable
44
45 \sa TableModel, TableView
46*/
47
48static constexpr QLatin1StringView displayRoleName("display");
49static constexpr QLatin1StringView decorationRoleName("decoration");
50static constexpr QLatin1StringView editRoleName("edit");
51static constexpr QLatin1StringView toolTipRoleName("toolTip");
52static constexpr QLatin1StringView statusTipRoleName("statusTip");
53static constexpr QLatin1StringView whatsThisRoleName("whatsThis");
54
55static constexpr QLatin1StringView fontRoleName("font");
56static constexpr QLatin1StringView textAlignmentRoleName("textAlignment");
57static constexpr QLatin1StringView backgroundRoleName("background");
58static constexpr QLatin1StringView foregroundRoleName("foreground");
59static constexpr QLatin1StringView checkStateRoleName("checkState");
60
61static constexpr QLatin1StringView accessibleTextRoleName("accessibleText");
62static constexpr QLatin1StringView accessibleDescriptionRoleName("accessibleDescription");
63
64static constexpr QLatin1StringView sizeHintRoleName("sizeHint");
65
66
67QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent)
68 : QObject(parent)
69{
70}
71
72QQmlTableModelColumn::~QQmlTableModelColumn()
73{
74}
75
76#define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, setterGetterName, setterSetterName, setterSignal, roleName) \
77QJSValue QQmlTableModelColumn::getterGetterName() const \
78{ \
79 return mGetters.value(roleName); \
80} \
81\
82void QQmlTableModelColumn::getterSetterName(const QJSValue &stringOrFunction) \
83{ \
84 if (!stringOrFunction.isString() && !stringOrFunction.isCallable()) { \
85 qmlWarning(this).quote() << "getter for " << roleName << " must be a function"; \
86 return; \
87 } \
88 if (stringOrFunction.strictlyEquals(decoration())) \
89 return; \
90\
91 mGetters[roleName] = stringOrFunction; \
92 emit decorationChanged(); \
93} \
94\
95QJSValue QQmlTableModelColumn::setterGetterName() const \
96{ \
97 return mSetters.value(roleName); \
98} \
99\
100void QQmlTableModelColumn::setterSetterName(const QJSValue &function) \
101{ \
102 if (!function.isCallable()) { \
103 qmlWarning(this).quote() << "setter for " << roleName << " must be a function"; \
104 return; \
105 } \
106\
107 if (function.strictlyEquals(getSetDisplay())) \
108 return; \
109\
110 mSetters[roleName] = function; \
111 emit setDisplayChanged(); \
112}
113
114DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged,
115 getSetDisplay, setSetDisplay, setDisplayChanged, displayRoleName)
116DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged,
117 getSetDecoration, setSetDecoration, setDecorationChanged, decorationRoleName)
118DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged,
119 getSetEdit, setSetEdit, setEditChanged, editRoleName)
120DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged,
121 getSetToolTip, setSetToolTip, setToolTipChanged, toolTipRoleName)
122DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged,
123 getSetStatusTip, setSetStatusTip, setStatusTipChanged, statusTipRoleName)
124DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged,
125 getSetWhatsThis, setSetWhatsThis, setWhatsThisChanged, whatsThisRoleName)
126
127DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged,
128 getSetFont, setSetFont, setFontChanged, fontRoleName)
129DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged,
130 getSetTextAlignment, setSetTextAlignment, setTextAlignmentChanged, textAlignmentRoleName)
131DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged,
132 getSetBackground, setSetBackground, setBackgroundChanged, backgroundRoleName)
133DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged,
134 getSetForeground, setSetForeground, setForegroundChanged, foregroundRoleName)
135DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged,
136 getSetCheckState, setSetCheckState, setCheckStateChanged, checkStateRoleName)
137
138DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged,
139 getSetAccessibleText, setSetAccessibleText, setAccessibleTextChanged, accessibleTextRoleName)
140DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged,
141 getSetAccessibleDescription, setSetAccessibleDescription, setAccessibleDescriptionChanged, accessibleDescriptionRoleName)
142
143DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged,
144 getSetSizeHint, setSetSizeHint, setSizeHintChanged, sizeHintRoleName)
145
146QJSValue QQmlTableModelColumn::getterAtRole(const QString &roleName)
147{
148 auto it = mGetters.find(key: roleName);
149 if (it == mGetters.end())
150 return QJSValue();
151 return *it;
152}
153
154QJSValue QQmlTableModelColumn::setterAtRole(const QString &roleName)
155{
156 auto it = mSetters.find(key: roleName);
157 if (it == mSetters.end())
158 return QJSValue();
159 return *it;
160}
161
162const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const
163{
164 return mGetters;
165}
166
167const QHash<int, QString> QQmlTableModelColumn::supportedRoleNames()
168{
169 static const QHash<int, QString> names {
170 {Qt::DisplayRole, displayRoleName},
171 {Qt::DecorationRole, decorationRoleName},
172 {Qt::EditRole, editRoleName},
173 {Qt::ToolTipRole, toolTipRoleName},
174 {Qt::StatusTipRole, statusTipRoleName},
175 {Qt::WhatsThisRole, whatsThisRoleName},
176 {Qt::FontRole, fontRoleName},
177 {Qt::TextAlignmentRole, textAlignmentRoleName},
178 {Qt::BackgroundRole, backgroundRoleName},
179 {Qt::ForegroundRole, foregroundRoleName},
180 {Qt::CheckStateRole, checkStateRoleName},
181 {Qt::AccessibleTextRole, accessibleTextRoleName},
182 {Qt::AccessibleDescriptionRole, accessibleDescriptionRoleName},
183 {Qt::SizeHintRole, sizeHintRoleName}
184 };
185 return names;
186}
187
188QT_END_NAMESPACE
189
190#include "moc_qqmltablemodelcolumn_p.cpp"
191

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtdeclarative/src/labs/models/qqmltablemodelcolumn.cpp