Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qrangemodel_impl.h
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:significant reason:default
4
5#ifndef QRANGEMODEL_IMPL_H
6#define QRANGEMODEL_IMPL_H
7
8#ifndef Q_QDOC
9
10#ifndef QRANGEMODEL_H
11#error Do not include qrangemodel_impl.h directly
12#endif
13
14#if 0
15#pragma qt_sync_skip_header_check
16#pragma qt_sync_stop_processing
17#endif
18
19#include <QtCore/qabstractitemmodel.h>
20#include <QtCore/qcollator.h>
21#include <QtCore/qquasivirtual_impl.h>
22#include <QtCore/qmetaobject.h>
23#include <QtCore/qvariant.h>
24#include <QtCore/qmap.h>
25#include <QtCore/qscopedvaluerollback.h>
26#include <QtCore/qset.h>
27#include <QtCore/qregularexpression.h>
28#include <QtCore/qvarlengtharray.h>
29
30#include <algorithm>
31#include <functional>
32#include <iterator>
33#include <type_traits>
34#include <QtCore/qxptype_traits.h>
35#include <tuple>
36#include <QtCore/q23utility.h>
37
38QT_BEGIN_NAMESPACE
39
40namespace QRangeModelDetails
41{
42 template <typename T, template <typename...> typename... Templates>
44
45 template <template <typename...> typename Template,
46 typename... Params,
47 template <typename...> typename... Templates>
49
50 template <typename T,
51 template <typename...> typename Template,
52 template <typename...> typename... Templates>
54
55 template <typename T, template <typename...> typename... Templates>
57
58 template <typename T, typename = void>
60
61 template <typename T>
62 struct is_validatable<T, std::void_t<decltype(*std::declval<T>())>>
63 : std::is_constructible<bool, T> {};
64
65 template <typename T, typename = void>
67
68 template <typename T>
71 std::is_pointer<decltype(std::declval<T&>().get())>,
72 std::is_same<decltype(*std::declval<T&>().get()), decltype(*std::declval<T&>())>,
74 >>>
75 : std::true_type
76 {};
77
78 // TODO: shouldn't we check is_smart_ptr && !is_copy_constructible && !is_copy_assignable
79 // to support users-specific ptrs?
80 template <typename T>
82#ifndef QT_NO_SCOPED_POINTER
84#endif
86 >;
87
88 template <typename T>
91
92 template <typename T>
94
95 template <typename T>
97
98 template <typename T>
99 static auto pointerTo(T&& t) {
100 using Type = q20::remove_cvref_t<T>;
101 if constexpr (is_any_of<Type, std::optional>())
102 return t ? std::addressof(*std::forward<T>(t)) : nullptr;
103 else if constexpr (std::is_pointer<Type>())
104 return t;
105 else if constexpr (is_smart_ptr<Type>())
106 return t.get();
107 else if constexpr (is_any_of<Type, std::reference_wrapper>())
108 return std::addressof(t.get());
109 else
110 return std::addressof(std::forward<T>(t));
111 }
112
113 template <typename T>
115 {
117 };
118 template <>
119 struct wrapped_helper<void>
120 {
121 using type = void;
122 };
123 template <typename T>
125
126 template <typename T>
129 >>;
130
131 template <typename T, typename = void>
133 template <typename T, std::size_t N>
134 struct tuple_like<std::array<T, N>> : std::false_type {};
135 template <typename T>
138 template <typename T>
139 [[maybe_unused]] static constexpr bool tuple_like_v = tuple_like<T>::value;
140
141 template <typename T, typename = void>
143 template <typename T, std::size_t N>
144 struct array_like<std::array<T, N>> : std::true_type {};
145 template <typename T, std::size_t N>
146 struct array_like<T[N]> : std::true_type {};
147 template <typename T>
148 [[maybe_unused]] static constexpr bool array_like_v = array_like<T>::value;
149
150 template <typename T, typename = void>
152 template <typename T>
155 template <typename T>
156 [[maybe_unused]] static constexpr bool has_metaobject_v = has_metaobject<T>::value;
157
158 template <typename T>
159 static constexpr bool isValid(const T &t) noexcept
160 {
161 if constexpr (std::is_array_v<T>)
162 return true;
163 else if constexpr (is_validatable<T>())
164 return bool(t);
165 else
166 return true;
167 }
168
169 template <typename T>
170 static decltype(auto) refTo(T&& t) {
171 Q_ASSERT(QRangeModelDetails::isValid(t));
172 // it's allowed to move only if the object holds unique ownership of the wrapped data
173 using Type = q20::remove_cvref_t<T>;
174 if constexpr (is_any_of<T, std::optional>())
175 return *std::forward<T>(t); // let std::optional resolve dereferencing
176 if constexpr (!is_wrapped<Type>() || is_any_unique_ptr<Type>())
177 return q23::forward_like<T>(*QRangeModelDetails::pointerTo(t));
178 else
179 return *QRangeModelDetails::pointerTo(t);
180 }
181
182 template <typename It>
183 auto key(It&& it) -> decltype(it.key()) { return std::forward<It>(it).key(); }
184 template <typename It>
185 auto key(It&& it) -> decltype((it->first)) { return std::forward<It>(it)->first; }
186
187 template <typename It>
188 auto value(It&& it) -> decltype(it.value()) { return std::forward<It>(it).value(); }
189 template <typename It>
190 auto value(It&& it) -> decltype((it->second)) { return std::forward<It>(it)->second; }
191
192 // use our own, ADL friendly versions of begin/end so that we can overload
193 // for pointers.
194 using std::begin;
195 using std::end;
196 template <typename C>
197 static auto adl_begin(C &&c) -> decltype(begin(QRangeModelDetails::refTo(std::forward<C>(c))))
198 { return begin(QRangeModelDetails::refTo(std::forward<C>(c))); }
199 template <typename C>
200 static auto adl_end(C &&c) -> decltype(end(QRangeModelDetails::refTo(std::forward<C>(c))))
201 { return end(QRangeModelDetails::refTo(std::forward<C>(c))); }
202 template <typename C>
203 static auto pos(C &&c, int i)
204 { return std::next(QRangeModelDetails::adl_begin(std::forward<C>(c)), i); }
205
206 // Test if a type is a range, and whether we can modify it using the
207 // standard C++ container member functions insert, erase, and resize.
208 // For the sake of QAIM, we cannot modify a range if it holds const data
209 // even if the range itself is not const; we'd need to initialize new rows
210 // and columns, and move old row and column data.
211 template <typename C, typename = void>
213
214 template <typename C>
215 struct test_insert<C, std::void_t<decltype(std::declval<C>().insert(
216 std::declval<typename C::const_iterator>(),
217 std::declval<typename C::size_type>(),
218 std::declval<typename C::value_type>()
219 ))>>
220 : std::true_type
221 {};
222
223 // Can we insert from another (identical) range? Required to support
224 // move-only types
225 template <typename C, typename = void>
227
228 template <typename C>
229 struct test_insert_range<C, std::void_t<decltype(std::declval<C&>().insert(
230 std::declval<typename C::const_iterator&>(),
231 std::declval<std::move_iterator<typename C::iterator>&>(),
232 std::declval<std::move_iterator<typename C::iterator>&>()
233 ))>>
234 : std::true_type
235 {};
236
237 template <typename C, typename = void>
239
240 template <typename C>
241 struct test_erase<C, std::void_t<decltype(std::declval<C>().erase(
242 std::declval<typename C::const_iterator>(),
243 std::declval<typename C::const_iterator>()
244 ))>>
245 : std::true_type
246 {};
247
248 template <typename C, typename = void>
250
251 template <typename C>
252 struct test_resize<C, std::void_t<decltype(std::declval<C>().resize(
253 std::declval<typename C::size_type>(),
254 std::declval<typename C::value_type>()
255 ))>>
256 : std::true_type
257 {};
258
259 // we use std::rotate in moveRows/Columns, which requires the values (which
260 // might be const if we only get a const iterator) to be swappable, and the
261 // iterator type to be at least a forward iterator
262 template <typename It>
263 using test_rotate = std::conjunction<
264 std::is_swappable<decltype(*std::declval<It>())>,
265 std::is_base_of<std::forward_iterator_tag,
266 typename std::iterator_traits<It>::iterator_category>
267 >;
268
269 template <typename C, typename = void>
271
272 template <typename C>
273 struct test_splice<C, std::void_t<decltype(std::declval<C>().splice(
274 std::declval<typename C::const_iterator>(),
275 std::declval<C&>(),
276 std::declval<typename C::const_iterator>(),
277 std::declval<typename C::const_iterator>()
278 ))>>
279 : std::true_type
280 {};
281
282 template <typename C>
283 static void rotate(C& c, int src, int count, int dst) {
284 auto& container = QRangeModelDetails::refTo(c);
285 using Container = std::remove_reference_t<decltype(container)>;
286
287 const auto srcBegin = QRangeModelDetails::pos(container, src);
288 const auto srcEnd = std::next(srcBegin, count);
289 const auto dstBegin = QRangeModelDetails::pos(container, dst);
290
291 if constexpr (test_splice<Container>::value) {
292 if (dst > src && dst < src + count) // dst must be out of the source range
293 container.splice(srcBegin, container, dstBegin, srcEnd);
294 else if (dst != src) // otherwise, std::list gets corrupted
295 container.splice(dstBegin, container, srcBegin, srcEnd);
296 } else {
297 if (src < dst) // moving right
298 std::rotate(srcBegin, srcEnd, dstBegin);
299 else // moving left
300 std::rotate(dstBegin, srcBegin, srcEnd);
301 }
302 }
303
304 // Test if a type is an associative container that we can use for multi-role
305 // data, i.e. has a key_type and a mapped_type typedef, and maps from int,
306 // Qt::ItemDataRole, or QString to QVariant. This excludes std::set (and
307 // unordered_set), which are not useful for us anyway even though they are
308 // considered associative containers.
309 template <typename C, typename = void> struct is_multi_role : std::false_type
310 {
311 static constexpr bool int_key = false;
312 };
313 template <typename C> // Qt::ItemDataRole -> QVariant, or QString -> QVariant, int -> QVariant
314 struct is_multi_role<C, std::void_t<typename C::key_type, typename C::mapped_type>>
315 : std::conjunction<std::disjunction<std::is_same<typename C::key_type, int>,
316 std::is_same<typename C::key_type, Qt::ItemDataRole>,
317 std::is_same<typename C::key_type, QString>>,
318 std::is_same<typename C::mapped_type, QVariant>>
319 {
320 static constexpr bool int_key = !std::is_same_v<typename C::key_type, QString>;
321 };
322 template <typename C>
323 [[maybe_unused]]
324 static constexpr bool is_multi_role_v = is_multi_role<C>::value;
325
326 using std::size;
327 template <typename C, typename = void>
329 template <typename C>
330 struct test_size<C, std::void_t<decltype(size(std::declval<C&>()))>> : std::true_type {};
331
332 template <typename C, typename = void>
334 template <typename C>
335 struct test_cbegin<C, std::void_t<decltype(QRangeModelDetails::adl_begin(std::declval<const C&>()))>>
336 : std::true_type
337 {};
338
339 template <typename C, typename = void>
341 static constexpr bool is_mutable = !std::is_const_v<C>;
342 static constexpr bool has_insert = false;
343 static constexpr bool has_insert_range = false;
344 static constexpr bool has_erase = false;
345 static constexpr bool has_resize = false;
346 static constexpr bool has_rotate = false;
347 static constexpr bool has_splice = false;
348 static constexpr bool has_cbegin = false;
349 };
350 template <typename C>
352 decltype(QRangeModelDetails::adl_end(std::declval<C&>())),
354 >> : std::true_type
355 {
358 static constexpr bool is_mutable = !std::is_const_v<C> && !std::is_const_v<value_type>;
359 static constexpr bool has_insert = test_insert<C>();
360 static constexpr bool has_insert_range = test_insert_range<C>();
361 static constexpr bool has_erase = test_erase<C>();
362 static constexpr bool has_resize = test_resize<C>();
363 static constexpr bool has_rotate = test_rotate<iterator>();
364 static constexpr bool has_splice = test_splice<C>();
365 static constexpr bool has_cbegin = test_cbegin<C>::value;
366 };
367
368 // Specializations for types that look like ranges, but should be
369 // treated as values.
370 enum class Mutable { Yes, No };
371 template <Mutable IsMutable>
373 static constexpr bool is_mutable = IsMutable == Mutable::Yes;
374 static constexpr bool has_insert = false;
375 static constexpr bool has_erase = false;
376 static constexpr bool has_resize = false;
377 static constexpr bool has_rotate = false;
378 static constexpr bool has_splice = false;
379 static constexpr bool has_cbegin = true;
380 };
382 template <> struct range_traits<QString> : iterable_value<Mutable::Yes> {};
383 template <class CharT, class Traits, class Allocator>
386
387 // const T * and views are read-only
388 template <typename T> struct range_traits<const T *> : iterable_value<Mutable::No> {};
390
391 template <typename C>
393 template <typename C>
394 [[maybe_unused]] static constexpr bool is_range_v = is_range<C>();
395
396 // Detect an ItemAccess specialization with static read/writeRole members
397 template <typename T> struct QRangeModelItemAccess;
398
399 template <typename T>
401 {
404
405 template <typename Access, typename Test>
406 using hasReadRole_test = decltype(Access::readRole(std::declval<const Test &>(),
407 Qt::DisplayRole));
408 static constexpr bool hasReadRole = qxp::is_detected_v<hasReadRole_test, ItemAccess, ItemType>;
409
410 template <typename Access, typename Test>
413 static constexpr bool hasWriteRole = qxp::is_detected_v<hasWriteRole_test, ItemAccess, ItemType>;
414
415 template <typename Access, typename Test>
416 using hasFlags_test = decltype(Access::flags(std::declval<const Test&>()));
417
418 static constexpr bool hasFlags = qxp::is_detected_v<hasFlags_test, ItemAccess, ItemType>;
419
420 template <typename Access, typename Test>
421 using hasMimeTypes_test = decltype(Access::mimeTypes());
422 static constexpr bool hasMimeTypes = qxp::is_detected_v<hasMimeTypes_test, ItemAccess, ItemType>;
423
424 template <typename Access, typename Test>
425 using hasMimeData_test = decltype(Access::mimeData(std::declval<QSpan<const Test>>()));
426 static constexpr bool hasMimeData = qxp::is_detected_v<hasMimeData_test, ItemAccess, ItemType>;
427
428 template <typename Access>
430 std::declval<const QMimeData *>()
431 ));
432 static constexpr bool hasCanDropMimeData = qxp::is_detected_v<hasCanDropMimeData_test, ItemAccess>;
433 template <typename Access, typename Test>
435 std::declval<const QMimeData *>(),
437 );
438 static constexpr bool hasDropMimeData = qxp::is_detected_v<hasDropMimeData_test,
439 ItemAccess, ItemType>;
440
441 // full versions with all parameters
442 template <typename Access>
444 std::declval<const QMimeData *>(), Qt::CopyAction, 0, 0, std::declval<const QModelIndex &>()
445 ));
447 ItemAccess>;
448 template <typename Access, typename Test>
450 std::declval<const QMimeData *>(),
451 Qt::CopyAction, 0, 0, std::declval<const QModelIndex &>(),
453 ));
454 static constexpr bool hasDropMimeDataFull = qxp::is_detected_v<hasDropMimeDataFull_test,
455 ItemAccess, ItemType>;
456 };
457
458 // Detect which options are set to override default heuristics. Since
459 // QRangeModel is not yet defined we need to delay the evaluation.
460 template <typename T> struct QRangeModelRowOptions;
461
462 template <typename T, typename = void>
464 {
466 };
467
468 template <typename T>
470 : std::true_type
471 {
473 using RowCategory = decltype(rowCategory);
475 };
476
477 template <typename RowOptions>
479 template <typename row_type>
481
482 template <typename row_type>
483 using hasRowFlags_test = decltype(QRangeModelRowOptions<row_type>::flags(std::declval<const row_type &>()));
484 template <typename row_type>
485 static constexpr bool hasRowFlags = qxp::is_detected_v<hasRowFlags_test, row_type>;
486
487 // drag'n'drop handling
488 template <typename row_type>
489 using hasMimeTypes_test = decltype(QRangeModelRowOptions<row_type>::mimeTypes());
490 template <typename row_type>
491 static constexpr bool hasMimeTypes = qxp::is_detected_v<hasMimeTypes_test, row_type>;
492 template <typename row_type>
494 std::declval<const QModelIndexList &>())
495 );
496 template <typename row_type>
497 static constexpr bool hasMimeDataIndexList = qxp::is_detected_v<hasMimeDataIndexList_test, row_type>;
498 template <typename row_type>
500 // we don't call it with a QSpan, but with a range type. QSpan is a close enough match.
501 std::declval<QSpan<const row_type>>())
502 );
503 template <typename row_type>
504 static constexpr bool hasMimeDataRowSpan = qxp::is_detected_v<hasMimeDataRowSpan_test, row_type>;
505
506 // we allow simplified versions of (can)DropMimeData
507 template <typename row_type>
509 std::declval<const QMimeData *>()
510 ));
511 template <typename row_type>
512 static constexpr bool hasCanDropMimeData = qxp::is_detected_v<hasCanDropMimeData_test, row_type>;
513 template <typename row_type>
515 std::declval<const QMimeData *>(),
517 ));
518 template <typename row_type>
519 static constexpr bool hasDropMimeData = qxp::is_detected_v<hasDropMimeData_test, row_type>;
520
521 // the full versions get all the parameters
522 template <typename row_type>
524 std::declval<const QMimeData *>(), Qt::CopyAction, 0, 0, std::declval<const QModelIndex &>()
525 ));
526 template <typename row_type>
528 row_type>;
529 template <typename row_type>
531 std::declval<const QMimeData *>(),
532 Qt::CopyAction, 0, 0, std::declval<const QModelIndex &>(),
534 ));
535 template <typename row_type>
537 row_type>;
538
539 // Find out how many fixed elements can be retrieved from a row element.
540 // main template for simple values and ranges. Specializing for ranges
541 // is ambiguous with arrays, as they are also ranges
542 template <typename T, typename = void>
543 struct row_traits {
544 static constexpr bool is_range = is_range_v<q20::remove_cvref_t<T>>
546 // A static size of -1 indicates dynamically sized range
547 // A static size of 0 indicates that the specified type doesn't
548 // represent static or dynamic range.
549 static constexpr int static_size = is_range ? -1 : 0;
551 static constexpr int fixed_size() { return 1; }
552 static constexpr bool hasMetaObject = false;
553
555 {
556 return {};
557 }
558
559 template <typename C, typename Fn>
560 static bool for_element_at(C &&container, std::size_t idx, Fn &&fn)
561 {
562 if constexpr (is_range)
563 return std::forward<Fn>(fn)(*QRangeModelDetails::pos(std::forward<C>(container), idx));
564 else
565 return std::forward<Fn>(fn)(std::forward<C>(container));
566 }
567
568 template <typename Fn>
569 static bool for_each_element(const T &row, const QModelIndex &firstIndex, Fn &&fn)
570 {
571 if constexpr (static_size == 0) {
572 return std::forward<Fn>(fn)(firstIndex, QRangeModelDetails::pointerTo(row));
573 } else {
574 int columnIndex = -1;
575 return std::all_of(QRangeModelDetails::adl_begin(row),
576 QRangeModelDetails::adl_end(row), [&](const auto &item) {
577 return std::forward<Fn>(fn)(firstIndex.siblingAtColumn(++columnIndex),
578 QRangeModelDetails::pointerTo(item));
579 });
580 }
581 }
582 };
583
584 // Specialization for tuple-like semantics (prioritized over metaobject)
585 template <typename T>
587 {
588 static constexpr std::size_t size64 = std::tuple_size_v<T>;
589 static_assert(q20::in_range<int>(size64));
590 static constexpr int static_size = int(size64);
591
592 // are the types in a tuple all the same
593 template <std::size_t ...I>
594 static constexpr bool allSameTypes(std::index_sequence<I...>)
595 {
596 return (std::is_same_v<std::tuple_element_t<0, T>,
597 std::tuple_element_t<I, T>> && ...);
598 }
599
601 std::tuple_element_t<0, T>, void>;
602 static constexpr int fixed_size() { return 0; }
603 static constexpr bool hasMetaObject = false;
604
605 template <typename C, typename F>
607 {
609 constexpr size_t size = std::tuple_size_v<type>;
610 Q_ASSERT(idx < size);
611 return QtPrivate::applyIndexSwitch<size>(idx, [&](auto idxConstant) {
613 });
614 }
615
617 {
618 constexpr auto size = std::tuple_size_v<T>;
620
626 >();
627 if (metaType.isValid())
629 });
630 return result;
631 }
632
633 template <typename Fn, std::size_t ...Is>
634 static bool forEachTupleElement(const T &row, Fn &&fn, std::index_sequence<Is...>)
635 {
636 using std::get;
637 return (std::forward<Fn>(fn)(QRangeModelDetails::pointerTo(get<Is>(row))) && ...);
638 }
639
640 template <typename Fn>
641 static bool for_each_element(const T &row, const QModelIndex &firstIndex, Fn &&fn)
642 {
643 int column = -1;
644 return forEachTupleElement(row, [&column, &fn, &firstIndex](const QObject *item){
647 }
648 };
649
650 // Specialization for C arrays and std::array
651 template <typename T, std::size_t N>
652 struct row_traits<std::array<T, N>>
653 {
654 static_assert(q20::in_range<int>(N));
655 static constexpr int static_size = int(N);
656 using item_type = T;
657 static constexpr int fixed_size() { return 0; }
658 static constexpr bool hasMetaObject = false;
659
660 template <typename C, typename F>
666
668 {
669 return section;
670 }
671
672 template <typename Fn>
673 static bool for_each_element(const std::array<T, N> &row, const QModelIndex &firstIndex, Fn &&fn)
674 {
675 int columnIndex = -1;
677 QRangeModelDetails::adl_end(row), [&](const auto &item) {
680 });
681 }
682 };
683
684 template <typename T, std::size_t N>
685 struct row_traits<T[N]> : row_traits<std::array<T, N>> {};
686
687 // prioritize tuple-like over metaobject
688 template <typename T>
690 {
691 static constexpr int static_size = 0;
693 static int fixed_size() {
694 if constexpr (row_category<T>::isMultiRole) {
695 return 1;
696 } else {
697 // Interpret a gadget in a list as a multi-column row item. To make
698 // a list of multi-role items, wrap it into SingleColumn.
699 static const int columnCount = []{
701 return mo.propertyCount() - mo.propertyOffset();
702 }();
703 return columnCount;
704 }
705 }
706
707 static constexpr bool hasMetaObject = true;
708
709 template <typename C, typename F>
711 {
712 return std::forward<F>(function)(std::forward<C>(container));
713 }
714
716 {
718 if (fixed_size() == 1) {
721 } else if (section <= fixed_size()) {
725 }
726 return result;
727 }
728
729 template <typename Fn>
730 static bool for_each_element(const T &row, const QModelIndex &firstIndex, Fn &&fn)
731 {
733 }
734 };
735
736 template <typename T, typename = void>
738 {
739 template <typename That>
740 static QHash<int, QByteArray> roleNames(That *)
741 {
742 return That::roleNamesForSimpleType();
743 }
744 };
745
746 template <>
747 struct item_traits<void>
748 {
749 template <typename That>
751 {
753 }
754 };
755
756 template <typename T>
761
762 template <typename T>
764 {
765 template <typename That>
770 };
771
772 template <typename T>
773 [[maybe_unused]] static constexpr int static_size_v =
775
776 template <typename Range>
778 {
780
781 template <typename R = row_type>
782 auto newRow() -> decltype(R{}) { return R{}; }
783 };
784
785 template <typename Range>
787 {
789
790 template <typename R = row_type,
795 >,
796 bool> = true>
797 auto newRow() -> decltype(R(new QRangeModelDetails::wrapped_t<R>)) {
798 if constexpr (is_any_of<R, std::shared_ptr>())
800 else
801 return R(new QRangeModelDetails::wrapped_t<R>);
802 }
803
804 template <typename R = row_type,
806 auto newRow() -> decltype(R{}) { return R{}; }
807
808 template <typename R = row_type,
810 auto deleteRow(R&& row) -> decltype(delete row) { delete row; }
811 };
812
813 template <typename Range,
817
818 // Default tree traversal protocol implementation for row types that have
819 // the respective member functions. The trailing return type implicitly
820 // removes those functions that are not available.
821 template <typename Range>
823 {
824 template <typename R /*wrapped_row_type*/>
825 auto parentRow(const R& row) const -> decltype(row.parentRow())
826 {
827 return row.parentRow();
828 }
829
830 template <typename R /* = wrapped_row_type*/>
831 auto setParentRow(R &row, R* parent) -> decltype(row.setParentRow(parent))
832 {
833 row.setParentRow(parent);
834 }
835
836 template <typename R /* = wrapped_row_type*/>
837 auto childRows(const R &row) const -> decltype(row.childRows())
838 {
839 return row.childRows();
840 }
841
842 template <typename R /* = wrapped_row_type*/>
843 auto childRows(R &row) -> decltype(row.childRows())
844 {
845 return row.childRows();
846 }
847 };
848
849 template <typename P, typename R>
850 using protocol_parentRow_test = decltype(std::declval<P&>()
852 template <typename P, typename R>
854
855 template <typename P, typename R>
856 using protocol_childRows_test = decltype(std::declval<P&>()
858 template <typename P, typename R>
860
861 template <typename P, typename R>
865 template <typename P, typename R>
867
868 template <typename P, typename R>
871 template <typename P, typename R>
873
874 template <typename P, typename = void>
876 template <typename P>
877 struct protocol_newRow<P, std::void_t<decltype(std::declval<P&>().newRow())>>
878 : std::true_type {};
879
880 template <typename P, typename R, typename = void>
882 template <typename P, typename R>
884 std::void_t<decltype(std::declval<P&>().deleteRow(std::declval<R&&>()))>>
885 : std::true_type {};
886
887 template <typename Range,
888 typename Protocol = DefaultTreeProtocol<Range>,
889 typename R = typename range_traits<Range>::value_type,
890 typename = void>
892
893 template <typename Range, typename Protocol, typename R>
898
899 template <typename Range>
903 >, bool>;
904
905 template <typename Range, typename Protocol = DefaultTreeProtocol<Range>>
910 >, bool>;
911
912 template <typename Range, typename Protocol>
930
931 // Helpers for drag'n'drop:
932 // MimeDataEntry gives customisations access to a pair of either a row or
933 // an item (in form of the underlying type, i.e. unwrapped, as that's what
934 // customizations specialize RowOptions and ItemAccess for), and the
935 // corresponding index, with decomposition support for easy iteration.
936 template <typename Entry>
938 {
941 template<typename T, typename = void>
943
944#ifndef Q_CC_MSVC // MSVC selects this even for non-constexpr types
945 template<typename T>
947#endif
948
949 bool isValid() const { return QRangeModelDetails::isValid(m_entry); }
950 const wrapped_entry &entry() const
951 {
952 if constexpr (QRangeModelDetails::is_owning_or_raw_pointer<entry_type>()) {
953 // While we mark null-items or indexes in null-rows as not draggable,
954 // client code might override that, or explicitly call QRM::mimeData()
955 // with indexes that point at null-rows or -items.
956 if (Q_UNLIKELY(!QRangeModelDetails::isValid(m_entry))) {
957#ifndef QT_NO_DEBUG
958 qDebug("QRangeModel::mimeData: null-entry, test with isValid before accessing");
959#endif
960 constexpr bool is_constexpr_default_constructible_v =
961 is_constexpr_default_constructible<wrapped_entry>::value;
962 if constexpr (is_constexpr_default_constructible_v) {
963 Q_CONSTINIT static const wrapped_entry emptyDefault;
964 return QRangeModelDetails::refTo(emptyDefault);
965 } else {
966 // known to cause runtime initialization
967 static const wrapped_entry emptyDefault;
968 return QRangeModelDetails::refTo(emptyDefault);
969 }
970 }
971 }
972 return std::as_const(QRangeModelDetails::refTo(m_entry));
973 }
974
975 const QModelIndex &index() const { return m_index; }
976
977 template <std::size_t N>
978 friend decltype(auto) get(const MimeDataEntry &entry)
979 {
980 if constexpr (N == 0)
981 return entry.entry();
982 else if constexpr (N == 1)
983 return entry.index();
984 }
985 const Entry &m_entry;
987 };
988} // namespace QRangeModelDetails
989
990QT_END_NAMESPACE
991
992// decomposition protocol
993namespace std {
994template <typename T>
997template <typename T>
1000template <typename T>
1003} // namespace QRangeModelDetails
1004
1005QT_BEGIN_NAMESPACE
1006
1007namespace QRangeModelDetails {
1008 // A helper type for drop-support. Client code populates a sequence of
1009 // dropped things via an insertion iterator, and those get wrapped in a
1010 // DroppedEntry, which allows user code to also specify the position of the
1011 // thing in the target model.
1012 template <typename Entry>
1014 {
1015 struct Cell {
1018
1019 // implicit conversion is intentional
1020 Q_IMPLICIT Cell() noexcept : m_row(-1), m_column(-1) {}
1021 Q_IMPLICIT Cell(int row, int column = 0) noexcept : m_row(row), m_column(column) {}
1022
1023 friend bool operator==(const Cell &lhs, const Cell &rhs) noexcept
1024 {
1025 return lhs.m_row == rhs.m_row && lhs.m_column == rhs.m_column;
1026 }
1027 };
1028
1029 // implicit conversion from and to entry is intentional
1036
1037 // we only move the actual data out
1038 operator Entry&&() && { return std::move(m_entry); }
1039
1040 Entry m_entry;
1042 };
1043
1060
1061 template <bool cacheProperties, bool itemsAreQObjects>
1063 static constexpr bool cachesProperties = false;
1064
1066 };
1067
1069 {
1070 static constexpr bool cachesProperties = true;
1072
1074 {
1075 properties.clear();
1076 }
1077 protected:
1078 ~PropertyCache() = default;
1079 };
1080
1081 template <>
1082 struct PropertyData<true, false> : PropertyCache
1083 {};
1084
1086 {
1087 struct Connection {
1089 int role;
1090
1091 friend bool operator==(const Connection &lhs, const Connection &rhs) noexcept
1092 {
1093 return lhs.sender == rhs.sender && lhs.role == rhs.role;
1094 }
1095 friend size_t qHash(const Connection &c, size_t seed) noexcept
1096 {
1097 return qHashMulti(seed, c.sender, c.role);
1098 }
1099 };
1100
1103
1104 protected:
1105 ~ConnectionStorage() = default;
1106 };
1107
1108 template <>
1110 {};
1111
1112 template <>
1113 struct PropertyData<false, true> : PropertyData<false, false>, ConnectionStorage
1114 {
1116 };
1117
1118 // The storage of the model data. We might store it as a pointer, or as a
1119 // (copied- or moved-into) value (or smart pointer). But we always return a
1120 // raw pointer.
1121 template <typename ModelStorage, typename = void>
1129
1130 template <typename ModelStorage>
1139
1140 template <typename ModelStorage, typename PropertyStorage>
1142 PropertyStorage
1143 {
1147
1148 auto model() { return QRangeModelDetails::pointerTo(this->m_model); }
1149 auto model() const { return QRangeModelDetails::pointerTo(this->m_model); }
1150
1151 template <typename Model = ModelStorage>
1152 ModelData(Model &&model)
1154 {}
1155 };
1156} // namespace QRangeModelDetails
1157
1158class QRangeModel;
1159// forward declare so that we can declare friends
1160template <typename, typename, typename> class QRangeModelAdapter;
1161
1163{
1164 using Self = QRangeModelImplBase;
1166
1167public:
1168 // keep in sync with QRangeModel::AutoConnectPolicy
1174
1175 // keep in sync with QRangeModel::DropOperation
1184
1185 // overridable prototypes (quasi-pure-virtual methods)
1187 bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &data, int role);
1188 bool setData(const QModelIndex &index, const QVariant &data, int role);
1189 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &data);
1190 bool clearItemData(const QModelIndex &index);
1191 bool insertColumns(int column, int count, const QModelIndex &parent);
1192 bool removeColumns(int column, int count, const QModelIndex &parent);
1193 bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destParent, int destColumn);
1194 bool insertRows(int row, int count, const QModelIndex &parent);
1195 bool removeRows(int row, int count, const QModelIndex &parent);
1196 bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destParent, int destRow);
1197
1198 QModelIndex index(int row, int column, const QModelIndex &parent) const;
1199 QModelIndex sibling(int row, int column, const QModelIndex &index) const;
1200 int rowCount(const QModelIndex &parent) const;
1201 int columnCount(const QModelIndex &parent) const;
1202 Qt::ItemFlags flags(const QModelIndex &index) const;
1203 QVariant headerData(int section, Qt::Orientation orientation, int role) const;
1204 QVariant data(const QModelIndex &index, int role) const;
1205 QMap<int, QVariant> itemData(const QModelIndex &index) const;
1206 inline QHash<int, QByteArray> roleNames() const;
1207 QModelIndex parent(const QModelIndex &child) const;
1208
1209 void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const;
1211
1212 void interfaceVersion(int &version) const;
1213 void sort(int column, Qt::SortOrder order);
1214 QModelIndexList match(const QModelIndex &start, int role, const QVariant &value,
1215 int hits, Qt::MatchFlags flags) const;
1216
1217 Qt::DropActions adjustSupportedDragActions(Qt::DropActions dragActions);
1218 Qt::DropActions adjustSupportedDropActions(Qt::DropActions dropActions);
1220 bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
1221 const QModelIndex &parent) const;
1222 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
1223 const QModelIndex &parent);
1224 QMimeData *mimeData(const QModelIndexList &indexes) const;
1225
1226 // bindings for overriding
1227
1239
1246 using Data = Method<&Self::data>;
1250
1251 // 6.11
1254
1255 // 6.12
1257 using Sort = Method<&Self::sort>;
1261
1266
1267 template <typename C>
1268 using MethodTemplates = std::tuple<
1269 typename C::Destroy,
1270 typename C::InvalidateCaches,
1271 typename C::SetHeaderData,
1272 typename C::SetData,
1273 typename C::SetItemData,
1274 typename C::ClearItemData,
1275 typename C::InsertColumns,
1276 typename C::RemoveColumns,
1277 typename C::MoveColumns,
1278 typename C::InsertRows,
1279 typename C::RemoveRows,
1280 typename C::MoveRows,
1281 typename C::Index,
1282 typename C::Parent,
1283 typename C::Sibling,
1284 typename C::RowCount,
1285 typename C::ColumnCount,
1286 typename C::Flags,
1287 typename C::HeaderData,
1288 typename C::Data,
1289 typename C::ItemData,
1290 typename C::RoleNames,
1291 typename C::MultiData,
1292 typename C::SetAutoConnectPolicy,
1293 typename C::InterfaceVersion,
1294 typename C::Sort,
1295 typename C::Match,
1296 typename C::AdjustSupportedDragActions,
1297 typename C::AdjustSupportedDropActions,
1298 typename C::MimeTypes,
1299 typename C::CanDropMimeData,
1300 typename C::DropMimeData,
1301 typename C::MimeData
1302 >;
1303
1304 static Q_CORE_EXPORT QRangeModelImplBase *getImplementation(QRangeModel *model);
1305 static Q_CORE_EXPORT const QRangeModelImplBase *getImplementation(const QRangeModel *model);
1306
1307private:
1308 friend class QRangeModelPrivate;
1310
1311 QRangeModel *m_rangeModel;
1312
1313protected:
1314 explicit QRangeModelImplBase(QRangeModel *itemModel)
1315 : m_rangeModel(itemModel)
1316 {}
1317
1318 inline QModelIndex createIndex(int row, int column, const void *ptr = nullptr) const;
1319 inline QModelIndexList persistentIndexList() const;
1320 inline void changePersistentIndex(const QModelIndex &from, const QModelIndex &to);
1321 inline void dataChanged(const QModelIndex &from, const QModelIndex &to,
1322 const QList<int> &roles);
1323 inline void beginResetModel();
1324 inline void endResetModel();
1325 inline void beginInsertColumns(const QModelIndex &parent, int start, int count);
1326 inline void endInsertColumns();
1327 inline void beginRemoveColumns(const QModelIndex &parent, int start, int count);
1328 inline void endRemoveColumns();
1329 inline bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
1330 const QModelIndex &destParent, int destRow);
1331 inline void endMoveColumns();
1332 inline void beginInsertRows(const QModelIndex &parent, int start, int count);
1333 inline void endInsertRows();
1334 inline void beginRemoveRows(const QModelIndex &parent, int start, int count);
1335 inline void endRemoveRows();
1336 inline bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast,
1337 const QModelIndex &destParent, int destRow);
1338 inline void endMoveRows();
1339 inline void beginLayoutChange();
1340 inline void endLayoutChange();
1341 inline AutoConnectPolicy autoConnectPolicy() const;
1342 inline static Qt::partial_ordering compareData(const QVariant &lhs, const QVariant &rhs,
1343 const QCollator *collator);
1344
1345public:
1346 inline QAbstractItemModel &itemModel();
1347 inline const QAbstractItemModel &itemModel() const;
1348
1349 // implemented in qrangemodel.cpp
1351 const QMetaObject &metaObject);
1353
1354protected:
1355 Q_CORE_EXPORT QScopedValueRollback<bool> blockDataChangedDispatch();
1356
1358 const QMetaObject &metaObject);
1360 Q_CORE_EXPORT static bool connectProperty(const QModelIndex &index, const QObject *item,
1361 QRangeModelDetails::AutoConnectContext *context,
1362 int role, const QMetaProperty &property);
1363 Q_CORE_EXPORT static bool connectPropertyConst(const QModelIndex &index, const QObject *item,
1364 QRangeModelDetails::AutoConnectContext *context,
1365 int role, const QMetaProperty &property);
1366 Q_CORE_EXPORT static bool connectProperties(const QModelIndex &index, const QObject *item,
1367 QRangeModelDetails::AutoConnectContext *context,
1368 const QHash<int, QMetaProperty> &properties);
1369 Q_CORE_EXPORT static bool connectPropertiesConst(const QModelIndex &index, const QObject *item,
1370 QRangeModelDetails::AutoConnectContext *context,
1371 const QHash<int, QMetaProperty> &properties);
1372 Q_CORE_EXPORT int sortRole() const;
1373 Q_CORE_EXPORT const QCollator *sortCollator() const;
1374
1375 Q_CORE_EXPORT static QVariant convertMatchValue(const QVariant &value, Qt::MatchFlags flags);
1376 Q_CORE_EXPORT static bool matchValue(const QString &itemData, const QVariant &value,
1377 Qt::MatchFlags flags);
1378 static bool matchValue(const QVariant &itemData, const QVariant &value, Qt::MatchFlags flags)
1379 {
1380 if ((flags & Qt::MatchTypeMask) == Qt::MatchExactly)
1381 return itemData == value;
1382 return matchValue(itemData.toString(), value, flags);
1383 }
1384
1385 Q_CORE_EXPORT bool dropDataOnItem(const QMimeData *data, const QModelIndex &index);
1386};
1387
1388template <typename Structure, typename Range,
1389 typename Protocol = QRangeModelDetails::table_protocol_t<Range>>
1394{
1395public:
1405
1407 typename row_traits::item_type>>;
1409 && row_traits::hasMetaObject; // not treated as tuple
1410
1414 >,
1417 >
1418 >;
1420
1421 using const_row_reference = decltype(*std::declval<typename ModelData::const_iterator&>());
1422
1423 static_assert(!QRangeModelDetails::is_any_of<range_type, std::optional>() &&
1425 "Currently, std::optional is not supported for ranges and rows, as "
1426 "it has range semantics in c++26. Once the required behavior is clarified, "
1427 "std::optional for ranges and rows will be supported.");
1428
1429protected:
1430
1431 using Self = QRangeModelImpl<Structure, Range, Protocol>;
1433
1434 Structure& that() { return static_cast<Structure &>(*this); }
1435 const Structure& that() const { return static_cast<const Structure &>(*this); }
1436
1437 template <typename C>
1438 static constexpr int size(const C &c)
1439 {
1440 if (!QRangeModelDetails::isValid(c))
1441 return 0;
1442
1443 if constexpr (QRangeModelDetails::test_size<C>()) {
1444 using std::size;
1445 return int(size(c));
1446 } else {
1447#if defined(__cpp_lib_ranges)
1448 using std::ranges::distance;
1449#else
1450 using std::distance;
1451#endif
1452 using container_type = std::conditional_t<QRangeModelDetails::range_traits<C>::has_cbegin,
1453 const QRangeModelDetails::wrapped_t<C>,
1454 QRangeModelDetails::wrapped_t<C>>;
1455 container_type& container = const_cast<container_type &>(QRangeModelDetails::refTo(c));
1456 return int(distance(QRangeModelDetails::adl_begin(container),
1457 QRangeModelDetails::adl_end(container)));
1458 }
1459 }
1460
1463 static constexpr bool rows_are_owning_or_raw_pointers =
1466 static constexpr bool one_dimensional_range = static_column_count == 0;
1467
1469 {
1470 if constexpr (itemsAreQObjects || rowsAreQObjects)
1471 return this->blockDataChangedDispatch();
1472 else
1473 return false;
1474 }
1475
1476 // A row might be a value (or range of values), or a pointer.
1477 // row_ptr is always a pointer, and const_row_ptr is a pointer to const.
1480
1481 template <typename T>
1484
1485 // A iterator type to use as the input iterator with the
1486 // range_type::insert(pos, start, end) overload if available (it is in
1487 // std::vector, but not in QList). Generates a prvalue when dereferenced,
1488 // which then gets moved into the newly constructed row, which allows us to
1489 // implement insertRows() for move-only row types.
1491 {
1495 using iterator_category = std::input_iterator_tag;
1496 using difference_type = int;
1497
1498 value_type operator*() { return impl->makeEmptyRow(parentRow); }
1499 EmptyRowGenerator &operator++() { ++n; return *this; }
1500 friend bool operator==(const EmptyRowGenerator &lhs, const EmptyRowGenerator &rhs) noexcept
1501 { return lhs.n == rhs.n; }
1502 friend bool operator!=(const EmptyRowGenerator &lhs, const EmptyRowGenerator &rhs) noexcept
1503 { return !(lhs == rhs); }
1504
1506 Structure *impl = nullptr;
1507 const row_ptr parentRow = nullptr;
1508 };
1509
1510 // If we have a move-only row_type and can add/remove rows, then the range
1511 // must have an insert-from-range overload.
1514 "The range holding a move-only row-type must support insert(pos, start, end)");
1515
1518
1519public:
1520 static constexpr bool isMutable()
1521 {
1522 return range_features::is_mutable && row_features::is_mutable
1523 && std::is_reference_v<row_reference>
1524 && Structure::is_mutable_impl;
1525 }
1526 static constexpr bool dynamicRows() { return isMutable() && static_row_count < 0; }
1527 static constexpr bool dynamicColumns() { return static_column_count < 0; }
1528
1529 explicit QRangeModelImpl(Range &&model, Protocol&& protocol, QRangeModel *itemModel)
1533 {
1534 }
1535
1536
1537 // static interface, called by QRangeModelImplBase
1538
1539 void interfaceVersion(int &versionNumber) const
1540 {
1541 versionNumber = QT_VERSION;
1542 }
1543
1544 void invalidateCaches() { m_data.invalidateCaches(); }
1545
1546 // Not implemented
1547 bool setHeaderData(int , Qt::Orientation , const QVariant &, int ) { return false; }
1548
1549 // actual implementations
1550 QModelIndex index(int row, int column, const QModelIndex &parent) const
1551 {
1552 if (row < 0 || column < 0 || column >= columnCount(parent)
1553 || row >= rowCount(parent)) {
1554 return {};
1555 }
1556
1557 return that().indexImpl(row, column, parent);
1558 }
1559
1560 QModelIndex sibling(int row, int column, const QModelIndex &index) const
1561 {
1562 if (row == index.row() && column == index.column())
1563 return index;
1564
1565 // we use indexes at column -1 in drag'n'drop handling to mark full rows
1566 if (column >= this->columnCount({}))
1567 return {};
1568
1569 if (row == index.row())
1570 return this->createIndex(row, column, index.constInternalPointer());
1571
1572 const_row_ptr parentRow = static_cast<const_row_ptr>(index.constInternalPointer());
1573 const auto siblingCount = size(that().childrenOf(parentRow));
1574 if (row < 0 || row >= int(siblingCount))
1575 return {};
1576 return this->createIndex(row, column, parentRow);
1577 }
1578
1579 Qt::ItemFlags flags(const QModelIndex &index) const
1580 {
1581 if (!index.isValid()) {
1582 if constexpr (isMutable())
1583 return Qt::ItemIsDropEnabled;
1584 else
1585 return Qt::NoItemFlags;
1586 }
1587
1588 // try customization
1589 std::optional<Qt::ItemFlags> customFlags;
1590 if constexpr (QRangeModelDetails::hasRowFlags<wrapped_row_type>) {
1591 const_row_reference row = rowData(index);
1592 if (QRangeModelDetails::isValid(row)) {
1593 customFlags = QRangeModelDetails::QRangeModelRowOptions<wrapped_row_type>::flags(
1594 QRangeModelDetails::refTo(row)
1595 );
1596 }
1597 }
1598
1599 readAt(index, [&customFlags](auto &&ref){
1600 Q_UNUSED(ref);
1601 using wrapped_value_type = q20::remove_cvref_t<QRangeModelDetails::wrapped_t<decltype(ref)>>;
1602 if constexpr (QRangeModelDetails::item_access<wrapped_value_type>::hasFlags) {
1603 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<wrapped_value_type>;
1604 if (QRangeModelDetails::isValid(ref)) {
1605 customFlags = ItemAccess::flags(QRangeModelDetails::refTo(ref));
1606 return true;
1607 }
1608 }
1609 return false;
1610 });
1611
1612 Qt::ItemFlags f = customFlags ? *customFlags : Structure::defaultFlags();
1613 // adjust custom flags based on what is not possible
1614 if constexpr (!isMutable())
1615 f &= ~(Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
1616 if (index.column())
1617 f |= Qt::ItemNeverHasChildren;
1618 if (customFlags)
1619 return f;
1620
1621 // compute flags ourselves
1622 if (!this->itemModel().mimeTypes().isEmpty()) {
1623 f |= Qt::ItemIsDragEnabled;
1624 if constexpr (isMutable())
1625 f |= Qt::ItemIsDropEnabled;
1626 }
1627
1628 if constexpr (QRangeModelDetails::is_owning_or_raw_pointer<row_type>()) {
1629 // pointer rows might be null
1630 const_row_reference row = rowData(index);
1631 if (!QRangeModelDetails::isValid(row))
1632 f &= ~Qt::ItemIsDragEnabled;
1633 }
1634
1635 if constexpr (isMutable()) {
1636 // Note: Read-only items are still droppable - we can't know here
1637 // whether the model will insert data as new rows or children, or if
1638 // it will overwrite the data of the dropped-on item. So we allow
1639 // dropping on items that are not editable.
1640 if constexpr (row_traits::hasMetaObject) {
1641 if (index.column() < row_traits::fixed_size()) {
1642 const QMetaObject mo = wrapped_row_type::staticMetaObject;
1643 const QMetaProperty prop = mo.property(index.column() + mo.propertyOffset());
1644 if (prop.isWritable())
1645 f |= Qt::ItemIsEditable;
1646 }
1647 } else if constexpr (static_column_count <= 0) {
1648 using item_type = typename row_traits::item_type;
1649 if constexpr (QRangeModelDetails::is_owning_or_raw_pointer<item_type>()) {
1650 // pointer items might be null
1651 if (!readAt(index, [](auto &&i){ return QRangeModelDetails::isValid(i); }))
1652 f &= ~Qt::ItemIsDragEnabled;
1653 }
1654 f |= Qt::ItemIsEditable;
1655 } else if constexpr (std::is_reference_v<row_reference> && !std::is_const_v<row_reference>) {
1656 // we want to know if the elements in the tuple are const; they'd always be, if
1657 // we didn't remove the const of the range first.
1658 const_row_reference row = rowData(index);
1659 row_reference mutableRow = const_cast<row_reference>(row);
1660 if (QRangeModelDetails::isValid(mutableRow)) {
1661 row_traits::for_element_at(mutableRow, index.column(), [&f](auto &&ref){
1662 using target_type = decltype(ref);
1663 if constexpr (QRangeModelDetails::is_owning_or_raw_pointer<target_type>()) {
1664 // pointer items might be null
1665 if (!QRangeModelDetails::isValid(ref))
1666 f &= ~Qt::ItemIsDragEnabled;
1667 }
1668 if constexpr (std::is_const_v<std::remove_reference_t<target_type>>)
1669 f &= ~Qt::ItemIsEditable;
1670 else if constexpr (std::is_lvalue_reference_v<target_type>)
1671 f |= Qt::ItemIsEditable;
1672 });
1673 } else {
1674 // If there's no usable value stored in the row, then we can't
1675 // do anything with this item, except perhaps drop data into it
1676 f &= ~Qt::ItemIsEditable;
1677 }
1678 }
1679 }
1680 return f;
1681 }
1682
1683 QVariant headerData(int section, Qt::Orientation orientation, int role) const
1684 {
1685 QVariant result;
1686 if constexpr (QRangeModelDetails::hasHeaderData<wrapped_row_type>) {
1687 if (orientation == Qt::Horizontal) {
1688 result = QRangeModelDetails::QRangeModelRowOptions<wrapped_row_type>::headerData(
1689 section, role
1690 );
1691 if (result.isValid())
1692 return result;
1693 }
1694 }
1695
1696 if (role != Qt::DisplayRole || orientation != Qt::Horizontal
1697 || section < 0 || section >= columnCount({})) {
1698 return this->itemModel().QAbstractItemModel::headerData(section, orientation, role);
1699 }
1700
1701 result = row_traits::column_name(section);
1702 if (!result.isValid())
1703 result = this->itemModel().QAbstractItemModel::headerData(section, orientation, role);
1704 return result;
1705 }
1706
1707 QVariant data(const QModelIndex &index, int role) const
1708 {
1709 if (!index.isValid())
1710 return {};
1711
1712 QModelRoleData result(role);
1713 multiData(index, result);
1714 return std::move(result.data());
1715 }
1716
1717 static constexpr bool isRangeModelRole(int role)
1718 {
1719 return role == Qt::RangeModelDataRole
1720 || role == Qt::RangeModelAdapterRole;
1721 }
1722
1723 static constexpr bool isPrimaryRole(int role)
1724 {
1725 return role == Qt::DisplayRole || role == Qt::EditRole;
1726 }
1727
1728 QMap<int, QVariant> itemData(const QModelIndex &index) const
1729 {
1730 QMap<int, QVariant> result;
1731
1732 if (index.isValid()) {
1733 // optimisation for items backed by a QMap<int, QVariant> or equivalent
1734 if (!readAt(index, [&result](const auto &value) {
1735 if constexpr (std::is_convertible_v<decltype(value), decltype(result)>) {
1736 result = value;
1737 return true;
1738 }
1739 return false;
1740 })) {
1741 const auto roles = this->itemModel().roleNames().keys();
1742 QVarLengthArray<QModelRoleData, 16> roleDataArray;
1743 roleDataArray.reserve(roles.size());
1744 for (auto role : roles) {
1745 if (isRangeModelRole(role))
1746 continue;
1747 roleDataArray.emplace_back(role);
1748 }
1749 QModelRoleDataSpan roleDataSpan(roleDataArray);
1750 multiData(index, roleDataSpan);
1751
1752 for (QModelRoleData &roleData : roleDataSpan) {
1753 if (roleData.data().isValid())
1754 result[roleData.role()] = std::move(roleData.data());
1755 }
1756 }
1757 }
1758 return result;
1759 }
1760
1762 {
1763 template <typename value_type>
1764 bool operator()(const value_type &value) const
1765 {
1766 using multi_role = QRangeModelDetails::is_multi_role<value_type>;
1767 using wrapped_value_type = QRangeModelDetails::wrapped_t<value_type>;
1768
1769 const auto readModelData = [&value](QModelRoleData &roleData){
1770 if (!QRangeModelDetails::isValid(value)) {
1771 roleData.clearData();
1772 return true;
1773 }
1774
1775 const int role = roleData.role();
1776 if (role == Qt::RangeModelDataRole) {
1777 // Qt QML support: "modelData" role returns the entire multi-role item.
1778 // QML can only use raw pointers to QObject (so we unwrap), and gadgets
1779 // only by value (so we take the reference).
1780 if constexpr (std::is_copy_assignable_v<wrapped_value_type>)
1781 roleData.setData(QVariant::fromValue(QRangeModelDetails::refTo(value)));
1782 else
1783 roleData.setData(QVariant::fromValue(QRangeModelDetails::pointerTo(value)));
1784 } else if (role == Qt::RangeModelAdapterRole) {
1785 // for QRangeModelAdapter however, we want to respect smart pointer wrappers
1786 if constexpr (std::is_copy_assignable_v<value_type>)
1787 roleData.setData(QVariant::fromValue(value));
1788 else
1789 roleData.setData(QVariant::fromValue(QRangeModelDetails::pointerTo(value)));
1790 } else {
1791 return false;
1792 }
1793 return true;
1794 };
1795
1796 if constexpr (QRangeModelDetails::item_access<wrapped_value_type>::hasReadRole) {
1797 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<wrapped_value_type>;
1798 for (auto &roleData : roleDataSpan) {
1799 if (!readModelData(roleData)) {
1800 roleData.setData(ItemAccess::readRole(QRangeModelDetails::refTo(value),
1801 roleData.role()));
1802 }
1803 }
1804 } else if constexpr (multi_role()) {
1805 const auto roleNames = [this]() -> QHash<int, QByteArray> {
1806 Q_UNUSED(this);
1807 if constexpr (!multi_role::int_key)
1808 return that->itemModel().roleNames();
1809 else
1810 return {};
1811 }();
1812 using key_type = typename value_type::key_type;
1813 for (auto &roleData : roleDataSpan) {
1814 const auto &it = [&roleNames, &value, role = roleData.role()]{
1815 Q_UNUSED(roleNames);
1816 if constexpr (multi_role::int_key)
1817 return value.find(key_type(role));
1818 else
1819 return value.find(roleNames.value(role));
1820 }();
1821 if (it != QRangeModelDetails::adl_end(value))
1822 roleData.setData(QRangeModelDetails::value(it));
1823 else
1824 roleData.clearData();
1825 }
1826 } else if constexpr (has_metaobject<value_type>) {
1827 if (row_traits::fixed_size() <= 1) {
1828 for (auto &roleData : roleDataSpan) {
1829 if (!readModelData(roleData)) {
1830 roleData.setData(that->readRole(index, roleData.role(),
1831 QRangeModelDetails::pointerTo(value)));
1832 }
1833 }
1834 } else if (index.column() <= row_traits::fixed_size()) {
1835 for (auto &roleData : roleDataSpan) {
1836 const int role = roleData.role();
1837 if (isPrimaryRole(role)) {
1838 roleData.setData(that->readProperty(index,
1839 QRangeModelDetails::pointerTo(value)));
1840 } else {
1841 roleData.clearData();
1842 }
1843 }
1844 }
1845 } else {
1846 for (auto &roleData : roleDataSpan) {
1847 const int role = roleData.role();
1848 if (isPrimaryRole(role) || isRangeModelRole(role))
1849 roleData.setData(read(value));
1850 else
1851 roleData.clearData();
1852 }
1853 }
1854 return true;
1855 }
1856
1859 const QRangeModelImpl * const that;
1860 };
1861
1862 void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const
1863 {
1864 if (!readAt(index, ItemReader{index, roleDataSpan, this})) {
1865 for (auto &roleData : roleDataSpan)
1866 roleData.clearData();
1867 }
1868 }
1869
1870 bool setData(const QModelIndex &index, const QVariant &data, int role)
1871 {
1872 if (!index.isValid())
1873 return false;
1874
1875 if constexpr (isMutable()) {
1876 auto emitDataChanged = qScopeGuard([this, &index, role]{
1877 Q_EMIT this->dataChanged(index, index,
1878 role == Qt::EditRole || role == Qt::RangeModelDataRole
1879 || role == Qt::RangeModelAdapterRole
1880 ? QList<int>{} : QList<int>{role});
1881 });
1882 // we emit dataChanged at the end, block dispatches from auto-connected properties
1883 [[maybe_unused]] auto dataChangedBlocker = maybeBlockDataChangedDispatch();
1884
1885 const auto writeData = [this, column = index.column(), &data, role](auto &&target) -> bool {
1886 using value_type = q20::remove_cvref_t<decltype(target)>;
1887 using wrapped_value_type = QRangeModelDetails::wrapped_t<value_type>;
1888 using multi_role = QRangeModelDetails::is_multi_role<value_type>;
1889
1890 if constexpr (std::conjunction_v<QRangeModelDetails::is_any_owning_ptr<value_type>,
1891 std::is_default_constructible<wrapped_value_type>>) {
1892 if (!QRangeModelDetails::isValid(target))
1893 target.reset(new wrapped_value_type);
1894 }
1895
1896 auto setRangeModelDataRole = [&target, &data]{
1897 constexpr auto targetMetaType = QMetaType::fromType<value_type>();
1898 const auto dataMetaType = data.metaType();
1899 constexpr bool isWrapped = QRangeModelDetails::is_wrapped<value_type>();
1900 if constexpr (!std::is_copy_assignable_v<wrapped_value_type>) {
1901 // we don't support replacing objects that are stored as raw pointers,
1902 // as this makes object ownership very messy. But we can replace objects
1903 // stored in smart pointers, and we can initialize raw nullptr objects.
1904 if constexpr (isWrapped) {
1905 constexpr bool is_raw_pointer = std::is_pointer_v<value_type>;
1906 if constexpr (!is_raw_pointer && std::is_copy_assignable_v<value_type>) {
1907 if (data.canConvert(targetMetaType)) {
1908 target = data.value<value_type>();
1909 return true;
1910 }
1911 } else if constexpr (is_raw_pointer) {
1912 if (!QRangeModelDetails::isValid(target) && data.canConvert(targetMetaType)) {
1913 target = data.value<value_type>();
1914 return true;
1915 }
1916 } else {
1917 Q_UNUSED(target);
1918 }
1919 }
1920 // Otherwise we have a move-only or polymorph type. fall through to
1921 // error handling.
1922 } else if constexpr (isWrapped) {
1923 if (QRangeModelDetails::isValid(target)) {
1924 auto &targetRef = QRangeModelDetails::refTo(target);
1925 // we need to get a wrapped value type out of the QVariant, which
1926 // might carry a pointer. We have to try all alternatives.
1927 if (const auto mt = QMetaType::fromType<wrapped_value_type>();
1928 data.canConvert(mt)) {
1929 targetRef = data.value<wrapped_value_type>();
1930 return true;
1931 } else if (const auto mtp = QMetaType::fromType<wrapped_value_type *>();
1932 data.canConvert(mtp)) {
1933 targetRef = *data.value<wrapped_value_type *>();
1934 return true;
1935 }
1936 }
1937 } else if (targetMetaType == dataMetaType) {
1938 QRangeModelDetails::refTo(target) = data.value<value_type>();
1939 return true;
1940 } else if (dataMetaType.flags() & QMetaType::PointerToGadget) {
1941 QRangeModelDetails::refTo(target) = *data.value<value_type *>();
1942 return true;
1943 }
1944#ifndef QT_NO_DEBUG
1945 qCritical("Not able to assign %s to %s",
1946 qPrintable(QDebug::toString(data)), targetMetaType.name());
1947#endif
1948 return false;
1949 };
1950
1951 if constexpr (QRangeModelDetails::item_access<wrapped_value_type>::hasWriteRole) {
1952 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<wrapped_value_type>;
1953 if (isRangeModelRole(role))
1954 return setRangeModelDataRole();
1955 return ItemAccess::writeRole(QRangeModelDetails::refTo(target), data, role);
1956 } else if constexpr (has_metaobject<value_type>) {
1957 if (row_traits::fixed_size() <= 1) { // multi-role value
1958 if (isRangeModelRole(role))
1959 return setRangeModelDataRole();
1960 return writeRole(role, QRangeModelDetails::pointerTo(target), data);
1961 } else if (column <= row_traits::fixed_size() // multi-column
1962 && (isPrimaryRole(role) || isRangeModelRole(role))) {
1963 return writeProperty(column, QRangeModelDetails::pointerTo(target), data);
1964 }
1965 } else if constexpr (multi_role::value) {
1966 Qt::ItemDataRole roleToSet = Qt::ItemDataRole(role);
1967 // If there is an entry for EditRole, overwrite that; otherwise,
1968 // set the entry for DisplayRole.
1969 const auto roleNames = [this]() -> QHash<int, QByteArray> {
1970 Q_UNUSED(this);
1971 if constexpr (!multi_role::int_key)
1972 return this->itemModel().roleNames();
1973 else
1974 return {};
1975 }();
1976 if (role == Qt::EditRole) {
1977 if constexpr (multi_role::int_key) {
1978 if (target.find(roleToSet) == target.end())
1979 roleToSet = Qt::DisplayRole;
1980 } else {
1981 if (target.find(roleNames.value(roleToSet)) == target.end())
1982 roleToSet = Qt::DisplayRole;
1983 }
1984 }
1985 if constexpr (multi_role::int_key)
1986 return write(target[roleToSet], data);
1987 else
1988 return write(target[roleNames.value(roleToSet)], data);
1989 } else if (isPrimaryRole(role) || isRangeModelRole(role)) {
1990 return write(target, data);
1991 }
1992 return false;
1993 };
1994
1995 if (!writeAt(index, writeData)) {
1996 emitDataChanged.dismiss();
1997 return false;
1998 } else if constexpr (itemsAreQObjects || rowsAreQObjects) {
1999 if (isRangeModelRole(role) && this->autoConnectPolicy() == AutoConnectPolicy::Full) {
2000 if (QObject *item = data.value<QObject *>())
2001 Self::connectProperties(index, item, m_data.context, m_data.properties);
2002 }
2003 }
2004 return true;
2005 }
2006 return false;
2007 }
2008
2009 template <typename LHS, typename RHS>
2010 void updateTarget(LHS &org, RHS &&copy) noexcept
2011 {
2012 if constexpr (std::is_pointer_v<RHS>)
2013 return;
2014 else if constexpr (std::is_assignable_v<LHS, RHS>)
2015 org = std::forward<RHS>(copy);
2016 else
2017 qSwap(org, copy);
2018 }
2019 template <typename LHS, typename RHS>
2020 void updateTarget(LHS *org, RHS &&copy) noexcept
2021 {
2022 updateTarget(*org, std::forward<RHS>(copy));
2023 }
2024
2025 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &data)
2026 {
2027 if (!index.isValid() || data.isEmpty())
2028 return false;
2029
2030 if constexpr (isMutable()) {
2031 auto emitDataChanged = qScopeGuard([this, &index, &data]{
2032 Q_EMIT this->dataChanged(index, index, data.keys());
2033 });
2034 // we emit dataChanged at the end, block dispatches from auto-connected properties
2035 [[maybe_unused]] auto dataChangedBlocker = maybeBlockDataChangedDispatch();
2036
2037 bool tried = false;
2038 auto writeItemData = [this, &tried, &data](auto &target) -> bool {
2039 Q_UNUSED(this);
2040 using value_type = q20::remove_cvref_t<decltype(target)>;
2041 using multi_role = QRangeModelDetails::is_multi_role<value_type>;
2042 using wrapped_value_type = QRangeModelDetails::wrapped_t<value_type>;
2043
2044 // transactional: if possible, modify a copy and only
2045 // update target if all values from data could be stored.
2046 auto makeCopy = [](const value_type &original){
2047 if constexpr (!std::is_copy_assignable_v<wrapped_value_type>)
2048 return QRangeModelDetails::pointerTo(original); // no transaction support
2049 else if constexpr (std::is_pointer_v<decltype(original)>)
2050 return *original;
2051 else if constexpr (std::is_copy_assignable_v<value_type>)
2052 return original;
2053 else
2054 return QRangeModelDetails::pointerTo(original);
2055 };
2056
2057 const auto roleNames = this->itemModel().roleNames();
2058
2059 if constexpr (QRangeModelDetails::item_access<wrapped_value_type>::hasWriteRole) {
2060 tried = true;
2061 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<wrapped_value_type>;
2062 const auto roles = roleNames.keys();
2063 auto targetCopy = makeCopy(target);
2064 for (int role : roles) {
2065 if (!ItemAccess::writeRole(QRangeModelDetails::refTo(targetCopy),
2066 data.value(role), role)) {
2067 return false;
2068 }
2069 }
2070 updateTarget(target, std::move(targetCopy));
2071 return true;
2072 } else if constexpr (multi_role()) {
2073 using key_type = typename value_type::key_type;
2074 tried = true;
2075 const auto roleName = [&roleNames](int role) {
2076 return roleNames.value(role);
2077 };
2078
2079 // transactional: only update target if all values from data
2080 // can be stored. Storing never fails with int-keys.
2081 if constexpr (!multi_role::int_key)
2082 {
2083 auto invalid = std::find_if(data.keyBegin(), data.keyEnd(),
2084 [&roleName](int role) { return roleName(role).isEmpty(); }
2085 );
2086
2087 if (invalid != data.keyEnd()) {
2088#ifndef QT_NO_DEBUG
2089 qWarning("No role name set for %d", *invalid);
2090#endif
2091 return false;
2092 }
2093 }
2094
2095 for (auto &&[role, value] : data.asKeyValueRange()) {
2096 if constexpr (multi_role::int_key)
2097 target[static_cast<key_type>(role)] = value;
2098 else
2099 target[QString::fromUtf8(roleName(role))] = value;
2100 }
2101 return true;
2102 } else if constexpr (has_metaobject<value_type>) {
2103 if (row_traits::fixed_size() <= 1) {
2104 tried = true;
2105 auto targetCopy = makeCopy(target);
2106 for (auto &&[role, value] : data.asKeyValueRange()) {
2107 if (isRangeModelRole(role))
2108 continue;
2109 if (!writeRole(role, QRangeModelDetails::pointerTo(targetCopy), value)) {
2110 const QByteArray roleName = roleNames.value(role);
2111#ifndef QT_NO_DEBUG
2112 qWarning("Failed to write value '%s' to role '%s'",
2113 qPrintable(QDebug::toString(value)), roleName.data());
2114#endif
2115 return false;
2116 }
2117 }
2118 updateTarget(target, std::move(targetCopy));
2119 return true;
2120 }
2121 }
2122 return false;
2123 };
2124
2125 if (!writeAt(index, writeItemData)) {
2126 emitDataChanged.dismiss();
2127 if (!tried)
2128 return this->itemModel().QAbstractItemModel::setItemData(index, data);
2129 }
2130 return true;
2131 }
2132 return false;
2133 }
2134
2135 bool clearItemData(const QModelIndex &index)
2136 {
2137 if (!index.isValid())
2138 return false;
2139
2140 if constexpr (isMutable()) {
2141 auto emitDataChanged = qScopeGuard([this, &index]{
2142 Q_EMIT this->dataChanged(index, index, {});
2143 });
2144
2145 auto clearData = [column = index.column()](auto &&target) {
2146 if constexpr (row_traits::hasMetaObject) {
2147 if (row_traits::fixed_size() <= 1) {
2148 // multi-role object/gadget: reset all properties
2149 return resetProperty(-1, QRangeModelDetails::pointerTo(target));
2150 } else if (column <= row_traits::fixed_size()) {
2151 return resetProperty(column, QRangeModelDetails::pointerTo(target));
2152 }
2153 } else { // normal structs, values, associative containers
2154 target = {};
2155 return true;
2156 }
2157 return false;
2158 };
2159
2160 if (!writeAt(index, clearData)) {
2161 emitDataChanged.dismiss();
2162 return false;
2163 }
2164 return true;
2165 }
2166 return false;
2167 }
2168
2170 {
2171 // will be 'void' if columns don't all have the same type
2172 using item_type = QRangeModelDetails::wrapped_t<typename row_traits::item_type>;
2173 using item_traits = typename QRangeModelDetails::item_traits<item_type>;
2174 return item_traits::roleNames(this);
2175 }
2176
2177 bool autoConnectPropertiesInRow(const row_type &row, int rowIndex, const QModelIndex &parent) const
2178 {
2179 if (!QRangeModelDetails::isValid(row))
2180 return true; // nothing to do
2181 return row_traits::for_each_element(QRangeModelDetails::refTo(row),
2182 this->itemModel().index(rowIndex, 0, parent),
2183 [this](const QModelIndex &index, const QObject *item) {
2184 if constexpr (isMutable())
2185 return Self::connectProperties(index, item, m_data.context, m_data.properties);
2186 else
2187 return Self::connectPropertiesConst(index, item, m_data.context, m_data.properties);
2188 });
2189 }
2190
2191 void clearConnectionInRow(const row_type &row, int rowIndex, const QModelIndex &parent) const
2192 {
2193 if (!QRangeModelDetails::isValid(row))
2194 return;
2195 row_traits::for_each_element(QRangeModelDetails::refTo(row),
2196 this->itemModel().index(rowIndex, 0, parent),
2197 [this](const QModelIndex &, const QObject *item) {
2198 m_data.connections.removeIf([item](const auto &connection) {
2199 return connection.sender == item;
2200 });
2201 return true;
2202 });
2203 }
2204
2206 {
2207 if constexpr (itemsAreQObjects || rowsAreQObjects) {
2208 using item_type = std::remove_pointer_t<typename row_traits::item_type>;
2209 using Mapping = QRangeModelDetails::AutoConnectContext::AutoConnectMapping;
2210
2211 delete m_data.context;
2212 m_data.connections = {};
2213 switch (this->autoConnectPolicy()) {
2214 case AutoConnectPolicy::None:
2215 m_data.context = nullptr;
2216 break;
2217 case AutoConnectPolicy::Full:
2218 m_data.context = new QRangeModelDetails::AutoConnectContext(&this->itemModel());
2219 if constexpr (itemsAreQObjects) {
2220 m_data.properties = QRangeModelImplBase::roleProperties(this->itemModel(),
2221 item_type::staticMetaObject);
2222 m_data.context->mapping = Mapping::Roles;
2223 } else {
2224 m_data.properties = QRangeModelImplBase::columnProperties(wrapped_row_type::staticMetaObject);
2225 m_data.context->mapping = Mapping::Columns;
2226 }
2227 if (!m_data.properties.isEmpty())
2228 that().autoConnectPropertiesImpl();
2229 break;
2230 case AutoConnectPolicy::OnRead:
2231 m_data.context = new QRangeModelDetails::AutoConnectContext(&this->itemModel());
2232 if constexpr (itemsAreQObjects) {
2233 m_data.context->mapping = Mapping::Roles;
2234 } else {
2235 m_data.properties = QRangeModelImplBase::columnProperties(wrapped_row_type::staticMetaObject);
2236 m_data.context->mapping = Mapping::Columns;
2237 }
2238 break;
2239 }
2240 } else {
2241#ifndef QT_NO_DEBUG
2242 qWarning("All items in the range must be QObject subclasses");
2243#endif
2244 }
2245 }
2246
2247 struct unordered {
2248 friend constexpr bool operator<(unordered, QtPrivate::CompareAgainstLiteralZero) noexcept
2249 { return false; }
2250 friend constexpr bool operator>(unordered, QtPrivate::CompareAgainstLiteralZero) noexcept
2251 { return false; }
2252 };
2253
2254 struct Compare
2255 {
2256 template <typename C, typename LessThan>
2257 using sortMember_test = decltype(std::declval<C&>().sort(std::declval<LessThan &&>()));
2258 static constexpr bool hasSortMember = qxp::is_detected_v<sortMember_test, range_type, Compare>;
2259
2260 template <typename Stringish>
2261 using collatedCompare_test = decltype(
2262 std::declval<const QCollator&>().compare(std::declval<const Stringish&>(),
2263 std::declval<const Stringish&>())
2264 );
2265 template <typename Stringish>
2267 Stringish>;
2268
2269
2270 Compare(const QRangeModelImpl *impl, int column, Qt::SortOrder order)
2271 : that(impl), m_index(impl->createIndex(-1, column, nullptr))
2272 , collator(impl->sortCollator()), m_order(order), m_sortRole(impl->sortRole())
2273 {
2274 }
2275
2276 template <typename Item>
2277 auto operator()(const Item &lhs, const Item &rhs) const
2278 {
2279 auto ordering = compare(lhs, rhs);
2280 return m_order == Qt::AscendingOrder ? ordering < 0 : ordering > 0;
2281 }
2282
2283 template <typename Item>
2284 auto compare(const Item &lhs, const Item &rhs) const
2285 {
2286 using value_type = QRangeModelDetails::wrapped_t<Item>;
2287 using multi_role = QRangeModelDetails::is_multi_role<value_type>;
2288
2289 if constexpr (QRangeModelDetails::item_access<value_type>::hasReadRole
2290 || multi_role() || has_metaobject<value_type>) {
2291 QModelRoleData result(m_sortRole);
2292 // Minor abuse of QModelIndex: the reader needs an index to implement
2293 // lazy auto-connections, but we only have a column. So we construct
2294 // an invalid QModelIndex that carries only that column value. That's
2295 // enough for reading values, and the auto-connection logic skips for
2296 // invalid indexes.
2297 ItemReader reader{m_index, result, that};
2298 Q_ASSERT(!reader.index.isValid());
2299 reader(lhs);
2300 const QVariant lhsVariant = std::move(result.data());
2301 reader(rhs);
2302 const QVariant rhsVariant = std::move(result.data());
2303 return QRangeModelImplBase::compareData(lhsVariant, rhsVariant, collator);
2304 } else if constexpr (std::is_same_v<QVariant, value_type>) {
2305 return QRangeModelImplBase::compareData(lhs, rhs, collator);
2306 } else if constexpr (QtOrderingPrivate::CompareThreeWayTester::hasCompareThreeWay_v
2307 <value_type, value_type>) {
2308 // all types supported by QCollator are also three-way comparable
2309 if constexpr (hasCollatedCompare<value_type>) {
2310 if (collator) {
2311 using ordering = decltype(qCompareThreeWay(lhs, rhs));
2312 int res = collator->compare(lhs, rhs);
2313 if (res < 0)
2314 return ordering::less;
2315 if (res > 0)
2316 return ordering::greater;
2317 return ordering::equal;
2318 }
2319 }
2320 return qCompareThreeWay(lhs, rhs);
2321 } else {
2322 return unordered{};
2323 }
2324 }
2325
2326 bool checkComparable() const
2327 {
2328 return that->readAt(that->index(0, 0, {}), [this](const auto &item){
2329 // before we call std::stable_sort, check that we can compare the
2330 // types we'll ultimately get called with. This doesn't catch cases
2331 // where we end up comparing QVariant, and we cannot make this a
2332 // compile time check as long as readAt etc are not constexpr.
2333 using ordering = decltype(compare(item, item));
2334 if constexpr (std::is_same_v<ordering, unordered>) {
2335#ifndef QT_NO_DEBUG
2336 const QMetaType itemtype = QMetaType::fromType<QRangeModelDetails::wrapped_t<
2337 q20::remove_cvref_t<decltype(item)>>
2338 >();
2339 qCritical("QRangeModel: Cannot compare items of type %s in column %d!",
2340 itemtype.name(), m_index.column());
2341#else
2342 Q_UNUSED(this);
2343#endif
2344 return false;
2345 } else {
2346 return true;
2347 }
2348 });
2349 }
2350
2351 template <typename Item>
2352 static std::optional<bool> compareInvalid(const Item &lhs, const Item &rhs)
2353 {
2354 // invalid data > valid data
2355 if (!QRangeModelDetails::isValid(lhs))
2356 return false;
2357 if (!QRangeModelDetails::isValid(rhs))
2358 return true;
2359 return std::nullopt;
2360 }
2361
2362 const QRangeModelImpl * const that;
2364 const QCollator * const collator;
2366 const int m_sortRole;
2367 };
2368
2369 void sort(int column, Qt::SortOrder order)
2370 {
2371 if constexpr (isMutable() && std::is_swappable_v<row_type>) {
2372 if (rowCount({}) < 2 || column >= columnCount({}))
2373 return;
2374 Compare compare(this, column, order);
2375 if (!compare.checkComparable())
2376 return;
2377
2378 this->beginLayoutChange();
2379 QScopeGuard endLayoutChange([this]{ this->endLayoutChange(); });
2380 that().sortImpl([&compare](const auto &leftRow, const auto &rightRow) {
2381 if (auto anyInvalid = Compare::compareInvalid(leftRow, rightRow))
2382 return *anyInvalid;
2383 return row_traits::for_element_at(leftRow, compare.m_index.column(),
2384 [&rightRow, &compare](const auto &leftItem){
2385 return row_traits::for_element_at(rightRow, compare.m_index.column(),
2386 [&leftItem, &compare](const auto &rightItem){
2387 // Called by std::stable_sort. Since "column" is a runtime value, we
2388 // can't statically assert that lhs and rhs are of the same type.
2389 if constexpr (std::is_same_v<decltype(leftItem), decltype(rightItem)>) {
2390 if (auto anyInvalid = Compare::compareInvalid(leftItem, rightItem))
2391 return *anyInvalid;
2392 return compare(QRangeModelDetails::refTo(leftItem),
2393 QRangeModelDetails::refTo(rightItem));
2394 } else {
2395 Q_UNREACHABLE();
2396 }
2397 return false;
2398 });
2399 });
2400 });
2401 }
2402 }
2403
2404 template <typename LessThan>
2405 void sortSubRange(range_type &range, row_ptr expectedParent, const LessThan &lessThan)
2406 {
2407 auto begin = QRangeModelDetails::adl_begin(range);
2408 auto end = QRangeModelDetails::adl_end(range);
2409 if (begin == end)
2410 return;
2411
2412 QModelIndexList persistentIndexes = this->persistentIndexList();
2413 that().prunePersistentIndexList(persistentIndexes, expectedParent);
2414
2415 if (persistentIndexes.isEmpty()) {
2416 using It = typename range_features::iterator;
2417 constexpr bool is_random_access = std::is_base_of_v<std::random_access_iterator_tag,
2418 typename std::iterator_traits<It>::iterator_category>;
2419 // fast path if we have no persistent indexes: sort the range in place
2420 if constexpr (Compare::hasSortMember) {
2421 range.sort(lessThan);
2422 return;
2423 } else if constexpr (is_random_access) {
2424 std::stable_sort(begin, end, lessThan);
2425 return;
2426 }
2427 }
2428
2429 // slow path: create an indexed version of the range by adding a
2430 // column that records row movements.
2431 struct SortTracker
2432 {
2433 row_type row;
2434 int index;
2435 };
2436
2437 const int rangeSize = size(range);
2438 // Allocate all necessary memory here so that a potential exception
2439 // gets thrown before we have made any modifications.
2440 std::vector<SortTracker> tracked;
2441 tracked.reserve(rangeSize);
2442 std::vector<int> newRows;
2443 newRows.resize(rangeSize);
2444
2445 // move all rows into that index paired with its unsorted position
2446 int row = -1;
2447 for (auto &&it = std::move_iterator(begin); it != std::move_iterator(end); ++it)
2448 tracked.emplace_back(SortTracker{*it, ++row});
2449
2450 // sort the index based on a comparions of the data
2451 std::stable_sort(tracked.begin(), tracked.end(),
2452 [&lessThan](const SortTracker &lhs, const SortTracker &rhs){
2453 return lessThan(lhs.row, rhs.row);
2454 });
2455
2456 // write the values back to the range in (now ordered) sequence,
2457 // and create a mapping from old to new row
2458 auto sorted = std::move_iterator(tracked.begin());
2459 auto write = QRangeModelDetails::adl_begin(range);
2460 qsizetype changedIndexCount = 0;
2461 for (int newIndex = 0; newIndex < rangeSize; ++write, ++sorted, ++newIndex) {
2462 auto &&tracker = *sorted;
2463 changedIndexCount += (tracker.index != newIndex);
2464 *write = std::move(tracker.row);
2465 newRows[tracker.index] = newIndex;
2466 }
2467
2468 // free memory from intermediate vector
2469 tracked.clear();
2470 tracked.shrink_to_fit();
2471
2472 // update relevant persistent model indexes
2473 for (const auto &fromIndex : std::as_const(persistentIndexes)) {
2474 const int newRow = newRows.at(fromIndex.row());
2475 if (fromIndex.row() == newRow)
2476 continue;
2477 const QModelIndex toIndex = that().indexImpl(newRow,
2478 fromIndex.column(),
2479 fromIndex.parent());
2480 this->changePersistentIndex(fromIndex, toIndex);
2481 }
2482 }
2483
2484 QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits,
2485 Qt::MatchFlags flags) const
2486 {
2487 return that().matchImpl(start, role,
2488 QRangeModelImplBase::convertMatchValue(value, flags), hits, flags);
2489 }
2490
2491 bool matchRow(const_row_reference row, const QModelIndex &index, int role, const QVariant &value,
2492 Qt::MatchFlags flags) const
2493 {
2494 const uint matchType = (flags & Qt::MatchTypeMask).toInt();
2495
2496 return row_traits::for_element_at(row, index.column(), [&](const auto &element) {
2497 using value_type = q20::remove_cvref_t<decltype(element)>;
2498 using wrapped_value_type = QRangeModelDetails::wrapped_t<value_type>;
2499 using multi_role = QRangeModelDetails::is_multi_role<value_type>;
2500
2501 if constexpr (QRangeModelDetails::item_access<wrapped_value_type>::hasReadRole
2502 || multi_role() || has_metaobject<value_type>) {
2503 QModelRoleData roleData(role);
2504 ItemReader reader{index, roleData, this};
2505 reader(element);
2506 return QRangeModelImplBase::matchValue(roleData.data(), value, flags);
2507 } else if constexpr (std::is_same_v<wrapped_value_type, QVariant>) {
2508 return QRangeModelImplBase::matchValue(element, value, flags);
2509 } else {
2510 constexpr QMetaType mt = QMetaType::fromType<wrapped_value_type>();
2511 if (mt == value.metaType()) {
2512 if (matchType == Qt::MatchExactly)
2513 return mt.equals(QRangeModelDetails::pointerTo(element), value.constData());
2514 else if constexpr (std::is_same_v<wrapped_value_type, QString>)
2515 return QRangeModelImplBase::matchValue(element, value, flags);
2516 } else {
2517 return QRangeModelImplBase::matchValue(QVariant::fromValue(QRangeModelDetails::refTo(element)),
2518 value, flags);
2519 }
2520 }
2521 return false;
2522 });
2523 }
2524
2525 template <typename InsertFn>
2526 bool doInsertColumns(int column, int count, const QModelIndex &parent, InsertFn insertFn)
2527 {
2528 if (count == 0)
2529 return false;
2530 range_type * const children = childRange(parent);
2531 if (!children)
2532 return false;
2533
2534 this->beginInsertColumns(parent, column, column + count - 1);
2535
2536 for (auto &child : *children) {
2537 auto it = QRangeModelDetails::pos(child, column);
2538 (void)insertFn(QRangeModelDetails::refTo(child), it, count);
2539 }
2540
2541 this->endInsertColumns();
2542
2543 // endInsertColumns emits columnsInserted, at which point clients might
2544 // have populated the new columns with objects (if the columns aren't objects
2545 // themselves).
2546 if constexpr (itemsAreQObjects) {
2547 if (m_data.context && this->autoConnectPolicy() == AutoConnectPolicy::Full) {
2548 for (int r = 0; r < that().rowCount(parent); ++r) {
2549 for (int c = column; c < column + count; ++c) {
2550 const QModelIndex index = that().index(r, c, parent);
2551 writeAt(index, [this, &index](QObject *item){
2552 return Self::connectProperties(index, item,
2553 m_data.context, m_data.properties);
2554 });
2555 }
2556 }
2557 }
2558 }
2559
2560 return true;
2561 }
2562
2563 bool insertColumns(int column, int count, const QModelIndex &parent)
2564 {
2565 if constexpr (dynamicColumns() && isMutable() && row_features::has_insert) {
2566 return doInsertColumns(column, count, parent, [](auto &row, auto it, int n){
2567 row.insert(it, n, {});
2568 return true;
2569 });
2570 } else {
2571 return false;
2572 }
2573 }
2574
2575 bool removeColumns(int column, int count, const QModelIndex &parent)
2576 {
2577 if constexpr (dynamicColumns() && isMutable() && row_features::has_erase) {
2578 if (column < 0 || column + count > columnCount(parent))
2579 return false;
2580
2581 range_type * const children = childRange(parent);
2582 if (!children)
2583 return false;
2584
2585 if constexpr (itemsAreQObjects) {
2586 if (m_data.context && this->autoConnectPolicy() == AutoConnectPolicy::OnRead) {
2587 for (int r = 0; r < that().rowCount(parent); ++r) {
2588 for (int c = column; c < column + count; ++c) {
2589 const QModelIndex index = that().index(r, c, parent);
2590 writeAt(index, [this](QObject *item){
2591 m_data.connections.removeIf([item](const auto &connection) {
2592 return connection.sender == item;
2593 });
2594 return true;
2595 });
2596 }
2597 }
2598 }
2599 }
2600
2601 this->beginRemoveColumns(parent, column, column + count - 1);
2602 for (auto &child : *children) {
2603 const auto start = QRangeModelDetails::pos(child, column);
2604 QRangeModelDetails::refTo(child).erase(start, std::next(start, count));
2605 }
2606 this->endRemoveColumns();
2607 return true;
2608 }
2609 return false;
2610 }
2611
2612 bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count,
2613 const QModelIndex &destParent, int destColumn)
2614 {
2615 // we only support moving columns within the same parent
2616 if (sourceParent != destParent)
2617 return false;
2618 if constexpr (isMutable() && (row_features::has_rotate || row_features::has_splice)) {
2619 if (!Structure::canMoveColumns(sourceParent, destParent))
2620 return false;
2621
2622 if constexpr (dynamicColumns()) {
2623 // we only support ranges as columns, as other types might
2624 // not have the same data type across all columns
2625 range_type * const children = childRange(sourceParent);
2626 if (!children)
2627 return false;
2628
2629 if (!this->beginMoveColumns(sourceParent, sourceColumn, sourceColumn + count - 1,
2630 destParent, destColumn)) {
2631 return false;
2632 }
2633
2634 for (auto &child : *children)
2635 QRangeModelDetails::rotate(child, sourceColumn, count, destColumn);
2636
2637 this->endMoveColumns();
2638 return true;
2639 }
2640 }
2641 return false;
2642 }
2643
2644 template <typename InsertFn>
2645 bool doInsertRows(int row, int count, const QModelIndex &parent, InsertFn &&insertFn)
2646 {
2647 range_type *children = childRange(parent);
2648 if (!children)
2649 return false;
2650
2651 this->beginInsertRows(parent, row, row + count - 1);
2652
2653 row_ptr parentRow = parent.isValid()
2654 ? QRangeModelDetails::pointerTo(this->rowData(parent))
2655 : nullptr;
2656 (void)std::forward<InsertFn>(insertFn)(*children, parentRow, row, count);
2657
2658 // fix the parent in all children of the modified row, as the
2659 // references back to the parent might have become invalid.
2660 that().resetParentInChildren(children);
2661
2662 this->endInsertRows();
2663
2664 // endInsertRows emits rowsInserted, at which point clients might
2665 // have populated the new row with objects (if the rows aren't objects
2666 // themselves).
2667 if constexpr (itemsAreQObjects || rowsAreQObjects) {
2668 if (m_data.context && this->autoConnectPolicy() == AutoConnectPolicy::Full) {
2669 const auto begin = QRangeModelDetails::pos(children, row);
2670 const auto end = std::next(begin, count);
2671 int rowIndex = row;
2672 for (auto it = begin; it != end; ++it, ++rowIndex)
2673 autoConnectPropertiesInRow(*it, rowIndex, parent);
2674 }
2675 }
2676
2677 return true;
2678 }
2679
2680 bool insertRows(int row, int count, const QModelIndex &parent)
2681 {
2682 if constexpr (canInsertRows()) {
2683 return doInsertRows(row, count, parent,
2684 [this](range_type &children, row_ptr parentRow, int r, int n){
2685 EmptyRowGenerator generator{0, &that(), parentRow};
2686
2687 const auto pos = QRangeModelDetails::pos(children, r);
2688 if constexpr (range_features::has_insert_range) {
2689 children.insert(pos, std::move(generator), EmptyRowGenerator{n});
2690 } else if constexpr (rows_are_owning_or_raw_pointers) {
2691 auto start = children.insert(pos, n, nullptr); // MSVC doesn't like row_type{}
2692 std::copy(std::move(generator), EmptyRowGenerator{n}, start);
2693 } else {
2694 children.insert(pos, n, std::move(*generator));
2695 }
2696 return true;
2697 });
2698 } else {
2699 return false;
2700 }
2701 }
2702
2703 bool removeRows(int row, int count, const QModelIndex &parent = {})
2704 {
2705 if constexpr (canRemoveRows()) {
2706 const int prevRowCount = rowCount(parent);
2707 if (row < 0 || row + count > prevRowCount)
2708 return false;
2709
2710 range_type *children = childRange(parent);
2711 if (!children)
2712 return false;
2713
2714 if constexpr (itemsAreQObjects || rowsAreQObjects) {
2715 if (m_data.context && this->autoConnectPolicy() == AutoConnectPolicy::OnRead) {
2716 const auto begin = QRangeModelDetails::pos(children, row);
2717 const auto end = std::next(begin, count);
2718 int rowIndex = row;
2719 for (auto it = begin; it != end; ++it, ++rowIndex)
2720 clearConnectionInRow(*it, rowIndex, parent);
2721 }
2722 }
2723
2724 this->beginRemoveRows(parent, row, row + count - 1);
2725 [[maybe_unused]] bool callEndRemoveColumns = false;
2726 if constexpr (dynamicColumns()) {
2727 // if we remove the last row in a dynamic model, then we no longer
2728 // know how many columns we should have, so they will be reported as 0.
2729 if (prevRowCount == count) {
2730 if (const int columns = columnCount(parent)) {
2731 callEndRemoveColumns = true;
2732 this->beginRemoveColumns(parent, 0, columns - 1);
2733 }
2734 }
2735 }
2736 { // erase invalidates iterators
2737 const auto begin = QRangeModelDetails::pos(children, row);
2738 const auto end = std::next(begin, count);
2739 that().deleteRemovedRows(begin, end);
2740 children->erase(begin, end);
2741 }
2742 // fix the parent in all children of the modified row, as the
2743 // references back to the parent might have become invalid.
2744 that().resetParentInChildren(children);
2745
2746 if constexpr (dynamicColumns()) {
2747 if (callEndRemoveColumns) {
2748 Q_ASSERT(columnCount(parent) == 0);
2749 this->endRemoveColumns();
2750 }
2751 }
2752 this->endRemoveRows();
2753 return true;
2754 } else {
2755 return false;
2756 }
2757 }
2758
2759 bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
2760 const QModelIndex &destParent, int destRow)
2761 {
2762 if constexpr (isMutable() && (range_features::has_rotate || range_features::has_splice)) {
2763 if (!Structure::canMoveRows(sourceParent, destParent))
2764 return false;
2765
2766 if (sourceParent != destParent) {
2767 return that().moveRowsAcross(sourceParent, sourceRow, count,
2768 destParent, destRow);
2769 }
2770
2771 if (sourceRow == destRow || sourceRow == destRow - 1 || count <= 0
2772 || sourceRow < 0 || sourceRow + count - 1 >= this->rowCount(sourceParent)
2773 || destRow < 0 || destRow > this->rowCount(destParent)) {
2774 return false;
2775 }
2776
2777 range_type *source = childRange(sourceParent);
2778 // moving within the same range
2779 if (!this->beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1, destParent, destRow))
2780 return false;
2781
2782 QRangeModelDetails::rotate(source, sourceRow, count, destRow);
2783
2784 that().resetParentInChildren(source);
2785
2786 this->endMoveRows();
2787 return true;
2788 } else {
2789 return false;
2790 }
2791 }
2792
2793 const protocol_type& protocol() const { return QRangeModelDetails::refTo(ProtocolStorage::object()); }
2794 protocol_type& protocol() { return QRangeModelDetails::refTo(ProtocolStorage::object()); }
2795
2796 QModelIndex parent(const QModelIndex &child) const { return that().parentImpl(child); }
2797
2798 int rowCount(const QModelIndex &parent) const { return that().rowCountImpl(parent); }
2799
2800 static constexpr int fixedColumnCount()
2801 {
2802 if constexpr (one_dimensional_range)
2803 return row_traits::fixed_size();
2804 else
2805 return static_column_count;
2806 }
2807 int columnCount(const QModelIndex &parent) const { return that().columnCountImpl(parent); }
2808
2809 void destroy() { delete std::addressof(that()); }
2810
2811 Qt::DropActions adjustSupportedDragActions(Qt::DropActions dragActions) {
2812 if constexpr (!isMutable())
2813 dragActions &= ~Qt::MoveAction;
2814 return dragActions;
2815 }
2816 Qt::DropActions adjustSupportedDropActions(Qt::DropActions dropActions)
2817 {
2818 if constexpr (!isMutable())
2819 dropActions = Qt::IgnoreAction;
2820
2821 return dropActions;
2822 }
2823
2825 {
2826 using ItemType = QRangeModelDetails::wrapped_t<typename row_traits::item_type>;
2827 if constexpr (QRangeModelDetails::item_access<ItemType>::hasMimeTypes)
2828 return QRangeModelDetails::QRangeModelItemAccess<ItemType>::mimeTypes();
2829 else if constexpr (QRangeModelDetails::hasMimeTypes<wrapped_row_type>)
2830 return QRangeModelDetails::QRangeModelRowOptions<wrapped_row_type>::mimeTypes();
2831 else
2832 return this->itemModel().QAbstractItemModel::mimeTypes();
2833 }
2834
2835 bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
2836 const QModelIndex &target) const
2837 {
2838 if constexpr (isMutable()) {
2839 bool canDrop;
2840 using RowOptions = QRangeModelDetails::QRangeModelRowOptions<wrapped_row_type>;
2841 using ItemType = QRangeModelDetails::wrapped_t<typename row_traits::item_type>;
2842 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<ItemType>;
2843 if constexpr (QRangeModelDetails::item_access<ItemType>::hasCanDropMimeDataFull) {
2844 canDrop = ItemAccess::canDropMimeData(data, action, row, column, target);
2845 } else if constexpr (QRangeModelDetails::hasCanDropMimeDataFull<wrapped_row_type>) {
2846 canDrop = RowOptions::canDropMimeData(data, action, row, column, target);
2847 } else {
2848 canDrop = this->itemModel().QAbstractItemModel::canDropMimeData(data, action, row,
2849 column, target);
2850 if constexpr (QRangeModelDetails::item_access<ItemType>::hasCanDropMimeData)
2851 canDrop &= ItemAccess::canDropMimeData(data);
2852 else if constexpr (QRangeModelDetails::hasCanDropMimeData<wrapped_row_type>)
2853 canDrop &= RowOptions::canDropMimeData(data);
2854 }
2855 return canDrop;
2856 } else {
2857 return false;
2858 }
2859 }
2860
2861 // orientation == vertical: we drop rows; otherwise we drop individual items
2862 template <Qt::Orientation orient, typename Entry>
2863 bool doDropMimeData(std::vector<QRangeModelDetails::DroppedEntry<Entry>> &droppedEntries,
2864 DropOperation dropOperation, int row, int column, const QModelIndex &target)
2865 {
2866 using DroppedEntry = QRangeModelDetails::DroppedEntry<Entry>;
2867 using Cell = typename DroppedEntry::Cell;
2868 if (dropOperation == DropOperation::DontDrop)
2869 return false;
2870
2871 const bool dropOnTarget = row == -1 && column == -1 && target.isValid();
2872 const QModelIndex parent = dropOperation == DropOperation::InsertAsChildren
2873 ? target.siblingAtColumn(0) : target.parent();
2874
2875 Cell lastCell;
2876 if constexpr (orient == Qt::Horizontal)
2877 lastCell = {0, -1};
2878 else
2879 lastCell = {-1, 0};
2880 int bottomRow = -1;
2881 int rightColumn = -1;
2882 // set the target cell for all dropped entries and find the bottom-right
2883 // cell relative to the drop position
2884 int maxColumn = that().columnCount(parent) - 1;
2885 for (auto &droppedEntry : droppedEntries) {
2886 if (droppedEntry.m_cell == Cell{-1, -1}) {
2887 if constexpr (orient == Qt::Horizontal) {
2888 // auto-inserted items fill all columns before moving to
2889 // the next row
2890 droppedEntry.m_cell = {lastCell.m_row, lastCell.m_column + 1};
2891 if (droppedEntry.m_cell.m_column > maxColumn) {
2892 droppedEntry.m_cell.m_column = 0;
2893 ++droppedEntry.m_cell.m_row;
2894 }
2895 } else {
2896 droppedEntry.m_cell = {lastCell.m_row + 1, lastCell.m_column};
2897 }
2898 }
2899 lastCell = droppedEntry.m_cell;
2900 bottomRow = std::max(lastCell.m_row, bottomRow);
2901 rightColumn = std::max(lastCell.m_column, rightColumn);
2902 }
2903
2904 if (dropOperation == DropOperation::InsertAsChildren) {
2905 row = rowCount(parent);
2906 column = 0;
2907 } else if (dropOnTarget) {
2908 row = target.row();
2909 column = target.column();
2910 } else {
2911 if (row < 0)
2912 row = rowCount(parent);
2913 // dropping into empty space to the right of a table doesn't widen
2914 if (column < 0)
2915 column = 0;
2916 }
2917 const bool overwrite = dropOperation == DropOperation::OverwriteAndIgnore
2918 || dropOperation == DropOperation::OverwriteAndExtend;
2919
2920 // Compute if we need more rows, and try to add them. Abort if that fails.
2921 const int overwriteRows = overwrite
2922 ? std::min(bottomRow + 1, rowCount(parent) - row)
2923 : 0;
2924 const int newRows = dropOperation == DropOperation::OverwriteAndExtend
2925 ? bottomRow - overwriteRows + 1
2926 : (dropOperation == DropOperation::InsertAsChildren
2927 || dropOperation == DropOperation::InsertAsSiblings)
2928 ? bottomRow + 1
2929 : 0;
2930 if (newRows > 0 && !insertRows(row, newRows, parent))
2931 return false;
2932
2933 // Ditto for columns, but InsertAsSiblings/Children only applies to rows
2934 const int overwriteColumns = overwrite
2935 ? std::min(rightColumn + 1, columnCount(parent) - column)
2936 : 0;
2937 const int newColumns = dropOperation == DropOperation::OverwriteAndExtend
2938 ? rightColumn - overwriteColumns + 1 : 0;
2939 if (newColumns > 0 && !insertColumns(column, newColumns, parent))
2940 return false;
2941
2942 // access the target range
2943 range_type *parentRange = that().childRange(parent);
2944 if (!parentRange)
2945 return false;
2946 range_type &targetRange = *parentRange;
2947
2948 int maxRow = that().rowCount(parent) - 1;
2949 maxColumn = that().columnCount(parent) - 1;
2950 auto begin = std::move_iterator(droppedEntries.begin());
2951 auto end = std::move_iterator(droppedEntries.end());
2952 for (; begin != end; ++begin) {
2953 DroppedEntry droppedEntry = *begin;
2954 const Cell cell = {droppedEntry.m_cell.m_row + row, droppedEntry.m_cell.m_column + column};
2955 if (cell.m_row > maxRow || cell.m_column > maxColumn) // Ignore
2956 continue;
2957 auto writeRow = QRangeModelDetails::pos(targetRange, cell.m_row);
2958 if constexpr (orient == Qt::Vertical) { // complete rows
2959 if constexpr (QRangeModelDetails::is_owning_or_raw_pointer<row_type>()) {
2960 if (!*writeRow)
2961 *writeRow = this->protocol().newRow();
2962 **writeRow = std::move(droppedEntry);
2963 } else {
2964 *writeRow = std::move(droppedEntry);
2965 }
2966 } else {
2967 row_traits::for_element_at(*writeRow, cell.m_column, [&](auto &item){
2968 using item_type = q20::remove_cvref_t<decltype(item)>;
2969 using wrapped_item_type = QRangeModelDetails::wrapped_t<item_type>;
2970 if constexpr (QRangeModelDetails::is_any_owning_ptr<item_type>()) {
2971 if (!QRangeModelDetails::isValid(item))
2972 item.reset(new wrapped_item_type{std::move(droppedEntry)});
2973 else
2974 *item = std::move(droppedEntry);
2975 } else if (!QRangeModelDetails::isValid(item)) {
2976 return false;
2977 } else {
2978 item = std::move(droppedEntry);
2979 }
2980 return true;
2981 });
2982 }
2983 }
2984
2985 that().resetParentInChildren(&targetRange);
2986
2987 const QModelIndex topLeft = index(row, column, parent);
2988 const QModelIndex bottomRight = orient == Qt::Horizontal
2989 ? sibling(row + bottomRow, column + rightColumn, topLeft)
2990 : sibling(row + bottomRow, maxColumn, topLeft);
2991 this->dataChanged(topLeft, bottomRight, {});
2992
2993 return true;
2994 }
2995
2996 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
2997 const QModelIndex &target)
2998 {
2999 if constexpr (isMutable()) {
3000 if (!canDropMimeData(data, action, row, column, target))
3001 return false;
3002
3003 const bool dropOnTarget = row == -1 && column == -1 && target.isValid();
3004
3005 auto automaticDropOption = [=](auto dropResult){
3006 DropOperation dropOperation;
3007 if constexpr (std::is_same_v<bool, decltype(dropResult)>) {
3008 dropOperation = dropResult ? DropOperation::Automatic
3009 : DropOperation::DontDrop;
3010 } else { // it's a QRangeModel::DropOperation
3011 dropOperation = static_cast<DropOperation>(dropResult);
3012 }
3013
3014 if (dropOperation == DropOperation::Automatic) {
3015 if constexpr (!canInsertRows())
3016 dropOperation = DropOperation::OverwriteAndIgnore;
3017 else if (!dropOnTarget)
3018 dropOperation = DropOperation::InsertAsSiblings;
3019 else if (target.siblingAtColumn(0).flags().testFlag(Qt::ItemNeverHasChildren))
3020 dropOperation = DropOperation::OverwriteAndExtend;
3021 else
3022 dropOperation = DropOperation::InsertAsChildren;
3023 }
3024 return dropOperation;
3025 };
3026
3027 using ItemType = QRangeModelDetails::wrapped_t<typename row_traits::item_type>;
3028 if constexpr (QRangeModelDetails::item_access<ItemType>::hasDropMimeDataFull
3029 || QRangeModelDetails::item_access<ItemType>::hasDropMimeData) {
3030 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<ItemType>;
3031 using DroppedItem = QRangeModelDetails::DroppedEntry<ItemType>;
3032 std::vector<DroppedItem> droppedItems;
3033 DropOperation dropOperation = automaticDropOption([&]{
3034 auto inserter = std::back_inserter(droppedItems);
3035 if constexpr (QRangeModelDetails::item_access<ItemType>::hasDropMimeDataFull)
3036 return ItemAccess::dropMimeData(data, action, row, column, target, inserter);
3037 else
3038 return ItemAccess::dropMimeData(data, inserter);
3039 }());
3040 if (doDropMimeData<Qt::Horizontal>(droppedItems, dropOperation, row, column, target))
3041 return true;
3042 // fall through to try the default mime type
3043 } else if constexpr (QRangeModelDetails::hasDropMimeDataFull<wrapped_row_type>
3044 || QRangeModelDetails::hasDropMimeData<wrapped_row_type>) {
3045 using RowOptions = QRangeModelDetails::QRangeModelRowOptions<wrapped_row_type>;
3046 using DroppedRow = QRangeModelDetails::DroppedEntry<wrapped_row_type>;
3047 std::vector<DroppedRow> droppedRows;
3048 DropOperation dropOperation = automaticDropOption([&]{
3049 auto inserter = std::back_inserter(droppedRows);
3050 if constexpr (QRangeModelDetails::hasDropMimeDataFull<wrapped_row_type>)
3051 return RowOptions::dropMimeData(data, action, row, column, target, inserter);
3052 else
3053 return RowOptions::dropMimeData(data, inserter);
3054 }());
3055 if (doDropMimeData<Qt::Vertical>(droppedRows, dropOperation, row, column, target))
3056 return true;
3057 }
3058 // default mime type handling: dropping on item -> try to set the data
3059 if (dropOnTarget && that().dropOnItem(data, target))
3060 return true;
3061 }
3062 return false;
3063 }
3064
3065 // A bidirectional-iterator that, given a list of QModelIndex, dereferences
3066 // to a list of rows or items, plus QModelIndex, without copying any data.
3067 // For segments in indexes covering full rows, we skip over the individual
3068 // indexes and give the dereferenced index a column value of -1.
3070 {
3073 using iterator_category = std::bidirectional_iterator_tag;
3075 using reference [[maybe_unused]] = value_type;
3077 using pointer [[maybe_unused]] = void;
3078
3080 MimeDataRowIterator(base_iterator it, base_iterator begin, base_iterator end,
3081 const QRangeModelImpl *model)
3082 : m_it(it), m_begin(begin), m_end(end), m_model(model)
3083 , m_columnCount(model->columnCount({}))
3084 {
3085 updateCurrentIndexFullRow();
3086 }
3087
3089 {
3090 const QModelIndex &index = *m_it;
3091 return {m_model->rowData(index),
3092 m_currentIndexIsFullRow
3093 ? m_model->createIndex(m_it->row(), -1, m_it->internalPointer()) : index};
3094 }
3095
3097 if (m_currentIndexIsFullRow)
3098 m_it += m_columnCount;
3099 else
3100 ++m_it;
3101 updateCurrentIndexFullRow();
3102 return *this;
3103 }
3104 MimeDataRowIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
3105
3107 --m_it;
3108 m_currentIndexIsFullRow = false;
3109 const int lastColumn = m_columnCount - 1;
3110 if (m_it - m_begin >= lastColumn && m_it->column() == lastColumn) {
3111 const QModelIndex &firstInRow = m_it[-lastColumn];
3112 if (m_it->row() == firstInRow.row()
3113 && firstInRow.internalPointer() == m_it->internalPointer()) {
3114 m_currentIndexIsFullRow = true;
3115 m_it -= lastColumn;
3116 }
3117 }
3118 return *this;
3119 }
3120 MimeDataRowIterator operator--(int) { auto tmp = *this; --(*this); return tmp; }
3121
3122 MimeDataRowIterator operator-(difference_type n) const
3123 {
3124 auto tmp = *this; tmp.m_it -= n; return tmp;
3125 }
3126
3127 bool operator==(const MimeDataRowIterator &other) const { return m_it == other.m_it; }
3128 bool operator!=(const MimeDataRowIterator &other) const { return m_it != other.m_it; }
3129
3130 private:
3131 void updateCurrentIndexFullRow()
3132 {
3133 m_currentIndexIsFullRow = false;
3134 if (m_it == m_end || m_it->column() || m_end - m_it < m_columnCount)
3135 return;
3136 const QModelIndex &lastInRow = m_it[m_columnCount - 1];
3137 m_currentIndexIsFullRow = lastInRow.row() == m_it->row()
3138 && lastInRow.internalPointer() == m_it->internalPointer();
3139 }
3140
3141 base_iterator m_it;
3142 base_iterator m_begin;
3143 base_iterator m_end;
3144 const QRangeModelImpl *m_model;
3145 int m_columnCount = 0;
3146 bool m_currentIndexIsFullRow = false;
3147 };
3148
3150 {
3152 // row_traits::item_type is wrapped, and void if not the same for all columns
3154 void *, typename row_traits::item_type>;
3156 using iterator_category = std::bidirectional_iterator_tag;
3158 using reference [[maybe_unused]] = value_type;
3160 using pointer [[maybe_unused]] = void;
3161
3163 {
3164 const QModelIndex &index = *m_it;
3165 // pointer to the item as stored, including wrapping
3166 const item_type *pitem = nullptr;
3167 const auto &row = m_model->rowData(index);
3168 if (QRangeModelDetails::isValid(row)) {
3169 row_traits::for_element_at(QRangeModelDetails::refTo(row), index.column(),
3170 [&pitem](const auto &item){
3171 pitem = &item;
3172 return true;
3173 });
3174 }
3175 if constexpr (std::disjunction_v<QRangeModelDetails::is_owning_or_raw_pointer<row_type>,
3176 QRangeModelDetails::is_owning_or_raw_pointer<item_type>>) {
3177 if (Q_UNLIKELY(!QRangeModelDetails::isValid(pitem))) {
3178 // no use in warning about invalid item here, as the user
3179 // can't check for validity without dereferencing the iterator.
3180 constexpr bool is_constexpr_default_constructible_v =
3181 value_type::template is_constexpr_default_constructible<item_type>::value;
3182 if constexpr (is_constexpr_default_constructible_v) {
3183 Q_CONSTINIT static const item_type emptyDefault;
3184 return {emptyDefault, index};
3185 } else {
3186 // known to cause runtime initialization
3187 static const item_type emptyDefault;
3188 return {emptyDefault, index};
3189 }
3190 }
3191 }
3192 // this will decompose to a [wrapped_item_type, QModelIndex]
3193 return {*pitem, index};
3194 }
3195 MimeDataItemIterator &operator++() { ++m_it; return *this; }
3196 MimeDataItemIterator operator++(int) { auto tmp = *this; ++(*this); return tmp; }
3197
3198 MimeDataItemIterator &operator--() { --m_it; return *this; }
3199 MimeDataItemIterator operator--(int) { auto tmp = *this; --(*this); return tmp; }
3200
3201 MimeDataItemIterator operator-(difference_type n) const
3202 {
3203 auto tmp = *this; tmp.m_it -= n; return tmp;
3204 }
3205
3206 bool operator==(const MimeDataItemIterator &other) const { return m_it == other.m_it; }
3207 bool operator!=(const MimeDataItemIterator &other) const { return m_it != other.m_it; }
3208
3211 };
3212
3213 template <typename Iterator>
3215 Iterator begin() const { return m_begin; }
3216 Iterator end() const { return m_end; }
3217 auto rbegin() const { return std::reverse_iterator(m_end); }
3218 auto rend() const { return std::reverse_iterator(m_begin); }
3219 auto first() const { return *m_begin;}
3220 auto last() const { return *(m_end - 1);}
3221 bool isEmpty() const { return m_begin == m_end; }
3222 bool empty() const { return m_begin == m_end; }
3223 Iterator m_begin;
3224 Iterator m_end;
3225 };
3226
3227 QMimeData *mimeData(const QModelIndexList &indexes) const
3228 {
3229 QMimeData *result = nullptr;
3230 using RowOptions = QRangeModelDetails::QRangeModelRowOptions<wrapped_row_type>;
3231 using ItemType = QRangeModelDetails::wrapped_t<typename row_traits::item_type>;
3232
3233 if constexpr (QRangeModelDetails::item_access<ItemType>::hasMimeData) {
3234 using ItemAccess = QRangeModelDetails::QRangeModelItemAccess<ItemType>;
3235 const auto begin = MimeDataItemIterator{indexes.begin(), this};
3236 const auto end = MimeDataItemIterator{indexes.end(), this};
3237 result = ItemAccess::mimeData(MimeDataRange<MimeDataItemIterator>{begin, end});
3238 } else if constexpr (QRangeModelDetails::hasMimeDataRowSpan<wrapped_row_type>) {
3239 const auto begin = MimeDataRowIterator(indexes.begin(), indexes.begin(), indexes.end(), this);
3240 const auto end = MimeDataRowIterator(indexes.end(), indexes.begin(), indexes.end(), this);
3241 result = RowOptions::mimeData(MimeDataRange<MimeDataRowIterator>{begin, end});
3242 } else if constexpr (QRangeModelDetails::hasMimeDataIndexList<wrapped_row_type>) {
3243 result = RowOptions::mimeData(indexes);
3244 }
3245
3246 return result;
3247 }
3248
3249 template <typename BaseMethod, typename BaseMethod::template Overridden<Self> overridden>
3250 using Override = typename Ancestor::template Override<BaseMethod, overridden>;
3251
3260
3275
3279
3287
3292
3293protected:
3295 {
3297 }
3298
3300 {
3301 // We delete row objects if we are not operating on a reference or pointer
3302 // to a range, as in that case, the owner of the referenced/pointed to
3303 // range also owns the row entries.
3304 // ### Problem: if we get a copy of a range (no matter if shared or not),
3305 // then adding rows will create row objects in the model's copy, and the
3306 // client can never delete those. But copied rows will be the same pointer,
3307 // which we must not delete (as we didn't create them).
3308
3309 static constexpr bool modelCopied = !QRangeModelDetails::is_wrapped<Range>() &&
3310 (std::is_reference_v<Range> || std::is_const_v<std::remove_reference_t<Range>>);
3311
3312 static constexpr bool modelShared = QRangeModelDetails::is_any_shared_ptr<Range>();
3313
3314 static constexpr bool default_row_deleter = protocol_traits::is_default &&
3315 protocol_traits::has_deleteRow;
3316
3317 static constexpr bool ambiguousRowOwnership = (modelCopied || modelShared) &&
3318 rows_are_raw_pointers && default_row_deleter;
3319
3320 static_assert(!ambiguousRowOwnership,
3321 "Using of copied and shared tree and table models with rows as raw pointers, "
3322 "and the default protocol is not allowed due to ambiguity of rows ownership. "
3323 "Move the model in, use another row type, or implement a custom tree protocol.");
3324
3325 if constexpr (protocol_traits::has_deleteRow && !std::is_pointer_v<Range>
3326 && !QRangeModelDetails::is_any_of<Range, std::reference_wrapper>()) {
3327 const auto begin = QRangeModelDetails::adl_begin(*m_data.model());
3328 const auto end = QRangeModelDetails::adl_end(*m_data.model());
3329 that().deleteRemovedRows(begin, end);
3330 }
3331 }
3332
3333 static constexpr bool canInsertRows()
3334 {
3335 if constexpr (dynamicColumns() && !row_features::has_resize) {
3336 // If we operate on dynamic columns and cannot resize a newly
3337 // constructed row, then we cannot insert.
3338 return false;
3339 } else if constexpr (!protocol_traits::has_newRow) {
3340 // We also cannot insert if we cannot create a new row element
3341 return false;
3342 } else if constexpr (!range_features::has_insert_range
3343 && !std::is_copy_constructible_v<row_type>) {
3344 // And if the row is a move-only type, then the range needs to be
3345 // backed by a container that can move-insert default-constructed
3346 // row elements.
3347 return false;
3348 } else {
3349 return Structure::canInsertRowsImpl();
3350 }
3351 }
3352
3353 static constexpr bool canRemoveRows()
3354 {
3355 return Structure::canRemoveRowsImpl();
3356 }
3357
3358 template <typename F>
3359 bool writeAt(const QModelIndex &index, F&& writer)
3360 {
3361 row_reference row = rowData(index);
3362 if (!QRangeModelDetails::isValid(row))
3363 return false;
3364 return row_traits::for_element_at(row, index.column(), [&writer](auto &&target) {
3365 using target_type = decltype(target);
3366 // we can only assign to an lvalue reference
3367 if constexpr (std::is_lvalue_reference_v<target_type>
3368 && !std::is_const_v<std::remove_reference_t<target_type>>) {
3369 return writer(std::forward<target_type>(target));
3370 } else {
3371 return false;
3372 }
3373 });
3374 }
3375
3376 template <typename F>
3377 bool readAt(const QModelIndex &index, F&& reader) const {
3378 const_row_reference row = rowData(index);
3379 if (!QRangeModelDetails::isValid(row))
3380 return false;
3381 return row_traits::for_element_at(row, index.column(), std::forward<F>(reader));
3382 }
3383
3384 template <typename Value>
3385 static QVariant read(const Value &value)
3386 {
3387 if constexpr (std::is_constructible_v<QVariant, Value>)
3388 return QVariant(value);
3389 else
3390 return QVariant::fromValue(value);
3391 }
3392 template <typename Value>
3393 static QVariant read(Value *value)
3394 {
3395 if (value) {
3396 if constexpr (std::is_constructible_v<QVariant, Value *>)
3397 return QVariant(value);
3398 else
3399 return read(*value);
3400 }
3401 return {};
3402 }
3403
3404 template <typename Target>
3405 static bool write(Target &target, const QVariant &value)
3406 {
3407 using Type = std::remove_reference_t<Target>;
3408 if constexpr (std::is_constructible_v<Target, QVariant>) {
3409 target = value;
3410 return true;
3411 } else if (value.canConvert<Type>()) {
3412 target = value.value<Type>();
3413 return true;
3414 }
3415 return false;
3416 }
3417 template <typename Target>
3418 static bool write(Target *target, const QVariant &value)
3419 {
3420 if (target)
3421 return write(*target, value);
3422 return false;
3423 }
3424
3425 template <typename ItemType>
3427 {
3428 struct {
3429 operator QMetaProperty() const {
3430 const QByteArray roleName = that.itemModel().roleNames().value(role);
3431 const QMetaObject &mo = ItemType::staticMetaObject;
3432 if (const int index = mo.indexOfProperty(roleName.data());
3433 index >= 0) {
3434 return mo.property(index);
3435 }
3436 return {};
3437 }
3438 const QRangeModelImpl &that;
3439 const int role;
3440 } findProperty{*this, role};
3441
3442 if constexpr (ModelData::cachesProperties)
3443 return *m_data.properties.tryEmplace(role, findProperty).iterator;
3444 else
3445 return findProperty;
3446 }
3447
3448 void connectPropertyOnRead(const QModelIndex &index, int role,
3449 const QObject *gadget, const QMetaProperty &prop) const
3450 {
3451 if (!index.isValid())
3452 return;
3453 const typename ModelData::Connection connection = {gadget, role};
3454 if (prop.hasNotifySignal() && this->autoConnectPolicy() == AutoConnectPolicy::OnRead
3455 && !m_data.connections.contains(connection)) {
3456 if constexpr (isMutable())
3457 Self::connectProperty(index, gadget, m_data.context, role, prop);
3458 else
3459 Self::connectPropertyConst(index, gadget, m_data.context, role, prop);
3460 m_data.connections.insert(connection);
3461 }
3462 }
3463
3464 template <typename ItemType>
3465 QVariant readRole(const QModelIndex &index, int role, ItemType *gadget) const
3466 {
3467 using item_type = std::remove_pointer_t<ItemType>;
3468 QVariant result;
3469 QMetaProperty prop = roleProperty<item_type>(role);
3470 if (!prop.isValid() && role == Qt::EditRole) {
3471 role = Qt::DisplayRole;
3472 prop = roleProperty<item_type>(Qt::DisplayRole);
3473 }
3474
3475 if (prop.isValid()) {
3476 if constexpr (itemsAreQObjects)
3477 connectPropertyOnRead(index, role, gadget, prop);
3478 result = readProperty(prop, gadget);
3479 }
3480 return result;
3481 }
3482
3483 template <typename ItemType>
3484 QVariant readRole(const QModelIndex &index, int role, const ItemType &gadget) const
3485 {
3486 return readRole(index, role, &gadget);
3487 }
3488
3489 template <typename ItemType>
3490 static QVariant readProperty(const QMetaProperty &prop, ItemType *gadget)
3491 {
3492 if constexpr (std::is_base_of_v<QObject, ItemType>)
3493 return prop.read(gadget);
3494 else
3495 return prop.readOnGadget(gadget);
3496 }
3497
3498 template <typename ItemType>
3499 QVariant readProperty(const QModelIndex &index, ItemType *gadget) const
3500 {
3501 using item_type = std::remove_pointer_t<ItemType>;
3502 const QMetaObject &mo = item_type::staticMetaObject;
3503 const QMetaProperty prop = mo.property(index.column() + mo.propertyOffset());
3504
3505 if constexpr (rowsAreQObjects)
3506 connectPropertyOnRead(index, Qt::DisplayRole, gadget, prop);
3507
3508 return readProperty(prop, gadget);
3509 }
3510
3511 template <typename ItemType>
3512 QVariant readProperty(const QModelIndex &index, const ItemType &gadget) const
3513 {
3514 return readProperty(index, &gadget);
3515 }
3516
3517 template <typename ItemType>
3518 bool writeRole(int role, ItemType *gadget, const QVariant &data)
3519 {
3520 using item_type = std::remove_pointer_t<ItemType>;
3521 auto prop = roleProperty<item_type>(role);
3522 if (!prop.isValid() && role == Qt::EditRole)
3523 prop = roleProperty<item_type>(Qt::DisplayRole);
3524
3525 return prop.isValid() ? writeProperty(prop, gadget, data) : false;
3526 }
3527
3528 template <typename ItemType>
3529 bool writeRole(int role, ItemType &&gadget, const QVariant &data)
3530 {
3531 return writeRole(role, &gadget, data);
3532 }
3533
3534 template <typename ItemType>
3535 static bool writeProperty(const QMetaProperty &prop, ItemType *gadget, const QVariant &data)
3536 {
3537 if constexpr (std::is_base_of_v<QObject, ItemType>)
3538 return prop.write(gadget, data);
3539 else
3540 return prop.writeOnGadget(gadget, data);
3541 }
3542 template <typename ItemType>
3543 static bool writeProperty(int property, ItemType *gadget, const QVariant &data)
3544 {
3545 using item_type = std::remove_pointer_t<ItemType>;
3546 const QMetaObject &mo = item_type::staticMetaObject;
3547 return writeProperty(mo.property(property + mo.propertyOffset()), gadget, data);
3548 }
3549
3550 template <typename ItemType>
3551 static bool writeProperty(int property, ItemType &&gadget, const QVariant &data)
3552 {
3553 return writeProperty(property, &gadget, data);
3554 }
3555
3556 template <typename ItemType>
3557 static bool resetProperty(int property, ItemType *object)
3558 {
3559 using item_type = std::remove_pointer_t<ItemType>;
3560 const QMetaObject &mo = item_type::staticMetaObject;
3561 bool success = true;
3562 if (property == -1) {
3563 // reset all properties
3564 if constexpr (std::is_base_of_v<QObject, item_type>) {
3565 for (int p = mo.propertyOffset(); p < mo.propertyCount(); ++p)
3566 success = writeProperty(mo.property(p), object, {}) && success;
3567 } else { // reset a gadget by assigning a default-constructed
3568 *object = {};
3569 }
3570 } else {
3571 success = writeProperty(mo.property(property + mo.propertyOffset()), object, {});
3572 }
3573 return success;
3574 }
3575
3576 template <typename ItemType>
3577 static bool resetProperty(int property, ItemType &&object)
3578 {
3579 return resetProperty(property, &object);
3580 }
3581
3582 // helpers
3583 const_row_reference rowData(const QModelIndex &index) const
3584 {
3585 Q_ASSERT(index.isValid());
3586 return that().rowDataImpl(index);
3587 }
3588
3589 row_reference rowData(const QModelIndex &index)
3590 {
3591 Q_ASSERT(index.isValid());
3592 return that().rowDataImpl(index);
3593 }
3594
3595 const range_type *childRange(const QModelIndex &index) const
3596 {
3597 if (!index.isValid())
3598 return m_data.model();
3599 if (index.column()) // only items at column 0 can have children
3600 return nullptr;
3601 return that().childRangeImpl(index);
3602 }
3603
3604 range_type *childRange(const QModelIndex &index)
3605 {
3606 if (!index.isValid())
3607 return m_data.model();
3608 if (index.column()) // only items at column 0 can have children
3609 return nullptr;
3610 return that().childRangeImpl(index);
3611 }
3612
3613 template <typename, typename, typename> friend class QRangeModelAdapter;
3614
3616};
3617
3618// Implementations that depends on the model structure (flat vs tree) that will
3619// be specialized based on a protocol type. The main template implements tree
3620// support through a protocol type.
3621template <typename Range, typename Protocol>
3623 : public QRangeModelImpl<QGenericTreeItemModelImpl<Range, Protocol>, Range, Protocol>
3624{
3625 using Base = QRangeModelImpl<QGenericTreeItemModelImpl<Range, Protocol>, Range, Protocol>;
3626 friend class QRangeModelImpl<QGenericTreeItemModelImpl<Range, Protocol>, Range, Protocol>;
3627
3628 using range_type = typename Base::range_type;
3629 using range_features = typename Base::range_features;
3630 using row_type = typename Base::row_type;
3631 using row_ptr = typename Base::row_ptr;
3632 using const_row_ptr = typename Base::const_row_ptr;
3633
3634 using tree_traits = typename Base::protocol_traits;
3635 static constexpr bool is_mutable_impl = tree_traits::has_mutable_childRows;
3636
3637 static constexpr bool rows_are_any_refs_or_pointers = Base::rows_are_raw_pointers ||
3638 QRangeModelDetails::is_smart_ptr<row_type>() ||
3639 QRangeModelDetails::is_any_of<row_type, std::reference_wrapper>();
3640 static_assert(!Base::dynamicColumns(), "A tree must have a static number of columns!");
3641
3642public:
3643 QGenericTreeItemModelImpl(Range &&model, Protocol &&p, QRangeModel *itemModel)
3644 : Base(std::forward<Range>(model), std::forward<Protocol>(p), itemModel)
3645 {};
3646
3647 void setParentRow(range_type &children, row_ptr parent)
3648 {
3649 for (auto &&child : children)
3650 this->protocol().setParentRow(QRangeModelDetails::refTo(child), parent);
3651 resetParentInChildren(&children);
3652 }
3653
3654 void deleteRemovedRows(range_type &range)
3655 {
3656 deleteRemovedRows(QRangeModelDetails::adl_begin(range), QRangeModelDetails::adl_end(range));
3657 }
3658
3659 bool autoConnectProperties(const QModelIndex &parent) const
3660 {
3661 auto *children = this->childRange(parent);
3662 if (!children)
3663 return true;
3664 return autoConnectPropertiesRange(QRangeModelDetails::refTo(children), parent);
3665 }
3666
3667protected:
3668 QModelIndex indexImpl(int row, int column, const QModelIndex &parent) const
3669 {
3670 if (!parent.isValid())
3671 return this->createIndex(row, column);
3672 // only items at column 0 can have children
3673 if (parent.column())
3674 return QModelIndex();
3675
3676 const_row_ptr grandParent = static_cast<const_row_ptr>(parent.constInternalPointer());
3677 const auto &parentSiblings = childrenOf(grandParent);
3678 const auto it = QRangeModelDetails::pos(parentSiblings, parent.row());
3679 return this->createIndex(row, column, QRangeModelDetails::pointerTo(*it));
3680 }
3681
3682 QModelIndex parentImpl(const QModelIndex &child) const
3683 {
3684 if (!child.isValid())
3685 return {};
3686
3687 // no pointer to parent row - no parent
3688 const_row_ptr parentRow = static_cast<const_row_ptr>(child.constInternalPointer());
3689 if (!parentRow)
3690 return {};
3691
3692 // get the siblings of the parent via the grand parent
3693 auto &&grandParent = this->protocol().parentRow(QRangeModelDetails::refTo(parentRow));
3694 const range_type &parentSiblings = childrenOf(QRangeModelDetails::pointerTo(grandParent));
3695 // find the index of parentRow
3696 const auto begin = QRangeModelDetails::adl_begin(parentSiblings);
3697 const auto end = QRangeModelDetails::adl_end(parentSiblings);
3698 const auto it = std::find_if(begin, end, [parentRow](auto &&s){
3699 return QRangeModelDetails::pointerTo(std::forward<decltype(s)>(s)) == parentRow;
3700 });
3701 if (it != end)
3702 return this->createIndex(std::distance(begin, it), 0,
3703 QRangeModelDetails::pointerTo(grandParent));
3704 return {};
3705 }
3706
3707 int rowCountImpl(const QModelIndex &parent) const
3708 {
3709 return Base::size(this->childRange(parent));
3710 }
3711
3712 int columnCountImpl(const QModelIndex &) const
3713 {
3714 // All levels of a tree have to have the same, fixed, column count.
3715 // If static_column_count is -1 for a tree, static assert fires
3716 return Base::fixedColumnCount();
3717 }
3718
3719 static constexpr Qt::ItemFlags defaultFlags()
3720 {
3721 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
3722 }
3723
3724 static constexpr bool canInsertRowsImpl()
3725 {
3726 // We must not insert rows if we cannot adjust the parents of the
3727 // children of the following rows. We don't have to do that if the
3728 // range operates on pointers.
3729 return (rows_are_any_refs_or_pointers || tree_traits::has_setParentRow)
3730 && Base::dynamicRows() && range_features::has_insert;
3731 }
3732
3733 static constexpr bool canRemoveRowsImpl()
3734 {
3735 // We must not remove rows if we cannot adjust the parents of the
3736 // children of the following rows. We don't have to do that if the
3737 // range operates on pointers.
3738 return (rows_are_any_refs_or_pointers || tree_traits::has_setParentRow)
3739 && Base::dynamicRows() && range_features::has_erase;
3740 }
3741
3742 static constexpr bool canMoveColumns(const QModelIndex &, const QModelIndex &)
3743 {
3744 return true;
3745 }
3746
3747 static constexpr bool canMoveRows(const QModelIndex &, const QModelIndex &)
3748 {
3749 return true;
3750 }
3751
3752 bool moveRowsAcross(const QModelIndex &sourceParent, int sourceRow, int count,
3753 const QModelIndex &destParent, int destRow)
3754 {
3755 // If rows are pointers, then reference to the parent row don't
3756 // change, so we can move them around freely. Otherwise we need to
3757 // be able to explicitly update the parent pointer.
3758 if constexpr (!rows_are_any_refs_or_pointers && !tree_traits::has_setParentRow) {
3759 return false;
3760 } else if constexpr (!(range_features::has_insert && range_features::has_erase)) {
3761 return false;
3762 } else if (!this->beginMoveRows(sourceParent, sourceRow, sourceRow + count - 1,
3763 destParent, destRow)) {
3764 return false;
3765 }
3766
3767 range_type *source = this->childRange(sourceParent);
3768 range_type *destination = this->childRange(destParent);
3769
3770 // If we can insert data from another range into, then
3771 // use that to move the old data over.
3772 const auto destStart = QRangeModelDetails::pos(destination, destRow);
3773 if constexpr (range_features::has_insert_range) {
3774 const auto sourceStart = QRangeModelDetails::pos(*source, sourceRow);
3775 const auto sourceEnd = std::next(sourceStart, count);
3776
3777 destination->insert(destStart, std::move_iterator(sourceStart),
3778 std::move_iterator(sourceEnd));
3779 } else if constexpr (std::is_copy_constructible_v<row_type>) {
3780 // otherwise we have to make space first, and copy later.
3781 destination->insert(destStart, count, row_type{});
3782 }
3783
3784 row_ptr parentRow = destParent.isValid()
3785 ? QRangeModelDetails::pointerTo(this->rowData(destParent))
3786 : nullptr;
3787
3788 // if the source's parent was already inside the new parent row,
3789 // then the source row might have become invalid, so reset it.
3790 if (parentRow == static_cast<row_ptr>(sourceParent.internalPointer())) {
3791 if (sourceParent.row() < destRow) {
3792 source = this->childRange(sourceParent);
3793 } else {
3794 // the source parent moved down within destination
3795 source = this->childRange(this->createIndex(sourceParent.row() + count, 0,
3796 sourceParent.internalPointer()));
3797 }
3798 }
3799
3800 // move the data over and update the parent pointer
3801 {
3802 const auto writeStart = QRangeModelDetails::pos(destination, destRow);
3803 const auto writeEnd = std::next(writeStart, count);
3804 const auto sourceStart = QRangeModelDetails::pos(source, sourceRow);
3805 const auto sourceEnd = std::next(sourceStart, count);
3806
3807 for (auto write = writeStart, read = sourceStart; write != writeEnd; ++write, ++read) {
3808 // move data over if not already done, otherwise
3809 // only fix the parent pointer
3810 if constexpr (!range_features::has_insert_range)
3811 *write = std::move(*read);
3812 this->protocol().setParentRow(QRangeModelDetails::refTo(*write), parentRow);
3813 }
3814 // remove the old rows from the source parent
3815 source->erase(sourceStart, sourceEnd);
3816 }
3817
3818 // Fix the parent pointers in children of both source and destination
3819 // ranges, as the references to the entries might have become invalid.
3820 // We don't have to do that if the rows are pointers, as in that case
3821 // the references to the entries are stable.
3822 resetParentInChildren(destination);
3824
3825 this->endMoveRows();
3826 return true;
3827 }
3828
3829 auto makeEmptyRow(row_ptr parentRow)
3830 {
3831 // tree traversal protocol: if we are here, then it must be possible
3832 // to change the parent of a row.
3833 static_assert(tree_traits::has_setParentRow);
3834 row_type empty_row = this->protocol().newRow();
3835 if (QRangeModelDetails::isValid(empty_row) && parentRow)
3836 this->protocol().setParentRow(QRangeModelDetails::refTo(empty_row), parentRow);
3837 return empty_row;
3838 }
3839
3840 template <typename It, typename Sentinel>
3841 void deleteRemovedRows(It &&begin, Sentinel &&end)
3842 {
3843 if constexpr (tree_traits::has_deleteRow) {
3844 for (auto it = begin; it != end; ++it) {
3845 if constexpr (Base::isMutable()) {
3846 decltype(auto) children = this->protocol().childRows(QRangeModelDetails::refTo(*it));
3847 if (QRangeModelDetails::isValid(children)) {
3848 deleteRemovedRows(QRangeModelDetails::adl_begin(children),
3849 QRangeModelDetails::adl_end(children));
3850 QRangeModelDetails::refTo(children) = range_type{ };
3851 }
3852 }
3853
3854 this->protocol().deleteRow(std::move(*it));
3855 }
3856 }
3857 }
3858
3859 void resetParentInChildren(range_type *children)
3860 {
3861 const auto persistentIndexList = this->persistentIndexList();
3862 const auto [firstColumn, lastColumn] = [&persistentIndexList]{
3863 int first = std::numeric_limits<int>::max();
3864 int last = -1;
3865 for (const auto &pmi : persistentIndexList) {
3866 first = (std::min)(pmi.column(), first);
3867 last = (std::max)(pmi.column(), last);
3868 }
3869 return std::pair(first, last);
3870 }();
3871
3872 resetParentInChildrenRecursive(children, firstColumn, lastColumn);
3873 }
3874
3875 void resetParentInChildrenRecursive(range_type *children, int pmiFromColumn, int pmiToColumn)
3876 {
3877 if constexpr (tree_traits::has_setParentRow && !rows_are_any_refs_or_pointers) {
3878 const bool changePersistentIndexes = pmiToColumn >= pmiFromColumn;
3879 const auto begin = QRangeModelDetails::adl_begin(*children);
3880 const auto end = QRangeModelDetails::adl_end(*children);
3881 for (auto it = begin; it != end; ++it) {
3882 decltype(auto) maybeChildren = this->protocol().childRows(*it);
3883 if (QRangeModelDetails::isValid(maybeChildren)) {
3884 auto &childrenRef = QRangeModelDetails::refTo(maybeChildren);
3885 auto *parentRow = QRangeModelDetails::pointerTo(*it);
3886
3887 int row = 0;
3888 for (auto &child : childrenRef) {
3889 const_row_ptr oldParent = this->protocol().parentRow(child);
3890 if (oldParent != parentRow) {
3891 if (changePersistentIndexes) {
3892 for (int column = pmiFromColumn; column <= pmiToColumn; ++column) {
3893 this->changePersistentIndex(this->createIndex(row, column, oldParent),
3894 this->createIndex(row, column, parentRow));
3895 }
3896 }
3897 this->protocol().setParentRow(child, parentRow);
3898 }
3899 ++row;
3900 }
3901 resetParentInChildrenRecursive(&childrenRef, pmiFromColumn, pmiToColumn);
3902 }
3903 }
3904 }
3905 }
3906
3907 bool autoConnectPropertiesRange(const range_type &range, const QModelIndex &parent) const
3908 {
3909 int rowIndex = 0;
3910 for (const auto &row : range) {
3911 if (!this->autoConnectPropertiesInRow(row, rowIndex, parent))
3912 return false;
3913 Q_ASSERT(QRangeModelDetails::isValid(row));
3914 const auto &children = this->protocol().childRows(QRangeModelDetails::refTo(row));
3915 if (QRangeModelDetails::isValid(children)) {
3916 if (!autoConnectPropertiesRange(QRangeModelDetails::refTo(children),
3917 this->itemModel().index(rowIndex, 0, parent))) {
3918 return false;
3919 }
3920 }
3921 ++rowIndex;
3922 }
3923 return true;
3924 }
3925
3927 {
3928 return autoConnectPropertiesRange(*this->m_data.model(), {});
3929 }
3930
3931 decltype(auto) rowDataImpl(const QModelIndex &index) const
3932 {
3933 const_row_ptr parentRow = static_cast<const_row_ptr>(index.constInternalPointer());
3934 const range_type &siblings = childrenOf(parentRow);
3935 Q_ASSERT(index.row() < int(Base::size(siblings)));
3936 return *QRangeModelDetails::pos(siblings, index.row());
3937 }
3938
3939 decltype(auto) rowDataImpl(const QModelIndex &index)
3940 {
3941 row_ptr parentRow = static_cast<row_ptr>(index.internalPointer());
3942 range_type &siblings = childrenOf(parentRow);
3943 Q_ASSERT(index.row() < int(Base::size(siblings)));
3944 return *QRangeModelDetails::pos(siblings, index.row());
3945 }
3946
3947 const range_type *childRangeImpl(const QModelIndex &index) const
3948 {
3949 const auto &row = this->rowData(index);
3950 if (!QRangeModelDetails::isValid(row))
3951 return static_cast<const range_type *>(nullptr);
3952
3953 decltype(auto) children = this->protocol().childRows(QRangeModelDetails::refTo(row));
3954 return QRangeModelDetails::pointerTo(std::forward<decltype(children)>(children));
3955 }
3956
3957 range_type *childRangeImpl(const QModelIndex &index)
3958 {
3959 auto &row = this->rowData(index);
3960 if (!QRangeModelDetails::isValid(row))
3961 return static_cast<range_type *>(nullptr);
3962
3963 decltype(auto) children = this->protocol().childRows(QRangeModelDetails::refTo(row));
3964 using Children = std::remove_reference_t<decltype(children)>;
3965
3966 if constexpr (QRangeModelDetails::is_any_of<Children, std::optional>())
3967 if constexpr (std::is_default_constructible<typename Children::value_type>()) {
3968 if (!children)
3969 children.emplace(range_type{});
3970 }
3971
3972 return QRangeModelDetails::pointerTo(std::forward<decltype(children)>(children));
3973 }
3974
3975 const range_type &childrenOf(const_row_ptr row) const
3976 {
3977 return row ? QRangeModelDetails::refTo(this->protocol().childRows(*row))
3978 : *this->m_data.model();
3979 }
3980
3981 range_type &childrenOf(row_ptr row)
3982 {
3983 return row ? QRangeModelDetails::refTo(this->protocol().childRows(*row))
3984 : *this->m_data.model();
3985 }
3986
3987 template <typename LessThan>
3988 void sortImplRecursive(range_type &range, row_ptr parentRow, const LessThan &lessThan)
3989 {
3990 for (auto &row : range) {
3991 decltype(auto) children = this->protocol().childRows(QRangeModelDetails::refTo(row));
3992 if (QRangeModelDetails::isValid(children)) {
3993 sortImplRecursive(QRangeModelDetails::refTo(children),
3994 QRangeModelDetails::pointerTo(row), lessThan);
3995 }
3996 }
3997 this->sortSubRange(range, parentRow, lessThan);
3998 }
3999
4000 template <typename LessThan>
4001 void sortImpl(const LessThan &lessThan)
4002 {
4003 sortImplRecursive(*this->m_data.model(), nullptr, lessThan);
4004 resetParentInChildren(this->m_data.model());
4005 }
4006
4007 void prunePersistentIndexList(QModelIndexList &list, row_ptr expectedParent)
4008 {
4009 erase_if(list, [expectedParent](const QModelIndex &index){
4010 return static_cast<row_ptr>(index.internalPointer()) != expectedParent;
4011 });
4012 }
4013
4014 void matchImplRecursive(const range_type &range, const_row_ptr parentPtr, int from, int to,
4015 int role, const QVariant &value, int hits, Qt::MatchFlags flags, int column,
4016 QModelIndexList &result) const
4017 {
4018 auto it = QRangeModelDetails::pos(range, from);
4019 auto end = QRangeModelDetails::adl_end(range);
4020
4021 const bool recurse = flags.testAnyFlag(Qt::MatchRecursive);
4022 const bool allHits = (hits == -1);
4023
4024 for (int r = from; it != end && r < to && (allHits || result.size() < hits); ++it, ++r) {
4025 const QModelIndex index = this->createIndex(r, column, parentPtr);
4026 if (this->matchRow(*it, index, role, value, flags))
4027 result.append(index);
4028
4029 if (recurse) {
4030 decltype(auto) children = this->protocol().childRows(QRangeModelDetails::refTo(*it));
4031
4032 if (QRangeModelDetails::isValid(children)) {
4033 matchImplRecursive(QRangeModelDetails::refTo(children),
4034 QRangeModelDetails::pointerTo(*it), 0,
4035 int(QRangeModelDetails::size(QRangeModelDetails::refTo(children))),
4036 role, value, hits, flags, column, result);
4037 }
4038 }
4039 }
4040 }
4041
4042 QModelIndexList matchImpl(const QModelIndex &start, int role, const QVariant &value, int hits,
4043 Qt::MatchFlags flags) const
4044 {
4045 QModelIndexList result;
4046 const bool wrap = flags.testAnyFlag(Qt::MatchWrap);
4047 const int column = start.column();
4048 const int from = start.row();
4049 const int to = this->rowCount(start.parent());
4050
4051 for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
4052 const int fromRow = (i == 0) ? from : 0;
4053 const int toRow = (i == 0) ? to : from;
4054 matchImplRecursive(*this->m_data.model(), nullptr, fromRow, toRow,
4055 role, value, hits, flags, column, result);
4056 }
4057 return result;
4058 }
4059
4060 // tree models don't overwrite the data at index, but instead insert a
4061 // child item
4062 bool dropOnItem(const QMimeData *, const QModelIndex &)
4063 {
4064 return false;
4065 }
4066};
4067
4068// specialization for flat models without protocol
4069template <typename Range>
4072{
4075
4076 static constexpr bool is_mutable_impl = true;
4077
4078public:
4079 using range_type = typename Base::range_type;
4081 using row_type = typename Base::row_type;
4082 using row_ptr = typename Base::row_ptr;
4084 using row_traits = typename Base::row_traits;
4085 using row_features = typename Base::row_features;
4086
4087 explicit QGenericTableItemModelImpl(Range &&model, QRangeModel *itemModel)
4088 : Base(std::forward<Range>(model), {}, itemModel)
4089 {}
4090
4091protected:
4092 QModelIndex indexImpl(int row, int column, const QModelIndex &) const
4093 {
4094 if constexpr (Base::dynamicColumns()) {
4095 if (column < int(Base::size(*QRangeModelDetails::pos(*this->m_data.model(), row))))
4096 return this->createIndex(row, column);
4097#ifndef QT_NO_DEBUG
4098 // if we got here, then column < columnCount(), but this row is too short
4099 qCritical("QRangeModel: Column-range at row %d is not large enough!", row);
4100#endif
4101 return {};
4102 } else {
4103 return this->createIndex(row, column);
4104 }
4105 }
4106
4107 QModelIndex parentImpl(const QModelIndex &) const
4108 {
4109 return {};
4110 }
4111
4112 int rowCountImpl(const QModelIndex &parent) const
4113 {
4114 if (parent.isValid())
4115 return 0;
4116 return int(Base::size(*this->m_data.model()));
4117 }
4118
4119 int columnCountImpl(const QModelIndex &parent) const
4120 {
4121 if (parent.isValid())
4122 return 0;
4123
4124 // in a table, all rows have the same number of columns (as the first row)
4125 if constexpr (Base::dynamicColumns()) {
4126 return int(Base::size(*this->m_data.model()) == 0
4127 ? 0
4128 : Base::size(*QRangeModelDetails::adl_begin(*this->m_data.model())));
4129 } else {
4130 return Base::fixedColumnCount();
4131 }
4132 }
4133
4134 static constexpr Qt::ItemFlags defaultFlags()
4135 {
4136 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemNeverHasChildren;
4137 }
4138
4139 static constexpr bool canInsertRowsImpl()
4140 {
4141 return Base::dynamicRows() && range_features::has_insert;
4142 }
4143
4144 static constexpr bool canRemoveRowsImpl()
4145 {
4146 return Base::dynamicRows() && range_features::has_erase;
4147 }
4148
4149 static constexpr bool canMoveColumns(const QModelIndex &source, const QModelIndex &destination)
4150 {
4151 return !source.isValid() && !destination.isValid();
4152 }
4153
4154 static constexpr bool canMoveRows(const QModelIndex &source, const QModelIndex &destination)
4155 {
4156 return !source.isValid() && !destination.isValid();
4157 }
4158
4159 constexpr bool moveRowsAcross(const QModelIndex &, int , int,
4160 const QModelIndex &, int) noexcept
4161 {
4162 // table/flat model: can't move rows between different parents
4163 return false;
4164 }
4165
4166 auto makeEmptyRow(typename Base::row_ptr)
4167 {
4168 row_type empty_row = this->protocol().newRow();
4169
4170 // dynamically sized rows all have to have the same column count
4171 if constexpr (Base::dynamicColumns() && row_features::has_resize) {
4172 if (QRangeModelDetails::isValid(empty_row))
4173 QRangeModelDetails::refTo(empty_row).resize(this->columnCount({}));
4174 }
4175
4176 return empty_row;
4177 }
4178
4179 template <typename It, typename Sentinel>
4180 void deleteRemovedRows(It &&begin, Sentinel &&end)
4181 {
4182 if constexpr (Base::protocol_traits::has_deleteRow) {
4183 for (auto it = begin; it != end; ++it)
4184 this->protocol().deleteRow(std::move(*it));
4185 }
4186 }
4187
4188 decltype(auto) rowDataImpl(const QModelIndex &index) const
4189 {
4190 Q_ASSERT(q20::cmp_less(index.row(), Base::size(*this->m_data.model())));
4191 return *QRangeModelDetails::pos(*this->m_data.model(), index.row());
4192 }
4193
4194 decltype(auto) rowDataImpl(const QModelIndex &index)
4195 {
4196 Q_ASSERT(q20::cmp_less(index.row(), Base::size(*this->m_data.model())));
4197 return *QRangeModelDetails::pos(*this->m_data.model(), index.row());
4198 }
4199
4200 const range_type *childRangeImpl(const QModelIndex &) const
4201 {
4202 return nullptr;
4203 }
4204
4205 range_type *childRangeImpl(const QModelIndex &)
4206 {
4207 return nullptr;
4208 }
4209
4210 const range_type &childrenOf(const_row_ptr row) const
4211 {
4212 Q_ASSERT(!row);
4213 return *this->m_data.model();
4214 }
4215
4217 {
4218 Q_ASSERT(!row);
4219 return *this->m_data.model();
4220 }
4221
4222 void resetParentInChildren(range_type *)
4223 {
4224 }
4225
4227 {
4228 bool result = true;
4229 int rowIndex = 0;
4230 for (const auto &row : *this->m_data.model()) {
4231 result &= this->autoConnectPropertiesInRow(row, rowIndex, {});
4232 ++rowIndex;
4233 }
4234 return result;
4235 }
4236
4237 template <typename LessThan>
4238 void sortImpl(const LessThan &lessThan)
4239 {
4240 this->sortSubRange(*this->m_data.model(), nullptr, lessThan);
4241 }
4242
4243 void prunePersistentIndexList(QModelIndexList &, typename Base::row_ptr) {}
4244
4245 QModelIndexList matchImpl(const QModelIndex &start, int role, const QVariant &value,
4246 int hits, Qt::MatchFlags flags) const
4247 {
4248 QModelIndexList result;
4249 const bool wrap = flags.testAnyFlag(Qt::MatchWrap);
4250 const bool allHits = (hits == -1);
4251 const int column = start.column();
4252 int from = start.row();
4253 int to = this->rowCount({});
4254 decltype(auto) siblings = *this->m_data.model();
4255
4256 for (int i = 0; (wrap && i < 2) || (!wrap && i < 1); ++i) {
4257 auto it = QRangeModelDetails::pos(siblings, from);
4258 auto end = QRangeModelDetails::adl_end(siblings);
4259 for (int r = from; it != end && r < to && (allHits || result.size() < hits);
4260 ++it, ++r) {
4261 if (!QRangeModelDetails::isValid(*it))
4262 continue;
4263 const QModelIndex index = this->createIndex(r, column, nullptr);
4264 if (this->matchRow(*it, index, role, value, flags))
4265 result.append(index);
4266 }
4267 from = 0;
4268 to = start.row();
4269 }
4270
4271 return result;
4272 }
4273
4274 // flat models can overwrite data of the dropped-on item
4275 bool dropOnItem(const QMimeData *data, const QModelIndex &index)
4276 {
4277 return this->dropDataOnItem(data, index);
4278 }
4279};
4280
4281QT_END_NAMESPACE
4282
4283#endif // Q_QDOC
4284
4285#endif // QRANGEMODEL_IMPL_H
QModelIndex parentImpl(const QModelIndex &) const
static constexpr bool canMoveRows(const QModelIndex &source, const QModelIndex &destination)
void deleteRemovedRows(It &&begin, Sentinel &&end)
QModelIndexList matchImpl(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
const range_type & childrenOf(const_row_ptr row) const
static constexpr bool canRemoveRowsImpl()
bool dropOnItem(const QMimeData *data, const QModelIndex &index)
QGenericTableItemModelImpl(Range &&model, QRangeModel *itemModel)
range_type * childRangeImpl(const QModelIndex &)
int columnCountImpl(const QModelIndex &parent) const
constexpr bool moveRowsAcross(const QModelIndex &, int, int, const QModelIndex &, int) noexcept
decltype(auto) rowDataImpl(const QModelIndex &index) const
static constexpr bool canInsertRowsImpl()
const range_type * childRangeImpl(const QModelIndex &) const
int rowCountImpl(const QModelIndex &parent) const
void resetParentInChildren(range_type *)
void prunePersistentIndexList(QModelIndexList &, typename Base::row_ptr)
static constexpr bool canMoveColumns(const QModelIndex &source, const QModelIndex &destination)
decltype(auto) rowDataImpl(const QModelIndex &index)
QModelIndex indexImpl(int row, int column, const QModelIndex &) const
range_type & childrenOf(row_ptr row)
static constexpr Qt::ItemFlags defaultFlags()
void sortImpl(const LessThan &lessThan)
auto makeEmptyRow(typename Base::row_ptr)
static constexpr bool canMoveColumns(const QModelIndex &, const QModelIndex &)
void matchImplRecursive(const range_type &range, const_row_ptr parentPtr, int from, int to, int role, const QVariant &value, int hits, Qt::MatchFlags flags, int column, QModelIndexList &result) const
bool dropOnItem(const QMimeData *, const QModelIndex &)
QModelIndexList matchImpl(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
auto makeEmptyRow(row_ptr parentRow)
void setParentRow(range_type &children, row_ptr parent)
range_type * childRangeImpl(const QModelIndex &index)
bool moveRowsAcross(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destParent, int destRow)
void resetParentInChildren(range_type *children)
int rowCountImpl(const QModelIndex &parent) const
range_type & childrenOf(row_ptr row)
static constexpr bool canRemoveRowsImpl()
void resetParentInChildrenRecursive(range_type *children, int pmiFromColumn, int pmiToColumn)
int columnCountImpl(const QModelIndex &) const
static constexpr bool canMoveRows(const QModelIndex &, const QModelIndex &)
const range_type & childrenOf(const_row_ptr row) const
decltype(auto) rowDataImpl(const QModelIndex &index)
QGenericTreeItemModelImpl(Range &&model, Protocol &&p, QRangeModel *itemModel)
void prunePersistentIndexList(QModelIndexList &list, row_ptr expectedParent)
bool autoConnectPropertiesRange(const range_type &range, const QModelIndex &parent) const
QModelIndex indexImpl(int row, int column, const QModelIndex &parent) const
void deleteRemovedRows(It &&begin, Sentinel &&end)
bool autoConnectProperties(const QModelIndex &parent) const
void sortImplRecursive(range_type &range, row_ptr parentRow, const LessThan &lessThan)
const range_type * childRangeImpl(const QModelIndex &index) const
static constexpr bool canInsertRowsImpl()
decltype(auto) rowDataImpl(const QModelIndex &index) const
void deleteRemovedRows(range_type &range)
static constexpr Qt::ItemFlags defaultFlags()
void sortImpl(const LessThan &lessThan)
QModelIndex parentImpl(const QModelIndex &child) const
const QAbstractItemModel & itemModel() const
void beginInsertRows(const QModelIndex &parent, int start, int count)
void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const
bool beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destParent, int destRow)
QStringList mimeTypes() const
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &data)
QMap< int, QVariant > itemData(const QModelIndex &index) const
bool insertRows(int row, int count, const QModelIndex &parent)
Qt::DropActions adjustSupportedDropActions(Qt::DropActions dropActions)
Qt::ItemFlags flags(const QModelIndex &index) const
bool beginMoveRows(const QModelIndex &sourceParent, int sourceFirst, int sourceLast, const QModelIndex &destParent, int destRow)
void beginRemoveRows(const QModelIndex &parent, int start, int count)
bool clearItemData(const QModelIndex &index)
QVariant data(const QModelIndex &index, int role) const
QRangeModelImplBase(QRangeModel *itemModel)
QModelIndexList persistentIndexList() const
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
int columnCount(const QModelIndex &parent) const
static Q_CORE_EXPORT bool connectProperties(const QModelIndex &index, const QObject *item, QRangeModelDetails::AutoConnectContext *context, const QHash< int, QMetaProperty > &properties)
QModelIndex sibling(int row, int column, const QModelIndex &index) const
void beginInsertColumns(const QModelIndex &parent, int start, int count)
bool insertColumns(int column, int count, const QModelIndex &parent)
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
void sort(int column, Qt::SortOrder order)
bool removeRows(int row, int count, const QModelIndex &parent)
Qt::DropActions adjustSupportedDragActions(Qt::DropActions dragActions)
static Q_CORE_EXPORT bool matchValue(const QString &itemData, const QVariant &value, Qt::MatchFlags flags)
QModelIndex parent(const QModelIndex &child) const
void changePersistentIndex(const QModelIndex &from, const QModelIndex &to)
QAbstractItemModel & itemModel()
QModelIndex createIndex(int row, int column, const void *ptr=nullptr) const
QHash< int, QByteArray > roleNames() const
void interfaceVersion(int &version) const
bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destParent, int destColumn)
bool setData(const QModelIndex &index, const QVariant &data, int role)
void dataChanged(const QModelIndex &from, const QModelIndex &to, const QList< int > &roles)
QVariant headerData(int section, Qt::Orientation orientation, int role) const
static Q_CORE_EXPORT bool connectPropertiesConst(const QModelIndex &index, const QObject *item, QRangeModelDetails::AutoConnectContext *context, const QHash< int, QMetaProperty > &properties)
static Qt::partial_ordering compareData(const QVariant &lhs, const QVariant &rhs, const QCollator *collator)
AutoConnectPolicy autoConnectPolicy() const
bool removeColumns(int column, int count, const QModelIndex &parent)
Q_CORE_EXPORT bool dropDataOnItem(const QMimeData *data, const QModelIndex &index)
static Q_CORE_EXPORT bool connectPropertyConst(const QModelIndex &index, const QObject *item, QRangeModelDetails::AutoConnectContext *context, int role, const QMetaProperty &property)
std::tuple< typename C::Destroy, typename C::InvalidateCaches, typename C::SetHeaderData, typename C::SetData, typename C::SetItemData, typename C::ClearItemData, typename C::InsertColumns, typename C::RemoveColumns, typename C::MoveColumns, typename C::InsertRows, typename C::RemoveRows, typename C::MoveRows, typename C::Index, typename C::Parent, typename C::Sibling, typename C::RowCount, typename C::ColumnCount, typename C::Flags, typename C::HeaderData, typename C::Data, typename C::ItemData, typename C::RoleNames, typename C::MultiData, typename C::SetAutoConnectPolicy, typename C::InterfaceVersion, typename C::Sort, typename C::Match, typename C::AdjustSupportedDragActions, typename C::AdjustSupportedDropActions, typename C::MimeTypes, typename C::CanDropMimeData, typename C::DropMimeData, typename C::MimeData > MethodTemplates
QModelIndex index(int row, int column, const QModelIndex &parent) const
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
QMimeData * mimeData(const QModelIndexList &indexes) const
bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &data, int role)
int rowCount(const QModelIndex &parent) const
Q_CORE_EXPORT int sortRole() const
static Q_CORE_EXPORT bool connectProperty(const QModelIndex &index, const QObject *item, QRangeModelDetails::AutoConnectContext *context, int role, const QMetaProperty &property)
void beginRemoveColumns(const QModelIndex &parent, int start, int count)
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destParent, int destRow)
bool removeRows(int row, int count, const QModelIndex &parent={})
static QVariant readProperty(const QMetaProperty &prop, ItemType *gadget)
QHash< int, QByteArray > roleNames() const
row_reference rowData(const QModelIndex &index)
static constexpr bool dynamicRows()
static QVariant read(const Value &value)
const range_type * childRange(const QModelIndex &index) const
bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count, const QModelIndex &destParent, int destColumn)
auto maybeBlockDataChangedDispatch()
static constexpr bool has_metaobject
QMetaProperty roleProperty(int role) const
int columnCount(const QModelIndex &parent) const
bool writeRole(int role, ItemType &&gadget, const QVariant &data)
static bool write(Target *target, const QVariant &value)
void connectPropertyOnRead(const QModelIndex &index, int role, const QObject *gadget, const QMetaProperty &prop) const
Qt::ItemFlags flags(const QModelIndex &index) const
bool insertColumns(int column, int count, const QModelIndex &parent)
bool doDropMimeData(std::vector< QRangeModelDetails::DroppedEntry< Entry > > &droppedEntries, DropOperation dropOperation, int row, int column, const QModelIndex &target)
QVariant data(const QModelIndex &index, int role) const
QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits, Qt::MatchFlags flags) const
QVariant headerData(int section, Qt::Orientation orientation, int role) const
static constexpr bool isRangeModelRole(int role)
bool clearItemData(const QModelIndex &index)
void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &target)
static constexpr int fixedColumnCount()
const protocol_type & protocol() const
static bool resetProperty(int property, ItemType &&object)
QModelIndex sibling(int row, int column, const QModelIndex &index) const
static bool writeProperty(int property, ItemType &&gadget, const QVariant &data)
bool setItemData(const QModelIndex &index, const QMap< int, QVariant > &data)
bool insertRows(int row, int count, const QModelIndex &parent)
static constexpr bool canInsertRows()
void interfaceVersion(int &versionNumber) const
static bool writeProperty(const QMetaProperty &prop, ItemType *gadget, const QVariant &data)
QVariant readProperty(const QModelIndex &index, ItemType *gadget) const
void sort(int column, Qt::SortOrder order)
static constexpr int size(const C &c)
const_row_reference rowData(const QModelIndex &index) const
bool writeAt(const QModelIndex &index, F &&writer)
static constexpr bool one_dimensional_range
static constexpr bool rows_are_raw_pointers
bool setHeaderData(int, Qt::Orientation, const QVariant &, int)
bool removeColumns(int column, int count, const QModelIndex &parent)
static constexpr bool isMutable()
bool setData(const QModelIndex &index, const QVariant &data, int role)
QModelIndex parent(const QModelIndex &child) const
static QVariant read(Value *value)
static bool write(Target &target, const QVariant &value)
bool writeRole(int role, ItemType *gadget, const QVariant &data)
QVariant readRole(const QModelIndex &index, int role, const ItemType &gadget) const
Qt::DropActions adjustSupportedDragActions(Qt::DropActions dragActions)
QRangeModelImpl< Structure, Range, Protocol > Self
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &target) const
static constexpr bool canRemoveRows()
static constexpr int static_column_count
QVariant readProperty(const QModelIndex &index, const ItemType &gadget) const
protocol_type & protocol()
int rowCount(const QModelIndex &parent) const
static constexpr bool rows_are_owning_or_raw_pointers
QModelIndex index(int row, int column, const QModelIndex &parent) const
static constexpr int static_row_count
static bool writeProperty(int property, ItemType *gadget, const QVariant &data)
QRangeModelImpl(Range &&model, Protocol &&protocol, QRangeModel *itemModel)
void sortSubRange(range_type &range, row_ptr expectedParent, const LessThan &lessThan)
const Structure & that() const
static constexpr bool isPrimaryRole(int role)
void updateTarget(LHS *org, RHS &&copy) noexcept
static constexpr bool itemsAreQObjects
static constexpr bool dynamicColumns()
range_type * childRange(const QModelIndex &index)
bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destParent, int destRow)
QStringList mimeTypes() const
QVariant readRole(const QModelIndex &index, int role, ItemType *gadget) const
static bool resetProperty(int property, ItemType *object)
bool autoConnectPropertiesInRow(const row_type &row, int rowIndex, const QModelIndex &parent) const
bool readAt(const QModelIndex &index, F &&reader) const
static constexpr bool rowsAreQObjects
bool doInsertColumns(int column, int count, const QModelIndex &parent, InsertFn insertFn)
void updateTarget(LHS &org, RHS &&copy) noexcept
QMimeData * mimeData(const QModelIndexList &indexes) const
Qt::DropActions adjustSupportedDropActions(Qt::DropActions dropActions)
QMap< int, QVariant > itemData(const QModelIndex &index) const
void clearConnectionInRow(const row_type &row, int rowIndex, const QModelIndex &parent) const
bool doInsertRows(int row, int count, const QModelIndex &parent, InsertFn &&insertFn)
bool matchRow(const_row_reference row, const QModelIndex &index, int role, const QVariant &value, Qt::MatchFlags flags) const
decltype(QRangeModelRowOptions< row_type >::mimeTypes()) hasMimeTypes_test
static auto adl_end(C &&c) -> decltype(end(QRangeModelDetails::refTo(std::forward< C >(c))))
decltype(QRangeModelRowOptions< row_type >::flags(std::declval< const row_type & >())) hasRowFlags_test
static constexpr bool hasMimeDataRowSpan
static constexpr bool hasMimeTypes
static constexpr bool has_metaobject_v
static constexpr bool is_range_v
static constexpr bool array_like_v
static constexpr bool hasMimeDataIndexList
static void rotate(C &c, int src, int count, int dst)
static constexpr bool hasDropMimeData
static constexpr bool tuple_like_v
typename QRangeModelDetails::wrapped_helper< T >::type wrapped_t
auto value(It &&it) -> decltype(it.value())
std::conjunction< std::is_swappable< decltype(*std::declval< It >())>, std::is_base_of< std::forward_iterator_tag, typename std::iterator_traits< It >::iterator_category > > test_rotate
auto key(It &&it) -> decltype(it.key())
static constexpr bool hasDropMimeDataFull
static constexpr int static_size_v
static constexpr bool isValid(const T &t) noexcept
static auto pos(C &&c, int i)
static constexpr bool hasHeaderData
static constexpr bool hasCanDropMimeData
static decltype(auto) refTo(T &&t)
static constexpr bool hasRowFlags
static auto pointerTo(T &&t)
static constexpr bool hasCanDropMimeDataFull
static auto adl_begin(C &&c) -> decltype(begin(QRangeModelDetails::refTo(std::forward< C >(c))))
static constexpr bool is_multi_role_v
friend bool operator==(const Connection &lhs, const Connection &rhs) noexcept
friend size_t qHash(const Connection &c, size_t seed) noexcept
auto setParentRow(R &row, R *parent) -> decltype(row.setParentRow(parent))
auto childRows(R &row) -> decltype(row.childRows())
auto childRows(const R &row) const -> decltype(row.childRows())
auto parentRow(const R &row) const -> decltype(row.parentRow())
friend bool operator==(const Cell &lhs, const Cell &rhs) noexcept
const QModelIndex & index() const
friend decltype(auto) get(const MimeDataEntry &entry)
const wrapped_entry & entry() const
QHash< int, QMetaProperty > properties
static constexpr bool cachesProperties
std::remove_const_t< ModelStorage > m_model
static constexpr bool hasCanDropMimeDataFull
static constexpr bool hasMimeData
decltype(Access::flags(std::declval< const Test & >())) hasFlags_test
static constexpr bool hasReadRole
static constexpr bool hasWriteRole
static constexpr bool hasDropMimeData
decltype(Access::mimeTypes()) hasMimeTypes_test
static constexpr bool hasMimeTypes
static constexpr bool hasDropMimeDataFull
static constexpr bool hasCanDropMimeData
static QHash< int, QByteArray > roleNames(That *)
static constexpr bool has_mutable_childRows
static constexpr bool has_insert_range
static constexpr int fixed_size()
static bool for_each_element(const T &row, const QModelIndex &firstIndex, Fn &&fn)
static bool for_element_at(C &&container, std::size_t idx, Fn &&fn)
static constexpr bool hasMetaObject
decltype(std::declval< C & >().sort(std::declval< LessThan && >())) sortMember_test
static constexpr bool hasCollatedCompare
const QRangeModelImpl *const that
const QCollator *const collator
auto compare(const Item &lhs, const Item &rhs) const
static constexpr bool hasSortMember
auto operator()(const Item &lhs, const Item &rhs) const
Compare(const QRangeModelImpl *impl, int column, Qt::SortOrder order)
const Qt::SortOrder m_order
static std::optional< bool > compareInvalid(const Item &lhs, const Item &rhs)
std::input_iterator_tag iterator_category
friend bool operator==(const EmptyRowGenerator &lhs, const EmptyRowGenerator &rhs) noexcept
friend bool operator!=(const EmptyRowGenerator &lhs, const EmptyRowGenerator &rhs) noexcept
bool operator()(const value_type &value) const
const QRangeModelImpl *const that
std::bidirectional_iterator_tag iterator_category
bool operator!=(const MimeDataItemIterator &other) const
MimeDataItemIterator operator-(difference_type n) const
bool operator==(const MimeDataItemIterator &other) const
MimeDataRowIterator operator-(difference_type n) const
bool operator!=(const MimeDataRowIterator &other) const
bool operator==(const MimeDataRowIterator &other) const
MimeDataRowIterator(base_iterator it, base_iterator begin, base_iterator end, const QRangeModelImpl *model)
std::bidirectional_iterator_tag iterator_category
friend constexpr bool operator<(unordered, QtPrivate::CompareAgainstLiteralZero) noexcept
friend constexpr bool operator>(unordered, QtPrivate::CompareAgainstLiteralZero) noexcept