X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fclient%2Ftreemodel.cpp;h=6dd7626ad419f87e251cd7435c852378876754d8;hp=d84f0e018e0090645090f47b421b8f56fc7b2c43;hb=012df68ce8a743a71bfe3beda529a21c02daddb6;hpb=7f22197b5318ccbe7f0b6b644127a71c025ca917 diff --git a/src/client/treemodel.cpp b/src/client/treemodel.cpp index d84f0e01..6dd7626a 100644 --- a/src/client/treemodel.cpp +++ b/src/client/treemodel.cpp @@ -38,16 +38,35 @@ TreeItem::~TreeItem() { qDeleteAll(childItems); } +uint TreeItem::id() const { + return (uint)this; +} + void TreeItem::appendChild(TreeItem *item) { childItems.append(item); + childHash[item->id()] = item; } void TreeItem::removeChild(int row) { + if(row >= childItems.size()) + return; + TreeItem *treeitem = childItems.value(row); childItems.removeAt(row); + childHash.remove(childHash.key(treeitem)); } -TreeItem *TreeItem::child(int row) { - return childItems.value(row); +TreeItem *TreeItem::child(int row) const { + if(row < childItems.size()) + return childItems.value(row); + else + return 0; +} + +TreeItem *TreeItem::childById(const uint &id) const { + if(childHash.contains(id)) + return childHash.value(id); + else + return 0; } int TreeItem::childCount() const { @@ -106,6 +125,21 @@ QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent) con return QModelIndex(); } +QModelIndex TreeModel::indexById(uint id, const QModelIndex &parent) const { + TreeItem *parentItem; + + if(!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast(parent.internalPointer()); + + TreeItem *childItem = parentItem->childById(id); + if(childItem) + return createIndex(childItem->row(), 0, childItem); + else + return QModelIndex(); +} + QModelIndex TreeModel::parent(const QModelIndex &index) const { if(!index.isValid()) return QModelIndex(); @@ -165,8 +199,13 @@ bool TreeModel::removeRow(int row, const QModelIndex &parent) { if(row > rowCount(parent)) return false; + TreeItem *item; + if(!parent.isValid()) + item = rootItem; + else + item = static_cast(parent.internalPointer()); + beginRemoveRows(parent, row, row); - TreeItem *item = static_cast(parent.internalPointer()); item->removeChild(row); endRemoveRows(); return true; @@ -181,8 +220,15 @@ bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) { if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) return false; + TreeItem *item; + if(!parent.isValid()) + item = rootItem; + else + item = static_cast(parent.internalPointer()); + + beginRemoveRows(parent, row, row + count - 1); - TreeItem *item = static_cast(parent.internalPointer()); + for(int i = row + count - 1; i >= 0; i--) { item->removeChild(i); } @@ -191,5 +237,5 @@ bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) { } void TreeModel::clear() { - removeRows(0, rowCount(), QModelIndex()); + removeRows(0, rowCount()); }