+void TreeModel::itemDataChanged(int column)
+{
+ auto *item = qobject_cast<AbstractTreeItem *>(sender());
+ QModelIndex leftIndex, rightIndex;
+
+ if (item == rootItem)
+ return;
+
+ if (column == -1) {
+ leftIndex = createIndex(item->row(), 0, item);
+ rightIndex = createIndex(item->row(), item->columnCount() - 1, item);
+ }
+ else {
+ leftIndex = createIndex(item->row(), column, item);
+ rightIndex = leftIndex;
+ }
+
+ emit dataChanged(leftIndex, rightIndex);
+}
+
+
+void TreeModel::connectItem(AbstractTreeItem *item)
+{
+ connect(item, &AbstractTreeItem::dataChanged,
+ this, &TreeModel::itemDataChanged);
+
+ connect(item, &AbstractTreeItem::beginAppendChilds,
+ this, &TreeModel::beginAppendChilds);
+ connect(item, &AbstractTreeItem::endAppendChilds,
+ this, &TreeModel::endAppendChilds);
+
+ connect(item, &AbstractTreeItem::beginRemoveChilds,
+ this, &TreeModel::beginRemoveChilds);
+ connect(item, &AbstractTreeItem::endRemoveChilds,
+ this, &TreeModel::endRemoveChilds);
+}
+
+
+void TreeModel::beginAppendChilds(int firstRow, int lastRow)
+{
+ auto *parentItem = qobject_cast<AbstractTreeItem *>(sender());
+ if (!parentItem) {
+ qWarning() << "TreeModel::beginAppendChilds(): cannot append Children to unknown parent";
+ return;
+ }
+
+ QModelIndex parent = indexByItem(parentItem);
+ Q_ASSERT(!_aboutToRemoveOrInsert);
+
+ _aboutToRemoveOrInsert = true;
+ _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
+ beginInsertRows(parent, firstRow, lastRow);
+}
+
+
+void TreeModel::endAppendChilds()
+{
+ auto *parentItem = qobject_cast<AbstractTreeItem *>(sender());
+ if (!parentItem) {
+ qWarning() << "TreeModel::endAppendChilds(): cannot append Children to unknown parent";
+ return;
+ }
+ Q_ASSERT(_aboutToRemoveOrInsert);
+ ChildStatus cs = _childStatus;
+#ifndef QT_NO_DEBUG
+ QModelIndex parent = indexByItem(parentItem);
+ Q_ASSERT(cs.parent == parent);
+ Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
+#endif
+ _aboutToRemoveOrInsert = false;
+ for (int i = cs.start; i <= cs.end; i++) {
+ connectItem(parentItem->child(i));
+ }
+ endInsertRows();
+}
+
+
+void TreeModel::beginRemoveChilds(int firstRow, int lastRow)
+{
+ auto *parentItem = qobject_cast<AbstractTreeItem *>(sender());
+ if (!parentItem) {
+ qWarning() << "TreeModel::beginRemoveChilds(): cannot append Children to unknown parent";
+ return;
+ }
+
+ for (int i = firstRow; i <= lastRow; i++) {
+ disconnect(parentItem->child(i), nullptr, this, nullptr);
+ }
+
+ // consitency checks
+ QModelIndex parent = indexByItem(parentItem);
+ Q_ASSERT(firstRow <= lastRow);
+ Q_ASSERT(parentItem->childCount() > lastRow);
+ Q_ASSERT(!_aboutToRemoveOrInsert);
+ _aboutToRemoveOrInsert = true;
+ _childStatus = ChildStatus(parent, rowCount(parent), firstRow, lastRow);
+
+ beginRemoveRows(parent, firstRow, lastRow);
+}
+
+
+void TreeModel::endRemoveChilds()
+{
+ auto *parentItem = qobject_cast<AbstractTreeItem *>(sender());
+ if (!parentItem) {
+ qWarning() << "TreeModel::endRemoveChilds(): cannot remove Children from unknown parent";
+ return;
+ }
+
+ // concistency checks
+ Q_ASSERT(_aboutToRemoveOrInsert);
+#ifndef QT_NO_DEBUG
+ ChildStatus cs = _childStatus;
+ QModelIndex parent = indexByItem(parentItem);
+ Q_ASSERT(cs.parent == parent);
+ Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
+#endif
+ _aboutToRemoveOrInsert = false;
+
+ endRemoveRows();
+}
+
+
+void TreeModel::clear()
+{
+ rootItem->removeAllChilds();
+}
+
+
+void TreeModel::debug_rowsAboutToBeInserted(const QModelIndex &parent, int start, int end)
+{
+ qDebug() << "debug_rowsAboutToBeInserted" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
+}
+
+
+void TreeModel::debug_rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end)
+{
+ AbstractTreeItem *parentItem;
+ parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
+ if (!parentItem)
+ parentItem = rootItem;
+ qDebug() << "debug_rowsAboutToBeRemoved" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
+
+ QModelIndex child;
+ for (int i = end; i >= start; i--) {
+ child = parent.child(i, 0);
+ Q_ASSERT(parentItem->child(i));
+ qDebug() << ">>>" << i << child << child.data().toString();
+ }
+}
+
+
+void TreeModel::debug_rowsInserted(const QModelIndex &parent, int start, int end)
+{
+ AbstractTreeItem *parentItem;
+ parentItem = static_cast<AbstractTreeItem *>(parent.internalPointer());
+ if (!parentItem)
+ parentItem = rootItem;
+ qDebug() << "debug_rowsInserted:" << parent << parentItem << parent.data().toString() << rowCount(parent) << start << end;
+
+ QModelIndex child;
+ for (int i = start; i <= end; i++) {
+ child = parent.child(i, 0);
+ Q_ASSERT(parentItem->child(i));
+ qDebug() << "<<<" << i << child << child.data().toString();
+ }
+}
+
+
+void TreeModel::debug_rowsRemoved(const QModelIndex &parent, int start, int end)
+{
+ qDebug() << "debug_rowsRemoved" << parent << parent.internalPointer() << parent.data().toString() << rowCount(parent) << start << end;
+}
+
+
+void TreeModel::debug_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
+{
+ qDebug() << "debug_dataChanged" << topLeft << bottomRight;
+ QStringList displayData;
+ for (int row = topLeft.row(); row <= bottomRight.row(); row++) {
+ displayData = QStringList();
+ for (int column = topLeft.column(); column <= bottomRight.column(); column++) {
+ displayData << data(topLeft.sibling(row, column), Qt::DisplayRole).toString();
+ }
+ qDebug() << " row:" << row << displayData;
+ }