Fixing the Bug where a clear of the BufferTreeModel would not result in cleard hashlists.
[quassel.git] / src / client / treemodel.cpp
index 3f26449..6dd7626 100644 (file)
@@ -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<TreeItem *>(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();
@@ -162,10 +196,46 @@ QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int rol
 }
 
 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<TreeItem*>(parent.internalPointer());
+  
   beginRemoveRows(parent, row, row);
-  TreeItem *item = static_cast<TreeItem*>(parent.internalPointer());
   item->removeChild(row);
   endRemoveRows();
   return true;
 }
 
+bool TreeModel::removeRows(int row, int count, const QModelIndex &parent) {
+  // check if there is work to be done
+  if(count == 0)
+    return true;
+
+  // out of range check
+  if(row + count - 1 > rowCount(parent) || row < 0 || count < 0) 
+    return false;
+  
+  TreeItem *item;
+  if(!parent.isValid())
+    item = rootItem;
+  else
+    item = static_cast<TreeItem*>(parent.internalPointer());
+  
+  
+  beginRemoveRows(parent, row, row + count - 1);
+
+  for(int i = row + count - 1; i >= 0; i--) {
+    item->removeChild(i);
+  }
+  endRemoveRows();
+  return true;
+}
+
+void TreeModel::clear() {
+  removeRows(0, rowCount());
+}