16class QT_TECH_PREVIEW_API QRangeModelAdapter
18 using Impl = QRangeModelDetails::RangeImplementation<Range, Protocol>;
19 using Storage = QRangeModelDetails::AdapterStorage<Model, Impl>;
24 using range_type = Range;
26 using range_type = QRangeModelDetails::wrapped_t<Range>;
28 using const_row_reference =
typename Impl::const_row_reference;
29 using row_reference =
typename Impl::row_reference;
30 using range_features =
typename QRangeModelDetails::range_traits<range_type>;
31 using row_type = std::remove_reference_t<row_reference>;
32 using row_features = QRangeModelDetails::range_traits<
typename Impl::wrapped_row_type>;
33 using row_ptr =
typename Impl::wrapped_row_type *;
34 using row_traits =
typename Impl::row_traits;
35 using item_type = std::remove_reference_t<
typename row_traits::item_type>;
36 using data_type =
typename QRangeModelDetails::data_type<item_type>::type;
37 using const_data_type = QRangeModelDetails::asConst_t<data_type>;
38 using protocol_traits =
typename Impl::protocol_traits;
40 template <
typename I>
static constexpr bool is_list = I::protocol_traits::is_list;
41 template <
typename I>
static constexpr bool is_table = I::protocol_traits::is_table;
42 template <
typename I>
static constexpr bool is_tree = I::protocol_traits::is_tree;
43 template <
typename I>
static constexpr bool canInsertColumns = I::dynamicColumns()
45 && row_features::has_insert;
46 template <
typename I>
static constexpr bool canRemoveColumns = I::dynamicColumns()
48 && row_features::has_erase;
50 template <
typename I>
using if_writable = std::enable_if_t<I::isMutable(),
bool>;
51 template <
typename I>
static constexpr bool canSort = I::isMutable()
52 && std::is_swappable_v<row_type>;
53 template <
typename I>
using if_canSort = std::enable_if_t<canSort<I>,
bool>;
54 template <
typename I>
using if_list = std::enable_if_t<is_list<I>,
bool>;
55 template <
typename I>
using unless_list = std::enable_if_t<!is_list<I>,
bool>;
56 template <
typename I>
using if_table = std::enable_if_t<is_table<I>,
bool>;
57 template <
typename I>
using if_tree = std::enable_if_t<is_tree<I>,
bool>;
58 template <
typename I>
using unless_tree = std::enable_if_t<!is_tree<I>,
bool>;
59 template <
typename I>
using if_flat = std::enable_if_t<is_list<I> || is_table<I>,
bool>;
62 using if_canInsertRows = std::enable_if_t<I::canInsertRows(),
bool>;
63 template <
typename I>
static constexpr bool canRemoveRows = I::canRemoveRows();
65 using if_canRemoveRows = std::enable_if_t<canRemoveRows<I>,
bool>;
67 using if_canMoveItems = std::enable_if_t<F::has_rotate || F::has_splice,
bool>;
70 using if_canInsertColumns = std::enable_if_t<canInsertColumns<I>,
bool>;
72 using if_canRemoveColumns = std::enable_if_t<canRemoveColumns<I>,
bool>;
74 template <
typename Row>
75 static constexpr bool is_compatible_row = std::is_convertible_v<Row, const_row_reference>;
76 template <
typename Row>
77 using if_compatible_row = std::enable_if_t<is_compatible_row<Row>,
bool>;
80 static constexpr bool is_compatible_row_range = is_compatible_row<
81 decltype(*std::begin(std::declval<C&>()))
84 using if_compatible_row_range = std::enable_if_t<is_compatible_row_range<C>,
bool>;
85 template <
typename Data>
86 static constexpr bool is_compatible_data = std::is_convertible_v<Data, data_type>;
87 template <
typename Data>
88 using if_compatible_data = std::enable_if_t<is_compatible_data<Data>,
bool>;
90 static constexpr bool is_compatible_data_range = is_compatible_data<
91 typename QRangeModelDetails::data_type<
92 typename QRangeModelDetails::row_traits<
93 decltype(*std::begin(std::declval<C&>()))
98 using if_compatible_column_data = std::enable_if_t<is_compatible_data<C>
99 || is_compatible_data_range<C>,
bool>;
100 template <
typename C>
101 using if_compatible_column_range = std::enable_if_t<is_compatible_data_range<C>,
bool>;
103 template <
typename R>
104 using if_assignable_range = std::enable_if_t<std::is_assignable_v<range_type, R>,
bool>;
108 template <
typename ...Args>
109 static constexpr bool is_emplaceable =
sizeof...(Args) == 0
110 || std::is_constructible_v<std::remove_cv_t<row_type>, Args...>;
111 template <
typename ...Args>
112 using if_emplaceable = std::enable_if_t<is_emplaceable<Args...>,
bool>;
114 template <
typename It>
115 static constexpr bool is_compatible_row_iterator
116 = QRangeModelDetails::is_compatible_forward_iterator<It, const_row_reference>::value;
117 template <
typename It>
118 using if_compatible_row_iterator = std::enable_if_t<is_compatible_row_iterator<It>,
bool>;
122 template <
typename I>
static constexpr bool canInsertCopies = I::canInsertRows()
123 && range_features::has_insert
124 && std::is_copy_constructible_v<row_type>
125 && !std::is_pointer_v<row_type>;
126 template <
typename I>
127 using if_canInsertCopies = std::enable_if_t<canInsertCopies<I>,
bool>;
130 template <
typename I>
static constexpr bool canAssignCopies = I::isMutable()
131 && (QRangeModelDetails::has_assign_count<range_type>::value
132 || (range_features::has_insert
133 && range_features::has_erase))
134 && std::is_copy_constructible_v<row_type>
135 && !std::is_pointer_v<row_type>;
136 template <
typename I>
137 using if_canAssignCopies = std::enable_if_t<canAssignCopies<I>,
bool>;
139 friend class QRangeModel;
140 template <
typename T>
141 static constexpr bool is_adapter = QRangeModelDetails::is_any_of<q20::remove_cvref_t<T>,
142 QRangeModelAdapter>::value;
143 template <
typename T>
144 using unless_adapter = std::enable_if_t<!is_adapter<T>,
bool>;
146 template <
typename R,
typename P>
147 using if_compatible_model_params =
151 std::is_convertible<R &&, Range>,
152 std::is_convertible<R &&, Range &&>
154 std::is_convertible<P, Protocol>
157#if !defined(Q_OS_VXWORKS) && !defined(Q_OS_INTEGRITY)
163 template <
typename T>
static constexpr T asMutable(
const T &);
164 template <
typename T>
static constexpr T *asMutable(
const T *);
165 template <
template <
typename,
typename...>
typename U,
typename T,
typename ...Args>
166 static constexpr U<T, Args...> asMutable(
const U<
const T, Args...> &);
168 template <
typename T>
169 using asMutable_t =
decltype(asMutable(std::declval<T>()));
170 friend class QRangeModelAdapter<asMutable_t<Range>, Protocol, Model>;
172 template <
typename R,
typename P,
typename M>
173 friend class QRangeModelAdapter;
176 explicit QRangeModelAdapter(
const std::shared_ptr<QRangeModel> &model,
const QModelIndex &root,
178 : storage{model, root}
181 explicit QRangeModelAdapter(QRangeModel *model)
189 using value_type =
int;
190 using const_value_type =
const int;
191 using pointer =
void *;
193 using value_type = data_type;
194 using const_value_type = const_data_type;
195 using pointer = QRangeModelDetails::data_pointer_t<const_value_type>;
198 explicit DataReference(
const QModelIndex &index)
noexcept
202 DataReference(
const DataReference &other) =
default;
203 DataReference(DataReference &&other) =
default;
206 DataReference &operator=(
const DataReference &other)
212 ~DataReference() =
default;
214 DataReference &operator=(
const value_type &value)
220 DataReference &operator=(value_type &&value)
222 assign(std::move(value));
226 const_value_type get()
const
228 Q_ASSERT_X(m_index.isValid(),
"QRangeModelAdapter::at",
"Index at position is invalid");
229 return QRangeModelDetails::dataAtIndex<q20::remove_cvref_t<value_type>>(m_index);
232 operator const_value_type()
const
237 pointer operator->()
const
242 bool isValid()
const {
return m_index.isValid(); }
247 template <
typename Value>
248 void assign(Value &&value)
250 constexpr Qt::ItemDataRole dataRole = Qt::RangeModelAdapterRole;
252 if (m_index.isValid()) {
253 auto model =
const_cast<QAbstractItemModel *>(m_index.model());
254 [[maybe_unused]]
bool couldWrite =
false;
255 if constexpr (std::is_same_v<q20::remove_cvref_t<Value>, QVariant>) {
256 couldWrite = model->setData(m_index, value, dataRole);
258 couldWrite = model->setData(m_index,
259 QVariant::fromValue(std::forward<Value>(value)),
264 qWarning() <<
"Writing value of type"
265 << QMetaType::fromType<q20::remove_cvref_t<Value>>().name()
266 <<
"to role" << dataRole <<
"at index" << m_index <<
"failed";
269 qCritical(
"Data reference for invalid index, can't write to model");
274 friend inline bool comparesEqual(
const DataReference &lhs,
const DataReference &rhs)
276 return lhs.m_index == rhs.m_index
277 || lhs.get() == rhs.get();
279 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(DataReference)
281 friend inline bool comparesEqual(
const DataReference &lhs,
const value_type &rhs)
283 return lhs.get() == rhs;
285 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(DataReference, value_type)
289#define QRMA_DATAREFERENCE_RELATIONAL_OPERATOR(op, expression)
290 template <typename U = value_type>
291 friend QTypeTraits::compare_lt_result<U>
292 operator
op(const DataReference &lhs, const DataReference &rhs)
293 { const value_type &a = lhs.get(); const value_type &b = rhs.get(); return expression; }
294 template <typename U = value_type>
295 friend QTypeTraits::compare_lt_result<U>
296 operator
op(const DataReference &lhs, const value_type &b)
297 { const value_type &a = lhs.get(); return expression; }
298 template <typename U = value_type>
299 friend QTypeTraits::compare_lt_result<U>
300 operator
op(const value_type &a, const DataReference &rhs)
301 { const value_type &b = rhs.get(); return expression; }
307#undef QRMA_DATAREFERENCE_RELATIONAL_OPERATOR
312 friend void swap(DataReference lhs, DataReference rhs) =
delete;
314#ifndef QT_NO_DEBUG_STREAM
315 friend inline QDebug operator<<(QDebug dbg,
const DataReference &ref)
317 return dbg << ref.get();
320#ifndef QT_NO_DATASTREAM
321 friend inline QDataStream &operator<<(QDataStream &ds,
const DataReference &ref)
323 return ds << ref.get();
325 friend inline QDataStream &operator>>(QDataStream &ds, DataReference &ref)
329 ref = std::move(value);
334 template <
typename Iterator,
typename Adapter>
335 struct ColumnIteratorBase
337 using iterator_category = std::random_access_iterator_tag;
338 using difference_type =
int;
340 ColumnIteratorBase() =
default;
341 ColumnIteratorBase(
const ColumnIteratorBase &other) =
default;
342 ColumnIteratorBase(ColumnIteratorBase &&other) =
default;
343 ColumnIteratorBase &operator=(
const ColumnIteratorBase &other) =
default;
344 ColumnIteratorBase &operator=(ColumnIteratorBase &&other) =
default;
346 ColumnIteratorBase(
const QModelIndex &rowIndex,
int column, Adapter *adapter)
noexcept
347 : m_rowIndex(rowIndex), m_column(column), m_adapter(adapter)
351 void swap(ColumnIteratorBase &other)
noexcept
353 std::swap(m_rowIndex, other.m_rowIndex);
354 std::swap(m_column, other.m_column);
355 q_ptr_swap(m_adapter, other.m_adapter);
358 friend Iterator &operator++(Iterator &that)
363 friend Iterator operator++(Iterator &that,
int)
369 friend Iterator operator+(
const Iterator &that, difference_type n)
371 return {that.m_rowIndex, that.m_column + n, that.m_adapter};
373 friend Iterator operator+(difference_type n,
const Iterator &that)
377 friend Iterator &operator+=(Iterator &that, difference_type n)
383 friend Iterator &operator--(Iterator &that)
388 friend Iterator operator--(Iterator &that,
int)
394 friend Iterator operator-(
const Iterator &that, difference_type n)
396 return {that.m_rowIndex, that.m_column - n, that.m_adapter};
398 friend Iterator operator-(difference_type n,
const Iterator &that)
402 friend Iterator &operator-=(Iterator &that, difference_type n)
408 friend difference_type operator-(
const Iterator &lhs,
const Iterator &rhs)
410 Q_PRE(lhs.m_rowIndex == rhs.m_rowIndex);
411 Q_PRE(lhs.m_adapter == rhs.m_adapter);
412 return lhs.m_column - rhs.m_column;
416 ~ColumnIteratorBase() =
default;
417 QModelIndex m_rowIndex;
419 Adapter *m_adapter =
nullptr;
422 friend bool comparesEqual(
const Iterator &lhs,
const Iterator &rhs)
424 Q_ASSERT(lhs.m_rowIndex == rhs.m_rowIndex);
425 return lhs.m_column == rhs.m_column;
427 friend Qt::strong_ordering compareThreeWay(
const Iterator &lhs,
const Iterator &rhs)
429 Q_ASSERT(lhs.m_rowIndex == rhs.m_rowIndex);
430 return qCompareThreeWay(lhs.m_column, rhs.m_column);
433 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Iterator)
435#ifndef QT_NO_DEBUG_STREAM
436 friend inline QDebug operator<<(QDebug dbg,
const Iterator &it)
438 QDebugStateSaver saver(dbg);
440 return dbg <<
"ColumnIterator(" << it.m_rowIndex.siblingAtColumn(it.m_column) <<
")";
445 struct ConstColumnIterator : ColumnIteratorBase<ConstColumnIterator,
const QRangeModelAdapter>
448 using Base = ColumnIteratorBase<ConstColumnIterator,
const QRangeModelAdapter>;
450 using difference_type =
typename Base::difference_type;
451 using value_type = data_type;
452 using reference = const_data_type;
453 using pointer = QRangeModelDetails::data_pointer_t<value_type>;
456 ~ConstColumnIterator() =
default;
458 pointer operator->()
const
460 return pointer{operator*()};
463 reference operator*()
const
465 return std::as_const(
this->m_adapter)->at(
this->m_rowIndex.row(),
this->m_column);
468 reference operator[](difference_type n)
const
474 struct ColumnIterator : ColumnIteratorBase<ColumnIterator, QRangeModelAdapter>
477 using Base = ColumnIteratorBase<ColumnIterator, QRangeModelAdapter>;
479 using difference_type =
typename Base::difference_type;
481 using value_type = data_type;
482 using reference = DataReference;
483 using pointer = reference;
486 ~ColumnIterator() =
default;
488 operator ConstColumnIterator()
const
490 return ConstColumnIterator{
this->m_rowIndex,
this->m_column,
this->m_adapter};
493 pointer operator->()
const
498 reference operator*()
const
500 return reference{
this->m_rowIndex.siblingAtColumn(
this->m_column)};
503 reference operator[](difference_type n)
const
509 template <
typename Reference,
typename const_row_type,
510 bool = std::is_reference_v<const_row_type>,
511 bool = std::is_pointer_v<const_row_type>>
514 const_row_type get()
const
516 const Reference *that =
static_cast<
const Reference *>(
this);
517 const auto *impl = that->m_adapter->storage.implementation();
518 auto *childRange = impl->childRange(that->m_index.parent());
519 if constexpr (std::is_convertible_v<
const row_type &, const_row_type>) {
520 return *std::next(QRangeModelDetails::adl_begin(childRange), that->m_index.row());
522 const auto &row = *std::next(QRangeModelDetails::adl_begin(childRange),
523 that->m_index.row());
524 return const_row_type{QRangeModelDetails::adl_begin(row),
525 QRangeModelDetails::adl_end(row)};
529 const_row_type operator->()
const
534 operator const_row_type()
const
540 template <
typename Reference,
typename const_row_type>
541 struct RowGetter<Reference, const_row_type,
true,
false>
543 const_row_type get()
const
545 const Reference *that =
static_cast<
const Reference *>(
this);
546 const auto *impl = that->m_adapter->storage.implementation();
547 return *std::next(QRangeModelDetails::begin(
548 QRangeModelDetails::refTo(impl->childRange(that->m_index.parent()))),
549 that->m_index.row());
552 auto operator->()
const
554 return std::addressof(get());
557 operator const_row_type()
const
563 template <
typename Reference,
typename const_row_type>
564 struct RowGetter<Reference, const_row_type,
false,
true>
566 const_row_type get()
const
568 const Reference *that =
static_cast<
const Reference *>(
this);
569 const auto *impl = that->m_adapter->storage.implementation();
570 return *std::next(QRangeModelDetails::begin(
571 QRangeModelDetails::refTo(impl->childRange(that->m_index.parent()))),
572 that->m_index.row());
575 const_row_type operator->()
const
580 operator const_row_type()
const
586 template <
typename Reference,
typename Adapter>
587 struct RowReferenceBase : RowGetter<Reference, QRangeModelDetails::asConstRow_t<row_type>>
589 using const_iterator = ConstColumnIterator;
590 using size_type =
int;
591 using difference_type =
int;
592 using const_row_type = QRangeModelDetails::asConstRow_t<row_type>;
594 RowReferenceBase(
const QModelIndex &index, Adapter *adapter)
noexcept
595 : m_index(index), m_adapter(adapter)
598 template <
typename I = Impl, if_tree<I> =
true>
599 bool hasChildren()
const
601 return m_adapter->model()->hasChildren(m_index);
604 template <
typename I = Impl, if_tree<I> =
true>
605 auto children()
const
607 using ConstRange = QRangeModelDetails::asConst_t<Range>;
608 return QRangeModelAdapter<ConstRange, Protocol, Model>(m_adapter->storage.m_model,
609 m_index, std::in_place);
612 ConstColumnIterator cbegin()
const
614 return ConstColumnIterator{m_index, 0, m_adapter};
616 ConstColumnIterator cend()
const
618 return ConstColumnIterator{m_index, m_adapter->columnCount(), m_adapter};
621 ConstColumnIterator begin()
const {
return cbegin(); }
622 ConstColumnIterator end()
const {
return cend(); }
624 size_type size()
const
626 return m_adapter->columnCount();
629 auto at(
int column)
const
631 Q_ASSERT(column >= 0 && column < m_adapter->columnCount());
632 return *ConstColumnIterator{m_index, column, m_adapter};
635 auto operator[](
int column)
const
641 friend struct RowGetter<Reference, const_row_type>;
642 ~RowReferenceBase() =
default;
649 template <
typename Other>
650 static bool rowsEqual(
const Reference &lhs,
const Other &rhs)
652 if constexpr (QTypeTraits::has_operator_equal_v<std::remove_cv_t<row_type>>)
653 return lhs.get() == rhs.get();
660 friend bool comparesEqual(
const Reference &lhs,
const Reference &rhs)
662 return lhs.m_index == rhs.m_index || rowsEqual(lhs, rhs);
664 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(Reference)
668 friend const QModelIndex &rowReferenceIndex(
const Reference &ref)
noexcept
677 template <
typename Other>
678 using if_comparable_row_reference = std::enable_if_t<
679 !std::is_same_v<Other, Reference>
680 && std::is_same_v<q20::remove_cvref_t<
decltype(rowReferenceIndex(std::declval<
const Other &>()))>,
682 && std::is_same_v<q20::remove_cvref_t<
decltype(std::declval<
const Other &>().get())>,
683 q20::remove_cvref_t<const_row_type>>,
bool>;
685 template <
typename Other, if_comparable_row_reference<Other> =
true>
686 friend bool operator==(
const Reference &lhs,
const Other &rhs)
688 return lhs.m_index == rowReferenceIndex(rhs) || rowsEqual(lhs, rhs);
690#if !defined(__cpp_impl_three_way_comparison)
691 template <
typename Other, if_comparable_row_reference<Other> =
true>
692 friend bool operator!=(
const Reference &lhs,
const Other &rhs)
694 return !(lhs == rhs);
699 friend bool comparesEqual(
const Reference &lhs,
const row_type &rhs)
701 return lhs.get() == rhs;
703 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(Reference, row_type)
705#ifndef QT_NO_DEBUG_STREAM
706 friend inline QDebug operator<<(QDebug dbg,
const Reference &ref)
708 QDebugStateSaver saver(dbg);
710 return dbg <<
"RowReference(" << ref.m_index <<
")";
713#ifndef QT_NO_DATASTREAM
714 friend inline QDataStream &operator<<(QDataStream &ds,
const Reference &ref)
716 return ds << ref.get();
721 struct ConstRowReference : RowReferenceBase<ConstRowReference,
const QRangeModelAdapter>
724 using Base = RowReferenceBase<ConstRowReference,
const QRangeModelAdapter>;
728 ConstRowReference() =
default;
729 ConstRowReference(
const ConstRowReference &) =
default;
730 ConstRowReference(ConstRowReference &&) =
default;
731 ConstRowReference &operator=(
const ConstRowReference &) =
default;
732 ConstRowReference &operator=(ConstRowReference &&) =
default;
733 ~ConstRowReference() =
default;
736 struct RowReference : RowReferenceBase<RowReference, QRangeModelAdapter>
739 using Base = RowReferenceBase<RowReference, QRangeModelAdapter>;
741 using iterator = ColumnIterator;
742 using const_iterator =
typename Base::const_iterator;
743 using size_type =
typename Base::size_type;
744 using difference_type =
typename Base::difference_type;
746 using row_type =
int;
748 using const_row_type =
typename Base::const_row_type;
751 RowReference() =
delete;
752 ~RowReference() =
default;
753 RowReference(
const RowReference &other) =
default;
754 RowReference(RowReference &&other) =
default;
757 RowReference &operator=(
const ConstRowReference &other)
763 RowReference &operator=(
const RowReference &other)
769 RowReference &operator=(
const row_type &other)
775 RowReference &operator=(row_type &&other)
777 assign(std::move(other));
781 operator ConstRowReference()
const
783 return ConstRowReference{
this->m_index,
this->m_adapter};
786 template <
typename ConstRowType = const_row_type,
787 std::enable_if_t<!std::is_same_v<ConstRowType,
const row_type &>,
bool> =
true>
788 RowReference &operator=(
const ConstRowType &other)
794 template <
typename T,
typename It,
typename Sentinel>
795 RowReference &operator=(
const QRangeModelDetails::RowView<T, It, Sentinel> &other)
797 *
this = row_type{other.begin(), other.end()};
803 friend void swap(RowReference lhs, RowReference rhs) =
delete;
805 template <
typename I = Impl, if_tree<I> =
true>
808 return QRangeModelAdapter(
this->m_adapter->storage.m_model,
this->m_index,
813 ColumnIterator begin()
815 return ColumnIterator{
this->m_index, 0,
this->m_adapter};
821 return ColumnIterator{
this->m_index,
this->m_adapter->columnCount(),
this->m_adapter};
827 Q_ASSERT(column >= 0 && column <
this->m_adapter->columnCount());
828 return *ColumnIterator{
this->m_index, column,
this->m_adapter};
831 using Base::operator[];
832 auto operator[](
int column)
838 template <
typename RHS>
839 void verifyRows(
const row_type &oldRow,
const RHS &newRow)
841 if constexpr (QRangeModelDetails::test_size<row_type>::value) {
843 Q_ASSERT_X(Impl::size(newRow) == Impl::size(oldRow),
844 "RowReference::operator=()",
845 "The new row has the wrong size!");
848 if constexpr (is_tree<Impl>) {
850 Q_ASSERT_X(QRangeModelDetails::isValid(newRow),
851 "RowReference::operator=()",
852 "An invalid row can not inserted into a tree!");
856 template <
typename R>
857 void assign(R &&other)
859 auto *impl =
this->m_adapter->storage.implementation();
860 decltype(
auto) oldRow = impl->rowData(
this->m_index);
862 verifyRows(oldRow, other);
864 if constexpr (is_tree<Impl>) {
865 auto &protocol = impl->protocol();
866 auto *oldParent = protocol.parentRow(QRangeModelDetails::refTo(oldRow));
870 if (
decltype(
auto) oldChildren = protocol.childRows(QRangeModelDetails::refTo(oldRow));
871 QRangeModelDetails::isValid(oldChildren)) {
872 if (
int oldChildCount =
this->m_adapter->model()->rowCount(
this->m_index)) {
873 impl->beginRemoveRows(
this->m_index, 0, oldChildCount - 1);
874 impl->deleteRemovedRows(QRangeModelDetails::refTo(oldChildren));
876 QRangeModelDetails::refTo(oldChildren) = range_type{};
877 impl->endRemoveRows();
881 if constexpr (protocol_traits::has_deleteRow)
882 protocol.deleteRow(oldRow);
883 oldRow = std::forward<R>(other);
884 if constexpr (protocol_traits::has_setParentRow) {
885 protocol.setParentRow(QRangeModelDetails::refTo(oldRow),
886 QRangeModelDetails::pointerTo(oldParent));
887 if (
decltype(
auto) newChildren = protocol.childRows(QRangeModelDetails::refTo(oldRow));
888 QRangeModelDetails::isValid(newChildren)) {
889 impl->beginInsertRows(
this->m_index, 0,
890 Impl::size(QRangeModelDetails::refTo(newChildren)) - 1);
891 impl->setParentRow(QRangeModelDetails::refTo(newChildren),
892 QRangeModelDetails::pointerTo(oldRow));
893 impl->endInsertRows();
897 oldRow = std::forward<R>(other);
899 this->m_adapter->emitDataChanged(
this->m_index,
900 this->m_index.siblingAtColumn(
this->m_adapter->columnCount() - 1));
901 if constexpr (Impl::itemsAreQObjects) {
902 if (
this->m_adapter->model()->autoConnectPolicy() == QRangeModel::AutoConnectPolicy::Full) {
903 impl->autoConnectPropertiesInRow(oldRow,
this->m_index.row(),
this->m_index.parent());
904 if constexpr (is_tree<Impl>)
905 impl->autoConnectProperties(
this->m_index);
911#ifndef QT_NO_DATASTREAM
912 friend inline QDataStream &operator>>(QDataStream &ds, RowReference &ref)
922 template <
typename Iterator,
typename Adapter>
923 struct RowIteratorBase : QRangeModelDetails::ParentIndex<is_tree<Impl>>
925 using iterator_category = std::random_access_iterator_tag;
926 using difference_type =
int;
928 RowIteratorBase() =
default;
929 RowIteratorBase(
const RowIteratorBase &) =
default;
930 RowIteratorBase(RowIteratorBase &&) =
default;
931 RowIteratorBase &operator=(
const RowIteratorBase &) =
default;
932 RowIteratorBase &operator=(RowIteratorBase &&) =
default;
934 RowIteratorBase(
int row,
const QModelIndex &parent, Adapter *adapter)
935 : QRangeModelDetails::ParentIndex<is_tree<Impl>>{parent}
936 , m_row(row), m_adapter(adapter)
939 void swap(RowIteratorBase &other)
noexcept
941 qSwap(m_row, other.m_row);
942 qSwap(
this->m_rootIndex, other.m_rootIndex);
943 q_ptr_swap(m_adapter, other.m_adapter);
946 friend Iterator &operator++(Iterator &that)
951 friend Iterator operator++(Iterator &that,
int)
957 friend Iterator operator+(
const Iterator &that, difference_type n)
959 return {that.m_row + n, that.root(), that.m_adapter};
961 friend Iterator operator+(difference_type n,
const Iterator &that)
965 friend Iterator &operator+=(Iterator &that, difference_type n)
971 friend Iterator &operator--(Iterator &that)
976 friend Iterator operator--(Iterator &that,
int)
982 friend Iterator operator-(
const Iterator &that, difference_type n)
984 return {that.m_row - n, that.root(), that.m_adapter};
986 friend Iterator operator-(difference_type n,
const Iterator &that)
990 friend Iterator &operator-=(Iterator &that, difference_type n)
996 friend difference_type operator-(
const Iterator &lhs,
const Iterator &rhs)
998 return lhs.m_row - rhs.m_row;
1002 friend class QRangeModelAdapter;
1003 ~RowIteratorBase() =
default;
1005 Adapter *m_adapter =
nullptr;
1008 friend bool comparesEqual(
const Iterator &lhs,
const Iterator &rhs)
noexcept
1010 return lhs.m_row == rhs.m_row && lhs.root() == rhs.root();
1012 friend Qt::strong_ordering compareThreeWay(
const Iterator &lhs,
const Iterator &rhs)
noexcept
1014 if (lhs.root() == rhs.root())
1015 return qCompareThreeWay(lhs.m_row, rhs.m_row);
1016 return qCompareThreeWay(lhs.root(), rhs.root());
1019 Q_DECLARE_STRONGLY_ORDERED(Iterator)
1021#ifndef QT_NO_DEBUG_STREAM
1022 friend inline QDebug operator<<(QDebug dbg,
const Iterator &it)
1024 QDebugStateSaver saver(dbg);
1026 return dbg <<
"RowIterator(" << it.m_row << it.root() <<
")";
1032 struct ConstRowIterator :
public RowIteratorBase<ConstRowIterator,
const QRangeModelAdapter>
1035 using Base = RowIteratorBase<ConstRowIterator,
const QRangeModelAdapter>;
1039 using difference_type =
typename Base::difference_type;
1040 using value_type = std::conditional_t<is_list<Impl>,
1043 using reference = std::conditional_t<is_list<Impl>,
1046 using pointer = std::conditional_t<is_list<Impl>,
1047 QRangeModelDetails::data_pointer_t<const_data_type>,
1050 ConstRowIterator(
const ConstRowIterator &other) =
default;
1051 ConstRowIterator(ConstRowIterator &&other) =
default;
1052 ConstRowIterator &operator=(
const ConstRowIterator &other) =
default;
1053 ConstRowIterator &operator=(ConstRowIterator &&other) =
default;
1054 ~ConstRowIterator() =
default;
1056 pointer operator->()
const
1058 return pointer{operator*()};
1061 reference operator*()
const
1063 if constexpr (is_list<Impl>) {
1064 return this->m_adapter->at(
this->m_row);
1066 const QModelIndex index =
this->m_adapter->model()->index(
this->m_row, 0,
1068 return ConstRowReference{index,
this->m_adapter};
1072 reference operator[](difference_type n)
const
1074 return *(*
this + n);
1078 struct RowIterator :
public RowIteratorBase<RowIterator, QRangeModelAdapter>
1081 using Base = RowIteratorBase<RowIterator, QRangeModelAdapter>;
1085 using difference_type =
typename Base::difference_type;
1090 using value_type = std::conditional_t<is_list<Impl>,
1092 std::remove_cv_t<row_type>>;
1093 using reference = std::conditional_t<is_list<Impl>,
1096 using pointer = std::conditional_t<is_list<Impl>,
1100 RowIterator(
const RowIterator &other) =
default;
1101 RowIterator(RowIterator &&other) =
default;
1102 RowIterator &operator=(
const RowIterator &other) =
default;
1103 RowIterator &operator=(RowIterator &&other) =
default;
1104 ~RowIterator() =
default;
1106 operator ConstRowIterator()
const
1108 return ConstRowIterator{
this->m_row,
this->root(),
this->m_adapter};
1111 pointer operator->()
const
1113 return pointer{operator*()};
1116 reference operator*()
const
1118 const QModelIndex index =
this->m_adapter->model()->index(
this->m_row, 0,
this->root());
1119 if constexpr (is_list<Impl>) {
1120 return reference{index};
1122 return reference{index,
this->m_adapter};
1126 reference operator[](difference_type n)
const
1128 return *(*
this + n);
1132 using const_iterator = ConstRowIterator;
1133 using iterator = RowIterator;
1134 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
1135 using reverse_iterator = std::reverse_iterator<iterator>;
1137 using value_type =
typename Range::value_type;
1138 using reference = RowReference;
1139 using const_reference = ConstRowReference;
1141 using value_type = std::remove_cv_t<row_type>;
1142 using reference =
typename iterator::reference;
1143 using const_reference =
typename const_iterator::reference;
1145 using size_type =
int;
1146 using difference_type =
int;
1148 template <
typename R,
1150 if_compatible_model_params<R, P> =
true>
1151 explicit QRangeModelAdapter(R &&range, P &&protocol)
1152 : QRangeModelAdapter(
new Model(QRangeModelDetails::forwardOrConvert<Range>(std::forward<R>(range)),
1153 QRangeModelDetails::forwardOrConvert<Protocol>(std::forward<P>(protocol))))
1156 template <
typename R,
1158 if_compatible_model_params<R, P> =
true,
1159 unless_adapter<R> =
true>
1160 explicit QRangeModelAdapter(R &&range)
1161 : QRangeModelAdapter(
new Model(QRangeModelDetails::forwardOrConvert<Range>(std::forward<R>(range))))
1166 Model *model()
const
1168 return storage.m_model.get();
1171 const range_type &range()
const
1173 return QRangeModelDetails::refTo(storage.implementation()->childRange(storage.root()));
1176 template <
typename NewRange = range_type, if_assignable_range<NewRange> =
true>
1177 void assign(NewRange &&newRange)
1179 assignImpl(qsizetype(Impl::size(QRangeModelDetails::refTo(newRange))),
1180 [&newRange](
auto &oldRange) {
1181 oldRange = std::forward<NewRange>(newRange);
1185 template <
typename NewRange = range_type, if_assignable_range<NewRange> =
true,
1186 unless_adapter<NewRange> =
true>
1187 QRangeModelAdapter &operator=(NewRange &&newRange)
1189 assign(std::forward<NewRange>(newRange));
1193 template <
typename Row, if_assignable_range<std::initializer_list<Row>> =
true>
1194 void assign(std::initializer_list<Row> newRange)
1196 assignImpl(newRange.size(), [&newRange](
auto &oldRange) {
1197 oldRange = newRange;
1201 template <
typename Row, if_assignable_range<std::initializer_list<Row>> =
true>
1202 QRangeModelAdapter &operator=(std::initializer_list<Row> newRange)
1208 template <
typename I = Impl, if_canAssignCopies<I> =
true>
1209 void assign(size_type count,
const value_type &value)
1212 assignImpl(size_t(count), [count, &value](
auto &oldRange) {
1213 if constexpr (QRangeModelDetails::has_assign_count<range_type>::value) {
1214 oldRange.assign(count, value);
1216 oldRange.erase(QRangeModelDetails::adl_begin(oldRange),
1217 QRangeModelDetails::adl_end(oldRange));
1218 oldRange.insert(QRangeModelDetails::adl_end(oldRange), count, value);
1223 template <
typename InputIterator,
typename Sentinel,
typename I = Impl, if_writable<I> =
true,
1224 if_compatible_row_iterator<InputIterator> =
true>
1225 void assign(InputIterator first, Sentinel last)
1227 assignImpl(size_t(std::distance(first, last)), [first, last](
auto &oldRange) {
1228 oldRange.assign(first, last);
1232 template <
typename I = Impl, if_canSort<I> =
true>
1233 void sort(
int column = 0, Qt::SortOrder order = Qt::AscendingOrder)
1235 storage->sort(column, order);
1239 ConstRowIterator cbegin()
const
1241 return ConstRowIterator{ 0, storage.root(),
this };
1243 ConstRowIterator begin()
const {
return cbegin(); }
1245 ConstRowIterator cend()
const
1247 return ConstRowIterator{ rowCount(), storage.root(),
this };
1249 ConstRowIterator end()
const {
return cend(); }
1251 template <
typename I = Impl, if_writable<I> =
true>
1254 return RowIterator{ 0, storage.root(),
this };
1257 template <
typename I = Impl, if_writable<I> =
true>
1260 return RowIterator{ rowCount(), storage.root(),
this };
1263 const_reverse_iterator crbegin()
const {
return const_reverse_iterator(cend()); }
1264 const_reverse_iterator rbegin()
const {
return crbegin(); }
1265 const_reverse_iterator crend()
const {
return const_reverse_iterator(cbegin()); }
1266 const_reverse_iterator rend()
const {
return crend(); }
1268 template <
typename I = Impl, if_writable<I> =
true>
1269 reverse_iterator rbegin() {
return reverse_iterator(end()); }
1270 template <
typename I = Impl, if_writable<I> =
true>
1271 reverse_iterator rend() {
return reverse_iterator(begin()); }
1278 bool isEmpty()
const
1280 return rowCount() == 0;
1288 decltype(
auto) front()
const
1294 template <
typename I = Impl, if_writable<I> =
true>
1301 decltype(
auto) back()
const
1304 return at(size() - 1);
1307 template <
typename I = Impl, if_writable<I> =
true>
1311 return at(size() - 1);
1314 template <
typename I = Impl, if_list<I> =
true>
1315 QModelIndex index(
int row)
const
1317 return storage->index(row, 0, storage.root());
1320 template <
typename I = Impl, unless_list<I> =
true>
1321 QModelIndex index(
int row,
int column)
const
1323 return storage->index(row, column, storage.root());
1326 template <
typename I = Impl, if_tree<I> =
true>
1327 QModelIndex index(QSpan<
const int> path,
int col)
const
1329 QModelIndex result = storage.root();
1330 auto count = path.size();
1331 for (
const int r : path) {
1333 result = storage->index(r, 0, result);
1335 result = storage->index(r, col, result);
1340 int columnCount()
const
1343 return storage->columnCount({});
1346 int rowCount()
const
1348 return storage->rowCount(storage.root());
1351 template <
typename I = Impl, if_tree<I> =
true>
1352 int rowCount(
int row)
const
1354 return rowCount(index(row, 0));
1357 template <
typename I = Impl, if_tree<I> =
true>
1358 int rowCount(QSpan<
const int> path)
const
1360 return rowCount(index(path, 0));
1363 template <
typename I = Impl, if_tree<I> =
true>
1364 int rowCount(
const QModelIndex &index)
const
1366 return storage->rowCount(index);
1369 template <
typename I = Impl, if_tree<I> =
true>
1370 constexpr bool hasChildren(
int row)
const
1372 return hasChildren(index(row, 0));
1375 template <
typename I = Impl, if_tree<I> =
true>
1376 constexpr bool hasChildren(QSpan<
const int> path)
const
1378 return hasChildren(index(path, 0));
1381 template <
typename I = Impl, if_tree<I> =
true>
1382 constexpr bool hasChildren(
const QModelIndex &index)
const
1384 return storage.m_model->hasChildren(index);
1387 template <
typename I = Impl, if_list<I> =
true>
1388 QVariant data(
int row)
const
1390 return data(index(row));
1393 template <
typename I = Impl, if_list<I> =
true>
1394 QVariant data(
int row,
int role)
const
1396 return data(index(row), role);
1399 template <
typename I = Impl, if_list<I> =
true, if_writable<I> =
true>
1400 bool setData(
int row,
const QVariant &value,
int role = Qt::EditRole)
1402 return setData(index(row), value, role);
1405 template <
typename I = Impl, unless_list<I> =
true>
1406 QVariant data(
int row,
int column)
const
1408 return data(index(row, column));
1411 template <
typename I = Impl, unless_list<I> =
true>
1412 QVariant data(
int row,
int column,
int role)
const
1414 return data(index(row, column), role);
1417 template <
typename I = Impl, unless_list<I> =
true, if_writable<I> =
true>
1418 bool setData(
int row,
int column,
const QVariant &value,
int role = Qt::EditRole)
1420 return setData(index(row, column), value, role);
1423 template <
typename I = Impl, if_tree<I> =
true>
1424 QVariant data(QSpan<
const int> path,
int column)
const
1426 return data(index(path, column));
1429 template <
typename I = Impl, if_tree<I> =
true>
1430 QVariant data(QSpan<
const int> path,
int column,
int role)
const
1432 return data(index(path, column), role);
1435 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1436 bool setData(QSpan<
const int> path,
int column,
const QVariant &value,
int role = Qt::EditRole)
1438 return setData(index(path, column), value, role);
1441 QVariant data(
const QModelIndex &index)
const
1443 return QRangeModelDetails::dataAtIndex<QVariant>(index);
1446 QVariant data(
const QModelIndex &index,
int role)
const
1448 return QRangeModelDetails::dataAtIndex<QVariant>(index, role);
1451 template <
typename I = Impl, if_writable<I> =
true>
1452 bool setData(
const QModelIndex &index,
const QVariant &value,
int role = Qt::EditRole)
1454 return storage->setData(index, value, role);
1459 template <
typename I= Impl, if_list<I> =
true>
1460 const_data_type at(
int row)
const
1462 return QRangeModelDetails::dataAtIndex<data_type>(index(row));
1464 template <
typename I = Impl, if_list<I> =
true>
1465 const_data_type operator[](
int row)
const {
return at(row); }
1467 template <
typename I= Impl, if_list<I> =
true, if_writable<I> =
true>
1469 const QModelIndex idx =
this->index(row);
1470 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::at",
"Index at row is invalid");
1471 return DataReference{idx};
1473 template <
typename I = Impl, if_list<I> =
true, if_writable<I> =
true>
1474 auto operator[](
int row) {
1475 const QModelIndex idx =
this->index(row);
1476 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::operator[]",
"Index at row is invalid");
1477 return DataReference{idx};
1481 template <
typename I = Impl, unless_list<I> =
true>
1482 decltype(
auto) at(
int row)
const
1484 return ConstRowReference{index(row, 0),
this}.get();
1486 template <
typename I = Impl, unless_list<I> =
true>
1487 decltype(
auto) operator[](
int row)
const {
return at(row); }
1489 template <
typename I = Impl, if_table<I> =
true, if_writable<I> =
true>
1492 return RowReference{index(row, 0),
this};
1494 template <
typename I = Impl, if_table<I> =
true, if_writable<I> =
true>
1495 auto operator[](
int row) {
return at(row); }
1498 template <
typename I = Impl, unless_list<I> =
true>
1499 const_data_type at(
int row,
int column)
const
1501 return QRangeModelDetails::dataAtIndex<data_type>(index(row, column));
1504#ifdef __cpp_multidimensional_subscript
1505 template <
typename I = Impl, unless_list<I> =
true>
1506 const_data_type operator[](
int row,
int column)
const {
return at(row, column); }
1509 template <
typename I = Impl, unless_list<I> =
true, if_writable<I> =
true>
1510 auto at(
int row,
int column)
1512 const QModelIndex idx =
this->index(row, column);
1513 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::at",
"Index at cell is invalid");
1514 return DataReference{idx};
1516#ifdef __cpp_multidimensional_subscript
1517 template <
typename I = Impl, unless_list<I> =
true, if_writable<I> =
true>
1518 auto operator[](
int row,
int column)
1520 const QModelIndex idx =
this->index(row, column);
1521 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::operator[]",
"Index at cell is invalid");
1522 return DataReference{idx};
1528 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1531 return RowReference{index(row, 0),
this};
1533 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1534 auto operator[](
int row) {
return at(row); }
1537 template <
typename I = Impl, if_tree<I> =
true>
1538 decltype(
auto) at(QSpan<
const int> path)
const
1540 return ConstRowReference{index(path, 0),
this}.get();
1542 template <
typename I = Impl, if_tree<I> =
true>
1543 decltype(
auto) operator[](QSpan<
const int> path)
const {
return at(path); }
1545 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1546 auto at(QSpan<
const int> path)
1548 return RowReference{index(path, 0),
this};
1550 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1551 auto operator[](QSpan<
const int> path) {
return at(path); }
1554 template <
typename I = Impl, if_tree<I> =
true>
1555 const_data_type at(QSpan<
const int> path,
int column)
const
1558 return QRangeModelDetails::dataAtIndex<data_type>(index(path, column));
1561#ifdef __cpp_multidimensional_subscript
1562 template <
typename I = Impl, if_tree<I> =
true>
1563 const_data_type operator[](QSpan<
const int> path,
int column)
const {
return at(path, column); }
1566 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1567 auto at(QSpan<
const int> path,
int column)
1570 const QModelIndex idx =
this->index(path, column);
1571 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::at",
"Index at path is invalid");
1572 return DataReference{idx};
1574#ifdef __cpp_multidimensional_subscript
1575 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1576 auto operator[](QSpan<
const int> path,
int column)
1578 const QModelIndex idx =
this->index(path, column);
1579 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::operator[]",
"Index at path is invalid");
1580 return DataReference{idx};
1584 const_data_type at(
const QModelIndex &index)
const
1586 Q_ASSERT_X(index.isValid(),
"QRangeModelAdapter::at",
"Index is invalid");
1587 return QRangeModelDetails::dataAtIndex<data_type>(index);
1590 template <
typename I = Impl, if_writable<I> =
true>
1591 auto at(
const QModelIndex &index)
1593 Q_ASSERT_X(index.isValid(),
"QRangeModelAdapter::at",
"Index is invalid");
1594 return DataReference{index};
1597 template <
typename I = Impl, if_canInsertRows<I> =
true>
1598 bool insertRow(
int before)
1600 return storage.m_model->insertRow(before);
1603 template <
typename I = Impl, if_canInsertRows<I> =
true, if_tree<I> =
true>
1604 bool insertRow(QSpan<
const int> before)
1606 Q_PRE(before.size());
1607 return insertRow(before.back(),
this->index(before.first(before.size() - 1), 0));
1610 template <
typename I = Impl, if_canInsertRows<I> =
true, if_tree<I> =
true>
1611 bool insertRow(
int before,
const QModelIndex &parent)
1613 return storage.m_model->insertRow(before, parent);
1616 template <
typename D = row_type,
typename I = Impl,
1617 if_canInsertRows<I> =
true, if_compatible_row<D> =
true>
1618 bool insertRow(
int before, D &&data)
1620 return insertRowImpl(before, storage.root(), std::forward<D>(data));
1623 template <
typename D = row_type,
typename I = Impl,
1624 if_canInsertRows<I> =
true, if_compatible_row<D> =
true, if_tree<I> =
true>
1625 bool insertRow(QSpan<
const int> before, D &&data)
1627 return insertRow(before.back(),
this->index(before.sliced(0, before.size() - 1), 0),
1628 std::forward<D>(data));
1631 template <
typename D = row_type,
typename I = Impl,
1632 if_canInsertRows<I> =
true, if_compatible_row<D> =
true, if_tree<I> =
true>
1633 bool insertRow(
int before,
const QModelIndex &parent, D &&data)
1635 return insertRowImpl(before, parent, std::forward<D>(data));
1638 template <
typename C,
typename I = Impl,
1639 if_canInsertRows<I> =
true, if_compatible_row_range<C> =
true>
1640 bool insertRows(
int before, C &&data)
1642 return insertRowsImpl(before, storage.root(), std::forward<C>(data));
1645 template <
typename C,
typename I = Impl,
1646 if_canInsertRows<I> =
true, if_compatible_row_range<C> =
true, if_tree<I> =
true>
1647 bool insertRows(QSpan<
const int> before, C &&data)
1649 return insertRows(before.back(),
this->index(before.sliced(0, before.size() - 1), 0),
1650 std::forward<C>(data));
1653 template <
typename C,
typename I = Impl,
1654 if_canInsertRows<I> =
true, if_compatible_row_range<C> =
true, if_tree<I> =
true>
1655 bool insertRows(
int before,
const QModelIndex &parent, C &&data)
1657 return insertRowsImpl(before, parent, std::forward<C>(data));
1660 template <
typename I = Impl, if_canInsertRows<I> =
true>
1661 iterator insert(const_iterator pos,
const value_type &value)
1663 return insertAt(pos, 1, [
this, &value](
int row) {
1664 return insertRowImpl(row, storage.root(), value);
1668 template <
typename I = Impl, if_canInsertRows<I> =
true>
1669 iterator insert(const_iterator pos, value_type &&value)
1671 return insertAt(pos, 1, [
this, &value](
int row) {
1672 return insertRowImpl(row, storage.root(), std::move(value));
1676 template <
typename I = Impl, if_canInsertCopies<I> =
true>
1677 iterator insert(const_iterator pos, size_type count,
const value_type &value)
1679 return insertAt(pos, count, [
this, count, &value](
int row) {
1680 return insertCopiesImpl(row, count, storage.root(), value);
1684 template <
typename InputIterator,
typename I = Impl, if_canInsertRows<I> =
true,
1685 if_compatible_row_iterator<InputIterator> =
true>
1686 iterator insert(const_iterator pos, InputIterator first, InputIterator last)
1688 const QRangeModelDetails::IteratorRange<InputIterator> range{first, last};
1689 return insertAt(pos, q26::saturating_cast<
int>(range.size()), [
this, &range](
int row) {
1690 return insertRowsImpl(row, storage.root(), range);
1694 template <
typename I = Impl, if_canInsertRows<I> =
true>
1695 iterator insert(const_iterator pos, std::initializer_list<value_type> values)
1697 return insertAt(pos,
int(values.size()), [
this, &values](
int row) {
1698 return insertRowsImpl(row, storage.root(), values);
1702 template <
typename ...Args,
typename I = Impl, if_canInsertRows<I> =
true,
1703 if_emplaceable<Args...> =
true>
1704 iterator emplace(const_iterator pos, Args &&...args)
1706 return insertAt(pos, 1, [
this, &args...](
int row) {
1707 if constexpr (
sizeof...(Args) == 0)
1708 return storage->insertRows(row, 1, storage.root());
1710 return insertRowImpl(row, storage.root(), std::forward<Args>(args)...);
1714 template <
typename I = Impl, if_canInsertRows<I> =
true>
1715 void push_back(
const value_type &value)
1717 insert(cend(), value);
1720 template <
typename I = Impl, if_canInsertRows<I> =
true>
1721 void push_back(value_type &&value)
1723 insert(cend(), std::move(value));
1726 template <
typename I = Impl, if_canInsertRows<I> =
true>
1727 void push_front(
const value_type &value)
1729 insert(cbegin(), value);
1732 template <
typename I = Impl, if_canInsertRows<I> =
true>
1733 void push_front(value_type &&value)
1735 insert(cbegin(), std::move(value));
1738 template <
typename ...Args,
typename I = Impl, if_canInsertRows<I> =
true,
1739 if_emplaceable<Args...> =
true>
1740 reference emplace_back(Args &&...args)
1742 return *emplace(cend(), std::forward<Args>(args)...);
1745 template <
typename ...Args,
typename I = Impl, if_canInsertRows<I> =
true,
1746 if_emplaceable<Args...> =
true>
1747 reference emplace_front(Args &&...args)
1749 return *emplace(cbegin(), std::forward<Args>(args)...);
1752 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1753 iterator erase(const_iterator pos)
1755 Q_PRE(pos != cend());
1756 return erase(pos, std::next(pos));
1759 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1760 iterator erase(const_iterator first, const_iterator last)
1762 const int row = rowOf(first);
1763 const int count = rowOf(last) - row;
1765 if (count > 0 && !storage->removeRows(row, count, storage.root()))
1767 return iterator{row, storage.root(),
this};
1770 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1777 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1784 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1787 erase(cbegin(), cend());
1790 template <
typename I = Impl, if_canInsertRows<I> =
true, if_canRemoveRows<I> =
true>
1791 void resize(size_type newSize)
1793 Q_PRE(newSize >= 0);
1794 const int oldSize = size();
1795 if (newSize < oldSize)
1796 erase(cbegin() + newSize, cend());
1797 else if (newSize > oldSize)
1798 storage->insertRows(oldSize, newSize - oldSize, storage.root());
1801 template <
typename I = Impl, if_canInsertCopies<I> =
true, if_canRemoveRows<I> =
true>
1802 void resize(size_type newSize,
const value_type &value)
1804 Q_PRE(newSize >= 0);
1805 const int oldSize = size();
1806 if (newSize < oldSize)
1807 erase(cbegin() + newSize, cend());
1808 else if (newSize > oldSize)
1809 insert(cend(), newSize - oldSize, value);
1812 template <
typename Predicate,
typename I = Impl, if_canRemoveRows<I> =
true>
1813 size_type removeIf(Predicate pred)
1821 std::vector<
bool> matches;
1822 matches.reserve(size_t(size()));
1823 const range_type &rows = range();
1824 for (
auto it = QRangeModelDetails::adl_begin(rows);
1825 it != QRangeModelDetails::adl_end(rows); ++it) {
1826 matches.push_back(
bool(pred(*it)));
1832 size_type removed = 0;
1833 int last =
int(matches.size());
1835 while (last > 0 && !matches[last - 1])
1838 while (first > 0 && matches[first - 1])
1841 if (!storage->removeRows(first, last - first, storage.root()))
1843 removed += last - first;
1850 template <
typename AT = value_type,
typename I = Impl, if_canRemoveRows<I> =
true>
1851 size_type removeAll(
const AT &value)
1853 return removeIf([&value](
const auto &row) {
return row == value; });
1856 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1857 bool removeRow(
int row)
1859 return removeRows(row, 1);
1862 template <
typename I = Impl, if_canRemoveRows<I> =
true, if_tree<I> =
true>
1863 bool removeRow(QSpan<
const int> path)
1865 return removeRows(path, 1);
1868 template <
typename I = Impl, if_canRemoveRows<I> =
true, if_tree<I> =
true>
1869 bool removeRow(
int row,
const QModelIndex &parent)
1871 return removeRows(row, 1, parent);
1874 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1875 bool removeRows(
int row,
int count)
1877 return storage->removeRows(row, count, storage.root());
1880 template <
typename I = Impl, if_canRemoveRows<I> =
true, if_tree<I> =
true>
1881 bool removeRows(QSpan<
const int> path,
int count)
1883 return removeRows(path.back(), count,
this->index(path.first(path.size() - 1), 0));
1886 template <
typename I = Impl, if_canRemoveRows<I> =
true, if_tree<I> =
true>
1887 bool removeRows(
int row,
int count,
const QModelIndex &parent)
1889 return storage->removeRows(row, count, parent);
1892 template <
typename F = range_features, if_canMoveItems<F> =
true>
1893 bool moveRow(
int source,
int destination)
1895 return moveRows(source, 1, destination);
1898 template <
typename F = range_features, if_canMoveItems<F> =
true>
1899 bool moveRows(
int source,
int count,
int destination)
1901 return storage->moveRows(storage.root(), source, count, storage.root(), destination);
1904 template <
typename I = Impl,
typename F = range_features,
1905 if_canMoveItems<F> =
true, if_tree<I> =
true>
1906 bool moveRow(QSpan<
const int> source, QSpan<
const int> destination)
1908 return moveRows(source, 1, destination);
1911 template <
typename I = Impl,
typename F = range_features,
1912 if_canMoveItems<F> =
true, if_tree<I> =
true>
1913 bool moveRow(
const QModelIndex &sourceParent,
int sourceRow,
1914 const QModelIndex &destinationParent,
int destinationChild)
1916 return moveRows(sourceParent, sourceRow, 1, destinationParent, destinationChild);
1919 template <
typename I = Impl,
typename F = range_features,
1920 if_canMoveItems<F> =
true, if_tree<I> =
true>
1921 bool moveRows(QSpan<
const int> source,
int count, QSpan<
const int> destination)
1923 return moveRows(
this->index(source.first(source.size() - 1), 0),
1926 this->index(destination.first(destination.size() - 1), 0),
1927 destination.back());
1930 template <
typename I = Impl,
typename F = range_features,
1931 if_canMoveItems<F> =
true, if_tree<I> =
true>
1932 bool moveRows(
const QModelIndex &sourceParent,
int sourceRow,
int count,
1933 const QModelIndex &destinationParent,
int destinationChild)
1935 return storage->moveRows(sourceParent, sourceRow, count,
1936 destinationParent, destinationChild);
1939 template <
typename I = Impl, if_canInsertColumns<I> =
true>
1940 bool insertColumn(
int before)
1942 return storage.m_model->insertColumn(before);
1945 template <
typename D,
typename I = Impl,
1946 if_canInsertColumns<I> =
true, if_compatible_column_data<D> =
true>
1947 bool insertColumn(
int before, D &&data)
1949 return insertColumnImpl(before, storage.root(), std::forward<D>(data));
1952 template <
typename C,
typename I = Impl,
1953 if_canInsertColumns<I> =
true, if_compatible_column_range<C> =
true>
1954 bool insertColumns(
int before, C &&data)
1956 return insertColumnsImpl(before, storage.root(), std::forward<C>(data));
1959 template <
typename I = Impl, if_canRemoveColumns<I> =
true>
1960 bool removeColumn(
int column)
1962 return storage.m_model->removeColumn(column);
1965 template <
typename I = Impl, if_canRemoveColumns<I> =
true>
1966 bool removeColumns(
int column,
int count)
1968 return storage->removeColumns(column, count, {});
1971 template <
typename F = row_features, if_canMoveItems<F> =
true>
1972 bool moveColumn(
int from,
int to)
1974 return moveColumns(from, 1, to);
1977 template <
typename F = row_features, if_canMoveItems<F> =
true>
1978 bool moveColumns(
int from,
int count,
int to)
1980 return storage->moveColumns(storage.root(), from, count, storage.root(), to);
1983 template <
typename I = Impl,
typename F = row_features,
1984 if_canMoveItems<F> =
true, if_tree<I> =
true>
1985 bool moveColumn(QSpan<
const int> source,
int to)
1987 const QModelIndex parent =
this->index(source.first(source.size() - 1), 0);
1988 return storage->moveColumns(parent, source.back(), 1, parent, to);
1991 template <
typename I = Impl,
typename F = row_features,
1992 if_canMoveItems<F> =
true, if_tree<I> =
true>
1993 bool moveColumns(QSpan<
const int> source,
int count,
int destination)
1995 const QModelIndex parent =
this->index(source.first(source.size() - 1), 0);
1996 return storage->moveColumns(parent, source.back(), count, parent, destination);
2000 template <
typename AT = value_type,
typename I = Impl, if_canRemoveRows<I> =
true>
2001 friend size_type erase(QRangeModelAdapter &adapter,
const AT &value)
2003 return adapter.removeAll(value);
2006 template <
typename Predicate,
typename I = Impl, if_canRemoveRows<I> =
true>
2007 friend size_type erase_if(QRangeModelAdapter &adapter, Predicate pred)
2009 return adapter.removeIf(std::move(pred));
2013 bool comparesEqual(
const QRangeModelAdapter &lhs,
const QRangeModelAdapter &rhs)
noexcept
2015 return lhs.storage.m_model == rhs.storage.m_model;
2017 Q_DECLARE_EQUALITY_COMPARABLE(QRangeModelAdapter)
2020 bool comparesEqual(
const QRangeModelAdapter &lhs,
const range_type &rhs)
2022 return lhs.range() == rhs;
2024 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QRangeModelAdapter, range_type)
2027 void emitDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight)
2029 Q_EMIT storage.implementation()->dataChanged(topLeft, bottomRight, {});
2032 void beginAssignImpl(Impl *impl, range_type *oldRange,
int newLastRow)
2034 const QModelIndex root = storage.root();
2035 const qsizetype oldLastRow = qsizetype(Impl::size(oldRange)) - 1;
2037 if (!root.isValid()) {
2038 impl->beginResetModel();
2039 impl->deleteOwnedRows();
2040 }
else if constexpr (is_tree<Impl>) {
2041 if (oldLastRow >= 0) {
2042 impl->beginRemoveRows(root, 0, model()->rowCount(root) - 1);
2043 impl->deleteRemovedRows(QRangeModelDetails::refTo(oldRange));
2044 impl->endRemoveRows();
2046 if (newLastRow >= 0)
2047 impl->beginInsertRows(root, 0, newLastRow);
2049 Q_ASSERT_X(
false,
"QRangeModelAdapter::assign",
2050 "Internal error: The root index in a table or list must be invalid.");
2054 void endAssignImpl(Impl *impl,
int newLastRow)
2056 const QModelIndex root = storage.root();
2057 if (!root.isValid()) {
2058 if constexpr (is_tree<Impl> && protocol_traits::has_setParentRow) {
2061 impl->setParentRow(QRangeModelDetails::refTo(impl->childRange(root)),
nullptr);
2063 impl->endResetModel();
2064 }
else if constexpr (is_tree<Impl>) {
2065 if (newLastRow >= 0) {
2066 Q_ASSERT(model()->hasChildren(root));
2069 impl->setParentRow(QRangeModelDetails::refTo(impl->childRange(root)),
2070 QRangeModelDetails::pointerTo(impl->rowData(root)));
2071 impl->endInsertRows();
2076 template <
typename Assigner>
2077 void assignImpl(std::size_t newSize, Assigner &&assigner)
2079 const auto sz = q26::saturating_cast<
int>(newSize);
2080 Q_ASSERT_X(q20::cmp_equal(sz, newSize),
2081 "QRangeModelAdapter::assign",
"New range is too large");
2082 const int newLastRow = sz - 1;
2084 auto *impl = storage.implementation();
2085 auto *oldRange = impl->childRange(storage.root());
2086 beginAssignImpl(impl, oldRange, newLastRow);
2087 assigner(QRangeModelDetails::refTo(oldRange));
2088 endAssignImpl(impl, newLastRow);
2090 if constexpr (Impl::itemsAreQObjects) {
2091 if (model()->autoConnectPolicy() == QRangeModel::AutoConnectPolicy::Full) {
2092 const auto begin = QRangeModelDetails::begin(QRangeModelDetails::refTo(oldRange));
2093 const auto end = QRangeModelDetails::end(QRangeModelDetails::refTo(oldRange));
2095 for (
auto it = begin; it != end; ++it, ++rowIndex)
2096 impl->autoConnectPropertiesInRow(*it, rowIndex, storage.root());
2102 template <
typename P,
typename Row>
2103 static auto setParentRow(P &protocol, Row &newRow, row_ptr parentRow)
2104 ->
decltype(protocol.setParentRow(newRow, parentRow))
2106 return protocol.setParentRow(newRow, parentRow);
2108 template <
typename ...Args>
static constexpr void setParentRow(Args &&...)
2110 static_assert(!protocol_traits::has_setParentRow,
2111 "Internal error, wrong setParentRow overload called");
2116 int rowOf(
const const_iterator &it)
const
2118 Q_PRE(it.m_adapter && it.m_adapter->model() == model());
2119 Q_PRE(it.root() == storage.root());
2120 Q_PRE(it.m_row >= 0 && it.m_row <= rowCount());
2124 template <
typename Inserter>
2125 iterator insertAt(const_iterator pos,
int count, Inserter &&inserter)
2127 const int row = rowOf(pos);
2128 if (count > 0 && !std::forward<Inserter>(inserter)(row))
2130 return iterator{row, storage.root(),
this};
2133 bool insertCopiesImpl(
int before,
int count,
const QModelIndex &parent,
2134 const row_type &value)
2136 return storage->doInsertRows(before, count, parent, [&value,
this]
2137 (range_type &range,
auto *parentRow,
int row,
int n) {
2138 Q_UNUSED(parentRow);
2140 const auto oldSize = range.size();
2141 auto newRow = range.insert(QRangeModelDetails::pos(range, row), n, value);
2142 if constexpr (protocol_traits::has_setParentRow) {
2143 for (
int i = 0; i < n; ++i, ++newRow) {
2144 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
2145 QRangeModelDetails::pointerTo(parentRow));
2150 return range.size() == oldSize + n;
2154 template <
typename ...Args>
2155 bool insertRowImpl(
int before,
const QModelIndex &parent, Args &&...args)
2157 return storage.implementation()->doInsertRows(before, 1, parent, [&args...,
this]
2158 (range_type &range,
auto *parentRow,
int row,
int count) {
2160 const auto oldSize = range.size();
2161 auto newRow = range.emplace(QRangeModelDetails::pos(range, row),
2162 std::forward<Args>(args)...);
2163 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
2164 QRangeModelDetails::pointerTo(parentRow));
2165 return range.size() == oldSize + count;
2169 template <
typename LHS>
2170 static auto selfInsertion(LHS *lhs, LHS *rhs) ->
decltype(lhs == rhs)
2174 qCritical(
"Inserting data into itself is not supported");
2180 template <
typename LHS,
typename RHS>
2181 static constexpr bool selfInsertion(LHS *, RHS *) {
return false; }
2183 template <
typename C>
2184 bool insertRowsImpl(
int before,
const QModelIndex &parent, C &&data)
2186 bool result =
false;
2187 result = storage->doInsertRows(before,
int(std::size(data)), parent, [&data,
this]
2188 (range_type &range,
auto *parentRow,
int row,
int count){
2189 Q_UNUSED(parentRow);
2191 const auto pos = QRangeModelDetails::pos(range, row);
2192 const auto oldSize = range.size();
2194 auto dataRange = [&data]{
2195 if constexpr (std::is_rvalue_reference_v<C&&>) {
2196 return std::make_pair(
2197 std::move_iterator(std::begin(data)),
2198 std::move_iterator(std::end(data))
2201 return std::make_pair(std::begin(data), std::end(data));
2205 if constexpr (range_features::has_insert_range) {
2206 if (selfInsertion(&range, &data))
2208 auto start = range.insert(pos, dataRange.first, dataRange.second);
2209 if constexpr (protocol_traits::has_setParentRow) {
2211 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*start),
2212 QRangeModelDetails::pointerTo(parentRow));
2220 auto newRow = range.insert(pos, count, row_type{});
2221 while (dataRange.first != dataRange.second) {
2222 *newRow = *dataRange.first;
2223 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
2224 QRangeModelDetails::pointerTo(parentRow));
2229 return range.size() == oldSize + count;
2234 template <
typename D,
typename =
void>
2235 struct DataFromList {
2236 static constexpr auto first(D &data) {
return &data; }
2237 static constexpr auto next(D &, D *entry) {
return entry; }
2240 template <
typename D>
2241 struct DataFromList<D, std::enable_if_t<QRangeModelDetails::range_traits<D>::value>>
2243 static constexpr auto first(D &data) {
return std::begin(data); }
2244 static constexpr auto next(D &data,
typename D::iterator entry)
2247 if (entry == std::end(data))
2248 entry = first(data);
2253 template <
typename D,
typename =
void>
struct RowFromTable
2255 static constexpr auto first(D &data) {
return &data; }
2256 static constexpr auto next(D &, D *entry) {
return entry; }
2259 template <
typename D>
2260 struct RowFromTable<D, std::enable_if_t<std::conjunction_v<
2261 QRangeModelDetails::range_traits<D>,
2262 QRangeModelDetails::range_traits<
typename D::value_type>
2267 template <
typename D>
2268 bool insertColumnImpl(
int before,
const QModelIndex &parent, D data)
2270 auto entry = DataFromList<D>::first(data);
2272 return storage->doInsertColumns(before, 1, parent, [&entry, &data]
2273 (
auto &range,
auto pos,
int count) {
2274 const auto oldSize = range.size();
2275 range.insert(pos, *entry);
2276 entry = DataFromList<D>::next(data, entry);
2277 return range.size() == oldSize + count;
2281 template <
typename C>
2282 bool insertColumnsImpl(
int before,
const QModelIndex &parent, C data)
2284 bool result =
false;
2285 auto entries = RowFromTable<C>::first(data);
2286 auto begin = std::begin(*entries);
2287 auto end = std::end(*entries);
2288 result = storage->doInsertColumns(before,
int(std::size(*entries)), parent,
2289 [&begin, &end, &entries, &data](
auto &range,
auto pos,
int count) {
2290 const auto oldSize = range.size();
2291 if constexpr (row_features::has_insert_range) {
2292 range.insert(pos, begin, end);
2294 auto start = range.insert(pos, count, {});
2295 std::copy(begin, end, start);
2297 entries = RowFromTable<C>::next(data, entries);
2298 begin = std::begin(*entries);
2299 end = std::end(*entries);
2300 return range.size() == oldSize + count;