-QVariant TreeModel::data(const QModelIndex &index, int role) const {
- if(!index.isValid())
- return QVariant();
-
- AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
- return item->data(index.column(), role);
-}
-
-bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role) {
- if(!index.isValid())
- return false;
-
- AbstractTreeItem *item = static_cast<AbstractTreeItem *>(index.internalPointer());
- return item->setData(index.column(), value, role);
-}
-
-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 Childs 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 Childs to unknown parent";
- return;
- }
- Q_ASSERT(_aboutToRemoveOrInsert);
- ChildStatus cs = _childStatus;
- QModelIndex parent = indexByItem(parentItem);
- 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 Childs 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 Childs from unknown parent";
- return;
- }
-
- // concistency checks
- Q_ASSERT(_aboutToRemoveOrInsert);
- ChildStatus cs = _childStatus;
- QModelIndex parent = indexByItem(parentItem);
- 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;
- AbstractTreeItem *childItem;
- for(int i = end; i >= start; i--) {
- child = parent.child(i, 0);
- childItem = parentItem->child(i);
- Q_ASSERT(childItem);
- qDebug() << ">>>" << i << child << childItem->id() << 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;
- AbstractTreeItem *childItem;
- for(int i = start; i <= end; i++) {
- child = parent.child(i, 0);
- childItem = parentItem->child(i);
- Q_ASSERT(childItem);
- qDebug() << "<<<" << i << child << childItem->id() << 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();