From 0d163379dfe9f6080bc3412e6bbefa9d58f20bf1 Mon Sep 17 00:00:00 2001 From: Lasse Holmstedt <lasse.holmstedt@nokia.com> Date: Thu, 15 Jul 2010 13:38:10 +0200 Subject: [PATCH] Changed handling of selected items to use weak pointers graphics items could be destroyed after selecting something. --- .../editor/abstractformeditortool.cpp | 7 ++--- .../editor/abstractformeditortool.h | 4 +-- .../qmlobserver/qdeclarativedesignview.cpp | 28 ++++++++++++++----- .../qml/qmlobserver/qdeclarativedesignview.h | 5 ++-- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/tools/qml/qmlobserver/editor/abstractformeditortool.cpp b/src/tools/qml/qmlobserver/editor/abstractformeditortool.cpp index b508b7eea38..c939cc7b799 100644 --- a/src/tools/qml/qmlobserver/editor/abstractformeditortool.cpp +++ b/src/tools/qml/qmlobserver/editor/abstractformeditortool.cpp @@ -60,15 +60,14 @@ QGraphicsScene* AbstractFormEditorTool::scene() const return view()->scene(); } -void AbstractFormEditorTool::setItems(const QList<QGraphicsItem*> &itemList) +void AbstractFormEditorTool::updateSelectedItems() { - m_itemList = itemList; - selectedItemsChanged(m_itemList); + selectedItemsChanged(items()); } QList<QGraphicsItem*> AbstractFormEditorTool::items() const { - return m_itemList; + return view()->selectedItems(); } bool AbstractFormEditorTool::topItemIsMovable(const QList<QGraphicsItem*> & itemList) diff --git a/src/tools/qml/qmlobserver/editor/abstractformeditortool.h b/src/tools/qml/qmlobserver/editor/abstractformeditortool.h index ae27805ab02..c59e031decd 100644 --- a/src/tools/qml/qmlobserver/editor/abstractformeditortool.h +++ b/src/tools/qml/qmlobserver/editor/abstractformeditortool.h @@ -74,7 +74,7 @@ public: virtual void clear() = 0; virtual void graphicsObjectsChanged(const QList<QGraphicsObject*> &itemList) = 0; - void setItems(const QList<QGraphicsItem*> &itemList); + void updateSelectedItems(); QList<QGraphicsItem*> items() const; bool topItemIsMovable(const QList<QGraphicsItem*> &itemList); @@ -89,7 +89,7 @@ public: static QDeclarativeItem *toQDeclarativeItem(QGraphicsItem *item); protected: - virtual void selectedItemsChanged(const QList<QGraphicsItem*> &itemList) = 0; + virtual void selectedItemsChanged(const QList<QGraphicsItem*> &objectList) = 0; QDeclarativeDesignView *view() const; QGraphicsScene* scene() const; diff --git a/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp b/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp index f1afaaa40e9..c8019f26537 100644 --- a/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp +++ b/src/tools/qml/qmlobserver/qdeclarativedesignview.cpp @@ -262,13 +262,29 @@ void QDeclarativeDesignView::changeTool(Constants::DesignTool tool, Constants::T void QDeclarativeDesignView::setSelectedItems(QList<QGraphicsItem *> items) { - m_currentSelection = items; - m_currentTool->setItems(items); + m_currentSelection.clear(); + foreach(QGraphicsItem *item, items) { + if (item) { + QGraphicsObject *obj = item->toGraphicsObject(); + if (obj) + m_currentSelection << obj; + } + } + m_currentTool->updateSelectedItems(); } -QList<QGraphicsItem *> QDeclarativeDesignView::selectedItems() const +QList<QGraphicsItem *> QDeclarativeDesignView::selectedItems() { - return m_currentSelection; + QList<QGraphicsItem *> selection; + foreach(const QWeakPointer<QGraphicsObject> &selectedObject, m_currentSelection) { + if (selectedObject.isNull()) { + m_currentSelection.removeOne(selectedObject); + } else { + selection << selectedObject.data(); + } + } + + return selection; } AbstractFormEditorTool *QDeclarativeDesignView::currentTool() const @@ -333,8 +349,6 @@ QList<QGraphicsItem*> QDeclarativeDesignView::selectableItems(const QRectF &scen void QDeclarativeDesignView::changeToSingleSelectTool() { - //qDebug() << "changing to selection tool"; - m_currentToolMode = Constants::SelectionToolMode; m_selectionTool->setRubberbandSelectionMode(false); @@ -352,7 +366,7 @@ void QDeclarativeDesignView::changeToSelectTool() m_currentTool->clear(); m_currentTool = m_selectionTool; m_currentTool->clear(); - m_currentTool->setItems(m_currentSelection); + m_currentTool->updateSelectedItems(); } void QDeclarativeDesignView::changeToMarqueeSelectTool() diff --git a/src/tools/qml/qmlobserver/qdeclarativedesignview.h b/src/tools/qml/qmlobserver/qdeclarativedesignview.h index fae5fca983e..332d7f03fbd 100644 --- a/src/tools/qml/qmlobserver/qdeclarativedesignview.h +++ b/src/tools/qml/qmlobserver/qdeclarativedesignview.h @@ -3,6 +3,7 @@ #include "qmlviewerconstants.h" #include <qdeclarativeview.h> +#include <QWeakPointer> QT_FORWARD_DECLARE_CLASS(QDeclarativeItem); QT_FORWARD_DECLARE_CLASS(QMouseEvent); @@ -32,7 +33,7 @@ public: ~QDeclarativeDesignView(); void setSelectedItems(QList<QGraphicsItem *> items); - QList<QGraphicsItem *> selectedItems() const; + QList<QGraphicsItem *> selectedItems(); AbstractFormEditorTool *currentTool() const; LayerItem *manipulatorLayer() const; @@ -102,7 +103,7 @@ private: private: QPointF m_cursorPos; - QList<QGraphicsItem *> m_currentSelection; + QList<QWeakPointer<QGraphicsObject> > m_currentSelection; Constants::DesignTool m_currentToolMode; AbstractFormEditorTool *m_currentTool; -- GitLab