15class QT_TECH_PREVIEW_API QRangeModelAdapter
17 using Impl = QRangeModelDetails::RangeImplementation<Range, Protocol>;
18 using Storage = QRangeModelDetails::AdapterStorage<Model, Impl>;
23 using range_type = Range;
25 using range_type = QRangeModelDetails::wrapped_t<Range>;
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;
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()
44 && row_features::has_insert;
45 template <
typename I>
static constexpr bool canRemoveColumns = I::dynamicColumns()
47 && row_features::has_erase;
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>;
58 using if_canInsertRows = std::enable_if_t<I::canInsertRows(),
bool>;
60 using if_canRemoveRows = std::enable_if_t<I::canRemoveRows(),
bool>;
62 using if_canMoveItems = std::enable_if_t<F::has_rotate || F::has_splice,
bool>;
65 using if_canInsertColumns = std::enable_if_t<canInsertColumns<I>,
bool>;
67 using if_canRemoveColumns = std::enable_if_t<canRemoveColumns<I>,
bool>;
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>;
75 static constexpr bool is_compatible_row_range = is_compatible_row<
76 decltype(*std::begin(std::declval<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>;
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&>()))
93 using if_compatible_column_data = std::enable_if_t<is_compatible_data<C>
94 || is_compatible_data_range<C>,
bool>;
96 using if_compatible_column_range = std::enable_if_t<is_compatible_data_range<C>,
bool>;
99 using if_assignable_range = std::enable_if_t<std::is_assignable_v<range_type, R>,
bool>;
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>;
108 template <
typename R,
typename P>
109 using if_compatible_model_params =
113 std::is_convertible<R &&, Range>,
114 std::is_convertible<R &&, Range &&>
116 std::is_convertible<P, Protocol>
119#if !defined(Q_OS_VXWORKS) && !defined(Q_OS_INTEGRITY)
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...> &);
130 template <
typename T>
131 using asMutable_t =
decltype(asMutable(std::declval<T>()));
132 friend class QRangeModelAdapter<asMutable_t<Range>, Protocol, Model>;
134 template <
typename R,
typename P,
typename M>
135 friend class QRangeModelAdapter;
138 explicit QRangeModelAdapter(
const std::shared_ptr<QRangeModel> &model,
const QModelIndex &root,
140 : storage{model, root}
143 explicit QRangeModelAdapter(QRangeModel *model)
150 using value_type = data_type;
151 using const_value_type = const_data_type;
152 using pointer = QRangeModelDetails::data_pointer_t<const_value_type>;
154 explicit DataReference(
const QModelIndex &index)
noexcept
158 DataReference(
const DataReference &other) =
default;
159 DataReference(DataReference &&other) =
default;
162 DataReference &operator=(
const DataReference &other)
168 DataReference &operator=(DataReference &&other)
174 ~DataReference() =
default;
176 DataReference &operator=(
const value_type &value)
182 DataReference &operator=(value_type &&value)
184 assign(std::move(value));
188 const_value_type get()
const
190 Q_ASSERT_X(m_index.isValid(),
"QRangeModelAdapter::at",
"Index at position is invalid");
191 return QRangeModelDetails::dataAtIndex<q20::remove_cvref_t<value_type>>(m_index);
194 operator const_value_type()
const
199 pointer operator->()
const
204 bool isValid()
const {
return m_index.isValid(); }
209 template <
typename Value>
210 void assign(Value &&value)
212 constexpr Qt::ItemDataRole dataRole = Qt::RangeModelAdapterRole;
214 if (m_index.isValid()) {
215 auto model =
const_cast<QAbstractItemModel *>(m_index.model());
216 [[maybe_unused]]
bool couldWrite =
false;
217 if constexpr (std::is_same_v<q20::remove_cvref_t<Value>, QVariant>) {
218 couldWrite = model->setData(m_index, value, dataRole);
220 couldWrite = model->setData(m_index,
221 QVariant::fromValue(std::forward<Value>(value)),
226 qWarning() <<
"Writing value of type"
227 << QMetaType::fromType<q20::remove_cvref_t<Value>>().name()
228 <<
"to role" << dataRole <<
"at index" << m_index <<
"failed";
231 qCritical(
"Data reference for invalid index, can't write to model");
236 friend inline bool comparesEqual(
const DataReference &lhs,
const DataReference &rhs)
238 return lhs.m_index == rhs.m_index
239 || lhs.get() == rhs.get();
241 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(DataReference);
243 friend inline bool comparesEqual(
const DataReference &lhs,
const value_type &rhs)
245 return lhs.get() == rhs;
247 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(DataReference, value_type);
249 friend inline void swap(DataReference lhs, DataReference rhs)
251 const value_type lhsValue = lhs.get();
256#ifndef QT_NO_DEBUG_STREAM
257 friend inline QDebug operator<<(QDebug dbg,
const DataReference &ref)
259 return dbg << ref.get();
262#ifndef QT_NO_DATASTREAM
263 friend inline QDataStream &operator<<(QDataStream &ds,
const DataReference &ref)
265 return ds << ref.get();
267 friend inline QDataStream &operator>>(QDataStream &ds, DataReference &ref)
271 ref = std::move(value);
276 template <
typename Iterator,
typename Adapter>
277 struct ColumnIteratorBase
279 using iterator_category = std::random_access_iterator_tag;
280 using difference_type =
int;
282 ColumnIteratorBase() =
default;
283 ColumnIteratorBase(
const ColumnIteratorBase &other) =
default;
284 ColumnIteratorBase(ColumnIteratorBase &&other) =
default;
285 ColumnIteratorBase &operator=(
const ColumnIteratorBase &other) =
default;
286 ColumnIteratorBase &operator=(ColumnIteratorBase &&other) =
default;
288 ColumnIteratorBase(
const QModelIndex &rowIndex,
int column, Adapter *adapter)
noexcept
289 : m_rowIndex(rowIndex), m_column(column), m_adapter(adapter)
293 void swap(ColumnIteratorBase &other)
noexcept
295 std::swap(m_rowIndex, other.m_rowIndex);
296 std::swap(m_column, other.m_column);
297 q_ptr_swap(m_adapter, other.m_adapter);
300 friend Iterator &operator++(Iterator &that)
305 friend Iterator operator++(Iterator &that,
int)
311 friend Iterator operator+(
const Iterator &that, difference_type n)
313 return {that.m_rowIndex, that.m_column + n, that.m_adapter};
315 friend Iterator operator+(difference_type n,
const Iterator &that)
319 friend Iterator &operator+=(Iterator &that, difference_type n)
325 friend Iterator &operator--(Iterator &that)
330 friend Iterator operator--(Iterator &that,
int)
336 friend Iterator operator-(
const Iterator &that, difference_type n)
338 return {that.m_rowIndex, that.m_column - n, that.m_adapter};
340 friend Iterator operator-(difference_type n,
const Iterator &that)
344 friend Iterator &operator-=(Iterator &that, difference_type n)
350 friend difference_type operator-(
const Iterator &lhs,
const Iterator &rhs)
352 Q_PRE(lhs.m_rowIndex == rhs.m_rowIndex);
353 Q_PRE(lhs.m_adapter == rhs.m_adapter);
354 return lhs.m_column - rhs.m_column;
358 ~ColumnIteratorBase() =
default;
359 QModelIndex m_rowIndex;
361 Adapter *m_adapter =
nullptr;
364 friend bool comparesEqual(
const Iterator &lhs,
const Iterator &rhs)
366 Q_ASSERT(lhs.m_rowIndex == rhs.m_rowIndex);
367 return lhs.m_column == rhs.m_column;
369 friend Qt::strong_ordering compareThreeWay(
const Iterator &lhs,
const Iterator &rhs)
371 Q_ASSERT(lhs.m_rowIndex == rhs.m_rowIndex);
372 return qCompareThreeWay(lhs.m_column, rhs.m_column);
375 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Iterator)
377#ifndef QT_NO_DEBUG_STREAM
378 friend inline QDebug operator<<(QDebug dbg,
const Iterator &it)
380 QDebugStateSaver saver(dbg);
382 return dbg <<
"ColumnIterator(" << it.m_rowIndex.siblingAtColumn(it.m_column) <<
")";
387 struct ConstColumnIterator : ColumnIteratorBase<ConstColumnIterator,
const QRangeModelAdapter>
390 using Base = ColumnIteratorBase<ConstColumnIterator,
const QRangeModelAdapter>;
392 using difference_type =
typename Base::difference_type;
393 using value_type = data_type;
394 using reference = const_data_type;
395 using pointer = QRangeModelDetails::data_pointer_t<value_type>;
398 using Base::operator=;
399 ~ConstColumnIterator() =
default;
401 pointer operator->()
const
403 return pointer{operator*()};
406 reference operator*()
const
408 return std::as_const(
this->m_adapter)->at(
this->m_rowIndex.row(),
this->m_column);
411 reference operator[](difference_type n)
const
417 struct ColumnIterator : ColumnIteratorBase<ColumnIterator, QRangeModelAdapter>
420 using Base = ColumnIteratorBase<ColumnIterator, QRangeModelAdapter>;
422 using difference_type =
typename Base::difference_type;
423 using value_type = DataReference;
424 using reference = DataReference;
425 using pointer = reference;
428 using Base::operator=;
429 ~ColumnIterator() =
default;
431 operator ConstColumnIterator()
const
433 return ConstColumnIterator{
this->m_rowIndex,
this->m_column,
this->m_adapter};
436 pointer operator->()
const
441 reference operator*()
const
443 return reference{
this->m_rowIndex.siblingAtColumn(
this->m_column)};
446 reference operator[](difference_type n)
const
452 template <
typename Reference,
typename const_row_type,
typename =
void>
455 const_row_type get()
const
457 const Reference *that =
static_cast<
const Reference *>(
this);
458 const auto *impl = that->m_adapter->storage.implementation();
459 auto *childRange = impl->childRange(that->m_index.parent());
460 if constexpr (std::is_convertible_v<
const row_type &, const_row_type>) {
461 return *std::next(QRangeModelDetails::adl_begin(childRange), that->m_index.row());
463 const auto &row = *std::next(QRangeModelDetails::adl_begin(childRange),
464 that->m_index.row());
465 return const_row_type{QRangeModelDetails::adl_begin(row),
466 QRangeModelDetails::adl_end(row)};
470 const_row_type operator->()
const
475 operator const_row_type()
const
481 template <
typename Reference,
typename const_row_type>
482 struct RowGetter<Reference, const_row_type,
483 std::enable_if_t<std::is_reference_v<const_row_type>>>
485 const_row_type get()
const
487 const Reference *that =
static_cast<
const Reference *>(
this);
488 const auto *impl = that->m_adapter->storage.implementation();
489 return *std::next(QRangeModelDetails::begin(
490 QRangeModelDetails::refTo(impl->childRange(that->m_index.parent()))),
491 that->m_index.row());
494 auto operator->()
const
496 return std::addressof(get());
499 operator const_row_type()
const
505 template <
typename Reference,
typename const_row_type>
506 struct RowGetter<Reference, const_row_type,
507 std::enable_if_t<std::is_pointer_v<const_row_type>>>
509 const_row_type get()
const
511 const Reference *that =
static_cast<
const Reference *>(
this);
512 const auto *impl = that->m_adapter->storage.implementation();
513 return *std::next(QRangeModelDetails::begin(
514 QRangeModelDetails::refTo(impl->childRange(that->m_index.parent()))),
515 that->m_index.row());
518 const_row_type operator->()
const
523 operator const_row_type()
const
529 template <
typename Reference,
typename Adapter>
530 struct RowReferenceBase : RowGetter<Reference, QRangeModelDetails::asConstRow_t<row_type>>
532 using const_iterator = ConstColumnIterator;
533 using size_type =
int;
534 using difference_type =
int;
535 using const_row_type = QRangeModelDetails::asConstRow_t<row_type>;
537 RowReferenceBase(
const QModelIndex &index, Adapter *adapter)
noexcept
538 : m_index(index), m_adapter(adapter)
541 template <
typename I = Impl, if_tree<I> =
true>
542 bool hasChildren()
const
544 return m_adapter->model()->hasChildren(m_index);
547 template <
typename I = Impl, if_tree<I> =
true>
548 auto children()
const
550 using ConstRange = QRangeModelDetails::asConst_t<Range>;
551 return QRangeModelAdapter<ConstRange, Protocol, Model>(m_adapter->storage.m_model,
552 m_index, std::in_place);
555 ConstColumnIterator cbegin()
const
557 return ConstColumnIterator{m_index, 0, m_adapter};
559 ConstColumnIterator cend()
const
561 return ConstColumnIterator{m_index, m_adapter->columnCount(), m_adapter};
564 ConstColumnIterator begin()
const {
return cbegin(); }
565 ConstColumnIterator end()
const {
return cend(); }
567 size_type size()
const
569 return m_adapter->columnCount();
572 auto at(
int column)
const
574 Q_ASSERT(column >= 0 && column < m_adapter->columnCount());
575 return *ConstColumnIterator{m_index, column, m_adapter};
578 auto operator[](
int column)
const
584 friend struct RowGetter<Reference, const_row_type>;
585 ~RowReferenceBase() =
default;
590 friend bool comparesEqual(
const Reference &lhs,
const Reference &rhs)
592 Q_ASSERT(lhs.m_adapter == rhs.m_adapter);
593 return lhs.m_index == rhs.m_index;
595 friend Qt::strong_ordering compareThreeWay(
const Reference &lhs,
const Reference &rhs)
597 Q_ASSERT(lhs.m_adapter == rhs.m_adapter);
598 return qCompareThreeWay(lhs.m_index, rhs.m_index);
601 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Reference)
603 friend bool comparesEqual(
const Reference &lhs,
const row_type &rhs)
605 return lhs.get() == rhs;
607 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(Reference, row_type)
609#ifndef QT_NO_DEBUG_STREAM
610 friend inline QDebug operator<<(QDebug dbg,
const Reference &ref)
612 QDebugStateSaver saver(dbg);
614 return dbg <<
"RowReference(" << ref.m_index <<
")";
617#ifndef QT_NO_DATASTREAM
618 friend inline QDataStream &operator<<(QDataStream &ds,
const Reference &ref)
620 return ds << ref.get();
625 struct ConstRowReference : RowReferenceBase<ConstRowReference,
const QRangeModelAdapter>
628 using Base = RowReferenceBase<ConstRowReference,
const QRangeModelAdapter>;
632 ConstRowReference() =
default;
633 ConstRowReference(
const ConstRowReference &) =
default;
634 ConstRowReference(ConstRowReference &&) =
default;
635 ConstRowReference &operator=(
const ConstRowReference &) =
default;
636 ConstRowReference &operator=(ConstRowReference &&) =
default;
637 ~ConstRowReference() =
default;
640 struct RowReference : RowReferenceBase<RowReference, QRangeModelAdapter>
643 using Base = RowReferenceBase<RowReference, QRangeModelAdapter>;
645 using iterator = ColumnIterator;
646 using const_iterator =
typename Base::const_iterator;
647 using size_type =
typename Base::size_type;
648 using difference_type =
typename Base::difference_type;
649 using const_row_type =
typename Base::const_row_type;
652 RowReference() =
delete;
653 ~RowReference() =
default;
654 RowReference(
const RowReference &other) =
default;
655 RowReference(RowReference &&other) =
default;
658 RowReference &operator=(
const ConstRowReference &other)
664 RowReference &operator=(
const RowReference &other)
670 RowReference &operator=(
const row_type &other)
676 RowReference &operator=(row_type &&other)
678 assign(std::move(other));
682 operator ConstRowReference()
const
684 return ConstRowReference{
this->m_index,
this->m_adapter};
687 template <
typename ConstRowType = const_row_type,
688 std::enable_if_t<!std::is_same_v<ConstRowType,
const row_type &>,
bool> =
true>
689 RowReference &operator=(
const ConstRowType &other)
695 template <
typename T,
typename It,
typename Sentinel>
696 RowReference &operator=(
const QRangeModelDetails::RowView<T, It, Sentinel> &other)
698 *
this = row_type{other.begin(), other.end()};
702 friend inline void swap(RowReference lhs, RowReference rhs)
704 auto lhsRow = lhs.get();
706 rhs = std::move(lhsRow);
709 template <
typename I = Impl, if_tree<I> =
true>
712 return QRangeModelAdapter(
this->m_adapter->storage.m_model,
this->m_index,
717 ColumnIterator begin()
719 return ColumnIterator{
this->m_index, 0,
this->m_adapter};
725 return ColumnIterator{
this->m_index,
this->m_adapter->columnCount(),
this->m_adapter};
731 Q_ASSERT(column >= 0 && column <
this->m_adapter->columnCount());
732 return *ColumnIterator{
this->m_index, column,
this->m_adapter};
735 using Base::operator[];
736 auto operator[](
int column)
742 template <
typename RHS>
743 void verifyRows(
const row_type &oldRow,
const RHS &newRow)
745 if constexpr (QRangeModelDetails::test_size<row_type>::value) {
747 Q_ASSERT_X(Impl::size(newRow) == Impl::size(oldRow),
748 "RowReference::operator=()",
749 "The new row has the wrong size!");
752 if constexpr (is_tree<Impl>) {
754 Q_ASSERT_X(QRangeModelDetails::isValid(newRow),
755 "RowReference::operator=()",
756 "An invalid row can not inserted into a tree!");
760 template <
typename R>
761 void assign(R &&other)
763 auto *impl =
this->m_adapter->storage.implementation();
764 decltype(
auto) oldRow = impl->rowData(
this->m_index);
766 verifyRows(oldRow, other);
768 if constexpr (is_tree<Impl>) {
769 auto &protocol = impl->protocol();
770 auto *oldParent = protocol.parentRow(QRangeModelDetails::refTo(oldRow));
774 if (
decltype(
auto) oldChildren = protocol.childRows(QRangeModelDetails::refTo(oldRow));
775 QRangeModelDetails::isValid(oldChildren)) {
776 if (
int oldChildCount =
this->m_adapter->model()->rowCount(
this->m_index)) {
777 impl->beginRemoveRows(
this->m_index, 0, oldChildCount - 1);
778 impl->deleteRemovedRows(QRangeModelDetails::refTo(oldChildren));
780 QRangeModelDetails::refTo(oldChildren) = range_type{};
781 impl->endRemoveRows();
785 if constexpr (protocol_traits::has_deleteRow)
786 protocol.deleteRow(oldRow);
787 oldRow = std::forward<R>(other);
788 if constexpr (protocol_traits::has_setParentRow) {
789 protocol.setParentRow(QRangeModelDetails::refTo(oldRow),
790 QRangeModelDetails::pointerTo(oldParent));
791 if (
decltype(
auto) newChildren = protocol.childRows(QRangeModelDetails::refTo(oldRow));
792 QRangeModelDetails::isValid(newChildren)) {
793 impl->beginInsertRows(
this->m_index, 0,
794 Impl::size(QRangeModelDetails::refTo(newChildren)) - 1);
795 impl->setParentRow(QRangeModelDetails::refTo(newChildren),
796 QRangeModelDetails::pointerTo(oldRow));
797 impl->endInsertRows();
801 oldRow = std::forward<R>(other);
803 this->m_adapter->emitDataChanged(
this->m_index,
804 this->m_index.siblingAtColumn(
this->m_adapter->columnCount() - 1));
805 if constexpr (Impl::itemsAreQObjects) {
806 if (
this->m_adapter->model()->autoConnectPolicy() == QRangeModel::AutoConnectPolicy::Full) {
807 impl->autoConnectPropertiesInRow(oldRow,
this->m_index.row(),
this->m_index.parent());
808 if constexpr (is_tree<Impl>)
809 impl->autoConnectProperties(
this->m_index);
815#ifndef QT_NO_DATASTREAM
816 friend inline QDataStream &operator>>(QDataStream &ds, RowReference &ref)
826 template <
typename Iterator,
typename Adapter>
827 struct RowIteratorBase : QRangeModelDetails::ParentIndex<is_tree<Impl>>
829 using iterator_category = std::random_access_iterator_tag;
830 using difference_type =
int;
832 RowIteratorBase() =
default;
833 RowIteratorBase(
const RowIteratorBase &) =
default;
834 RowIteratorBase(RowIteratorBase &&) =
default;
835 RowIteratorBase &operator=(
const RowIteratorBase &) =
default;
836 RowIteratorBase &operator=(RowIteratorBase &&) =
default;
838 RowIteratorBase(
int row,
const QModelIndex &parent, Adapter *adapter)
839 : QRangeModelDetails::ParentIndex<is_tree<Impl>>{parent}
840 , m_row(row), m_adapter(adapter)
843 void swap(RowIteratorBase &other)
noexcept
845 qSwap(m_row, other.m_row);
846 qSwap(
this->m_rootIndex, other.m_rootIndex);
847 q_ptr_swap(m_adapter, other.m_adapter);
850 friend Iterator &operator++(Iterator &that)
855 friend Iterator operator++(Iterator &that,
int)
861 friend Iterator operator+(
const Iterator &that, difference_type n)
863 return {that.m_row + n, that.root(), that.m_adapter};
865 friend Iterator operator+(difference_type n,
const Iterator &that)
869 friend Iterator &operator+=(Iterator &that, difference_type n)
875 friend Iterator &operator--(Iterator &that)
880 friend Iterator operator--(Iterator &that,
int)
886 friend Iterator operator-(
const Iterator &that, difference_type n)
888 return {that.m_row - n, that.root(), that.m_adapter};
890 friend Iterator operator-(difference_type n,
const Iterator &that)
894 friend Iterator &operator-=(Iterator &that, difference_type n)
900 friend difference_type operator-(
const Iterator &lhs,
const Iterator &rhs)
902 return lhs.m_row - rhs.m_row;
906 ~RowIteratorBase() =
default;
908 Adapter *m_adapter =
nullptr;
911 friend bool comparesEqual(
const Iterator &lhs,
const Iterator &rhs)
noexcept
913 return lhs.m_row == rhs.m_row && lhs.root() == rhs.root();
915 friend Qt::strong_ordering compareThreeWay(
const Iterator &lhs,
const Iterator &rhs)
noexcept
917 if (lhs.root() == rhs.root())
918 return qCompareThreeWay(lhs.m_row, rhs.m_row);
919 return qCompareThreeWay(lhs.root(), rhs.root());
922 Q_DECLARE_STRONGLY_ORDERED(Iterator)
924#ifndef QT_NO_DEBUG_STREAM
925 friend inline QDebug operator<<(QDebug dbg,
const Iterator &it)
927 QDebugStateSaver saver(dbg);
929 return dbg <<
"RowIterator(" << it.m_row << it.root() <<
")";
935 struct ConstRowIterator :
public RowIteratorBase<ConstRowIterator,
const QRangeModelAdapter>
938 using Base = RowIteratorBase<ConstRowIterator,
const QRangeModelAdapter>;
942 using difference_type =
typename Base::difference_type;
943 using value_type = std::conditional_t<is_list<Impl>,
946 using reference = std::conditional_t<is_list<Impl>,
949 using pointer = std::conditional_t<is_list<Impl>,
950 QRangeModelDetails::data_pointer_t<const_data_type>,
953 ConstRowIterator(
const ConstRowIterator &other) =
default;
954 ConstRowIterator(ConstRowIterator &&other) =
default;
955 ConstRowIterator &operator=(
const ConstRowIterator &other) =
default;
956 ConstRowIterator &operator=(ConstRowIterator &&other) =
default;
957 ~ConstRowIterator() =
default;
959 pointer operator->()
const
961 return pointer{operator*()};
964 reference operator*()
const
966 if constexpr (is_list<Impl>) {
967 return this->m_adapter->at(
this->m_row);
969 const QModelIndex index =
this->m_adapter->model()->index(
this->m_row, 0,
971 return ConstRowReference{index,
this->m_adapter};
975 reference operator[](difference_type n)
const
981 struct RowIterator :
public RowIteratorBase<RowIterator, QRangeModelAdapter>
984 using Base = RowIteratorBase<RowIterator, QRangeModelAdapter>;
988 using difference_type =
typename Base::difference_type;
989 using value_type = std::conditional_t<is_list<Impl>,
992 using reference = std::conditional_t<is_list<Impl>,
995 using pointer = std::conditional_t<is_list<Impl>,
999 RowIterator(
const RowIterator &other) =
default;
1000 RowIterator(RowIterator &&other) =
default;
1001 RowIterator &operator=(
const RowIterator &other) =
default;
1002 RowIterator &operator=(RowIterator &&other) =
default;
1003 ~RowIterator() =
default;
1005 operator ConstRowIterator()
const
1007 return ConstRowIterator{
this->m_row,
this->root(),
this->m_adapter};
1010 pointer operator->()
const
1012 return pointer{operator*()};
1015 reference operator*()
const
1017 const QModelIndex index =
this->m_adapter->model()->index(
this->m_row, 0,
this->root());
1018 if constexpr (is_list<Impl>) {
1019 return reference{index};
1021 return reference{index,
this->m_adapter};
1025 reference operator[](difference_type n)
const
1027 return *(*
this + n);
1031 using const_iterator = ConstRowIterator;
1032 using iterator = RowIterator;
1034 template <
typename R,
1036 if_compatible_model_params<R, P> =
true>
1037 explicit QRangeModelAdapter(R &&range, P &&protocol)
1038 : QRangeModelAdapter(
new Model(QRangeModelDetails::forwardOrConvert<Range>(std::forward<R>(range)),
1039 QRangeModelDetails::forwardOrConvert<Protocol>(std::forward<P>(protocol))))
1042 template <
typename R,
1044 if_compatible_model_params<R, P> =
true,
1045 unless_adapter<R> =
true>
1046 explicit QRangeModelAdapter(R &&range)
1047 : QRangeModelAdapter(
new Model(QRangeModelDetails::forwardOrConvert<Range>(std::forward<R>(range))))
1052 Model *model()
const
1054 return storage.m_model.get();
1057 const range_type &range()
const
1059 return QRangeModelDetails::refTo(storage.implementation()->childRange(storage.root()));
1062 template <
typename NewRange = range_type, if_assignable_range<NewRange> =
true>
1063 void assign(NewRange &&newRange)
1065 assignImpl(qsizetype(Impl::size(QRangeModelDetails::refTo(newRange))),
1066 [&newRange](
auto &oldRange) {
1067 oldRange = std::forward<NewRange>(newRange);
1071 template <
typename NewRange = range_type, if_assignable_range<NewRange> =
true,
1072 unless_adapter<NewRange> =
true>
1073 QRangeModelAdapter &operator=(NewRange &&newRange)
1075 assign(std::forward<NewRange>(newRange));
1079 template <
typename Row, if_assignable_range<std::initializer_list<Row>> =
true>
1080 void assign(std::initializer_list<Row> newRange)
1082 assignImpl(newRange.size(), [&newRange](
auto &oldRange) {
1083 oldRange = newRange;
1087 template <
typename Row, if_assignable_range<std::initializer_list<Row>> =
true>
1088 QRangeModelAdapter &operator=(std::initializer_list<Row> newRange)
1094 template <
typename InputIterator,
typename Sentinel,
typename I = Impl, if_writable<I> =
true>
1095 void assign(InputIterator first, Sentinel last)
1097 assignImpl(size_t(std::distance(first, last)), [first, last](
auto &oldRange) {
1098 oldRange.assign(first, last);
1103 ConstRowIterator cbegin()
const
1105 return ConstRowIterator{ 0, storage.root(),
this };
1107 ConstRowIterator begin()
const {
return cbegin(); }
1109 ConstRowIterator cend()
const
1111 return ConstRowIterator{ rowCount(), storage.root(),
this };
1113 ConstRowIterator end()
const {
return cend(); }
1115 template <
typename I = Impl, if_writable<I> =
true>
1118 return RowIterator{ 0, storage.root(),
this };
1121 template <
typename I = Impl, if_writable<I> =
true>
1124 return RowIterator{ rowCount(), storage.root(),
this };
1132 template <
typename I = Impl, if_list<I> =
true>
1133 QModelIndex index(
int row)
const
1135 return storage->index(row, 0, storage.root());
1138 template <
typename I = Impl, unless_list<I> =
true>
1139 QModelIndex index(
int row,
int column)
const
1141 return storage->index(row, column, storage.root());
1144 template <
typename I = Impl, if_tree<I> =
true>
1145 QModelIndex index(QSpan<
const int> path,
int col)
const
1147 QModelIndex result = storage.root();
1148 auto count = path.size();
1149 for (
const int r : path) {
1151 result = storage->index(r, 0, result);
1153 result = storage->index(r, col, result);
1158 int columnCount()
const
1161 return storage->columnCount({});
1164 int rowCount()
const
1166 return storage->rowCount(storage.root());
1169 template <
typename I = Impl, if_tree<I> =
true>
1170 int rowCount(
int row)
const
1172 return storage->rowCount(index(row, 0));
1175 template <
typename I = Impl, if_tree<I> =
true>
1176 int rowCount(QSpan<
const int> path)
const
1178 return storage->rowCount(index(path, 0));
1181 template <
typename I = Impl, if_tree<I> =
true>
1182 constexpr bool hasChildren(
int row)
const
1184 return storage.m_model->hasChildren(index(row, 0));
1187 template <
typename I = Impl, if_tree<I> =
true>
1188 constexpr bool hasChildren(QSpan<
const int> path)
const
1190 return storage.m_model->hasChildren(index(path, 0));
1193 template <
typename I = Impl, if_list<I> =
true>
1194 QVariant data(
int row)
const
1196 return QRangeModelDetails::dataAtIndex<QVariant>(index(row));
1199 template <
typename I = Impl, if_list<I> =
true>
1200 QVariant data(
int row,
int role)
const
1202 return QRangeModelDetails::dataAtIndex<QVariant>(index(row), role);
1205 template <
typename I = Impl, if_list<I> =
true, if_writable<I> =
true>
1206 bool setData(
int row,
const QVariant &value,
int role = Qt::EditRole)
1208 return storage->setData(index(row), value, role);
1211 template <
typename I = Impl, unless_list<I> =
true>
1212 QVariant data(
int row,
int column)
const
1214 return QRangeModelDetails::dataAtIndex<QVariant>(index(row, column));
1217 template <
typename I = Impl, unless_list<I> =
true>
1218 QVariant data(
int row,
int column,
int role)
const
1220 return QRangeModelDetails::dataAtIndex<QVariant>(index(row, column), role);
1223 template <
typename I = Impl, unless_list<I> =
true, if_writable<I> =
true>
1224 bool setData(
int row,
int column,
const QVariant &value,
int role = Qt::EditRole)
1226 return storage->setData(index(row, column), value, role);
1229 template <
typename I = Impl, if_tree<I> =
true>
1230 QVariant data(QSpan<
const int> path,
int column)
const
1232 return QRangeModelDetails::dataAtIndex<QVariant>(index(path, column));
1235 template <
typename I = Impl, if_tree<I> =
true>
1236 QVariant data(QSpan<
const int> path,
int column,
int role)
const
1238 return QRangeModelDetails::dataAtIndex<QVariant>(index(path, column), role);
1241 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1242 bool setData(QSpan<
const int> path,
int column,
const QVariant &value,
int role = Qt::EditRole)
1244 return storage->setData(index(path, column), value, role);
1249 template <
typename I= Impl, if_list<I> =
true>
1250 const_data_type at(
int row)
const
1252 return QRangeModelDetails::dataAtIndex<data_type>(index(row));
1254 template <
typename I = Impl, if_list<I> =
true>
1255 const_data_type operator[](
int row)
const {
return at(row); }
1257 template <
typename I= Impl, if_list<I> =
true, if_writable<I> =
true>
1259 const QModelIndex idx =
this->index(row);
1260 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::at",
"Index at row is invalid");
1261 return DataReference{idx};
1263 template <
typename I = Impl, if_list<I> =
true, if_writable<I> =
true>
1264 auto operator[](
int row) {
1265 const QModelIndex idx =
this->index(row);
1266 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::operator[]",
"Index at row is invalid");
1267 return DataReference{idx};
1271 template <
typename I = Impl, unless_list<I> =
true>
1272 decltype(
auto) at(
int row)
const
1274 return ConstRowReference{index(row, 0),
this}.get();
1276 template <
typename I = Impl, unless_list<I> =
true>
1277 decltype(
auto) operator[](
int row)
const {
return at(row); }
1279 template <
typename I = Impl, if_table<I> =
true, if_writable<I> =
true>
1282 return RowReference{index(row, 0),
this};
1284 template <
typename I = Impl, if_table<I> =
true, if_writable<I> =
true>
1285 auto operator[](
int row) {
return at(row); }
1288 template <
typename I = Impl, unless_list<I> =
true>
1289 const_data_type at(
int row,
int column)
const
1291 return QRangeModelDetails::dataAtIndex<data_type>(index(row, column));
1294#ifdef __cpp_multidimensional_subscript
1295 template <
typename I = Impl, unless_list<I> =
true>
1296 const_data_type operator[](
int row,
int column)
const {
return at(row, column); }
1299 template <
typename I = Impl, unless_list<I> =
true, if_writable<I> =
true>
1300 auto at(
int row,
int column)
1302 const QModelIndex idx =
this->index(row, column);
1303 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::at",
"Index at cell is invalid");
1304 return DataReference{idx};
1306#ifdef __cpp_multidimensional_subscript
1307 template <
typename I = Impl, unless_list<I> =
true, if_writable<I> =
true>
1308 auto operator[](
int row,
int column)
1310 const QModelIndex idx =
this->index(row, column);
1311 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::operator[]",
"Index at cell is invalid");
1312 return DataReference{idx};
1318 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1321 return RowReference{index(row, 0),
this};
1323 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1324 auto operator[](
int row) {
return at(row); }
1327 template <
typename I = Impl, if_tree<I> =
true>
1328 decltype(
auto) at(QSpan<
const int> path)
const
1330 return ConstRowReference{index(path, 0),
this}.get();
1332 template <
typename I = Impl, if_tree<I> =
true>
1333 decltype(
auto) operator[](QSpan<
const int> path)
const {
return at(path); }
1335 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1336 auto at(QSpan<
const int> path)
1338 return RowReference{index(path, 0),
this};
1340 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1341 auto operator[](QSpan<
const int> path) {
return at(path); }
1344 template <
typename I = Impl, if_tree<I> =
true>
1345 const_data_type at(QSpan<
const int> path,
int column)
const
1348 return QRangeModelDetails::dataAtIndex<data_type>(index(path, column));
1351#ifdef __cpp_multidimensional_subscript
1352 template <
typename I = Impl, if_tree<I> =
true>
1353 const_data_type operator[](QSpan<
const int> path,
int column)
const {
return at(path, column); }
1356 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1357 auto at(QSpan<
const int> path,
int column)
1360 const QModelIndex idx =
this->index(path, column);
1361 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::at",
"Index at path is invalid");
1362 return DataReference{idx};
1364#ifdef __cpp_multidimensional_subscript
1365 template <
typename I = Impl, if_tree<I> =
true, if_writable<I> =
true>
1366 auto operator[](QSpan<
const int> path,
int column)
1368 const QModelIndex idx =
this->index(path, column);
1369 Q_ASSERT_X(idx.isValid(),
"QRangeModelAdapter::operator[]",
"Index at path is invalid");
1370 return DataReference{idx};
1374 template <
typename I = Impl, if_canInsertRows<I> =
true>
1375 bool insertRow(
int before)
1377 return storage.m_model->insertRow(before);
1380 template <
typename I = Impl, if_canInsertRows<I> =
true, if_tree<I> =
true>
1381 bool insertRow(QSpan<
const int> before)
1383 Q_PRE(before.size());
1384 return storage.m_model->insertRow(before.back(),
this->index(before.first(before.size() - 1), 0));
1387 template <
typename D = row_type,
typename I = Impl,
1388 if_canInsertRows<I> =
true, if_compatible_row<D> =
true>
1389 bool insertRow(
int before, D &&data)
1391 return insertRowImpl(before, storage.root(), std::forward<D>(data));
1394 template <
typename D = row_type,
typename I = Impl,
1395 if_canInsertRows<I> =
true, if_compatible_row<D> =
true, if_tree<I> =
true>
1396 bool insertRow(QSpan<
const int> before, D &&data)
1398 return insertRowImpl(before.back(),
this->index(before.sliced(0, before.size() - 1), 0),
1399 std::forward<D>(data));
1402 template <
typename C,
typename I = Impl,
1403 if_canInsertRows<I> =
true, if_compatible_row_range<C> =
true>
1404 bool insertRows(
int before, C &&data)
1406 return insertRowsImpl(before, storage.root(), std::forward<C>(data));
1409 template <
typename C,
typename I = Impl,
1410 if_canInsertRows<I> =
true, if_compatible_row_range<C> =
true, if_tree<I> =
true>
1411 bool insertRows(QSpan<
const int> before, C &&data)
1413 return insertRowsImpl(before.back(),
this->index(before.sliced(0, before.size() - 1), 0),
1414 std::forward<C>(data));
1417 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1418 bool removeRow(
int row)
1420 return removeRows(row, 1);
1423 template <
typename I = Impl, if_canRemoveRows<I> =
true, if_tree<I> =
true>
1424 bool removeRow(QSpan<
const int> path)
1426 return removeRows(path, 1);
1429 template <
typename I = Impl, if_canRemoveRows<I> =
true>
1430 bool removeRows(
int row,
int count)
1432 return storage->removeRows(row, count, storage.root());
1435 template <
typename I = Impl, if_canRemoveRows<I> =
true, if_tree<I> =
true>
1436 bool removeRows(QSpan<
const int> path,
int count)
1438 return storage->removeRows(path.back(), count,
1439 this->index(path.first(path.size() - 1), 0));
1442 template <
typename F = range_features, if_canMoveItems<F> =
true>
1443 bool moveRow(
int source,
int destination)
1445 return moveRows(source, 1, destination);
1448 template <
typename F = range_features, if_canMoveItems<F> =
true>
1449 bool moveRows(
int source,
int count,
int destination)
1451 return storage->moveRows(storage.root(), source, count, storage.root(), destination);
1454 template <
typename I = Impl,
typename F = range_features,
1455 if_canMoveItems<F> =
true, if_tree<I> =
true>
1456 bool moveRow(QSpan<
const int> source, QSpan<
const int> destination)
1458 return moveRows(source, 1, destination);
1461 template <
typename I = Impl,
typename F = range_features,
1462 if_canMoveItems<F> =
true, if_tree<I> =
true>
1463 bool moveRows(QSpan<
const int> source,
int count, QSpan<
const int> destination)
1465 return storage->moveRows(
this->index(source.first(source.size() - 1), 0),
1468 this->index(destination.first(destination.size() - 1), 0),
1469 destination.back());
1472 template <
typename I = Impl, if_canInsertColumns<I> =
true>
1473 bool insertColumn(
int before)
1475 return storage.m_model->insertColumn(before);
1478 template <
typename D,
typename I = Impl,
1479 if_canInsertColumns<I> =
true, if_compatible_column_data<D> =
true>
1480 bool insertColumn(
int before, D &&data)
1482 return insertColumnImpl(before, storage.root(), std::forward<D>(data));
1485 template <
typename C,
typename I = Impl,
1486 if_canInsertColumns<I> =
true, if_compatible_column_range<C> =
true>
1487 bool insertColumns(
int before, C &&data)
1489 return insertColumnsImpl(before, storage.root(), std::forward<C>(data));
1492 template <
typename I = Impl, if_canRemoveColumns<I> =
true>
1493 bool removeColumn(
int column)
1495 return storage.m_model->removeColumn(column);
1498 template <
typename I = Impl, if_canRemoveColumns<I> =
true>
1499 bool removeColumns(
int column,
int count)
1501 return storage->removeColumns(column, count, {});
1504 template <
typename F = row_features, if_canMoveItems<F> =
true>
1505 bool moveColumn(
int from,
int to)
1507 return moveColumns(from, 1, to);
1510 template <
typename F = row_features, if_canMoveItems<F> =
true>
1511 bool moveColumns(
int from,
int count,
int to)
1513 return storage->moveColumns(storage.root(), from, count, storage.root(), to);
1516 template <
typename I = Impl,
typename F = row_features,
1517 if_canMoveItems<F> =
true, if_tree<I> =
true>
1518 bool moveColumn(QSpan<
const int> source,
int to)
1520 const QModelIndex parent =
this->index(source.first(source.size() - 1), 0);
1521 return storage->moveColumns(parent, source.back(), 1, parent, to);
1524 template <
typename I = Impl,
typename F = row_features,
1525 if_canMoveItems<F> =
true, if_tree<I> =
true>
1526 bool moveColumns(QSpan<
const int> source,
int count,
int destination)
1528 const QModelIndex parent =
this->index(source.first(source.size() - 1), 0);
1529 return storage->moveColumns(parent, source.back(), count, parent, destination);
1534 bool comparesEqual(
const QRangeModelAdapter &lhs,
const QRangeModelAdapter &rhs)
noexcept
1536 return lhs.storage.m_model == rhs.storage.m_model;
1538 Q_DECLARE_EQUALITY_COMPARABLE(QRangeModelAdapter)
1541 bool comparesEqual(
const QRangeModelAdapter &lhs,
const range_type &rhs)
1543 return lhs.range() == rhs;
1545 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QRangeModelAdapter, range_type)
1548 void emitDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight)
1550 Q_EMIT storage.implementation()->dataChanged(topLeft, bottomRight, {});
1553 void beginAssignImpl(Impl *impl, range_type *oldRange,
int newLastRow)
1555 const QModelIndex root = storage.root();
1556 const qsizetype oldLastRow = qsizetype(Impl::size(oldRange)) - 1;
1558 if (!root.isValid()) {
1559 impl->beginResetModel();
1560 impl->deleteOwnedRows();
1561 }
else if constexpr (is_tree<Impl>) {
1562 if (oldLastRow >= 0) {
1563 impl->beginRemoveRows(root, 0, model()->rowCount(root) - 1);
1564 impl->deleteRemovedRows(QRangeModelDetails::refTo(oldRange));
1565 impl->endRemoveRows();
1567 if (newLastRow >= 0)
1568 impl->beginInsertRows(root, 0, newLastRow);
1570 Q_ASSERT_X(
false,
"QRangeModelAdapter::assign",
1571 "Internal error: The root index in a table or list must be invalid.");
1575 void endAssignImpl(Impl *impl,
int newLastRow)
1577 const QModelIndex root = storage.root();
1578 if (!root.isValid()) {
1579 impl->endResetModel();
1580 }
else if constexpr (is_tree<Impl>) {
1581 if (newLastRow >= 0) {
1582 Q_ASSERT(model()->hasChildren(root));
1585 impl->setParentRow(QRangeModelDetails::refTo(impl->childRange(root)),
1586 QRangeModelDetails::pointerTo(impl->rowData(root)));
1587 impl->endInsertRows();
1592 template <
typename Assigner>
1593 void assignImpl(std::size_t newSize, Assigner &&assigner)
1595 const auto sz = q26::saturate_cast<
int>(newSize);
1596 Q_ASSERT_X(q20::cmp_equal(sz, newSize),
1597 "QRangeModelAdapter::assign",
"New range is too large");
1598 const int newLastRow = sz - 1;
1600 auto *impl = storage.implementation();
1601 auto *oldRange = impl->childRange(storage.root());
1602 beginAssignImpl(impl, oldRange, newLastRow);
1603 assigner(QRangeModelDetails::refTo(oldRange));
1604 endAssignImpl(impl, newLastRow);
1606 if constexpr (Impl::itemsAreQObjects) {
1607 if (model()->autoConnectPolicy() == QRangeModel::AutoConnectPolicy::Full) {
1608 const auto begin = QRangeModelDetails::begin(QRangeModelDetails::refTo(oldRange));
1609 const auto end = QRangeModelDetails::end(QRangeModelDetails::refTo(oldRange));
1611 for (
auto it = begin; it != end; ++it, ++rowIndex)
1612 impl->autoConnectPropertiesInRow(*it, rowIndex, storage.root());
1618 template <
typename P,
typename Row>
1619 static auto setParentRow(P &protocol, Row &newRow, row_ptr parentRow)
1620 ->
decltype(protocol.setParentRow(newRow, parentRow))
1622 return protocol.setParentRow(newRow, parentRow);
1624 template <
typename ...Args>
static constexpr void setParentRow(Args &&...)
1626 static_assert(!protocol_traits::has_setParentRow,
1627 "Internal error, wrong setParentRow overload called");
1630 template <
typename D>
1631 bool insertRowImpl(
int before,
const QModelIndex &parent, D &&data)
1633 return storage.implementation()->doInsertRows(before, 1, parent, [&data,
this]
1634 (range_type &range,
auto *parentRow,
int row,
int count) {
1636 const auto oldSize = range.size();
1637 auto newRow = range.emplace(QRangeModelDetails::pos(range, row), std::forward<D>(data));
1638 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
1639 QRangeModelDetails::pointerTo(parentRow));
1640 return range.size() == oldSize + count;
1644 template <
typename LHS>
1645 static auto selfInsertion(LHS *lhs, LHS *rhs) ->
decltype(lhs == rhs)
1649 qCritical(
"Inserting data into itself is not supported");
1655 template <
typename LHS,
typename RHS>
1656 static constexpr bool selfInsertion(LHS *, RHS *) {
return false; }
1658 template <
typename C>
1659 bool insertRowsImpl(
int before,
const QModelIndex &parent, C &&data)
1661 bool result =
false;
1662 result = storage->doInsertRows(before,
int(std::size(data)), parent, [&data,
this]
1663 (range_type &range,
auto *parentRow,
int row,
int count){
1664 Q_UNUSED(parentRow);
1666 const auto pos = QRangeModelDetails::pos(range, row);
1667 const auto oldSize = range.size();
1669 auto dataRange = [&data]{
1670 if constexpr (std::is_rvalue_reference_v<C&&>) {
1671 return std::make_pair(
1672 std::move_iterator(std::begin(data)),
1673 std::move_iterator(std::end(data))
1676 return std::make_pair(std::begin(data), std::end(data));
1680 if constexpr (range_features::has_insert_range) {
1681 if (selfInsertion(&range, &data))
1683 auto start = range.insert(pos, dataRange.first, dataRange.second);
1684 if constexpr (protocol_traits::has_setParentRow) {
1686 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*start),
1687 QRangeModelDetails::pointerTo(parentRow));
1695 auto newRow = range.insert(pos, count, row_type{});
1696 while (dataRange.first != dataRange.second) {
1697 *newRow = *dataRange.first;
1698 setParentRow(storage->protocol(), QRangeModelDetails::refTo(*newRow),
1699 QRangeModelDetails::pointerTo(parentRow));
1704 return range.size() == oldSize + count;
1709 template <
typename D,
typename =
void>
1710 struct DataFromList {
1711 static constexpr auto first(D &data) {
return &data; }
1712 static constexpr auto next(D &, D *entry) {
return entry; }
1715 template <
typename D>
1716 struct DataFromList<D, std::enable_if_t<QRangeModelDetails::range_traits<D>::value>>
1718 static constexpr auto first(D &data) {
return std::begin(data); }
1719 static constexpr auto next(D &data,
typename D::iterator entry)
1722 if (entry == std::end(data))
1723 entry = first(data);
1728 template <
typename D,
typename =
void>
struct RowFromTable
1730 static constexpr auto first(D &data) {
return &data; }
1731 static constexpr auto next(D &, D *entry) {
return entry; }
1734 template <
typename D>
1735 struct RowFromTable<D, std::enable_if_t<std::conjunction_v<
1736 QRangeModelDetails::range_traits<D>,
1737 QRangeModelDetails::range_traits<
typename D::value_type>
1742 template <
typename D>
1743 bool insertColumnImpl(
int before,
const QModelIndex &parent, D data)
1745 auto entry = DataFromList<D>::first(data);
1747 return storage->doInsertColumns(before, 1, parent, [&entry, &data]
1748 (
auto &range,
auto pos,
int count) {
1749 const auto oldSize = range.size();
1750 range.insert(pos, *entry);
1751 entry = DataFromList<D>::next(data, entry);
1752 return range.size() == oldSize + count;
1756 template <
typename C>
1757 bool insertColumnsImpl(
int before,
const QModelIndex &parent, C data)
1759 bool result =
false;
1760 auto entries = RowFromTable<C>::first(data);
1761 auto begin = std::begin(*entries);
1762 auto end = std::end(*entries);
1763 result = storage->doInsertColumns(before,
int(std::size(*entries)), parent,
1764 [&begin, &end, &entries, &data](
auto &range,
auto pos,
int count) {
1765 const auto oldSize = range.size();
1766 if constexpr (row_features::has_insert_range) {
1767 range.insert(pos, begin, end);
1769 auto start = range.insert(pos, count, {});
1770 std::copy(begin, end, start);
1772 entries = RowFromTable<C>::next(data, entries);
1773 begin = std::begin(*entries);
1774 end = std::end(*entries);
1775 return range.size() == oldSize + count;