-
- AbstractTreeItem *childItem = parentItem->childById(parent.column(), id);
-
- if(childItem)
- return createIndex(childItem->row(), 0, childItem);
- else
- return QModelIndex();
-}
-
-QModelIndex TreeModel::indexByItem(AbstractTreeItem *item) const {
- if(item == 0) {
- qWarning() << "TreeModel::indexByItem(AbstractTreeItem *item) received NULL-Pointer";
- return QModelIndex();
- }
-
- if(item == rootItem)
- return QModelIndex();
- else
- return createIndex(item->row(), 0, item);
-
-}
-
-QModelIndex TreeModel::parent(const QModelIndex &index) const {
- if(!index.isValid())
- return QModelIndex();
-
- AbstractTreeItem *childItem = static_cast<AbstractTreeItem *>(index.internalPointer());
- AbstractTreeItem *parentItem = static_cast<AbstractTreeItem *>(childItem->parent());
-
- if(parentItem == rootItem)
- return QModelIndex();
-
- return createIndex(parentItem->row(), 0, parentItem);
-}
-
-int TreeModel::rowCount(const QModelIndex &parent) const {
- AbstractTreeItem *parentItem;
- if(!parent.isValid())
- parentItem = rootItem;
- else
- parentItem = static_cast<AbstractTreeItem*>(parent.internalPointer());
-
- return parentItem->childCount(parent.column());
-}
-
-int TreeModel::columnCount(const QModelIndex &parent) const {
- Q_UNUSED(parent)
- // since there the Qt Views don't draw more columns than the header has columns
- // we can be lazy and simply return the count of header columns
- // actually this gives us more freedom cause we don't have to ensure that a rows parent
- // has equal or more columns than that row
-
-// if(parent.isValid()) {
-// AbstractTreeItem *child;
-// if(child = static_cast<AbstractTreeItem *>(parent.internalPointer())->child(parent.column(), parent.row()))
-// return child->columnCount();
-// else
-// return static_cast<AbstractTreeItem*>(parent.internalPointer())->columnCount();
-// } else {
-// return rootItem->columnCount();
-// }
-
- return rootItem->columnCount();
-}
-
-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);
-}
-
-Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const {
- AbstractTreeItem *item;
- if(!index.isValid())
- item = rootItem;
- else
- 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(), item);
- } else {
- leftIndex = createIndex(item->row(), column, item);
- rightIndex = leftIndex;
- }
-
- emit dataChanged(leftIndex, rightIndex);
-}
-
-void TreeModel::appendChild(AbstractTreeItem *parent, AbstractTreeItem *child) {
- if(parent == 0 or child == 0) {
- qWarning() << "TreeModel::appendChild(parent, child) parent and child have to be valid pointers!" << parent << child;
- return;
- }
-
- int nextRow = parent->childCount();
- beginInsertRows(indexByItem(parent), nextRow, nextRow);
- parent->appendChild(child);
- endInsertRows();
-
- connect(child, SIGNAL(dataChanged(int)),
- this, SLOT(itemDataChanged(int)));
-
- connect(child, SIGNAL(newChild(AbstractTreeItem *)),
- this, SLOT(newChild(AbstractTreeItem *)));
-
- connect(child, SIGNAL(childDestroyed(int)),
- this, SLOT(childDestroyed(int)));
-}
-
-void TreeModel::newChild(AbstractTreeItem *child) {
- appendChild(static_cast<AbstractTreeItem *>(sender()), child);