1// Copyright (C) 2018 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 "qqmldelegatecomponent_p.h"
5#include <QtQmlModels/private/qqmladaptormodel_p.h>
6
7QT_BEGIN_NAMESPACE
8
9/*!
10 \qmltype DelegateChoice
11//! \nativetype QQmlDelegateChoice
12 \inqmlmodule QtQml.Models
13 \since 6.9
14 \brief Encapsulates a delegate and when to use it.
15
16 The DelegateChoice type wraps a delegate and defines the circumstances
17 in which it should be chosen.
18
19 DelegateChoices can be nested inside a DelegateChooser.
20
21 \sa DelegateChooser
22*/
23
24/*!
25 \qmlproperty variant QtQml.Models::DelegateChoice::roleValue
26 This property holds the value used to match the role data for the role provided by \l DelegateChooser::role.
27*/
28QVariant QQmlDelegateChoice::roleValue() const
29{
30 return m_value;
31}
32
33void QQmlDelegateChoice::setRoleValue(const QVariant &value)
34{
35 if (m_value == value)
36 return;
37 m_value = value;
38 emit roleValueChanged();
39 emit changed();
40}
41
42/*!
43 \qmlproperty int QtQml.Models::DelegateChoice::row
44 This property holds the value used to match the row value of model elements.
45 With models that have only the index property (and thus only one column), this property
46 should be intended as an index, and set to the desired index value.
47
48 \note Setting both row and index has undefined behavior. The two are equivalent and only
49 one should be used.
50
51 \sa index
52*/
53
54/*!
55 \qmlproperty int QtQml.Models::DelegateChoice::index
56 This property holds the value used to match the index value of model elements.
57 This is effectively an alias for \l row.
58
59 \sa row
60*/
61int QQmlDelegateChoice::row() const
62{
63 return m_row;
64}
65
66void QQmlDelegateChoice::setRow(int r)
67{
68 if (m_row == r)
69 return;
70 m_row = r;
71 emit rowChanged();
72 emit indexChanged();
73 emit changed();
74}
75
76/*!
77 \qmlproperty int QtQml.Models::DelegateChoice::column
78 This property holds the value used to match the column value of model elements.
79*/
80int QQmlDelegateChoice::column() const
81{
82 return m_column;
83}
84
85void QQmlDelegateChoice::setColumn(int c)
86{
87 if (m_column == c)
88 return;
89 m_column = c;
90 emit columnChanged();
91 emit changed();
92}
93
94QQmlComponent *QQmlDelegateChoice::delegate() const
95{
96 return m_delegate;
97}
98
99/*!
100 \qmlproperty Component QtQml.Models::DelegateChoice::delegate
101 This property holds the delegate to use if this choice matches the model item.
102*/
103void QQmlDelegateChoice::setDelegate(QQmlComponent *delegate)
104{
105 if (m_delegate == delegate)
106 return;
107 QQmlAbstractDelegateComponent *adc = static_cast<QQmlAbstractDelegateComponent *>(m_delegate);
108 if (adc)
109 disconnect(sender: adc, signal: &QQmlAbstractDelegateComponent::delegateChanged, receiver: this, slot: &QQmlDelegateChoice::delegateChanged);
110 m_delegate = delegate;
111 adc = static_cast<QQmlAbstractDelegateComponent *>(delegate);
112 if (adc)
113 connect(sender: adc, signal: &QQmlAbstractDelegateComponent::delegateChanged, context: this, slot: &QQmlDelegateChoice::delegateChanged);
114 emit delegateChanged();
115 emit changed();
116}
117
118bool QQmlDelegateChoice::match(int row, int column, const QVariant &value) const
119{
120 if (!m_value.isValid() && m_row < 0 && m_column < 0)
121 return true;
122
123 bool roleMatched = true;
124 if (m_value.isValid()) {
125 roleMatched = (value == m_value);
126 if (!roleMatched) {
127 bool valueOk = false;
128 bool mValueOk = false;
129 roleMatched = (value.toInt(ok: &valueOk) == m_value.toInt(ok: &mValueOk) && valueOk && mValueOk);
130 }
131 if (!roleMatched)
132 roleMatched = (value.toString() == m_value.toString());
133 }
134 const bool rowMatched = (m_row < 0 ) ? true : m_row == row;
135 const bool columnMatched = (m_column < 0 ) ? true : m_column == column;
136 return roleMatched && rowMatched && columnMatched;
137}
138
139/*!
140 \qmltype DelegateChooser
141//! \nativetype QQmlDelegateChooser
142 \inqmlmodule QtQml.Models
143 \since 6.9
144 \brief Allows a view to use different delegates for different types of items in the model.
145
146 The DelegateChooser is a special \l Component type intended for those scenarios where a
147 Component is required by a view and used as a delegate. DelegateChooser encapsulates a set of \l
148 {DelegateChoice}s. These choices are used to determine the delegate that will be instantiated for
149 each item in the model. The selection of the choice is performed based on the value that a model
150 item has for \l role, and also based on index.
151
152 DelegateChooser is commonly used when a view needs to display a set of delegates that are
153 significantly different from each other. For example, a typical phone settings view might include
154 toggle switches, sliders, radio buttons, and other visualizations based on the type of each
155 setting. In this case, DelegateChooser could provide an easy way to associate a different type of
156 delegate with each setting:
157
158 \qml
159 import QtQml.Models
160 import QtQuick
161 import QtQuick.Controls
162
163 ListView {
164 width: 200; height: 400
165
166 ListModel {
167 id: listModel
168 ListElement { type: "info"; ... }
169 ListElement { type: "switch"; ... }
170 ListElement { type: "swipe"; ... }
171 ListElement { type: "switch"; ... }
172 }
173
174 DelegateChooser {
175 id: chooser
176 role: "type"
177 DelegateChoice { roleValue: "info"; ItemDelegate { ... } }
178 DelegateChoice { roleValue: "switch"; SwitchDelegate { ... } }
179 DelegateChoice { roleValue: "swipe"; SwipeDelegate { ... } }
180 }
181
182 model: listModel
183 delegate: chooser
184 }
185 \endqml
186
187 \note This type is intended to transparently work only with TableView and any
188 DelegateModel-based view. Views (including user-defined views) that aren't internally based on a
189 DelegateModel need to explicitly support this type of component to make it function as described.
190
191 \sa DelegateChoice
192*/
193
194/*!
195 \qmlproperty string QtQml.Models::DelegateChooser::role
196 This property holds the role or the property name used to determine the delegate for a given model item.
197
198 \note For \l{QAbstractItemModel} based models, including \l{ListModel}, the DelegateChooser will
199 reevaluate the choice when the model signals that the role has changed. For any other type of model,
200 this choice will only be done once when the item for a given model index is created.
201
202 \sa DelegateChoice
203*/
204void QQmlDelegateChooser::setRole(const QString &role)
205{
206 if (m_role == role)
207 return;
208 m_role = role;
209 emit roleChanged();
210}
211
212/*!
213 \qmlproperty list<DelegateChoice> QtQml.Models::DelegateChooser::choices
214 \qmldefault
215
216 The list of DelegateChoices for the chooser.
217
218 The list is treated as an ordered list, where the first DelegateChoice to match
219 will be used be a view.
220
221 It should not generally be necessary to refer to the \c choices property,
222 as it is the default property for DelegateChooser and thus all child items are
223 automatically assigned to this property.
224*/
225
226QQmlListProperty<QQmlDelegateChoice> QQmlDelegateChooser::choices()
227{
228 return QQmlListProperty<QQmlDelegateChoice>(this, nullptr,
229 QQmlDelegateChooser::choices_append,
230 QQmlDelegateChooser::choices_count,
231 QQmlDelegateChooser::choices_at,
232 QQmlDelegateChooser::choices_clear,
233 QQmlDelegateChooser::choices_replace,
234 QQmlDelegateChooser::choices_removeLast);
235}
236
237void QQmlDelegateChooser::choices_append(QQmlListProperty<QQmlDelegateChoice> *prop, QQmlDelegateChoice *choice)
238{
239 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
240 q->m_choices.append(t: choice);
241 connect(sender: choice, signal: &QQmlDelegateChoice::changed, context: q, slot: &QQmlAbstractDelegateComponent::delegateChanged);
242 emit q->delegateChanged();
243}
244
245qsizetype QQmlDelegateChooser::choices_count(QQmlListProperty<QQmlDelegateChoice> *prop)
246{
247 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser*>(prop->object);
248 return q->m_choices.size();
249}
250
251QQmlDelegateChoice *QQmlDelegateChooser::choices_at(QQmlListProperty<QQmlDelegateChoice> *prop, qsizetype index)
252{
253 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser*>(prop->object);
254 return q->m_choices.at(i: index);
255}
256
257void QQmlDelegateChooser::choices_clear(QQmlListProperty<QQmlDelegateChoice> *prop)
258{
259 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
260 for (QQmlDelegateChoice *choice : q->m_choices)
261 disconnect(sender: choice, signal: &QQmlDelegateChoice::changed, receiver: q, slot: &QQmlAbstractDelegateComponent::delegateChanged);
262 q->m_choices.clear();
263 emit q->delegateChanged();
264}
265
266void QQmlDelegateChooser::choices_replace(QQmlListProperty<QQmlDelegateChoice> *prop,
267 qsizetype index, QQmlDelegateChoice *choice)
268{
269 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
270 disconnect(sender: q->m_choices[index], signal: &QQmlDelegateChoice::changed,
271 receiver: q, slot: &QQmlAbstractDelegateComponent::delegateChanged);
272 q->m_choices[index] = choice;
273 connect(sender: choice, signal: &QQmlDelegateChoice::changed, context: q,
274 slot: &QQmlAbstractDelegateComponent::delegateChanged);
275 emit q->delegateChanged();
276}
277
278void QQmlDelegateChooser::choices_removeLast(QQmlListProperty<QQmlDelegateChoice> *prop)
279{
280 QQmlDelegateChooser *q = static_cast<QQmlDelegateChooser *>(prop->object);
281 disconnect(sender: q->m_choices.takeLast(), signal: &QQmlDelegateChoice::changed,
282 receiver: q, slot: &QQmlAbstractDelegateComponent::delegateChanged);
283 emit q->delegateChanged();
284}
285
286QQmlComponent *QQmlDelegateChooser::delegate(QQmlAdaptorModel *adaptorModel, int row, int column) const
287{
288 QVariant v;
289 if (!m_role.isNull())
290 v = value(adaptorModel, row, column, role: m_role);
291 if (!v.isValid()) { // check if the row only has modelData, for example if the row is a QVariantMap
292 v = value(adaptorModel, row, column, QStringLiteral("modelData"));
293
294 if (v.isValid()) {
295 if (v.canConvert(targetType: QMetaType(QMetaType::QVariantMap)))
296 v = v.toMap().value(key: m_role);
297 else if (v.canConvert(targetType: QMetaType(QMetaType::QObjectStar)))
298 v = v.value<QObject*>()->property(name: m_role.toUtf8());
299 }
300 }
301
302 // loop through choices, finding first one that fits
303 for (int i = 0; i < m_choices.size(); ++i) {
304 const QQmlDelegateChoice *choice = m_choices.at(i);
305 if (choice->match(row, column, value: v))
306 return choice->delegate();
307 }
308
309 return nullptr;
310}
311
312QT_END_NAMESPACE
313
314#include "moc_qqmldelegatecomponent_p.cpp"
315

source code of qtdeclarative/src/qmlmodels/qqmldelegatecomponent.cpp