+Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
+{
+ if (!index.isValid()) {
+ return rootItem->flags() & Qt::ItemIsDropEnabled;
+ }
+ else {
+ AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
+ return item->flags();
+ }
+}
+
+
+QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+ return rootItem->data(section, role);
+ else
+ return QVariant();
+}
+
+
+void TreeModel::itemDataChanged(int column)
+{
+ AbstractTreeItem *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, SIGNAL(dataChanged(int)),
+ this, SLOT(itemDataChanged(int)));
+
+ connect(item, SIGNAL(beginAppendChilds(int, int)),
+ this, SLOT(beginAppendChilds(int, int)));
+ connect(item, SIGNAL(endAppendChilds()),
+ this, SLOT(endAppendChilds()));
+
+ connect(item, SIGNAL(beginRemoveChilds(int, int)),
+ this, SLOT(beginRemoveChilds(int, int)));
+ connect(item, SIGNAL(endRemoveChilds()),
+ this, SLOT(endRemoveChilds()));
+}
+
+
+void TreeModel::beginAppendChilds(int firstRow, int lastRow)
+{
+ AbstractTreeItem *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()
+{
+ AbstractTreeItem *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);
+#endif
+ Q_ASSERT(cs.parent == parent);
+ Q_ASSERT(rowCount(parent) == cs.childCount + cs.end - cs.start + 1);
+
+ _aboutToRemoveOrInsert = false;
+ for (int i = cs.start; i <= cs.end; i++) {
+ connectItem(parentItem->child(i));
+ }
+ endInsertRows();
+}
+
+
+void TreeModel::beginRemoveChilds(int firstRow, int lastRow)
+{
+ AbstractTreeItem *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), 0, this, 0);
+ }
+
+ // 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()
+{
+ AbstractTreeItem *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);
+#endif
+ Q_ASSERT(cs.parent == parent);
+ Q_ASSERT(rowCount(parent) == cs.childCount - cs.end + cs.start - 1);
+ _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();
+ }