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

git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@4353 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
blacksun
2016-02-24 10:43:40 +00:00
parent 014f3c8f13
commit a35ce68d7a
5 changed files with 31 additions and 12 deletions

View File

@@ -24,6 +24,7 @@
* @param parent : the parent item of this item * @param parent : the parent item of this item
*/ */
ElementCollectionItem::ElementCollectionItem(ElementCollectionItem *parent) : ElementCollectionItem::ElementCollectionItem(ElementCollectionItem *parent) :
QObject(parent),
m_parent_item (parent) m_parent_item (parent)
{} {}
@@ -52,7 +53,12 @@ void ElementCollectionItem::appendChild(ElementCollectionItem *item) {
*/ */
bool ElementCollectionItem::removeChild(int row, int count) 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++) for (int i=0 ; i<count ; i++)
{ {
@@ -60,6 +66,8 @@ bool ElementCollectionItem::removeChild(int row, int count)
delete eci; delete eci;
} }
emit endRemoveRows();
return true; return true;
} }
@@ -74,7 +82,9 @@ bool ElementCollectionItem::insertChild(int row, ElementCollectionItem *item)
{ {
if (m_child_items.contains(item)) return false; if (m_child_items.contains(item)) return false;
beginInsertRows(this, row, row);
m_child_items.insert(row, item); m_child_items.insert(row, item);
endInsertRows();
return true; return true;
} }

View File

@@ -31,8 +31,10 @@ class QList<ElementCollectionItem>;
* This class must be herited for specialisation. * This class must be herited for specialisation.
* This item is used by ElementsCollectionModel for manage the elements collection * This item is used by ElementsCollectionModel for manage the elements collection
*/ */
class ElementCollectionItem class ElementCollectionItem : public QObject
{ {
Q_OBJECT
public: public:
ElementCollectionItem(ElementCollectionItem *parent = nullptr); ElementCollectionItem(ElementCollectionItem *parent = nullptr);
virtual ~ElementCollectionItem(); virtual ~ElementCollectionItem();
@@ -72,6 +74,12 @@ class ElementCollectionItem
virtual bool canRemoveContent(); virtual bool canRemoveContent();
virtual bool removeContent(); 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: protected:
ElementCollectionItem *m_parent_item; ElementCollectionItem *m_parent_item;
QList <ElementCollectionItem *> m_child_items; QList <ElementCollectionItem *> m_child_items;

View File

@@ -149,7 +149,7 @@ bool ElementsCollectionModel::removeRows(int row, int count, const QModelIndex &
else else
eci = static_cast<ElementCollectionItem *>(parent.internalPointer()); 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)); beginRemoveRows(parent, row, (row + count -1));
bool r = eci->removeChild(row, count); bool r = eci->removeChild(row, count);
@@ -205,19 +205,16 @@ bool ElementsCollectionModel::dropMimeData(const QMimeData *data, Qt::DropAction
{ {
if (!parent.isValid()) return false; if (!parent.isValid()) return false;
ElementLocation location(data);
if (location.isNull()) return false;
ElementCollectionItem *eci = static_cast<ElementCollectionItem*> (parent.internalPointer()); ElementCollectionItem *eci = static_cast<ElementCollectionItem*> (parent.internalPointer());
if (!eci) return false; if (!eci || eci->isElement()) return false;
if (eci->isElement()) eci = eci->parent();
int i = eci->rowForInsertItem(location.fileName()); connect(eci, &ElementCollectionItem::beginInsertRows, [this, &parent](ElementCollectionItem *eci, int first, int last){ Q_UNUSED(eci); this->beginInsertRows(parent, first, last); });
if (i < 0) return false; 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); bool rb = eci->dropMimeData(data, action, row, column);
endInsertRows();
return rb; return rb;
} }

View File

@@ -30,6 +30,8 @@
*/ */
class FileElementCollectionItem : public ElementCollectionItem class FileElementCollectionItem : public ElementCollectionItem
{ {
Q_OBJECT
public: public:
FileElementCollectionItem(ElementCollectionItem *parent = nullptr); FileElementCollectionItem(ElementCollectionItem *parent = nullptr);
~FileElementCollectionItem(); ~FileElementCollectionItem();

View File

@@ -31,6 +31,8 @@ class QETProject;
*/ */
class XmlProjectElementCollectionItem : public ElementCollectionItem class XmlProjectElementCollectionItem : public ElementCollectionItem
{ {
Q_OBJECT
public: public:
XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent = nullptr); XmlProjectElementCollectionItem(QETProject *project, ElementCollectionItem *parent = nullptr);
private: private: