From ff7c0776d68d9749b07f54a3e26c342dfb12f081 Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Sat, 9 Aug 2008 16:38:00 +0200 Subject: [PATCH] fixing segfaults on part if the part results in an empty category (BR #232) --- src/client/treemodel.cpp | 28 ++++++++++++++++++++++++++++ src/client/treemodel.h | 8 ++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/client/treemodel.cpp b/src/client/treemodel.cpp index 3b18d036..4933f0d9 100644 --- a/src/client/treemodel.cpp +++ b/src/client/treemodel.cpp @@ -24,6 +24,15 @@ #include #include +class RemoveChildLaterEvent : public QEvent { +public: + RemoveChildLaterEvent(AbstractTreeItem *child) : QEvent(QEvent::User), _child(child) {}; + inline AbstractTreeItem *child() { return _child; } +private: + AbstractTreeItem *_child; +}; + + /***************************************** * Abstract Items of a TreeModel *****************************************/ @@ -101,6 +110,25 @@ void AbstractTreeItem::removeAllChilds() { checkForDeletion(); } +void AbstractTreeItem::removeChildLater(AbstractTreeItem *child) { + Q_ASSERT(child); + QCoreApplication::postEvent(this, new RemoveChildLaterEvent(child)); +} + +void AbstractTreeItem::customEvent(QEvent *event) { + if(event->type() != QEvent::User) + return; + + event->accept(); + + RemoveChildLaterEvent *removeEvent = static_cast(event); + int childRow = _childItems.indexOf(removeEvent->child()); + if(childRow == -1) + return; + + removeChild(childRow); +} + bool AbstractTreeItem::reParent(AbstractTreeItem *newParent) { // currently we support only re parenting if the child that's about to be // adopted does not have any children itself. diff --git a/src/client/treemodel.h b/src/client/treemodel.h index cc602de7..e4b5ca3a 100644 --- a/src/client/treemodel.h +++ b/src/client/treemodel.h @@ -79,13 +79,17 @@ signals: void beginRemoveChilds(int firstRow, int lastRow); void endRemoveChilds(); - + +protected: + void customEvent(QEvent *event); + private: QList _childItems; Qt::ItemFlags _flags; TreeItemFlags _treeItemFlags; - inline void checkForDeletion() { if(treeItemFlags() & DeleteOnLastChildRemoved && childCount() == 0) parent()->removeChild(this); } + void removeChildLater(AbstractTreeItem *child); + inline void checkForDeletion() { if(treeItemFlags() & DeleteOnLastChildRemoved && childCount() == 0) parent()->removeChildLater(this); } }; -- 2.20.1