[qet] [4353] New elements panel : fix crash at drag and drop due to a wrong use of QAbstractItemModel

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 4353
Author:   blacksun
Date:     2016-02-24 11:43:40 +0100 (Wed, 24 Feb 2016)
Log Message:
-----------
New elements panel : fix crash at drag and drop due to a wrong use of QAbstractItemModel

Modified Paths:
--------------
    trunk/sources/ElementsCollection/elementcollectionitem.cpp
    trunk/sources/ElementsCollection/elementcollectionitem.h
    trunk/sources/ElementsCollection/elementscollectionmodel.cpp
    trunk/sources/ElementsCollection/fileelementcollectionitem.h
    trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.h

Modified: trunk/sources/ElementsCollection/elementcollectionitem.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementcollectionitem.cpp	2016-02-21 18:53:40 UTC (rev 4352)
+++ trunk/sources/ElementsCollection/elementcollectionitem.cpp	2016-02-24 10:43:40 UTC (rev 4353)
@@ -24,6 +24,7 @@
  * @param parent : the parent item of this item
  */
 ElementCollectionItem::ElementCollectionItem(ElementCollectionItem *parent) :
+	QObject(parent),
 	m_parent_item (parent)
 {}
 
@@ -52,14 +53,21 @@
  */
 bool ElementCollectionItem::removeChild(int row, int count)
 {
-	if (!(0 <= row+count  && row+count <= m_child_items.size())) return false;
+	if (!(1 <= row+count  && row+count <= m_child_items.size())) return false;
 
+	int last_ = row + (count-1);
+	if (last_ < row) return false;
+
+	emit beginRemoveRows(this, row, last_);
+
 	for (int i=0 ; i<count ; i++)
 	{
 		ElementCollectionItem *eci = m_child_items.takeAt(row);
 		delete eci;
 	}
 
+	emit endRemoveRows();
+
 	return true;
 }
 
@@ -74,7 +82,9 @@
 {
 	if (m_child_items.contains(item)) return false;
 
+	beginInsertRows(this, row, row);
 	m_child_items.insert(row, item);
+	endInsertRows();
 	return true;
 }
 

Modified: trunk/sources/ElementsCollection/elementcollectionitem.h
===================================================================
--- trunk/sources/ElementsCollection/elementcollectionitem.h	2016-02-21 18:53:40 UTC (rev 4352)
+++ trunk/sources/ElementsCollection/elementcollectionitem.h	2016-02-24 10:43:40 UTC (rev 4353)
@@ -31,8 +31,10 @@
  * This class must be herited for specialisation.
  * This item is used by ElementsCollectionModel for manage the elements collection
  */
-class ElementCollectionItem
+class ElementCollectionItem : public QObject
 {
+		Q_OBJECT
+
     public:
         ElementCollectionItem(ElementCollectionItem *parent = nullptr);
 		virtual ~ElementCollectionItem();
@@ -72,6 +74,12 @@
 		virtual bool canRemoveContent();
 		virtual bool removeContent();
 
+	signals:
+		void beginInsertRows(ElementCollectionItem *parent, int first, int last);
+		void endInsertRows();
+		void beginRemoveRows(ElementCollectionItem *parent, int first, int last);
+		void endRemoveRows();
+
 	protected:
         ElementCollectionItem *m_parent_item;
         QList <ElementCollectionItem *> m_child_items;

Modified: trunk/sources/ElementsCollection/elementscollectionmodel.cpp
===================================================================
--- trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-02-21 18:53:40 UTC (rev 4352)
+++ trunk/sources/ElementsCollection/elementscollectionmodel.cpp	2016-02-24 10:43:40 UTC (rev 4353)
@@ -149,7 +149,7 @@
 	else
 		eci = static_cast<ElementCollectionItem *>(parent.internalPointer());
 
-	if (!(0 <= row+count && row+count <= eci->childCount())) return false;
+	if (!(1 <= row+count && row+count <= eci->childCount())) return false;
 
 	beginRemoveRows(parent, row, (row + count -1));
 	bool r = eci->removeChild(row, count);
@@ -205,19 +205,16 @@
 {
 	if (!parent.isValid()) return false;
 
-	ElementLocation location(data);
-	if (location.isNull()) return false;
-
 	ElementCollectionItem *eci =  static_cast<ElementCollectionItem*> (parent.internalPointer());
-	if (!eci) return false;
-	if (eci->isElement()) eci = eci->parent();
+	if (!eci || eci->isElement()) return false;
 
-	int i = eci->rowForInsertItem(location.fileName());
-	if (i < 0) return false;
+	connect(eci, &ElementCollectionItem::beginInsertRows, [this, &parent](ElementCollectionItem *eci, int first, int last){ Q_UNUSED(eci); this->beginInsertRows(parent, first, last); });
+	connect(eci, &ElementCollectionItem::endInsertRows,   [this, &parent](){ this->endInsertRows(); });
+	connect(eci, &ElementCollectionItem::beginRemoveRows, [this, &parent](ElementCollectionItem *eci, int first, int last){ Q_UNUSED(eci); this->beginRemoveRows(parent, first, last); });
+	connect(eci, &ElementCollectionItem::endRemoveRows,   [this, &parent](){ this->endRemoveRows(); });
 
-	beginInsertRows(parent, i, i);
 	bool rb = eci->dropMimeData(data, action, row, column);
-	endInsertRows();
+
 	return rb;
 }
 

Modified: trunk/sources/ElementsCollection/fileelementcollectionitem.h
===================================================================
--- trunk/sources/ElementsCollection/fileelementcollectionitem.h	2016-02-21 18:53:40 UTC (rev 4352)
+++ trunk/sources/ElementsCollection/fileelementcollectionitem.h	2016-02-24 10:43:40 UTC (rev 4353)
@@ -30,6 +30,8 @@
  */
 class FileElementCollectionItem : public ElementCollectionItem
 {
+		Q_OBJECT
+
     public:
 		FileElementCollectionItem(ElementCollectionItem *parent = nullptr);
         ~FileElementCollectionItem();

Modified: trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.h
===================================================================
--- trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.h	2016-02-21 18:53:40 UTC (rev 4352)
+++ trunk/sources/ElementsCollection/xmlprojectelementcollectionitem.h	2016-02-24 10:43:40 UTC (rev 4353)
@@ -31,6 +31,8 @@
  */
 class XmlProjectElementCollectionItem : public ElementCollectionItem
 {
+		Q_OBJECT
+
     public:
         XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent = nullptr);
 	private:


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/