6#include <QtCore/qstack.h>
7#include <QtCore/qdebug.h>
14#if defined(QQMLTREEMODELADAPTOR_DEBUG) && !defined(QT_TESTLIB_LIB)
15# define ASSERT_CONSISTENCY() Q_ASSERT_X(testConsistency(true ), Q_FUNC_INFO, "Consistency test failed")
17# define ASSERT_CONSISTENCY qt_noop
20QQmlTreeModelToTableModel::QQmlTreeModelToTableModel(QObject *parent)
21 : QAbstractItemModel(parent)
25QAbstractItemModel *QQmlTreeModelToTableModel::model()
const
30void QQmlTreeModelToTableModel::connectToModel()
33 QObject::connect(m_model, &QAbstractItemModel::destroyed,
34 this, &QQmlTreeModelToTableModel::modelHasBeenDestroyed),
35 QObject::connect(m_model, &QAbstractItemModel::modelReset,
36 this, &QQmlTreeModelToTableModel::modelHasBeenReset),
37 QObject::connect(m_model, &QAbstractItemModel::dataChanged,
38 this, &QQmlTreeModelToTableModel::modelDataChanged),
40 QObject::connect(m_model, &QAbstractItemModel::layoutAboutToBeChanged,
41 this, &QQmlTreeModelToTableModel::modelLayoutAboutToBeChanged),
42 QObject::connect(m_model, &QAbstractItemModel::layoutChanged,
43 this, &QQmlTreeModelToTableModel::modelLayoutChanged),
45 QObject::connect(m_model, &QAbstractItemModel::rowsAboutToBeInserted,
46 this, &QQmlTreeModelToTableModel::modelRowsAboutToBeInserted),
47 QObject::connect(m_model, &QAbstractItemModel::rowsInserted,
48 this, &QQmlTreeModelToTableModel::modelRowsInserted),
49 QObject::connect(m_model, &QAbstractItemModel::rowsAboutToBeRemoved,
50 this, &QQmlTreeModelToTableModel::modelRowsAboutToBeRemoved),
51 QObject::connect(m_model, &QAbstractItemModel::rowsRemoved,
52 this, &QQmlTreeModelToTableModel::modelRowsRemoved),
53 QObject::connect(m_model, &QAbstractItemModel::rowsAboutToBeMoved,
54 this, &QQmlTreeModelToTableModel::modelRowsAboutToBeMoved),
55 QObject::connect(m_model, &QAbstractItemModel::rowsMoved,
56 this, &QQmlTreeModelToTableModel::modelRowsMoved),
58 QObject::connect(m_model, &QAbstractItemModel::columnsAboutToBeInserted,
59 this, &QQmlTreeModelToTableModel::modelColumnsAboutToBeInserted),
60 QObject::connect(m_model, &QAbstractItemModel::columnsAboutToBeRemoved,
61 this, &QQmlTreeModelToTableModel::modelColumnsAboutToBeRemoved),
62 QObject::connect(m_model, &QAbstractItemModel::columnsInserted,
63 this, &QQmlTreeModelToTableModel::modelColumnsInserted),
64 QObject::connect(m_model, &QAbstractItemModel::columnsRemoved,
65 this, &QQmlTreeModelToTableModel::modelColumnsRemoved)
69void QQmlTreeModelToTableModel::setModel(QAbstractItemModel *arg)
73 for (
auto &c : m_connections)
74 QObject::disconnect(c);
75 m_connections.fill({});
81 if (m_rootIndex.isValid() && m_rootIndex.model() != m_model)
82 m_rootIndex = QModelIndex();
86 showModelTopLevelItems();
89 emit modelChanged(arg);
93void QQmlTreeModelToTableModel::clearModelData()
97 m_expandedItems.clear();
101QModelIndex QQmlTreeModelToTableModel::rootIndex()
const
106void QQmlTreeModelToTableModel::setRootIndex(
const QModelIndex &idx)
108 if (m_rootIndex == idx)
115 showModelTopLevelItems();
116 emit rootIndexChanged();
119void QQmlTreeModelToTableModel::resetRootIndex()
121 setRootIndex(QModelIndex());
124QModelIndex QQmlTreeModelToTableModel::index(
int row,
int column,
const QModelIndex &parent)
const
126 return hasIndex(row, column, parent) ? createIndex(row, column) : QModelIndex();
129QModelIndex QQmlTreeModelToTableModel::parent(
const QModelIndex &child)
const
132 return QModelIndex();
135QHash<
int, QByteArray> QQmlTreeModelToTableModel::roleNames()
const
137 return m_model ? m_model->roleNames() : QAbstractItemModel::roleNames();
140int QQmlTreeModelToTableModel::rowCount(
const QModelIndex &)
const
144 return m_items.size();
147int QQmlTreeModelToTableModel::columnCount(
const QModelIndex &parent)
const
151 return m_model->columnCount(parent);
154QVariant QQmlTreeModelToTableModel::data(
const QModelIndex &index,
int role)
const
159 return m_model->data(mapToModel(index), role);
162bool QQmlTreeModelToTableModel::setData(
const QModelIndex &index,
const QVariant &value,
int role)
167 return m_model->setData(mapToModel(index), value, role);
170QVariant QQmlTreeModelToTableModel::headerData(
int section, Qt::Orientation orientation,
int role)
const
172 return m_model->headerData(section, orientation, role);
175Qt::ItemFlags QQmlTreeModelToTableModel::flags(
const QModelIndex &index)
const
177 return m_model->flags(mapToModel(index));
180int QQmlTreeModelToTableModel::depthAtRow(
int row)
const
182 if (row < 0 || row >= m_items.size())
184 return m_items.at(row).depth;
187int QQmlTreeModelToTableModel::itemIndex(
const QModelIndex &index)
const
190 if (!index.isValid() || index == m_rootIndex || m_items.isEmpty())
193 const int totalCount = m_items.size();
196 int localCount = qMin(m_lastItemIndex - 1, totalCount - m_lastItemIndex);
198 for (
int i = 0; i < localCount; ++i) {
199 const TreeItem &item1 = m_items.at(m_lastItemIndex + i);
200 if (item1.index == index) {
201 m_lastItemIndex = m_lastItemIndex + i;
202 return m_lastItemIndex;
204 const TreeItem &item2 = m_items.at(m_lastItemIndex - i - 1);
205 if (item2.index == index) {
206 m_lastItemIndex = m_lastItemIndex - i - 1;
207 return m_lastItemIndex;
211 for (
int j = qMax(0, m_lastItemIndex + localCount); j < totalCount; ++j) {
212 const TreeItem &item = m_items.at(j);
213 if (item.index == index) {
219 for (
int j = qMin(totalCount, m_lastItemIndex - localCount) - 1; j >= 0; --j) {
220 const TreeItem &item = m_items.at(j);
221 if (item.index == index) {
231bool QQmlTreeModelToTableModel::isVisible(
const QModelIndex &index)
233 return itemIndex(index) != -1;
236bool QQmlTreeModelToTableModel::childrenVisible(
const QModelIndex &index)
238 return (index == m_rootIndex && !m_items.isEmpty())
239 || (m_expandedItems.contains(index) && isVisible(index));
242QModelIndex QQmlTreeModelToTableModel::mapToModel(
const QModelIndex &index)
const
244 if (!index.isValid())
245 return QModelIndex();
247 const int row = index.row();
248 if (row < 0 || row > m_items.size() - 1)
249 return QModelIndex();
251 const QModelIndex sourceIndex = m_items.at(row).index;
252 return m_model->index(sourceIndex.row(), index.column(), sourceIndex.parent());
255QModelIndex QQmlTreeModelToTableModel::mapFromModel(
const QModelIndex &index)
const
257 if (!index.isValid())
258 return QModelIndex();
261 for (
int i = 0; i < m_items.size(); ++i) {
262 const QModelIndex proxyIndex = m_items[i].index;
263 if (proxyIndex.row() == index.row() && proxyIndex.parent() == index.parent()) {
270 return QModelIndex();
272 return this->index(row, index.column());
275QModelIndex QQmlTreeModelToTableModel::mapToModel(
int row)
const
277 if (row < 0 || row >= m_items.size())
278 return QModelIndex();
279 return m_items.at(row).index;
282QItemSelection QQmlTreeModelToTableModel::selectionForRowRange(
const QModelIndex &fromIndex,
const QModelIndex &toIndex)
const
284 int from = itemIndex(fromIndex);
285 int to = itemIndex(toIndex);
288 return QItemSelection();
289 return QItemSelection(toIndex, toIndex);
296 typedef std::pair<QModelIndex, QModelIndex> MIPair;
297 typedef QHash<QModelIndex, MIPair> MI2MIPairHash;
298 MI2MIPairHash ranges;
299 QModelIndex firstIndex = m_items.at(from).index;
300 QModelIndex lastIndex = firstIndex;
301 QModelIndex previousParent = firstIndex.parent();
302 bool selectLastRow =
false;
303 for (
int i = from + 1; i <= to || (selectLastRow =
true); i++) {
309 if (!selectLastRow) {
310 index = m_items.at(i).index;
311 parent = index.parent();
313 if (selectLastRow || previousParent != parent) {
314 const MI2MIPairHash::iterator &it = ranges.find(previousParent);
315 if (it == ranges.end())
316 ranges.insert(previousParent, MIPair(firstIndex, lastIndex));
318 it->second = lastIndex;
324 previousParent = parent;
330 sel.reserve(ranges.size());
331 for (
const MIPair &pair : std::as_const(ranges))
332 sel.append(QItemSelectionRange(pair.first, pair.second));
337void QQmlTreeModelToTableModel::showModelTopLevelItems(
bool doInsertRows)
342 if (m_model->hasChildren(m_rootIndex) && m_model->canFetchMore(m_rootIndex))
343 m_model->fetchMore(m_rootIndex);
344 const long topLevelRowCount = m_model->rowCount(m_rootIndex);
345 if (topLevelRowCount == 0)
348 showModelChildItems(TreeItem(m_rootIndex), 0, topLevelRowCount - 1, doInsertRows);
351void QQmlTreeModelToTableModel::showModelChildItems(
const TreeItem &parentItem,
int start,
int end,
bool doInsertRows,
bool doExpandPendingRows)
353 const QModelIndex &parentIndex = parentItem.index;
354 int rowIdx = parentIndex.isValid() && parentIndex != m_rootIndex ? itemIndex(parentIndex) + 1 : 0;
355 Q_ASSERT(rowIdx == 0 || parentItem.expanded);
356 if (parentIndex.isValid() && parentIndex != m_rootIndex && (rowIdx == 0 || !parentItem.expanded))
359 if (m_model->rowCount(parentIndex) == 0) {
360 if (m_model->hasChildren(parentIndex) && m_model->canFetchMore(parentIndex))
361 m_model->fetchMore(parentIndex);
365 int insertCount = end - start + 1;
372 const QModelIndex &nextSiblingIdx = m_model->index(end + 1, 0, parentIndex);
373 if (nextSiblingIdx.isValid()) {
374 startIdx = itemIndex(nextSiblingIdx);
376 const QModelIndex &prevSiblingIdx = m_model->index(start - 1, 0, parentIndex);
377 startIdx = lastChildIndex(prevSiblingIdx) + 1;
381 int rowDepth = rowIdx == 0 ? 0 : parentItem.depth + 1;
383 beginInsertRows(QModelIndex(), startIdx, startIdx + insertCount - 1);
384 m_items.reserve(m_items.size() + insertCount);
386 for (
int i = 0; i < insertCount; i++) {
387 const QModelIndex &cmi = m_model->index(start + i, 0, parentIndex);
388 const bool expanded = m_expandedItems.contains(cmi);
389 const TreeItem treeItem(cmi, rowDepth, expanded);
390 m_items.insert(startIdx + i, treeItem);
393 m_itemsToExpand.append(treeItem);
399 if (doExpandPendingRows)
400 expandPendingRows(doInsertRows);
404void QQmlTreeModelToTableModel::expand(
const QModelIndex &idx)
410 Q_ASSERT(!idx.isValid() || idx.model() == m_model);
412 if (!idx.isValid() || !m_model->hasChildren(idx))
414 if (m_expandedItems.contains(idx))
417 int row = itemIndex(idx);
421 m_expandedItems.insert(idx);
427void QQmlTreeModelToTableModel::collapse(
const QModelIndex &idx)
433 Q_ASSERT(!idx.isValid() || idx.model() == m_model);
435 if (!idx.isValid() || !m_model->hasChildren(idx))
437 if (!m_expandedItems.contains(idx))
440 int row = itemIndex(idx);
444 m_expandedItems.remove(idx);
450bool QQmlTreeModelToTableModel::isExpanded(
const QModelIndex &index)
const
456 Q_ASSERT(!index.isValid() || index.model() == m_model);
457 return !index.isValid() || m_expandedItems.contains(index);
460bool QQmlTreeModelToTableModel::isExpanded(
int row)
const
462 if (row < 0 || row >= m_items.size())
464 return m_items.at(row).expanded;
467bool QQmlTreeModelToTableModel::hasChildren(
int row)
const
469 if (row < 0 || row >= m_items.size())
471 return m_model->hasChildren(m_items[row].index);
474bool QQmlTreeModelToTableModel::hasSiblings(
int row)
const
476 const QModelIndex &index = mapToModel(row);
477 return index.row() != m_model->rowCount(index.parent()) - 1;
480void QQmlTreeModelToTableModel::expandRow(
int n)
482 if (!m_model || isExpanded(n))
485 TreeItem &item = m_items[n];
486 if ((item.index.flags() & Qt::ItemNeverHasChildren) || !m_model->hasChildren(item.index))
488 item.expanded =
true;
489 m_expandedItems.insert(item.index);
490 emit dataChanged(index(n, 0), index(n, 0), {ExpandedRole});
492 m_itemsToExpand.append(item);
496void QQmlTreeModelToTableModel::expandRecursively(
int row,
int depth)
498 Q_ASSERT(depth == -1 || depth > 0);
499 const int startDepth = depthAtRow(row);
501 auto expandHelp = [
this, depth, startDepth] (
const auto expandHelp,
const QModelIndex &index) ->
void {
502 const int rowToExpand = itemIndex(index);
503 if (!m_expandedItems.contains(index))
504 expandRow(rowToExpand);
506 if (depth != -1 && depthAtRow(rowToExpand) == startDepth + depth - 1)
509 const int childCount = m_model->rowCount(index);
510 for (
int childRow = 0; childRow < childCount; ++childRow) {
511 const QModelIndex childIndex = m_model->index(childRow, 0, index);
512 if (m_model->hasChildren(childIndex))
513 expandHelp(expandHelp, childIndex);
517 const QModelIndex index = m_items[row].index;
519 expandHelp(expandHelp, index);
522void QQmlTreeModelToTableModel::expandPendingRows(
bool doInsertRows)
524 while (!m_itemsToExpand.isEmpty()) {
525 const TreeItem item = m_itemsToExpand.takeFirst();
526 Q_ASSERT(item.expanded);
527 const QModelIndex &index = item.index;
528 int childrenCount = m_model->rowCount(index);
529 if (childrenCount == 0) {
530 if (m_model->hasChildren(index) && m_model->canFetchMore(index))
531 m_model->fetchMore(index);
538 showModelChildItems(item, 0, childrenCount - 1, doInsertRows,
false);
542void QQmlTreeModelToTableModel::collapseRecursively(
int row)
544 auto collapseHelp = [
this] (
const auto collapseHelp,
const QModelIndex &index) ->
void {
545 if (m_expandedItems.contains(index)) {
546 const int rowToCollapse = itemIndex(index);
547 if (rowToCollapse != -1)
548 collapseRow(rowToCollapse);
550 m_expandedItems.remove(index);
553 const int childCount = m_model->rowCount(index);
554 for (
int childRow = 0; childRow < childCount; ++childRow) {
555 const QModelIndex childIndex = m_model->index(childRow, 0, index);
556 if (m_model->hasChildren(childIndex))
557 collapseHelp(collapseHelp, childIndex);
561 const QModelIndex index = m_items[row].index;
563 collapseHelp(collapseHelp, index);
566void QQmlTreeModelToTableModel::collapseRow(
int n)
568 if (!m_model || !isExpanded(n))
571 SignalFreezer aggregator(
this);
573 TreeItem &item = m_items[n];
574 item.expanded =
false;
575 m_expandedItems.remove(item.index);
576 queueDataChanged(n, n, {ExpandedRole});
577 int childrenCount = m_model->rowCount(item.index);
578 if ((item.index.flags() & Qt::ItemNeverHasChildren) || !m_model->hasChildren(item.index) || childrenCount == 0)
581 const QModelIndex &emi = m_model->index(childrenCount - 1, 0, item.index);
582 int lastIndex = lastChildIndex(emi);
583 removeVisibleRows(n + 1, lastIndex);
586int QQmlTreeModelToTableModel::lastChildIndex(
const QModelIndex &index)
const
593 if (!m_expandedItems.contains(index))
594 return itemIndex(index);
596 QModelIndex parent = index.parent();
597 QModelIndex nextSiblingIndex;
598 while (parent.isValid()) {
599 nextSiblingIndex = parent.sibling(parent.row() + 1, 0);
600 if (nextSiblingIndex.isValid())
602 parent = parent.parent();
605 int firstIndex = nextSiblingIndex.isValid() ? itemIndex(nextSiblingIndex) : m_items.size();
606 return firstIndex - 1;
609void QQmlTreeModelToTableModel::removeVisibleRows(
int startIndex,
int endIndex,
bool doRemoveRows)
611 if (startIndex < 0 || endIndex < 0 || startIndex > endIndex)
615 beginRemoveRows(QModelIndex(), startIndex, endIndex);
616 m_items.erase(m_items.begin() + startIndex, m_items.begin() + endIndex + 1);
621 int lastIndex = m_items.size() - 1;
622 if (startIndex <= lastIndex)
623 queueDataChanged(startIndex, lastIndex, {ModelIndexRole});
627void QQmlTreeModelToTableModel::modelHasBeenDestroyed()
631 emit modelChanged(
nullptr);
634void QQmlTreeModelToTableModel::modelHasBeenReset()
638 showModelTopLevelItems();
642void QQmlTreeModelToTableModel::modelDataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight,
const QList<
int> &roles)
644 Q_ASSERT(topLeft.parent() == bottomRight.parent());
645 const QModelIndex &parent = topLeft.parent();
646 if (parent.isValid() && !childrenVisible(parent)) {
651 int topIndex = itemIndex(topLeft.siblingAtColumn(0));
654 for (
int i = topLeft.row(); i <= bottomRight.row(); i++) {
656 int bottomIndex = topIndex;
657 while (bottomIndex < m_items.size()) {
658 const QModelIndex &idx = m_items.at(bottomIndex).index;
659 if (idx.parent() != parent) {
663 if (idx.row() == bottomRight.row())
667 emit dataChanged(index(topIndex, topLeft.column()), index(bottomIndex, bottomRight.column()), roles);
669 i += bottomIndex - topIndex;
670 if (i == bottomRight.row())
672 topIndex = bottomIndex + 1;
673 while (topIndex < m_items.size()
674 && m_items.at(topIndex).index.parent() != parent)
680void QQmlTreeModelToTableModel::modelLayoutAboutToBeChanged(
const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint)
693 m_modelLayoutChanged =
false;
695 if (parents.isEmpty() || !parents[0].isValid()) {
697 emit layoutAboutToBeChanged();
698 m_modelLayoutChanged =
true;
703 for (
const QPersistentModelIndex &pmi : parents) {
704 if (!m_expandedItems.contains(pmi))
706 const int row = itemIndex(pmi);
709 const int rowCount = m_model->rowCount(pmi);
713 if (!m_modelLayoutChanged) {
714 emit layoutAboutToBeChanged();
715 m_modelLayoutChanged =
true;
718 const QModelIndex &lmi = m_model->index(rowCount - 1, 0, pmi);
719 const int lastRow = lastChildIndex(lmi);
720 removeVisibleRows(row + 1, lastRow,
false );
726void QQmlTreeModelToTableModel::modelLayoutChanged(
const QList<QPersistentModelIndex> &parents, QAbstractItemModel::LayoutChangeHint hint)
730 if (!m_modelLayoutChanged) {
735 if (m_items.isEmpty()) {
737 showModelTopLevelItems(
false );
738 const QModelIndex &mi = m_model->index(0, 0);
739 const int columnCount = m_model->columnCount(mi);
740 emit dataChanged(index(0, 0), index(m_items.size() - 1, columnCount - 1));
741 emit layoutChanged();
745 for (
const QPersistentModelIndex &pmi : parents) {
746 if (!m_expandedItems.contains(pmi))
748 const int row = itemIndex(pmi);
751 const int rowCount = m_model->rowCount(pmi);
755 const QModelIndex &lmi = m_model->index(rowCount - 1, 0, pmi);
756 const int columnCount = m_model->columnCount(lmi);
757 showModelChildItems(m_items.at(row), 0, rowCount - 1,
false );
758 const int lastRow = lastChildIndex(lmi);
759 emit dataChanged(index(row + 1, 0), index(lastRow, columnCount - 1));
762 emit layoutChanged();
767void QQmlTreeModelToTableModel::modelRowsAboutToBeInserted(
const QModelIndex & parent,
int start,
int end)
775void QQmlTreeModelToTableModel::modelRowsInserted(
const QModelIndex & parent,
int start,
int end)
778 int parentRow = itemIndex(parent);
779 if (parentRow >= 0) {
780 const bool isExpanded = m_items.at(parentRow).expanded;
781 queueDataChanged(parentRow, parentRow, {HasChildrenRole});
782 item = m_items.at(parentRow);
786 if ((!isExpanded && item.expanded) || !item.expanded) {
790 }
else if (parent == m_rootIndex) {
791 item = TreeItem(parent);
796 showModelChildItems(item, start, end);
800void QQmlTreeModelToTableModel::modelRowsAboutToBeRemoved(
const QModelIndex & parent,
int start,
int end)
803 enableSignalAggregation();
804 if (parent == m_rootIndex || childrenVisible(parent)) {
805 const QModelIndex &smi = m_model->index(start, 0, parent);
806 int startIndex = itemIndex(smi);
807 const QModelIndex &emi = m_model->index(end, 0, parent);
809 if (isExpanded(emi)) {
810 int rowCount = m_model->rowCount(emi);
812 const QModelIndex &idx = m_model->index(rowCount - 1, 0, emi);
813 endIndex = lastChildIndex(idx);
817 endIndex = itemIndex(emi);
819 removeVisibleRows(startIndex, endIndex);
822 for (
int r = start; r <= end; r++) {
823 const QModelIndex &cmi = m_model->index(r, 0, parent);
824 m_expandedItems.remove(cmi);
828void QQmlTreeModelToTableModel::modelRowsRemoved(
const QModelIndex & parent,
int start,
int end)
832 int parentRow = itemIndex(parent);
834 queueDataChanged(parentRow, parentRow, {HasChildrenRole});
835 disableSignalAggregation();
839void QQmlTreeModelToTableModel::modelRowsAboutToBeMoved(
const QModelIndex & sourceParent,
int sourceStart,
int sourceEnd,
const QModelIndex & destinationParent,
int destinationRow)
842 enableSignalAggregation();
843 m_visibleRowsMoved =
false;
844 if (!childrenVisible(sourceParent))
847 if (!childrenVisible(destinationParent)) {
848 modelRowsAboutToBeRemoved(sourceParent, sourceStart, sourceEnd);
850
851 if (isVisible(destinationParent) && m_model->rowCount(destinationParent) == 0) {
852 const int parentRow = itemIndex(destinationParent);
853 queueDataChanged(parentRow, parentRow, {HasChildrenRole});
856 int depthDifference = -1;
857 if (destinationParent.isValid()) {
858 int destParentIndex = itemIndex(destinationParent);
859 depthDifference = m_items.at(destParentIndex).depth;
861 if (sourceParent.isValid()) {
862 int sourceParentIndex = itemIndex(sourceParent);
863 depthDifference -= m_items.at(sourceParentIndex).depth;
868 int startIndex = itemIndex(m_model->index(sourceStart, 0, sourceParent));
869 const QModelIndex &emi = m_model->index(sourceEnd, 0, sourceParent);
871 if (isExpanded(emi)) {
872 int rowCount = m_model->rowCount(emi);
874 endIndex = lastChildIndex(m_model->index(rowCount - 1, 0, emi));
877 endIndex = itemIndex(emi);
880 if (destinationRow == m_model->rowCount(destinationParent)) {
881 const QModelIndex &emi = m_model->index(destinationRow - 1, 0, destinationParent);
882 destIndex = lastChildIndex(emi) + 1;
884 destIndex = itemIndex(m_model->index(destinationRow, 0, destinationParent));
887 int totalMovedCount = endIndex - startIndex + 1;
890
891 m_visibleRowsMoved = startIndex != destIndex &&
892 beginMoveRows(QModelIndex(), startIndex, endIndex, QModelIndex(), destIndex);
894 const QList<TreeItem> &buffer = m_items.mid(startIndex, totalMovedCount);
895 int bufferCopyOffset;
896 if (destIndex > endIndex) {
897 for (
int i = endIndex + 1; i < destIndex; i++) {
898 m_items.swapItemsAt(i, i - totalMovedCount);
900 bufferCopyOffset = destIndex - totalMovedCount;
903 for (
int i = startIndex - 1; i >= destIndex; i--) {
904 m_items.swapItemsAt(i, i + totalMovedCount);
906 bufferCopyOffset = destIndex;
908 for (
int i = 0; i < buffer.size(); i++) {
909 TreeItem item = buffer.at(i);
910 item.depth += depthDifference;
911 m_items.replace(bufferCopyOffset + i, item);
915
916
917
918
919
920
921
922
923
924 const int top = qMin(startIndex, bufferCopyOffset);
925 int bottom = qMax(endIndex, bufferCopyOffset + totalMovedCount - 1);
926 if (sourceParent != destinationParent) {
927 const QModelIndex &bottomParent =
928 bottom == endIndex ? sourceParent : destinationParent;
930 const int rowCount = m_model->rowCount(bottomParent);
932 bottom = qMax(bottom, lastChildIndex(m_model->index(rowCount - 1, 0, bottomParent)));
934 queueDataChanged(top, bottom, {ModelIndexRole});
936 if (depthDifference != 0)
937 queueDataChanged(bufferCopyOffset, bufferCopyOffset + totalMovedCount - 1, {DepthRole});
941void QQmlTreeModelToTableModel::modelRowsMoved(
const QModelIndex & sourceParent,
int sourceStart,
int sourceEnd,
const QModelIndex & destinationParent,
int destinationRow)
943 if (!childrenVisible(sourceParent)) {
944 modelRowsInserted(destinationParent, destinationRow, destinationRow + sourceEnd - sourceStart);
945 }
else if (!childrenVisible(destinationParent)) {
946 modelRowsRemoved(sourceParent, sourceStart, sourceEnd);
949 if (m_visibleRowsMoved)
952 if (isVisible(sourceParent) && m_model->rowCount(sourceParent) == 0) {
953 int parentRow = itemIndex(sourceParent);
954 collapseRow(parentRow);
955 queueDataChanged(parentRow, parentRow, {ExpandedRole, HasChildrenRole});
958 disableSignalAggregation();
963void QQmlTreeModelToTableModel::modelColumnsAboutToBeInserted(
const QModelIndex & parent,
int start,
int end)
966 beginInsertColumns({}, start, end);
969void QQmlTreeModelToTableModel::modelColumnsAboutToBeRemoved(
const QModelIndex & parent,
int start,
int end)
972 beginRemoveColumns({}, start, end);
975void QQmlTreeModelToTableModel::modelColumnsInserted(
const QModelIndex & parent,
int start,
int end)
982 showModelTopLevelItems();
986void QQmlTreeModelToTableModel::modelColumnsRemoved(
const QModelIndex & parent,
int start,
int end)
993 showModelTopLevelItems();
997void QQmlTreeModelToTableModel::dump()
const
1001 int count = m_items.size();
1004 int countWidth = floor(log10(
double(count))) + 1;
1005 qInfo() <<
"Dumping" <<
this;
1006 for (
int i = 0; i < count; i++) {
1007 const TreeItem &item = m_items.at(i);
1008 bool hasChildren = m_model->hasChildren(item.index);
1009 int children = m_model->rowCount(item.index);
1010 qInfo().noquote().nospace()
1011 << QStringLiteral(
"%1 ").arg(i, countWidth) << QString(4 * item.depth, QChar::fromLatin1(
'.'))
1012 << QLatin1String(!hasChildren ?
".. " : item.expanded ?
" v " :
" > ")
1013 << item.index << children;
1017bool QQmlTreeModelToTableModel::testConsistency(
bool dumpOnFail)
const
1020 if (!m_items.isEmpty()) {
1021 qWarning() <<
"Model inconsistency: No model but stored visible items";
1024 if (!m_expandedItems.isEmpty()) {
1025 qWarning() <<
"Model inconsistency: No model but stored expanded items";
1030 QModelIndex parent = m_rootIndex;
1031 QStack<QModelIndex> ancestors;
1032 QModelIndex idx = m_model->index(0, 0, parent);
1033 for (
int i = 0; i < m_items.size(); i++) {
1034 bool isConsistent =
true;
1035 const TreeItem &item = m_items.at(i);
1036 if (item.index != idx) {
1037 qWarning() <<
"QModelIndex inconsistency" << i << item.index;
1038 qWarning() <<
" expected" << idx;
1039 isConsistent =
false;
1041 if (item.index.parent() != parent) {
1042 qWarning() <<
"Parent inconsistency" << i << item.index;
1043 qWarning() <<
" stored index parent" << item.index.parent() <<
"model parent" << parent;
1044 isConsistent =
false;
1046 if (item.depth != ancestors.size()) {
1047 qWarning() <<
"Depth inconsistency" << i << item.index;
1048 qWarning() <<
" item depth" << item.depth <<
"ancestors stack" << ancestors.size();
1049 isConsistent =
false;
1051 if (item.expanded && !m_expandedItems.contains(item.index)) {
1052 qWarning() <<
"Expanded inconsistency" << i << item.index;
1053 qWarning() <<
" set" << m_expandedItems.contains(item.index) <<
"item" << item.expanded;
1054 isConsistent =
false;
1056 if (!isConsistent) {
1061 QModelIndex firstChildIndex;
1063 firstChildIndex = m_model->index(0, 0, idx);
1064 if (firstChildIndex.isValid()) {
1065 ancestors.push(parent);
1067 idx = m_model->index(0, 0, parent);
1069 while (idx.row() == m_model->rowCount(parent) - 1) {
1070 if (ancestors.isEmpty())
1073 parent = ancestors.pop();
1075 idx = m_model->index(idx.row() + 1, 0, parent);
1082void QQmlTreeModelToTableModel::enableSignalAggregation() {
1083 m_signalAggregatorStack++;
1086void QQmlTreeModelToTableModel::disableSignalAggregation() {
1087 m_signalAggregatorStack--;
1088 Q_ASSERT(m_signalAggregatorStack >= 0);
1089 if (m_signalAggregatorStack == 0) {
1090 emitQueuedSignals();
1094void QQmlTreeModelToTableModel::queueDataChanged(
int top,
int bottom,
1095 std::initializer_list<
int> roles)
1097 if (isAggregatingSignals())
1098 m_queuedDataChanged.append(DataChangedParams { top, bottom, roles });
1100 emit dataChanged(index(top, 0), index(bottom, 0), roles);
1103void QQmlTreeModelToTableModel::emitQueuedSignals()
1105 QVarLengthArray<DataChangedParams> combinedUpdates;
1107
1108
1109
1110
1111 for (
const DataChangedParams &dataChange : std::as_const(m_queuedDataChanged)) {
1112 const int startRow = dataChange.top;
1113 const int endRow = dataChange.bottom;
1114 bool merged =
false;
1115 for (DataChangedParams &combined : combinedUpdates) {
1116 int combinedStartRow = combined.top;
1117 int combinedEndRow = combined.bottom;
1118 if ((startRow <= combinedStartRow && endRow >= combinedStartRow) ||
1119 (startRow <= combinedEndRow && endRow >= combinedEndRow)) {
1120 if (startRow < combinedStartRow) {
1121 combined.top = dataChange.top;
1123 if (endRow > combinedEndRow) {
1124 combined.bottom = dataChange.bottom;
1126 for (
int role : dataChange.roles) {
1127 if (!combined.roles.contains(role))
1128 combined.roles.append(role);
1135 combinedUpdates.append(dataChange);
1140 for (
const DataChangedParams &dataChange : combinedUpdates) {
1141 const QModelIndex topLeft = index(dataChange.top, 0);
1142 const QModelIndex bottomRight = index(dataChange.bottom, 0);
1143 emit dataChanged(topLeft, bottomRight, {dataChange.roles.begin(), dataChange.roles.end()});
1145 m_queuedDataChanged.clear();
1150#include "moc_qqmltreemodeltotablemodel_p_p.cpp"
Combined button and popup list for selecting options.
#define ASSERT_CONSISTENCY