Commit 0d163379 authored by Lasse Holmstedt's avatar Lasse Holmstedt
Browse files

Changed handling of selected items to use weak pointers

graphics items could be destroyed after selecting something.
parent 28867b78
......@@ -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)
......
......@@ -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;
......
......@@ -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()
......
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment