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//! \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
25static const QString displayRoleName = QStringLiteral("display");
26static const QString decorationRoleName = QStringLiteral("decoration");
27static const QString editRoleName = QStringLiteral("edit");
28static const QString toolTipRoleName = QStringLiteral("toolTip");
29static const QString statusTipRoleName = QStringLiteral("statusTip");
30static const QString whatsThisRoleName = QStringLiteral("whatsThis");
31
32static const QString fontRoleName = QStringLiteral("font");
33static const QString textAlignmentRoleName = QStringLiteral("textAlignment");
34static const QString backgroundRoleName = QStringLiteral("background");
35static const QString foregroundRoleName = QStringLiteral("foreground");
36static const QString checkStateRoleName = QStringLiteral("checkState");
37
38static const QString accessibleTextRoleName = QStringLiteral("accessibleText");
39static const QString accessibleDescriptionRoleName = QStringLiteral("accessibleDescription");
40
41static const QString sizeHintRoleName = QStringLiteral("sizeHint");
42
43
44QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent)
45 : QObject(parent)
46{
47}
48
49QQmlTableModelColumn::~QQmlTableModelColumn()
50{
51}
52
53#define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, setterGetterName, setterSetterName, setterSignal, roleName) \
54QJSValue QQmlTableModelColumn::getterGetterName() const \
55{ \
56 return mGetters.value(roleName); \
57} \
58\
59void 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\
72QJSValue QQmlTableModelColumn::setterGetterName() const \
73{ \
74 return mSetters.value(roleName); \
75} \
76\
77void 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
91DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged,
92 getSetDisplay, setSetDisplay, setDisplayChanged, displayRoleName)
93DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged,
94 getSetDecoration, setSetDecoration, setDecorationChanged, decorationRoleName)
95DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged,
96 getSetEdit, setSetEdit, setEditChanged, editRoleName)
97DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged,
98 getSetToolTip, setSetToolTip, setToolTipChanged, toolTipRoleName)
99DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged,
100 getSetStatusTip, setSetStatusTip, setStatusTipChanged, statusTipRoleName)
101DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged,
102 getSetWhatsThis, setSetWhatsThis, setWhatsThisChanged, whatsThisRoleName)
103
104DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged,
105 getSetFont, setSetFont, setFontChanged, fontRoleName)
106DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged,
107 getSetTextAlignment, setSetTextAlignment, setTextAlignmentChanged, textAlignmentRoleName)
108DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged,
109 getSetBackground, setSetBackground, setBackgroundChanged, backgroundRoleName)
110DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged,
111 getSetForeground, setSetForeground, setForegroundChanged, foregroundRoleName)
112DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged,
113 getSetCheckState, setSetCheckState, setCheckStateChanged, checkStateRoleName)
114
115DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged,
116 getSetAccessibleText, setSetAccessibleText, setAccessibleTextChanged, accessibleTextRoleName)
117DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged,
118 getSetAccessibleDescription, setSetAccessibleDescription, setAccessibleDescriptionChanged, accessibleDescriptionRoleName)
119
120DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged,
121 getSetSizeHint, setSetSizeHint, setSizeHintChanged, sizeHintRoleName)
122
123QJSValue 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
131QJSValue 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
139const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const
140{
141 return mGetters;
142}
143
144const 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
164QT_END_NAMESPACE
165
166#include "moc_qqmltablemodelcolumn_p.cpp"
167

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