1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qqmltablemodelcolumn_p.h"
41
42 #include <QtQml/qqmlinfo.h>
43
44 QT_BEGIN_NAMESPACE
45
46 /*!
47 \qmltype TableModelColumn
48 \instantiates QQmlTableModelColumn
49 \inqmlmodule Qt.labs.qmlmodels
50 \brief Represents a column in a model.
51 \since 5.14
52
53 \section1 Supported Roles
54
55 TableModelColumn supports all of \l {Qt::ItemDataRole}{Qt's roles},
56 with the exception of \c Qt::InitialSortOrderRole.
57
58 \sa TableModel, TableView
59 */
60
61 static const QString displayRoleName = QStringLiteral("display");
62 static const QString decorationRoleName = QStringLiteral("decoration");
63 static const QString editRoleName = QStringLiteral("edit");
64 static const QString toolTipRoleName = QStringLiteral("toolTip");
65 static const QString statusTipRoleName = QStringLiteral("statusTip");
66 static const QString whatsThisRoleName = QStringLiteral("whatsThis");
67
68 static const QString fontRoleName = QStringLiteral("font");
69 static const QString textAlignmentRoleName = QStringLiteral("textAlignment");
70 static const QString backgroundRoleName = QStringLiteral("background");
71 static const QString foregroundRoleName = QStringLiteral("foreground");
72 static const QString checkStateRoleName = QStringLiteral("checkState");
73
74 static const QString accessibleTextRoleName = QStringLiteral("accessibleText");
75 static const QString accessibleDescriptionRoleName = QStringLiteral("accessibleDescription");
76
77 static const QString sizeHintRoleName = QStringLiteral("sizeHint");
78
79
QQmlTableModelColumn(QObject * parent)80 QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent)
81 : QObject(parent)
82 {
83 }
84
~QQmlTableModelColumn()85 QQmlTableModelColumn::~QQmlTableModelColumn()
86 {
87 }
88
89 #define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, setterGetterName, setterSetterName, setterSignal, roleName) \
90 QJSValue QQmlTableModelColumn::getterGetterName() const \
91 { \
92 return mGetters.value(roleName); \
93 } \
94 \
95 void QQmlTableModelColumn::getterSetterName(const QJSValue &stringOrFunction) \
96 { \
97 if (!stringOrFunction.isString() && !stringOrFunction.isCallable()) { \
98 qmlWarning(this).quote() << "getter for " << roleName << " must be a function"; \
99 return; \
100 } \
101 if (stringOrFunction.strictlyEquals(decoration())) \
102 return; \
103 \
104 mGetters[roleName] = stringOrFunction; \
105 emit decorationChanged(); \
106 } \
107 \
108 QJSValue QQmlTableModelColumn::setterGetterName() const \
109 { \
110 return mSetters.value(roleName); \
111 } \
112 \
113 void QQmlTableModelColumn::setterSetterName(const QJSValue &function) \
114 { \
115 if (!function.isCallable()) { \
116 qmlWarning(this).quote() << "setter for " << roleName << " must be a function"; \
117 return; \
118 } \
119 \
120 if (function.strictlyEquals(getSetDisplay())) \
121 return; \
122 \
123 mSetters[roleName] = function; \
124 emit setDisplayChanged(); \
125 }
126
DEFINE_ROLE_PROPERTIES(display,setDisplay,displayChanged,getSetDisplay,setSetDisplay,setDisplayChanged,displayRoleName)127 DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged,
128 getSetDisplay, setSetDisplay, setDisplayChanged, displayRoleName)
129 DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged,
130 getSetDecoration, setSetDecoration, setDecorationChanged, decorationRoleName)
131 DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged,
132 getSetEdit, setSetEdit, setEditChanged, editRoleName)
133 DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged,
134 getSetToolTip, setSetToolTip, setToolTipChanged, toolTipRoleName)
135 DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged,
136 getSetStatusTip, setSetStatusTip, setStatusTipChanged, statusTipRoleName)
137 DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged,
138 getSetWhatsThis, setSetWhatsThis, setWhatsThisChanged, whatsThisRoleName)
139
140 DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged,
141 getSetFont, setSetFont, setFontChanged, fontRoleName)
142 DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged,
143 getSetTextAlignment, setSetTextAlignment, setTextAlignmentChanged, textAlignmentRoleName)
144 DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged,
145 getSetBackground, setSetBackground, setBackgroundChanged, backgroundRoleName)
146 DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged,
147 getSetForeground, setSetForeground, setForegroundChanged, foregroundRoleName)
148 DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged,
149 getSetCheckState, setSetCheckState, setCheckStateChanged, checkStateRoleName)
150
151 DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged,
152 getSetAccessibleText, setSetAccessibleText, setAccessibleTextChanged, accessibleTextRoleName)
153 DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged,
154 getSetAccessibleDescription, setSetAccessibleDescription, setAccessibleDescriptionChanged, accessibleDescriptionRoleName)
155
156 DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged,
157 getSetSizeHint, setSetSizeHint, setSizeHintChanged, sizeHintRoleName)
158
159 QJSValue QQmlTableModelColumn::getterAtRole(const QString &roleName)
160 {
161 auto it = mGetters.find(roleName);
162 if (it == mGetters.end())
163 return QJSValue();
164 return *it;
165 }
166
setterAtRole(const QString & roleName)167 QJSValue QQmlTableModelColumn::setterAtRole(const QString &roleName)
168 {
169 auto it = mSetters.find(roleName);
170 if (it == mSetters.end())
171 return QJSValue();
172 return *it;
173 }
174
getters() const175 const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const
176 {
177 return mGetters;
178 }
179
supportedRoleNames()180 const QHash<int, QString> QQmlTableModelColumn::supportedRoleNames()
181 {
182 QHash<int, QString> names;
183 names[Qt::DisplayRole] = QLatin1String("display");
184 names[Qt::DecorationRole] = QLatin1String("decoration");
185 names[Qt::EditRole] = QLatin1String("edit");
186 names[Qt::ToolTipRole] = QLatin1String("toolTip");
187 names[Qt::StatusTipRole] = QLatin1String("statusTip");
188 names[Qt::WhatsThisRole] = QLatin1String("whatsThis");
189 names[Qt::FontRole] = QLatin1String("font");
190 names[Qt::TextAlignmentRole] = QLatin1String("textAlignment");
191 names[Qt::BackgroundRole] = QLatin1String("background");
192 names[Qt::ForegroundRole] = QLatin1String("foreground");
193 names[Qt::CheckStateRole] = QLatin1String("checkState");
194 names[Qt::AccessibleTextRole] = QLatin1String("accessibleText");
195 names[Qt::AccessibleDescriptionRole] = QLatin1String("accessibleDescription");
196 names[Qt::SizeHintRole] = QLatin1String("sizeHint");
197 return names;
198 }
199
200 QT_END_NAMESPACE
201