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
44QT_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
61static const QString displayRoleName = QStringLiteral("display");
62static const QString decorationRoleName = QStringLiteral("decoration");
63static const QString editRoleName = QStringLiteral("edit");
64static const QString toolTipRoleName = QStringLiteral("toolTip");
65static const QString statusTipRoleName = QStringLiteral("statusTip");
66static const QString whatsThisRoleName = QStringLiteral("whatsThis");
67
68static const QString fontRoleName = QStringLiteral("font");
69static const QString textAlignmentRoleName = QStringLiteral("textAlignment");
70static const QString backgroundRoleName = QStringLiteral("background");
71static const QString foregroundRoleName = QStringLiteral("foreground");
72static const QString checkStateRoleName = QStringLiteral("checkState");
73
74static const QString accessibleTextRoleName = QStringLiteral("accessibleText");
75static const QString accessibleDescriptionRoleName = QStringLiteral("accessibleDescription");
76
77static const QString sizeHintRoleName = QStringLiteral("sizeHint");
78
79
80QQmlTableModelColumn::QQmlTableModelColumn(QObject *parent)
81 : QObject(parent)
82{
83}
84
85QQmlTableModelColumn::~QQmlTableModelColumn()
86{
87}
88
89#define DEFINE_ROLE_PROPERTIES(getterGetterName, getterSetterName, getterSignal, setterGetterName, setterSetterName, setterSignal, roleName) \
90QJSValue QQmlTableModelColumn::getterGetterName() const \
91{ \
92 return mGetters.value(roleName); \
93} \
94\
95void 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\
108QJSValue QQmlTableModelColumn::setterGetterName() const \
109{ \
110 return mSetters.value(roleName); \
111} \
112\
113void 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
127DEFINE_ROLE_PROPERTIES(display, setDisplay, displayChanged,
128 getSetDisplay, setSetDisplay, setDisplayChanged, displayRoleName)
129DEFINE_ROLE_PROPERTIES(decoration, setDecoration, decorationChanged,
130 getSetDecoration, setSetDecoration, setDecorationChanged, decorationRoleName)
131DEFINE_ROLE_PROPERTIES(edit, setEdit, editChanged,
132 getSetEdit, setSetEdit, setEditChanged, editRoleName)
133DEFINE_ROLE_PROPERTIES(toolTip, setToolTip, toolTipChanged,
134 getSetToolTip, setSetToolTip, setToolTipChanged, toolTipRoleName)
135DEFINE_ROLE_PROPERTIES(statusTip, setStatusTip, statusTipChanged,
136 getSetStatusTip, setSetStatusTip, setStatusTipChanged, statusTipRoleName)
137DEFINE_ROLE_PROPERTIES(whatsThis, setWhatsThis, whatsThisChanged,
138 getSetWhatsThis, setSetWhatsThis, setWhatsThisChanged, whatsThisRoleName)
139
140DEFINE_ROLE_PROPERTIES(font, setFont, fontChanged,
141 getSetFont, setSetFont, setFontChanged, fontRoleName)
142DEFINE_ROLE_PROPERTIES(textAlignment, setTextAlignment, textAlignmentChanged,
143 getSetTextAlignment, setSetTextAlignment, setTextAlignmentChanged, textAlignmentRoleName)
144DEFINE_ROLE_PROPERTIES(background, setBackground, backgroundChanged,
145 getSetBackground, setSetBackground, setBackgroundChanged, backgroundRoleName)
146DEFINE_ROLE_PROPERTIES(foreground, setForeground, foregroundChanged,
147 getSetForeground, setSetForeground, setForegroundChanged, foregroundRoleName)
148DEFINE_ROLE_PROPERTIES(checkState, setCheckState, checkStateChanged,
149 getSetCheckState, setSetCheckState, setCheckStateChanged, checkStateRoleName)
150
151DEFINE_ROLE_PROPERTIES(accessibleText, setAccessibleText, accessibleTextChanged,
152 getSetAccessibleText, setSetAccessibleText, setAccessibleTextChanged, accessibleTextRoleName)
153DEFINE_ROLE_PROPERTIES(accessibleDescription, setAccessibleDescription, accessibleDescriptionChanged,
154 getSetAccessibleDescription, setSetAccessibleDescription, setAccessibleDescriptionChanged, accessibleDescriptionRoleName)
155
156DEFINE_ROLE_PROPERTIES(sizeHint, setSizeHint, sizeHintChanged,
157 getSetSizeHint, setSetSizeHint, setSizeHintChanged, sizeHintRoleName)
158
159QJSValue QQmlTableModelColumn::getterAtRole(const QString &roleName)
160{
161 auto it = mGetters.find(akey: roleName);
162 if (it == mGetters.end())
163 return QJSValue();
164 return *it;
165}
166
167QJSValue QQmlTableModelColumn::setterAtRole(const QString &roleName)
168{
169 auto it = mSetters.find(akey: roleName);
170 if (it == mSetters.end())
171 return QJSValue();
172 return *it;
173}
174
175const QHash<QString, QJSValue> QQmlTableModelColumn::getters() const
176{
177 return mGetters;
178}
179
180const 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
200QT_END_NAMESPACE
201

source code of qtdeclarative/src/imports/labsmodels/qqmltablemodelcolumn.cpp