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
qrangemodeladapter.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 QRANGEMODELADAPTER_H
6#define QRANGEMODELADAPTER_H
7
8#include <QtCore/qrangemodeladapter_impl.h>
9
10#include <QtCore/q26numeric.h>
11
12QT_BEGIN_NAMESPACE
13
14template <typename Range, typename Protocol = void, typename Model = QRangeModel>
15class QT_TECH_PREVIEW_API QRangeModelAdapter
16{
17 using Impl = QRangeModelDetails::RangeImplementation<Range, Protocol>;
18 using Storage = QRangeModelDetails::AdapterStorage<Model, Impl>;
19
20 Storage storage;
21
22#ifdef Q_QDOC
23 using range_type = Range;
24#else
25 using range_type = QRangeModelDetails::wrapped_t<Range>;
26#endif
27 using const_row_reference = typename Impl::const_row_reference;
28 using row_reference = typename Impl::row_reference;
29 using range_features = typename QRangeModelDetails::range_traits<range_type>;
30 using row_type = std::remove_reference_t<row_reference>;
31 using row_features = QRangeModelDetails::range_traits<typename Impl::wrapped_row_type>;
32 using row_ptr = typename Impl::wrapped_row_type *;
33 using row_traits = typename Impl::row_traits;
34 using item_type = std::remove_reference_t<typename row_traits::item_type>;
35 using data_type = typename QRangeModelDetails::data_type<item_type>::type;
36 using const_data_type = QRangeModelDetails::asConst_t<data_type>;
37 using protocol_traits = typename Impl::protocol_traits;
38
39 template <typename I> static constexpr bool is_list = I::protocol_traits::is_list;
40 template <typename I> static constexpr bool is_table = I::protocol_traits::is_table;
41 template <typename I> static constexpr bool is_tree = I::protocol_traits::is_tree;
42 template <typename I> static constexpr bool canInsertColumns = I::dynamicColumns()
43 && I::isMutable()
44 && row_features::has_insert;
45 template <typename I> static constexpr bool canRemoveColumns = I::dynamicColumns()
46 && I::isMutable()
47 && row_features::has_erase;
48
49 template <typename I> using if_writable = std::enable_if_t<I::isMutable(), bool>;
50 template <typename I> using if_list = std::enable_if_t<is_list<I>, bool>;
51 template <typename I> using unless_list = std::enable_if_t<!is_list<I>, bool>;
52 template <typename I> using if_table = std::enable_if_t<is_table<I>, bool>;
53 template <typename I> using if_tree = std::enable_if_t<is_tree<I>, bool>;
54 template <typename I> using unless_tree = std::enable_if_t<!is_tree<I>, bool>;
55 template <typename I> using if_flat = std::enable_if_t<is_list<I> || is_table<I>, bool>;
56
57 template <typename I>
58 using if_canInsertRows = std::enable_if_t<I::canInsertRows(), bool>;
59 template <typename I>
60 using if_canRemoveRows = std::enable_if_t<I::canRemoveRows(), bool>;
61 template <typename F>
62 using if_canMoveItems = std::enable_if_t<F::has_rotate || F::has_splice, bool>;
63
64 template <typename I>
65 using if_canInsertColumns = std::enable_if_t<canInsertColumns<I>, bool>;
66 template <typename I>
67 using if_canRemoveColumns = std::enable_if_t<canRemoveColumns<I>, bool>;
68
69 template <typename Row>
70 static constexpr bool is_compatible_row = std::is_convertible_v<Row, const_row_reference>;
71 template <typename Row>
72 using if_compatible_row = std::enable_if_t<is_compatible_row<Row>, bool>;
73
74 template <typename C>
75 static constexpr bool is_compatible_row_range = is_compatible_row<
76 decltype(*std::begin(std::declval<C&>()))
77 >;
78 template <typename C>
79 using if_compatible_row_range = std::enable_if_t<is_compatible_row_range<C>, bool>;
80 template <typename Data>
81 static constexpr bool is_compatible_data = std::is_convertible_v<Data, data_type>;
82 template <typename Data>
83 using if_compatible_data = std::enable_if_t<is_compatible_data<Data>, bool>;
84 template <typename C>
85 static constexpr bool is_compatible_data_range = is_compatible_data<
86 typename QRangeModelDetails::data_type<
87 typename QRangeModelDetails::row_traits<
88 decltype(*std::begin(std::declval<C&>()))
89 >::item_type
90 >::type
91 >;
92 template <typename C>
93 using if_compatible_column_data = std::enable_if_t<is_compatible_data<C>
94 || is_compatible_data_range<C>, bool>;
95 template <typename C>
96 using if_compatible_column_range = std::enable_if_t<is_compatible_data_range<C>, bool>;
97
98 template <typename R>
99 using if_assignable_range = std::enable_if_t<std::is_assignable_v<range_type, R>, bool>;
100
101 friend class QRangeModel;
102 template <typename T>
103 static constexpr bool is_adapter = QRangeModelDetails::is_any_of<q20::remove_cvref_t<T>,
104 QRangeModelAdapter>::value;
105 template <typename T>
106 using unless_adapter = std::enable_if_t<!is_adapter<T>, bool>;
107
108 template <typename R, typename P>
109 using if_compatible_model_params =
110 std::enable_if_t<
111 std::conjunction_v<
112 std::disjunction<
113 std::is_convertible<R &&, Range>,
114 std::is_convertible<R &&, Range &&> // for C-arrays
115 >,
116 std::is_convertible<P, Protocol> // note, only void is expected to be convertible to void
117 >, bool>;
118
119#if !defined(Q_OS_VXWORKS) && !defined(Q_OS_INTEGRITY)
120 // An adapter on a mutable range can make itself an adapter on a const
121 // version of that same range. To make the constructor for a sub-range
122 // accessible, befriend the mutable version. We can use more
123 // generic pattern matching here, as we only use as input what asConst
124 // might produce as output.
125 template <typename T> static constexpr T asMutable(const T &);
126 template <typename T> static constexpr T *asMutable(const T *);
127 template <template <typename, typename...> typename U, typename T, typename ...Args>
128 static constexpr U<T, Args...> asMutable(const U<const T, Args...> &);
129
130 template <typename T>
131 using asMutable_t = decltype(asMutable(std::declval<T>()));
132 friend class QRangeModelAdapter<asMutable_t<Range>, Protocol, Model>;
133#else
134 template <typename R, typename P, typename M>
135 friend class QRangeModelAdapter;
136#endif
137
138 explicit QRangeModelAdapter(const std::shared_ptr<QRangeModel> &model, const QModelIndex &root,
139 std::in_place_t) // disambiguate from range/protocol c'tor
140 : storage{model, root}
141 {}
142
143 explicit QRangeModelAdapter(QRangeModel *model)
144 : storage(model)
145 {}
146
147public:
148 struct DataReference
149 {
150#ifdef Q_QDOC
151 using value_type = int;
152 using const_value_type = const int;
153 using pointer = void *;
154#else
155 using value_type = data_type;
156 using const_value_type = const_data_type;
157 using pointer = QRangeModelDetails::data_pointer_t<const_value_type>;
158#endif
159
160 explicit DataReference(const QModelIndex &index) noexcept
161 : m_index(index)
162 {}
163
164 DataReference(const DataReference &other) = default;
165 DataReference(DataReference &&other) = default;
166
167 // reference (not std::reference_wrapper) semantics
168 DataReference &operator=(const DataReference &other)
169 {
170 *this = other.get();
171 return *this;
172 }
173
174 DataReference &operator=(DataReference &&other)
175 {
176 *this = other.get();
177 return *this;
178 }
179
180 ~DataReference() = default;
181
182 DataReference &operator=(const value_type &value)
183 {
184 assign(value);
185 return *this;
186 }
187
188 DataReference &operator=(value_type &&value)
189 {
190 assign(std::move(value));
191 return *this;
192 }
193
194 const_value_type get() const
195 {
196 Q_ASSERT_X(m_index.isValid(), "QRangeModelAdapter::at", "Index at position is invalid");
197 return QRangeModelDetails::dataAtIndex<q20::remove_cvref_t<value_type>>(m_index);
198 }
199
200 operator const_value_type() const
201 {
202 return get();
203 }
204
205 pointer operator->() const
206 {
207 return {get()};
208 }
209
210 bool isValid() const { return m_index.isValid(); }
211
212 private:
213 QModelIndex m_index;
214
215 template <typename Value>
216 void assign(Value &&value)
217 {
218 constexpr Qt::ItemDataRole dataRole = Qt::RangeModelAdapterRole;
219
220 if (m_index.isValid()) {
221 auto model = const_cast<QAbstractItemModel *>(m_index.model());
222 [[maybe_unused]] bool couldWrite = false;
223 if constexpr (std::is_same_v<q20::remove_cvref_t<Value>, QVariant>) {
224 couldWrite = model->setData(m_index, value, dataRole);
225 } else {
226 couldWrite = model->setData(m_index,
227 QVariant::fromValue(std::forward<Value>(value)),
228 dataRole);
229 }
230#ifndef QT_NO_DEBUG
231 if (!couldWrite) {
232 qWarning() << "Writing value of type"
233 << QMetaType::fromType<q20::remove_cvref_t<Value>>().name()
234 << "to role" << dataRole << "at index" << m_index << "failed";
235 }
236 } else {
237 qCritical("Data reference for invalid index, can't write to model");
238#endif
239 }
240 }
241
242 friend inline bool comparesEqual(const DataReference &lhs, const DataReference &rhs)
243 {
244 return lhs.m_index == rhs.m_index
245 || lhs.get() == rhs.get();
246 }
247 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(DataReference);
248
249 friend inline bool comparesEqual(const DataReference &lhs, const value_type &rhs)
250 {
251 return lhs.get() == rhs;
252 }
253 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(DataReference, value_type);
254
255 friend inline void swap(DataReference lhs, DataReference rhs)
256 {
257 const value_type lhsValue = lhs.get();
258 lhs = rhs;
259 rhs = lhsValue; // no point in moving, we have to go through QVariant anyway
260 }
261
262#ifndef QT_NO_DEBUG_STREAM
263 friend inline QDebug operator<<(QDebug dbg, const DataReference &ref)
264 {
265 return dbg << ref.get();
266 }
267#endif
268#ifndef QT_NO_DATASTREAM
269 friend inline QDataStream &operator<<(QDataStream &ds, const DataReference &ref)
270 {
271 return ds << ref.get();
272 }
273 friend inline QDataStream &operator>>(QDataStream &ds, DataReference &ref)
274 {
275 value_type value;
276 ds >> value;
277 ref = std::move(value);
278 return ds;
279 }
280#endif
281 };
282 template <typename Iterator, typename Adapter>
283 struct ColumnIteratorBase
284 {
285 using iterator_category = std::random_access_iterator_tag;
286 using difference_type = int;
287
288 ColumnIteratorBase() = default;
289 ColumnIteratorBase(const ColumnIteratorBase &other) = default;
290 ColumnIteratorBase(ColumnIteratorBase &&other) = default;
291 ColumnIteratorBase &operator=(const ColumnIteratorBase &other) = default;
292 ColumnIteratorBase &operator=(ColumnIteratorBase &&other) = default;
293
294 ColumnIteratorBase(const QModelIndex &rowIndex, int column, Adapter *adapter) noexcept
295 : m_rowIndex(rowIndex), m_column(column), m_adapter(adapter)
296 {
297 }
298
299 void swap(ColumnIteratorBase &other) noexcept
300 {
301 std::swap(m_rowIndex, other.m_rowIndex);
302 std::swap(m_column, other.m_column);
303 q_ptr_swap(m_adapter, other.m_adapter);
304 }
305
306 friend Iterator &operator++(Iterator &that)
307 {
308 ++that.m_column;
309 return that;
310 }
311 friend Iterator operator++(Iterator &that, int)
312 {
313 auto copy = that;
314 ++that;
315 return copy;
316 }
317 friend Iterator operator+(const Iterator &that, difference_type n)
318 {
319 return {that.m_rowIndex, that.m_column + n, that.m_adapter};
320 }
321 friend Iterator operator+(difference_type n, const Iterator &that)
322 {
323 return that + n;
324 }
325 friend Iterator &operator+=(Iterator &that, difference_type n)
326 {
327 that.m_column += n;
328 return that;
329 }
330
331 friend Iterator &operator--(Iterator &that)
332 {
333 --that.m_column;
334 return that;
335 }
336 friend Iterator operator--(Iterator &that, int)
337 {
338 auto copy = that;
339 --that;
340 return copy;
341 }
342 friend Iterator operator-(const Iterator &that, difference_type n)
343 {
344 return {that.m_rowIndex, that.m_column - n, that.m_adapter};
345 }
346 friend Iterator operator-(difference_type n, const Iterator &that)
347 {
348 return that - n;
349 }
350 friend Iterator &operator-=(Iterator &that, difference_type n)
351 {
352 that.m_column -= n;
353 return that;
354 }
355
356 friend difference_type operator-(const Iterator &lhs, const Iterator &rhs)
357 {
358 Q_PRE(lhs.m_rowIndex == rhs.m_rowIndex);
359 Q_PRE(lhs.m_adapter == rhs.m_adapter);
360 return lhs.m_column - rhs.m_column;
361 }
362
363 protected:
364 ~ColumnIteratorBase() = default;
365 QModelIndex m_rowIndex;
366 int m_column = -1;
367 Adapter *m_adapter = nullptr;
368
369 private:
370 friend bool comparesEqual(const Iterator &lhs, const Iterator &rhs)
371 {
372 Q_ASSERT(lhs.m_rowIndex == rhs.m_rowIndex);
373 return lhs.m_column == rhs.m_column;
374 }
375 friend Qt::strong_ordering compareThreeWay(const Iterator &lhs, const Iterator &rhs)
376 {
377 Q_ASSERT(lhs.m_rowIndex == rhs.m_rowIndex);
378 return qCompareThreeWay(lhs.m_column, rhs.m_column);
379 }
380
381 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Iterator)
382
383#ifndef QT_NO_DEBUG_STREAM
384 friend inline QDebug operator<<(QDebug dbg, const Iterator &it)
385 {
386 QDebugStateSaver saver(dbg);
387 dbg.nospace();
388 return dbg << "ColumnIterator(" << it.m_rowIndex.siblingAtColumn(it.m_column) << ")";
389 }
390#endif
391 };
392
393 struct ConstColumnIterator : ColumnIteratorBase<ConstColumnIterator, const QRangeModelAdapter>
394 {
395 private:
396 using Base = ColumnIteratorBase<ConstColumnIterator, const QRangeModelAdapter>;
397 public:
398 using difference_type = typename Base::difference_type;
399 using value_type = data_type;
400 using reference = const_data_type;
401 using pointer = QRangeModelDetails::data_pointer_t<value_type>;
402
403 using Base::Base;
404 using Base::operator=;
405 ~ConstColumnIterator() = default;
406
407 pointer operator->() const
408 {
409 return pointer{operator*()};
410 }
411
412 reference operator*() const
413 {
414 return std::as_const(this->m_adapter)->at(this->m_rowIndex.row(), this->m_column);
415 }
416
417 reference operator[](difference_type n) const
418 {
419 return *(*this + n);
420 }
421 };
422
423 struct ColumnIterator : ColumnIteratorBase<ColumnIterator, QRangeModelAdapter>
424 {
425 private:
426 using Base = ColumnIteratorBase<ColumnIterator, QRangeModelAdapter>;
427 public:
428 using difference_type = typename Base::difference_type;
429 using value_type = DataReference;
430 using reference = DataReference;
431 using pointer = reference;
432
433 using Base::Base;
434 using Base::operator=;
435 ~ColumnIterator() = default;
436
437 operator ConstColumnIterator() const
438 {
439 return ConstColumnIterator{this->m_rowIndex, this->m_column, this->m_adapter};
440 }
441
442 pointer operator->() const
443 {
444 return operator*();
445 }
446
447 reference operator*() const
448 {
449 return reference{this->m_rowIndex.siblingAtColumn(this->m_column)};
450 }
451
452 reference operator[](difference_type n) const
453 {
454 return *(*this + n);
455 }
456 };
457
458 template <typename Reference, typename const_row_type, typename = void>
459 struct RowGetter
460 {
461 const_row_type get() const
462 {
463 const Reference *that = static_cast<const Reference *>(this);
464 const auto *impl = that->m_adapter->storage.implementation();
465 auto *childRange = impl->childRange(that->m_index.parent());
466 if constexpr (std::is_convertible_v<const row_type &, const_row_type>) {
467 return *std::next(QRangeModelDetails::adl_begin(childRange), that->m_index.row());
468 } else {
469 const auto &row = *std::next(QRangeModelDetails::adl_begin(childRange),
470 that->m_index.row());
471 return const_row_type{QRangeModelDetails::adl_begin(row),
472 QRangeModelDetails::adl_end(row)};
473 }
474 }
475
476 const_row_type operator->() const
477 {
478 return {get()};
479 }
480
481 operator const_row_type() const
482 {
483 return get();
484 }
485 };
486
487 template <typename Reference, typename const_row_type>
488 struct RowGetter<Reference, const_row_type,
489 std::enable_if_t<std::is_reference_v<const_row_type>>>
490 {
491 const_row_type get() const
492 {
493 const Reference *that = static_cast<const Reference *>(this);
494 const auto *impl = that->m_adapter->storage.implementation();
495 return *std::next(QRangeModelDetails::begin(
496 QRangeModelDetails::refTo(impl->childRange(that->m_index.parent()))),
497 that->m_index.row());
498 }
499
500 auto operator->() const
501 {
502 return std::addressof(get());
503 }
504
505 operator const_row_type() const
506 {
507 return get();
508 }
509 };
510
511 template <typename Reference, typename const_row_type>
512 struct RowGetter<Reference, const_row_type,
513 std::enable_if_t<std::is_pointer_v<const_row_type>>>
514 {
515 const_row_type get() const
516 {
517 const Reference *that = static_cast<const Reference *>(this);
518 const auto *impl = that->m_adapter->storage.implementation();
519 return *std::next(QRangeModelDetails::begin(
520 QRangeModelDetails::refTo(impl->childRange(that->m_index.parent()))),
521 that->m_index.row());
522 }
523
524 const_row_type operator->() const
525 {
526 return get();
527 }
528
529 operator const_row_type() const
530 {
531 return get();
532 }
533 };
534
535 template <typename Reference, typename Adapter>
536 struct RowReferenceBase : RowGetter<Reference, QRangeModelDetails::asConstRow_t<row_type>>
537 {
538 using const_iterator = ConstColumnIterator;
539 using size_type = int;
540 using difference_type = int;
541 using const_row_type = QRangeModelDetails::asConstRow_t<row_type>;
542
543 RowReferenceBase(const QModelIndex &index, Adapter *adapter) noexcept
544 : m_index(index), m_adapter(adapter)
545 {}
546
547 template <typename I = Impl, if_tree<I> = true>
548 bool hasChildren() const
549 {
550 return m_adapter->model()->hasChildren(m_index);
551 }
552
553 template <typename I = Impl, if_tree<I> = true>
554 auto children() const
555 {
556 using ConstRange = QRangeModelDetails::asConst_t<Range>;
557 return QRangeModelAdapter<ConstRange, Protocol, Model>(m_adapter->storage.m_model,
558 m_index, std::in_place);
559 }
560
561 ConstColumnIterator cbegin() const
562 {
563 return ConstColumnIterator{m_index, 0, m_adapter};
564 }
565 ConstColumnIterator cend() const
566 {
567 return ConstColumnIterator{m_index, m_adapter->columnCount(), m_adapter};
568 }
569
570 ConstColumnIterator begin() const { return cbegin(); }
571 ConstColumnIterator end() const { return cend(); }
572
573 size_type size() const
574 {
575 return m_adapter->columnCount();
576 }
577
578 auto at(int column) const
579 {
580 Q_ASSERT(column >= 0 && column < m_adapter->columnCount());
581 return *ConstColumnIterator{m_index, column, m_adapter};
582 }
583
584 auto operator[](int column) const
585 {
586 return at(column);
587 }
588
589 protected:
590 friend struct RowGetter<Reference, const_row_type>;
591 ~RowReferenceBase() = default;
592 QModelIndex m_index;
593 Adapter *m_adapter;
594
595 private:
596 friend bool comparesEqual(const Reference &lhs, const Reference &rhs)
597 {
598 Q_ASSERT(lhs.m_adapter == rhs.m_adapter);
599 return lhs.m_index == rhs.m_index;
600 }
601 friend Qt::strong_ordering compareThreeWay(const Reference &lhs, const Reference &rhs)
602 {
603 Q_ASSERT(lhs.m_adapter == rhs.m_adapter);
604 return qCompareThreeWay(lhs.m_index, rhs.m_index);
605 }
606
607 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Reference)
608
609 friend bool comparesEqual(const Reference &lhs, const row_type &rhs)
610 {
611 return lhs.get() == rhs;
612 }
613 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(Reference, row_type)
614
615#ifndef QT_NO_DEBUG_STREAM
616 friend inline QDebug operator<<(QDebug dbg, const Reference &ref)
617 {
618 QDebugStateSaver saver(dbg);
619 dbg.nospace();
620 return dbg << "RowReference(" << ref.m_index << ")";
621 }
622#endif
623#ifndef QT_NO_DATASTREAM
624 friend inline QDataStream &operator<<(QDataStream &ds, const Reference &ref)
625 {
626 return ds << ref.get();
627 }
628#endif
629 };
630
631 struct ConstRowReference : RowReferenceBase<ConstRowReference, const QRangeModelAdapter>
632 {
633 private:
634 using Base = RowReferenceBase<ConstRowReference, const QRangeModelAdapter>;
635 public:
636 using Base::Base;
637
638 ConstRowReference() = default;
639 ConstRowReference(const ConstRowReference &) = default;
640 ConstRowReference(ConstRowReference &&) = default;
641 ConstRowReference &operator=(const ConstRowReference &) = default;
642 ConstRowReference &operator=(ConstRowReference &&) = default;
643 ~ConstRowReference() = default;
644 };
645
646 struct RowReference : RowReferenceBase<RowReference, QRangeModelAdapter>
647 {
648 private:
649 using Base = RowReferenceBase<RowReference, QRangeModelAdapter>;
650 public:
651 using iterator = ColumnIterator;
652 using const_iterator = typename Base::const_iterator;
653 using size_type = typename Base::size_type;
654 using difference_type = typename Base::difference_type;
655#ifdef Q_QDOC
656 using row_type = int;
657#endif
658 using const_row_type = typename Base::const_row_type;
659
660 using Base::Base;
661 RowReference() = delete;
662 ~RowReference() = default;
663 RowReference(const RowReference &other) = default;
664 RowReference(RowReference &&other) = default;
665
666 // assignment has reference (std::reference_wrapper) semantics
667 RowReference &operator=(const ConstRowReference &other)
668 {
669 *this = other.get();
670 return *this;
671 }
672
673 RowReference &operator=(const RowReference &other)
674 {
675 *this = other.get();
676 return *this;
677 }
678
679 RowReference &operator=(const row_type &other)
680 {
681 assign(other);
682 return *this;
683 }
684
685 RowReference &operator=(row_type &&other)
686 {
687 assign(std::move(other));
688 return *this;
689 }
690
691 operator ConstRowReference() const
692 {
693 return ConstRowReference{this->m_index, this->m_adapter};
694 }
695
696 template <typename ConstRowType = const_row_type,
697 std::enable_if_t<!std::is_same_v<ConstRowType, const row_type &>, bool> = true>
698 RowReference &operator=(const ConstRowType &other)
699 {
700 assign(other);
701 return *this;
702 }
703
704 template <typename T, typename It, typename Sentinel>
705 RowReference &operator=(const QRangeModelDetails::RowView<T, It, Sentinel> &other)
706 {
707 *this = row_type{other.begin(), other.end()};
708 return *this;
709 }
710
711 friend inline void swap(RowReference lhs, RowReference rhs)
712 {
713 auto lhsRow = lhs.get();
714 lhs = rhs.get();
715 rhs = std::move(lhsRow);
716 }
717
718 template <typename I = Impl, if_tree<I> = true>
719 auto children()
720 {
721 return QRangeModelAdapter(this->m_adapter->storage.m_model, this->m_index,
722 std::in_place);
723 }
724
725 using Base::begin;
726 ColumnIterator begin()
727 {
728 return ColumnIterator{this->m_index, 0, this->m_adapter};
729 }
730
731 using Base::end;
732 ColumnIterator end()
733 {
734 return ColumnIterator{this->m_index, this->m_adapter->columnCount(), this->m_adapter};
735 }
736
737 using Base::at;
738 auto at(int column)
739 {
740 Q_ASSERT(column >= 0 && column < this->m_adapter->columnCount());
741 return *ColumnIterator{this->m_index, column, this->m_adapter};
742 }
743
744 using Base::operator[];
745 auto operator[](int column)
746 {
747 return at(column);
748 }
749
750 private:
751 template <typename RHS>
752 void verifyRows(const row_type &oldRow, const RHS &newRow)
753 {
754 if constexpr (QRangeModelDetails::test_size<row_type>::value) {
755 // prevent that tables get populated with wrongly sized rows
756 Q_ASSERT_X(Impl::size(newRow) == Impl::size(oldRow),
757 "RowReference::operator=()",
758 "The new row has the wrong size!");
759 }
760
761 if constexpr (is_tree<Impl>) {
762 // we cannot hook invalid rows up to the tree hierarchy
763 Q_ASSERT_X(QRangeModelDetails::isValid(newRow),
764 "RowReference::operator=()",
765 "An invalid row can not inserted into a tree!");
766 }
767 }
768
769 template <typename R>
770 void assign(R &&other)
771 {
772 auto *impl = this->m_adapter->storage.implementation();
773 decltype(auto) oldRow = impl->rowData(this->m_index);
774
775 verifyRows(oldRow, other);
776
777 if constexpr (is_tree<Impl>) {
778 auto &protocol = impl->protocol();
779 auto *oldParent = protocol.parentRow(QRangeModelDetails::refTo(oldRow));
780
781 // the old children will be removed; we don't try to overwrite
782 // them with the new children, we replace them completely
783 if (decltype(auto) oldChildren = protocol.childRows(QRangeModelDetails::refTo(oldRow));
784 QRangeModelDetails::isValid(oldChildren)) {
785 if (int oldChildCount = this->m_adapter->model()->rowCount(this->m_index)) {
786 impl->beginRemoveRows(this->m_index, 0, oldChildCount - 1);
787 impl->deleteRemovedRows(QRangeModelDetails::refTo(oldChildren));
788 // make sure the list is empty before we emit rowsRemoved
789 QRangeModelDetails::refTo(oldChildren) = range_type{};
790 impl->endRemoveRows();
791 }
792 }
793
794 if constexpr (protocol_traits::has_deleteRow)
795 protocol.deleteRow(oldRow);
796 oldRow = std::forward<R>(other);
797 if constexpr (protocol_traits::has_setParentRow) {
798 protocol.setParentRow(QRangeModelDetails::refTo(oldRow),
799 QRangeModelDetails::pointerTo(oldParent));
800 if (decltype(auto) newChildren = protocol.childRows(QRangeModelDetails::refTo(oldRow));
801 QRangeModelDetails::isValid(newChildren)) {
802 impl->beginInsertRows(this->m_index, 0,
803 Impl::size(QRangeModelDetails::refTo(newChildren)) - 1);
804 impl->setParentRow(QRangeModelDetails::refTo(newChildren),
805 QRangeModelDetails::pointerTo(oldRow));
806 impl->endInsertRows();
807 }
808 }
809 } else {
810 oldRow = std::forward<R>(other);
811 }
812 this->m_adapter->emitDataChanged(this->m_index,
813 this->m_index.siblingAtColumn(this->m_adapter->columnCount() - 1));
814 if constexpr (Impl::itemsAreQObjects) {
815 if (this->m_adapter->model()->autoConnectPolicy() == QRangeModel::AutoConnectPolicy::Full) {
816 impl->autoConnectPropertiesInRow(oldRow, this->m_index.row(), this->m_index.parent());
817 if constexpr (is_tree<Impl>)
818 impl->autoConnectProperties(this->m_index);
819 }
820
821 }
822 }
823
824#ifndef QT_NO_DATASTREAM
825 friend inline QDataStream &operator>>(QDataStream &ds, RowReference &ref)
826 {
827 row_type value;
828 ds >> value;
829 ref = value;
830 return ds;
831 }
832#endif
833 };
834
835 template <typename Iterator, typename Adapter>
836 struct RowIteratorBase : QRangeModelDetails::ParentIndex<is_tree<Impl>>
837 {
838 using iterator_category = std::random_access_iterator_tag;
839 using difference_type = int;
840
841 RowIteratorBase() = default;
842 RowIteratorBase(const RowIteratorBase &) = default;
843 RowIteratorBase(RowIteratorBase &&) = default;
844 RowIteratorBase &operator=(const RowIteratorBase &) = default;
845 RowIteratorBase &operator=(RowIteratorBase &&) = default;
846
847 RowIteratorBase(int row, const QModelIndex &parent, Adapter *adapter)
848 : QRangeModelDetails::ParentIndex<is_tree<Impl>>{parent}
849 , m_row(row), m_adapter(adapter)
850 {}
851
852 void swap(RowIteratorBase &other) noexcept
853 {
854 qSwap(m_row, other.m_row);
855 qSwap(this->m_rootIndex, other.m_rootIndex);
856 q_ptr_swap(m_adapter, other.m_adapter);
857 }
858
859 friend Iterator &operator++(Iterator &that)
860 {
861 ++that.m_row;
862 return that;
863 }
864 friend Iterator operator++(Iterator &that, int)
865 {
866 auto copy = that;
867 ++that;
868 return copy;
869 }
870 friend Iterator operator+(const Iterator &that, difference_type n)
871 {
872 return {that.m_row + n, that.root(), that.m_adapter};
873 }
874 friend Iterator operator+(difference_type n, const Iterator &that)
875 {
876 return that + n;
877 }
878 friend Iterator &operator+=(Iterator &that, difference_type n)
879 {
880 that.m_row += n;
881 return that;
882 }
883
884 friend Iterator &operator--(Iterator &that)
885 {
886 --that.m_row;
887 return that;
888 }
889 friend Iterator operator--(Iterator &that, int)
890 {
891 auto copy = that;
892 --that;
893 return copy;
894 }
895 friend Iterator operator-(const Iterator &that, difference_type n)
896 {
897 return {that.m_row - n, that.root(), that.m_adapter};
898 }
899 friend Iterator operator-(difference_type n, const Iterator &that)
900 {
901 return that - n;
902 }
903 friend Iterator &operator-=(Iterator &that, difference_type n)
904 {
905 that.m_row -= n;
906 return that;
907 }
908
909 friend difference_type operator-(const Iterator &lhs, const Iterator &rhs)
910 {
911 return lhs.m_row - rhs.m_row;
912 }
913
914 protected:
915 ~RowIteratorBase() = default;
916 int m_row = -1;
917 Adapter *m_adapter = nullptr;
918
919 private:
920 friend bool comparesEqual(const Iterator &lhs, const Iterator &rhs) noexcept
921 {
922 return lhs.m_row == rhs.m_row && lhs.root() == rhs.root();
923 }
924 friend Qt::strong_ordering compareThreeWay(const Iterator &lhs, const Iterator &rhs) noexcept
925 {
926 if (lhs.root() == rhs.root())
927 return qCompareThreeWay(lhs.m_row, rhs.m_row);
928 return qCompareThreeWay(lhs.root(), rhs.root());
929 }
930
931 Q_DECLARE_STRONGLY_ORDERED(Iterator)
932
933#ifndef QT_NO_DEBUG_STREAM
934 friend inline QDebug operator<<(QDebug dbg, const Iterator &it)
935 {
936 QDebugStateSaver saver(dbg);
937 dbg.nospace();
938 return dbg << "RowIterator(" << it.m_row << it.root() << ")";
939 }
940#endif
941 };
942
943public:
944 struct ConstRowIterator : public RowIteratorBase<ConstRowIterator, const QRangeModelAdapter>
945 {
946 private:
947 using Base = RowIteratorBase<ConstRowIterator, const QRangeModelAdapter>;
948 public:
949 using Base::Base;
950
951 using difference_type = typename Base::difference_type;
952 using value_type = std::conditional_t<is_list<Impl>,
953 const_data_type,
954 ConstRowReference>;
955 using reference = std::conditional_t<is_list<Impl>,
956 const_data_type,
957 ConstRowReference>;
958 using pointer = std::conditional_t<is_list<Impl>,
959 QRangeModelDetails::data_pointer_t<const_data_type>,
960 ConstRowReference>;
961
962 ConstRowIterator(const ConstRowIterator &other) = default;
963 ConstRowIterator(ConstRowIterator &&other) = default;
964 ConstRowIterator &operator=(const ConstRowIterator &other) = default;
965 ConstRowIterator &operator=(ConstRowIterator &&other) = default;
966 ~ConstRowIterator() = default;
967
968 pointer operator->() const
969 {
970 return pointer{operator*()};
971 }
972
973 reference operator*() const
974 {
975 if constexpr (is_list<Impl>) {
976 return this->m_adapter->at(this->m_row);
977 } else {
978 const QModelIndex index = this->m_adapter->model()->index(this->m_row, 0,
979 this->root());
980 return ConstRowReference{index, this->m_adapter};
981 }
982 }
983
984 reference operator[](difference_type n) const
985 {
986 return *(*this + n);
987 }
988 };
989
990 struct RowIterator : public RowIteratorBase<RowIterator, QRangeModelAdapter>
991 {
992 private:
993 using Base = RowIteratorBase<RowIterator, QRangeModelAdapter>;
994 public:
995 using Base::Base;
996
997 using difference_type = typename Base::difference_type;
998 using value_type = std::conditional_t<is_list<Impl>,
999 DataReference,
1000 RowReference>;
1001 using reference = std::conditional_t<is_list<Impl>,
1002 DataReference,
1003 RowReference>;
1004 using pointer = std::conditional_t<is_list<Impl>,
1005 DataReference,
1006 RowReference>;
1007
1008 RowIterator(const RowIterator &other) = default;
1009 RowIterator(RowIterator &&other) = default;
1010 RowIterator &operator=(const RowIterator &other) = default;
1011 RowIterator &operator=(RowIterator &&other) = default;
1012 ~RowIterator() = default;
1013
1014 operator ConstRowIterator() const
1015 {
1016 return ConstRowIterator{this->m_row, this->root(), this->m_adapter};
1017 }
1018
1019 pointer operator->() const
1020 {
1021 return pointer{operator*()};
1022 }
1023
1024 reference operator*() const
1025 {
1026 const QModelIndex index = this->m_adapter->model()->index(this->m_row, 0, this->root());
1027 if constexpr (is_list<Impl>) {
1028 return reference{index};
1029 } else {
1030 return reference{index, this->m_adapter};
1031 }
1032 }
1033
1034 reference operator[](difference_type n) const
1035 {
1036 return *(*this + n);
1037 }
1038 };
1039
1040 using const_iterator = ConstRowIterator;
1041 using iterator = RowIterator;
1042
1043 template <typename R,
1044 typename P,
1045 if_compatible_model_params<R, P> = true>
1046 explicit QRangeModelAdapter(R &&range, P &&protocol)
1047 : QRangeModelAdapter(new Model(QRangeModelDetails::forwardOrConvert<Range>(std::forward<R>(range)),
1048 QRangeModelDetails::forwardOrConvert<Protocol>(std::forward<P>(protocol))))
1049 {}
1050
1051 template <typename R,
1052 typename P = void, // to enable the ctr for void protocols only
1053 if_compatible_model_params<R, P> = true,
1054 unless_adapter<R> = true>
1055 explicit QRangeModelAdapter(R &&range)
1056 : QRangeModelAdapter(new Model(QRangeModelDetails::forwardOrConvert<Range>(std::forward<R>(range))))
1057 {}
1058
1059 // compiler-generated copy/move SMF are fine!
1060
1061 Model *model() const
1062 {
1063 return storage.m_model.get();
1064 }
1065
1066 const range_type &range() const
1067 {
1068 return QRangeModelDetails::refTo(storage.implementation()->childRange(storage.root()));
1069 }
1070
1071 template <typename NewRange = range_type, if_assignable_range<NewRange> = true>
1072 void assign(NewRange &&newRange)
1073 {
1074 assignImpl(qsizetype(Impl::size(QRangeModelDetails::refTo(newRange))),
1075 [&newRange](auto &oldRange) {
1076 oldRange = std::forward<NewRange>(newRange);
1077 });
1078 }
1079
1080 template <typename NewRange = range_type, if_assignable_range<NewRange> = true,
1081 unless_adapter<NewRange> = true>
1082 QRangeModelAdapter &operator=(NewRange &&newRange)
1083 {
1084 assign(std::forward<NewRange>(newRange));
1085 return *this;
1086 }
1087
1088 template <typename Row, if_assignable_range<std::initializer_list<Row>> = true>
1089 void assign(std::initializer_list<Row> newRange)
1090 {
1091 assignImpl(newRange.size(), [&newRange](auto &oldRange) {
1092 oldRange = newRange;
1093 });
1094 }
1095
1096 template <typename Row, if_assignable_range<std::initializer_list<Row>> = true>
1097 QRangeModelAdapter &operator=(std::initializer_list<Row> newRange)
1098 {
1099 assign(newRange);
1100 return *this;
1101 }
1102
1103 template <typename InputIterator, typename Sentinel, typename I = Impl, if_writable<I> = true>
1104 void assign(InputIterator first, Sentinel last)
1105 {
1106 assignImpl(size_t(std::distance(first, last)), [first, last](auto &oldRange) {
1107 oldRange.assign(first, last);
1108 });
1109 }
1110
1111 // iterator API
1112 ConstRowIterator cbegin() const
1113 {
1114 return ConstRowIterator{ 0, storage.root(), this };
1115 }
1116 ConstRowIterator begin() const { return cbegin(); }
1117
1118 ConstRowIterator cend() const
1119 {
1120 return ConstRowIterator{ rowCount(), storage.root(), this };
1121 }
1122 ConstRowIterator end() const { return cend(); }
1123
1124 template <typename I = Impl, if_writable<I> = true>
1125 RowIterator begin()
1126 {
1127 return RowIterator{ 0, storage.root(), this };
1128 }
1129
1130 template <typename I = Impl, if_writable<I> = true>
1131 RowIterator end()
1132 {
1133 return RowIterator{ rowCount(), storage.root(), this };
1134 }
1135
1136 int size() const
1137 {
1138 return rowCount();
1139 }
1140
1141 template <typename I = Impl, if_list<I> = true>
1142 QModelIndex index(int row) const
1143 {
1144 return storage->index(row, 0, storage.root());
1145 }
1146
1147 template <typename I = Impl, unless_list<I> = true>
1148 QModelIndex index(int row, int column) const
1149 {
1150 return storage->index(row, column, storage.root());
1151 }
1152
1153 template <typename I = Impl, if_tree<I> = true>
1154 QModelIndex index(QSpan<const int> path, int col) const
1155 {
1156 QModelIndex result = storage.root();
1157 auto count = path.size();
1158 for (const int r : path) {
1159 if (--count)
1160 result = storage->index(r, 0, result);
1161 else
1162 result = storage->index(r, col, result);
1163 }
1164 return result;
1165 }
1166
1167 int columnCount() const
1168 {
1169 // all rows and tree branches have the same column count
1170 return storage->columnCount({});
1171 }
1172
1173 int rowCount() const
1174 {
1175 return storage->rowCount(storage.root());
1176 }
1177
1178 template <typename I = Impl, if_tree<I> = true>
1179 int rowCount(int row) const
1180 {
1181 return storage->rowCount(index(row, 0));
1182 }
1183
1184 template <typename I = Impl, if_tree<I> = true>
1185 int rowCount(QSpan<const int> path) const
1186 {
1187 return storage->rowCount(index(path, 0));
1188 }
1189
1190 template <typename I = Impl, if_tree<I> = true>
1191 constexpr bool hasChildren(int row) const
1192 {
1193 return storage.m_model->hasChildren(index(row, 0));
1194 }
1195
1196 template <typename I = Impl, if_tree<I> = true>
1197 constexpr bool hasChildren(QSpan<const int> path) const
1198 {
1199 return storage.m_model->hasChildren(index(path, 0));
1200 }
1201
1202 template <typename I = Impl, if_list<I> = true>
1203 QVariant data(int row) const
1204 {
1205 return QRangeModelDetails::dataAtIndex<QVariant>(index(row));
1206 }
1207
1208 template <typename I = Impl, if_list<I> = true>
1209 QVariant data(int row, int role) const
1210 {
1211 return QRangeModelDetails::dataAtIndex<QVariant>(index(row), role);
1212 }
1213
1214 template <typename I = Impl, if_list<I> = true, if_writable<I> = true>
1215 bool setData(int row, const QVariant &value, int role = Qt::EditRole)
1216 {
1217 return storage->setData(index(row), value, role);
1218 }
1219
1220 template <typename I = Impl, unless_list<I> = true>
1221 QVariant data(int row, int column) const
1222 {
1223 return QRangeModelDetails::dataAtIndex<QVariant>(index(row, column));
1224 }
1225
1226 template <typename I = Impl, unless_list<I> = true>
1227 QVariant data(int row, int column, int role) const
1228 {
1229 return QRangeModelDetails::dataAtIndex<QVariant>(index(row, column), role);
1230 }
1231
1232 template <typename I = Impl, unless_list<I> = true, if_writable<I> = true>
1233 bool setData(int row, int column, const QVariant &value, int role = Qt::EditRole)
1234 {
1235 return storage->setData(index(row, column), value, role);
1236 }
1237
1238 template <typename I = Impl, if_tree<I> = true>
1239 QVariant data(QSpan<const int> path, int column) const
1240 {
1241 return QRangeModelDetails::dataAtIndex<QVariant>(index(path, column));
1242 }
1243
1244 template <typename I = Impl, if_tree<I> = true>
1245 QVariant data(QSpan<const int> path, int column, int role) const
1246 {
1247 return QRangeModelDetails::dataAtIndex<QVariant>(index(path, column), role);
1248 }
1249
1250 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1251 bool setData(QSpan<const int> path, int column, const QVariant &value, int role = Qt::EditRole)
1252 {
1253 return storage->setData(index(path, column), value, role);
1254 }
1255
1256 // at/operator[int] for list: returns value at row
1257 // if multi-role value: return the entire object
1258 template <typename I= Impl, if_list<I> = true>
1259 const_data_type at(int row) const
1260 {
1261 return QRangeModelDetails::dataAtIndex<data_type>(index(row));
1262 }
1263 template <typename I = Impl, if_list<I> = true>
1264 const_data_type operator[](int row) const { return at(row); }
1265
1266 template <typename I= Impl, if_list<I> = true, if_writable<I> = true>
1267 auto at(int row) {
1268 const QModelIndex idx = this->index(row);
1269 Q_ASSERT_X(idx.isValid(), "QRangeModelAdapter::at", "Index at row is invalid");
1270 return DataReference{idx};
1271 }
1272 template <typename I = Impl, if_list<I> = true, if_writable<I> = true>
1273 auto operator[](int row) {
1274 const QModelIndex idx = this->index(row);
1275 Q_ASSERT_X(idx.isValid(), "QRangeModelAdapter::operator[]", "Index at row is invalid");
1276 return DataReference{idx};
1277 }
1278
1279 // at/operator[int] for table or tree: a reference or view of the row
1280 template <typename I = Impl, unless_list<I> = true>
1281 decltype(auto) at(int row) const
1282 {
1283 return ConstRowReference{index(row, 0), this}.get();
1284 }
1285 template <typename I = Impl, unless_list<I> = true>
1286 decltype(auto) operator[](int row) const { return at(row); }
1287
1288 template <typename I = Impl, if_table<I> = true, if_writable<I> = true>
1289 auto at(int row)
1290 {
1291 return RowReference{index(row, 0), this};
1292 }
1293 template <typename I = Impl, if_table<I> = true, if_writable<I> = true>
1294 auto operator[](int row) { return at(row); }
1295
1296 // at/operator[int, int] for table: returns value at row/column
1297 template <typename I = Impl, unless_list<I> = true>
1298 const_data_type at(int row, int column) const
1299 {
1300 return QRangeModelDetails::dataAtIndex<data_type>(index(row, column));
1301 }
1302
1303#ifdef __cpp_multidimensional_subscript
1304 template <typename I = Impl, unless_list<I> = true>
1305 const_data_type operator[](int row, int column) const { return at(row, column); }
1306#endif
1307
1308 template <typename I = Impl, unless_list<I> = true, if_writable<I> = true>
1309 auto at(int row, int column)
1310 {
1311 const QModelIndex idx = this->index(row, column);
1312 Q_ASSERT_X(idx.isValid(), "QRangeModelAdapter::at", "Index at cell is invalid");
1313 return DataReference{idx};
1314 }
1315#ifdef __cpp_multidimensional_subscript
1316 template <typename I = Impl, unless_list<I> = true, if_writable<I> = true>
1317 auto operator[](int row, int column)
1318 {
1319 const QModelIndex idx = this->index(row, column);
1320 Q_ASSERT_X(idx.isValid(), "QRangeModelAdapter::operator[]", "Index at cell is invalid");
1321 return DataReference{idx};
1322 }
1323#endif
1324
1325 // at/operator[int] for tree: return a wrapper that maintains reference to
1326 // parent.
1327 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1328 auto at(int row)
1329 {
1330 return RowReference{index(row, 0), this};
1331 }
1332 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1333 auto operator[](int row) { return at(row); }
1334
1335 // at/operator[path] for tree: a reference or view of the row
1336 template <typename I = Impl, if_tree<I> = true>
1337 decltype(auto) at(QSpan<const int> path) const
1338 {
1339 return ConstRowReference{index(path, 0), this}.get();
1340 }
1341 template <typename I = Impl, if_tree<I> = true>
1342 decltype(auto) operator[](QSpan<const int> path) const { return at(path); }
1343
1344 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1345 auto at(QSpan<const int> path)
1346 {
1347 return RowReference{index(path, 0), this};
1348 }
1349 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1350 auto operator[](QSpan<const int> path) { return at(path); }
1351
1352 // at/operator[path, column] for tree: return value
1353 template <typename I = Impl, if_tree<I> = true>
1354 const_data_type at(QSpan<const int> path, int column) const
1355 {
1356 Q_PRE(path.size());
1357 return QRangeModelDetails::dataAtIndex<data_type>(index(path, column));
1358 }
1359
1360#ifdef __cpp_multidimensional_subscript
1361 template <typename I = Impl, if_tree<I> = true>
1362 const_data_type operator[](QSpan<const int> path, int column) const { return at(path, column); }
1363#endif
1364
1365 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1366 auto at(QSpan<const int> path, int column)
1367 {
1368 Q_PRE(path.size());
1369 const QModelIndex idx = this->index(path, column);
1370 Q_ASSERT_X(idx.isValid(), "QRangeModelAdapter::at", "Index at path is invalid");
1371 return DataReference{idx};
1372 }
1373#ifdef __cpp_multidimensional_subscript
1374 template <typename I = Impl, if_tree<I> = true, if_writable<I> = true>
1375 auto operator[](QSpan<const int> path, int column)
1376 {
1377 const QModelIndex idx = this->index(path, column);
1378 Q_ASSERT_X(idx.isValid(), "QRangeModelAdapter::operator[]", "Index at path is invalid");
1379 return DataReference{idx};
1380 }
1381#endif
1382
1383 template <typename I = Impl, if_canInsertRows<I> = true>
1384 bool insertRow(int before)
1385 {
1386 return storage.m_model->insertRow(before);
1387 }
1388
1389 template <typename I = Impl, if_canInsertRows<I> = true, if_tree<I> = true>
1390 bool insertRow(QSpan<const int> before)
1391 {
1392 Q_PRE(before.size());
1393 return storage.m_model->insertRow(before.back(), this->index(before.first(before.size() - 1), 0));
1394 }
1395
1396 template <typename D = row_type, typename I = Impl,
1397 if_canInsertRows<I> = true, if_compatible_row<D> = true>
1398 bool insertRow(int before, D &&data)
1399 {
1400 return insertRowImpl(before, storage.root(), std::forward<D>(data));
1401 }
1402
1403 template <typename D = row_type, typename I = Impl,
1404 if_canInsertRows<I> = true, if_compatible_row<D> = true, if_tree<I> = true>
1405 bool insertRow(QSpan<const int> before, D &&data)
1406 {
1407 return insertRowImpl(before.back(), this->index(before.sliced(0, before.size() - 1), 0),
1408 std::forward<D>(data));
1409 }
1410
1411 template <typename C, typename I = Impl,
1412 if_canInsertRows<I> = true, if_compatible_row_range<C> = true>
1413 bool insertRows(int before, C &&data)
1414 {
1415 return insertRowsImpl(before, storage.root(), std::forward<C>(data));
1416 }
1417
1418 template <typename C, typename I = Impl,
1419 if_canInsertRows<I> = true, if_compatible_row_range<C> = true, if_tree<I> = true>
1420 bool insertRows(QSpan<const int> before, C &&data)
1421 {
1422 return insertRowsImpl(before.back(), this->index(before.sliced(0, before.size() - 1), 0),
1423 std::forward<C>(data));
1424 }
1425
1426 template <typename I = Impl, if_canRemoveRows<I> = true>
1427 bool removeRow(int row)
1428 {
1429 return removeRows(row, 1);
1430 }
1431
1432 template <typename I = Impl, if_canRemoveRows<I> = true, if_tree<I> = true>
1433 bool removeRow(QSpan<const int> path)
1434 {
1435 return removeRows(path, 1);
1436 }
1437
1438 template <typename I = Impl, if_canRemoveRows<I> = true>
1439 bool removeRows(int row, int count)
1440 {
1441 return storage->removeRows(row, count, storage.root());
1442 }
1443
1444 template <typename I = Impl, if_canRemoveRows<I> = true, if_tree<I> = true>
1445 bool removeRows(QSpan<const int> path, int count)
1446 {
1447 return storage->removeRows(path.back(), count,
1448 this->index(path.first(path.size() - 1), 0));
1449 }
1450
1451 template <typename F = range_features, if_canMoveItems<F> = true>
1452 bool moveRow(int source, int destination)
1453 {
1454 return moveRows(source, 1, destination);
1455 }
1456
1457 template <typename F = range_features, if_canMoveItems<F> = true>
1458 bool moveRows(int source, int count, int destination)
1459 {
1460 return storage->moveRows(storage.root(), source, count, storage.root(), destination);
1461 }
1462
1463 template <typename I = Impl, typename F = range_features,
1464 if_canMoveItems<F> = true, if_tree<I> = true>
1465 bool moveRow(QSpan<const int> source, QSpan<const int> destination)
1466 {
1467 return moveRows(source, 1, destination);
1468 }
1469
1470 template <typename I = Impl, typename F = range_features,
1471 if_canMoveItems<F> = true, if_tree<I> = true>
1472 bool moveRows(QSpan<const int> source, int count, QSpan<const int> destination)
1473 {
1474 return storage->moveRows(this->index(source.first(source.size() - 1), 0),
1475 source.back(),
1476 count,
1477 this->index(destination.first(destination.size() - 1), 0),
1478 destination.back());
1479 }
1480
1481 template <typename I = Impl, if_canInsertColumns<I> = true>
1482 bool insertColumn(int before)
1483 {
1484 return storage.m_model->insertColumn(before);
1485 }
1486
1487 template <typename D, typename I = Impl,
1488 if_canInsertColumns<I> = true, if_compatible_column_data<D> = true>
1489 bool insertColumn(int before, D &&data)
1490 {
1491 return insertColumnImpl(before, storage.root(), std::forward<D>(data));
1492 }
1493
1494 template <typename C, typename I = Impl,
1495 if_canInsertColumns<I> = true, if_compatible_column_range<C> = true>
1496 bool insertColumns(int before, C &&data)
1497 {
1498 return insertColumnsImpl(before, storage.root(), std::forward<C>(data));
1499 }
1500
1501 template <typename I = Impl, if_canRemoveColumns<I> = true>
1502 bool removeColumn(int column)
1503 {
1504 return storage.m_model->removeColumn(column);
1505 }
1506
1507 template <typename I = Impl, if_canRemoveColumns<I> = true>
1508 bool removeColumns(int column, int count)
1509 {
1510 return storage->removeColumns(column, count, {});
1511 }
1512
1513 template <typename F = row_features, if_canMoveItems<F> = true>
1514 bool moveColumn(int from, int to)
1515 {
1516 return moveColumns(from, 1, to);
1517 }
1518
1519 template <typename F = row_features, if_canMoveItems<F> = true>
1520 bool moveColumns(int from, int count, int to)
1521 {
1522 return storage->moveColumns(storage.root(), from, count, storage.root(), to);
1523 }
1524
1525 template <typename I = Impl, typename F = row_features,
1526 if_canMoveItems<F> = true, if_tree<I> = true>
1527 bool moveColumn(QSpan<const int> source, int to)
1528 {
1529 const QModelIndex parent = this->index(source.first(source.size() - 1), 0);
1530 return storage->moveColumns(parent, source.back(), 1, parent, to);
1531 }
1532
1533 template <typename I = Impl, typename F = row_features,
1534 if_canMoveItems<F> = true, if_tree<I> = true>
1535 bool moveColumns(QSpan<const int> source, int count, int destination)
1536 {
1537 const QModelIndex parent = this->index(source.first(source.size() - 1), 0);
1538 return storage->moveColumns(parent, source.back(), count, parent, destination);
1539 }
1540
1541private:
1542 friend inline
1543 bool comparesEqual(const QRangeModelAdapter &lhs, const QRangeModelAdapter &rhs) noexcept
1544 {
1545 return lhs.storage.m_model == rhs.storage.m_model;
1546 }
1547 Q_DECLARE_EQUALITY_COMPARABLE(QRangeModelAdapter)
1548
1549 friend inline
1550 bool comparesEqual(const QRangeModelAdapter &lhs, const range_type &rhs)
1551 {
1552 return lhs.range() == rhs;
1553 }
1554 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QRangeModelAdapter, range_type)
1555
1556
1557 void emitDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
1558 {
1559 Q_EMIT storage.implementation()->dataChanged(topLeft, bottomRight, {});
1560 }
1561
1562 void beginAssignImpl(Impl *impl, range_type *oldRange, int newLastRow)
1563 {
1564 const QModelIndex root = storage.root();
1565 const qsizetype oldLastRow = qsizetype(Impl::size(oldRange)) - 1;
1566
1567 if (!root.isValid()) {
1568 impl->beginResetModel();
1569 impl->deleteOwnedRows();
1570 } else if constexpr (is_tree<Impl>) {
1571 if (oldLastRow >= 0) {
1572 impl->beginRemoveRows(root, 0, model()->rowCount(root) - 1);
1573 impl->deleteRemovedRows(QRangeModelDetails::refTo(oldRange));
1574 impl->endRemoveRows();
1575 }
1576 if (newLastRow >= 0)
1577 impl->beginInsertRows(root, 0, newLastRow);
1578 } else {
1579 Q_ASSERT_X(false, "QRangeModelAdapter::assign",
1580 "Internal error: The root index in a table or list must be invalid.");
1581 }
1582 }
1583
1584 void endAssignImpl(Impl *impl, int newLastRow)
1585 {
1586 const QModelIndex root = storage.root();
1587 if (!root.isValid()) {
1588 impl->endResetModel();
1589 } else if constexpr (is_tree<Impl>) {
1590 if (newLastRow >= 0) {
1591 Q_ASSERT(model()->hasChildren(root));
1592 // if it was moved, then newRange is now likely to be empty. Get
1593 // the inserted row.
1594 impl->setParentRow(QRangeModelDetails::refTo(impl->childRange(root)),
1595 QRangeModelDetails::pointerTo(impl->rowData(root)));
1596 impl->endInsertRows();
1597 }
1598 }
1599 }
1600
1601 template <typename Assigner>
1602 void assignImpl(std::size_t newSize, Assigner &&assigner)
1603 {
1604 const auto sz = q26::saturate_cast<int>(newSize);
1605 Q_ASSERT_X(q20::cmp_equal(sz, newSize),
1606 "QRangeModelAdapter::assign", "New range is too large");
1607 const int newLastRow = sz - 1;
1608
1609 auto *impl = storage.implementation();
1610 auto *oldRange = impl->childRange(storage.root());
1611 beginAssignImpl(impl, oldRange, newLastRow);
1612 assigner(QRangeModelDetails::refTo(oldRange));
1613 endAssignImpl(impl, newLastRow);
1614
1615 if constexpr (Impl::itemsAreQObjects) {
1616 if (model()->autoConnectPolicy() == QRangeModel::AutoConnectPolicy::Full) {
1617 const auto begin = QRangeModelDetails::begin(QRangeModelDetails::refTo(oldRange));
1618 const auto end = QRangeModelDetails::end(QRangeModelDetails::refTo(oldRange));
1619 int rowIndex = 0;
1620 for (auto it = begin; it != end; ++it, ++rowIndex)
1621 impl->autoConnectPropertiesInRow(*it, rowIndex, storage.root());
1622 }
1623 }
1624 }
1625
1626
1627 template <typename P, typename Row>
1628 static auto setParentRow(P &protocol, Row &newRow, row_ptr parentRow)
1629 -> decltype(protocol.setParentRow(newRow, parentRow))
1630 {
1631 return protocol.setParentRow(newRow, parentRow);
1632 }
1633 template <typename ...Args> static constexpr void setParentRow(Args &&...)
1634 {
1635 static_assert(!protocol_traits::has_setParentRow,
1636 "Internal error, wrong setParentRow overload called");
1637 }
1638
1639 template <typename D>
1640 bool insertRowImpl(int before, const QModelIndex &parent, D &&data)
1641 {
1642 return storage.implementation()->doInsertRows(before, 1, parent, [&data, this]
1643 (range_type &range, auto *parentRow, int row, int count) {
1644 Q_UNUSED(this);
1645 const auto oldSize = range.size();
1646 auto newRow = range.emplace(QRangeModelDetails::pos(range, row), std::forward<D>(data));
1647 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
1648 QRangeModelDetails::pointerTo(parentRow));
1649 return range.size() == oldSize + count;
1650 });
1651 }
1652
1653 template <typename LHS>
1654 static auto selfInsertion(LHS *lhs, LHS *rhs) -> decltype(lhs == rhs)
1655 {
1656 if (lhs == rhs) {
1657#ifndef QT_NO_DEBUG
1658 qCritical("Inserting data into itself is not supported");
1659#endif
1660 return true;
1661 }
1662 return false;
1663 }
1664 template <typename LHS, typename RHS>
1665 static constexpr bool selfInsertion(LHS *, RHS *) { return false; }
1666
1667 template <typename C>
1668 bool insertRowsImpl(int before, const QModelIndex &parent, C &&data)
1669 {
1670 bool result = false;
1671 result = storage->doInsertRows(before, int(std::size(data)), parent, [&data, this]
1672 (range_type &range, auto *parentRow, int row, int count){
1673 Q_UNUSED(parentRow);
1674 Q_UNUSED(this);
1675 const auto pos = QRangeModelDetails::pos(range, row);
1676 const auto oldSize = range.size();
1677
1678 auto dataRange = [&data]{
1679 if constexpr (std::is_rvalue_reference_v<C&&>) {
1680 return std::make_pair(
1681 std::move_iterator(std::begin(data)),
1682 std::move_iterator(std::end(data))
1683 );
1684 } else {
1685 return std::make_pair(std::begin(data), std::end(data));
1686 }
1687 }();
1688
1689 if constexpr (range_features::has_insert_range) {
1690 if (selfInsertion(&range, &data))
1691 return false;
1692 auto start = range.insert(pos, dataRange.first, dataRange.second);
1693 if constexpr (protocol_traits::has_setParentRow) {
1694 while (count) {
1695 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*start),
1696 QRangeModelDetails::pointerTo(parentRow));
1697 ++start;
1698 --count;
1699 }
1700 } else {
1701 Q_UNUSED(start);
1702 }
1703 } else {
1704 auto newRow = range.insert(pos, count, row_type{});
1705 while (dataRange.first != dataRange.second) {
1706 *newRow = *dataRange.first;
1707 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
1708 QRangeModelDetails::pointerTo(parentRow));
1709 ++dataRange.first;
1710 ++newRow;
1711 }
1712 }
1713 return range.size() == oldSize + count;
1714 });
1715 return result;
1716 }
1717
1718 template <typename D, typename = void>
1719 struct DataFromList {
1720 static constexpr auto first(D &data) { return &data; }
1721 static constexpr auto next(D &, D *entry) { return entry; }
1722 };
1723
1724 template <typename D>
1725 struct DataFromList<D, std::enable_if_t<QRangeModelDetails::range_traits<D>::value>>
1726 {
1727 static constexpr auto first(D &data) { return std::begin(data); }
1728 static constexpr auto next(D &data, typename D::iterator entry)
1729 {
1730 ++entry;
1731 if (entry == std::end(data))
1732 entry = first(data);
1733 return entry;
1734 }
1735 };
1736
1737 template <typename D, typename = void> struct RowFromTable
1738 {
1739 static constexpr auto first(D &data) { return &data; }
1740 static constexpr auto next(D &, D *entry) { return entry; }
1741 };
1742
1743 template <typename D>
1744 struct RowFromTable<D, std::enable_if_t<std::conjunction_v<
1745 QRangeModelDetails::range_traits<D>,
1746 QRangeModelDetails::range_traits<typename D::value_type>
1747 >>
1748 > : DataFromList<D>
1749 {};
1750
1751 template <typename D>
1752 bool insertColumnImpl(int before, const QModelIndex &parent, D data)
1753 {
1754 auto entry = DataFromList<D>::first(data);
1755
1756 return storage->doInsertColumns(before, 1, parent, [&entry, &data]
1757 (auto &range, auto pos, int count) {
1758 const auto oldSize = range.size();
1759 range.insert(pos, *entry);
1760 entry = DataFromList<D>::next(data, entry);
1761 return range.size() == oldSize + count;
1762 });
1763 }
1764
1765 template <typename C>
1766 bool insertColumnsImpl(int before, const QModelIndex &parent, C data)
1767 {
1768 bool result = false;
1769 auto entries = RowFromTable<C>::first(data);
1770 auto begin = std::begin(*entries);
1771 auto end = std::end(*entries);
1772 result = storage->doInsertColumns(before, int(std::size(*entries)), parent,
1773 [&begin, &end, &entries, &data](auto &range, auto pos, int count) {
1774 const auto oldSize = range.size();
1775 if constexpr (row_features::has_insert_range) {
1776 range.insert(pos, begin, end);
1777 } else {
1778 auto start = range.insert(pos, count, {});
1779 std::copy(begin, end, start);
1780 }
1781 entries = RowFromTable<C>::next(data, entries);
1782 begin = std::begin(*entries);
1783 end = std::end(*entries);
1784 return range.size() == oldSize + count;
1785 });
1786 return result;
1787 }
1788};
1789
1790// Deduction guides are needed to correctly map single-parameter construction
1791// to the void-protocol case, and to disable construction from incompatible
1792// ranges.
1793template <typename Range, typename Protocol,
1794 QRangeModelDetails::if_can_construct<Range, Protocol> = true>
1796
1797template <typename Range,
1798 QRangeModelDetails::if_can_construct<Range> = true>
1799QRangeModelAdapter(Range &&) -> QRangeModelAdapter<Range, void>;
1800
1801QT_END_NAMESPACE
1802
1803#endif // QRANGEMODELADAPTER_H
QRangeModelAdapter(Range &&, Protocol &&) -> QRangeModelAdapter< Range, Protocol >