QmlDesigner: More cleanup in drag tool

Change-Id: Ia00e6bb1a9a37e61ae85420081824006040a8f53
Reviewed-by: Tim Jenssen <tim.jenssen@digia.com>
This commit is contained in:
Marco Bubke
2014-07-17 15:27:59 +02:00
parent 4fb83d3251
commit 6bac93411e
2 changed files with 119 additions and 146 deletions

View File

@@ -56,14 +56,13 @@ DragTool::DragTool(FormEditorView *editorView)
m_selectionIndicator(editorView->scene()->manipulatorLayerItem()), m_selectionIndicator(editorView->scene()->manipulatorLayerItem()),
m_timerHandler(new Internal::TimerHandler(this)), m_timerHandler(new Internal::TimerHandler(this)),
m_blockMove(false), m_blockMove(false),
m_Aborted(false) m_isAborted(false)
{ {
} }
DragTool::~DragTool() DragTool::~DragTool()
{ {
} }
void DragTool::clear() void DragTool::clear()
@@ -73,22 +72,16 @@ void DragTool::clear()
m_movingItem.clear(); m_movingItem.clear();
} }
void DragTool::mousePressEvent(const QList<QGraphicsItem*> &, void DragTool::mousePressEvent(const QList<QGraphicsItem*> &, QGraphicsSceneMouseEvent *)
QGraphicsSceneMouseEvent *)
{ {
} }
void DragTool::mouseMoveEvent(const QList<QGraphicsItem*> &, void DragTool::mouseMoveEvent(const QList<QGraphicsItem*> &, QGraphicsSceneMouseEvent *)
QGraphicsSceneMouseEvent *)
{ {
} }
void DragTool::hoverMoveEvent(const QList<QGraphicsItem*> &, void DragTool::hoverMoveEvent(const QList<QGraphicsItem*> &, QGraphicsSceneMouseEvent * /*event*/)
QGraphicsSceneMouseEvent * /*event*/)
{ {
} }
void DragTool::keyPressEvent(QKeyEvent *event) void DragTool::keyPressEvent(QKeyEvent *event)
@@ -96,45 +89,36 @@ void DragTool::keyPressEvent(QKeyEvent *event)
if (event->key() == Qt::Key_Escape) { if (event->key() == Qt::Key_Escape) {
abort(); abort();
event->accept(); event->accept();
commitTransaction(); m_rewriterTransaction.commit();
view()->changeToSelectionTool(); view()->changeToSelectionTool();
} }
} }
void DragTool::keyReleaseEvent(QKeyEvent *) void DragTool::keyReleaseEvent(QKeyEvent *)
{ {
} }
void DragTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/, void DragTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneMouseEvent *)
QGraphicsSceneMouseEvent *)
{ {
} }
void DragTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> & /*itemList*/, void DragTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> & /*itemList*/, QGraphicsSceneMouseEvent * /*event*/)
QGraphicsSceneMouseEvent * /*event*/)
{ {
} }
void DragTool::itemsAboutToRemoved(const QList<FormEditorItem*> & /* removedItemList */) void DragTool::itemsAboutToRemoved(const QList<FormEditorItem*> & /* removedItemList */)
{ {
} }
void DragTool::selectedItemsChanged(const QList<FormEditorItem*> &) void DragTool::selectedItemsChanged(const QList<FormEditorItem*> &)
{ {
} }
void DragTool::updateMoveManipulator() void DragTool::updateMoveManipulator()
{ {
if (m_moveManipulator.isActive())
return;
} }
void DragTool::beginWithPoint(const QPointF &beginPoint) void DragTool::beginWithPoint(const QPointF &beginPoint)
@@ -156,8 +140,6 @@ void DragTool::createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry,
m_dragNode = QmlItemNode::createQmlItemNode(view(), itemLibraryEntry, pos, parentNode); m_dragNode = QmlItemNode::createQmlItemNode(view(), itemLibraryEntry, pos, parentNode);
Q_ASSERT(m_dragNode.modelNode().isValid());
QList<QmlItemNode> nodeList; QList<QmlItemNode> nodeList;
nodeList.append(m_dragNode); nodeList.append(m_dragNode);
m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList)); m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
@@ -167,19 +149,18 @@ void DragTool::createQmlItemNodeFromImage(const QString &imageName,
const QmlItemNode &parentNode, const QmlItemNode &parentNode,
const QPointF &scenePos) const QPointF &scenePos)
{ {
if (!parentNode.isValid()) if (parentNode.isValid()) {
return; MetaInfo metaInfo = MetaInfo::global();
MetaInfo metaInfo = MetaInfo::global(); FormEditorItem *parentItem = scene()->itemForQmlItemNode(parentNode);
QPointF pos = parentItem->mapFromScene(scenePos);
FormEditorItem *parentItem = scene()->itemForQmlItemNode(parentNode); m_dragNode = QmlItemNode::createQmlItemNodeFromImage(view(), imageName, pos, parentNode);
QPointF pos = parentItem->mapFromScene(scenePos);
m_dragNode = QmlItemNode::createQmlItemNodeFromImage(view(), imageName, pos, parentNode); QList<QmlItemNode> nodeList;
nodeList.append(m_dragNode);
QList<QmlItemNode> nodeList; m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
nodeList.append(m_dragNode); }
m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
} }
static inline bool isAncestorOf(FormEditorItem *formEditorItem, FormEditorItem *newParentItem) static inline bool isAncestorOf(FormEditorItem *formEditorItem, FormEditorItem *newParentItem)
@@ -189,10 +170,9 @@ static inline bool isAncestorOf(FormEditorItem *formEditorItem, FormEditorItem *
return false; return false;
} }
FormEditorItem* DragTool::calculateContainer(const QPointF &point, FormEditorItem * currentItem) FormEditorItem* DragTool::calculateTargetContainer(const QList<QGraphicsItem*> &itemList, FormEditorItem * currentItem)
{ {
QList<QGraphicsItem *> list = scene()->items(point); foreach (QGraphicsItem *item, itemList) {
foreach (QGraphicsItem *item, list) {
FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item); FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
if (formEditorItem if (formEditorItem
&& formEditorItem != currentItem && formEditorItem != currentItem
@@ -204,6 +184,7 @@ FormEditorItem* DragTool::calculateContainer(const QPointF &point, FormEditorIte
if (scene()->rootFormEditorItem()) if (scene()->rootFormEditorItem())
return scene()->rootFormEditorItem(); return scene()->rootFormEditorItem();
return 0; return 0;
} }
@@ -235,54 +216,63 @@ void DragTool::instancePropertyChange(const QList<QPair<ModelNode, PropertyName>
void DragTool::clearMoveDelay() void DragTool::clearMoveDelay()
{ {
if (!m_blockMove) if (m_blockMove) {
return; m_blockMove = false;
m_blockMove = false; if (m_dragNode.isValid())
if (m_dragNode.isValid()) beginWithPoint(m_startPoint);
beginWithPoint(m_startPoint); }
} }
void DragTool::abort() void DragTool::abort()
{ {
if (m_Aborted) if (!m_isAborted) {
return; m_isAborted = true;
m_Aborted = true; if (m_dragNode.isValid())
m_dragNode.destroy();
if (m_dragNode.isValid())
m_dragNode.destroy();
}
void DragTool::commitTransaction()
{
try {
m_rewriterTransaction.commit();
} catch (RewritingException &e) {
e.showException();
} }
} }
void DragTool::dropEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent * event) static bool canHandleMimeData(const QMimeData *mimeData)
{ {
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") || return mimeData->hasFormat(QStringLiteral("application/vnd.bauhaus.itemlibraryinfo"))
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) { || mimeData->hasFormat(QStringLiteral("application/vnd.bauhaus.libraryresource"));
}
static bool hasItemLibraryInfo(const QMimeData *mimeData)
{
return mimeData->hasFormat(QStringLiteral("application/vnd.bauhaus.itemlibraryinfo"));
}
static bool hasLibraryResources(const QMimeData *mimeData)
{
return mimeData->hasFormat(QStringLiteral("application/vnd.bauhaus.libraryresource"));
}
void DragTool::dropEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent *event)
{
if (canHandleMimeData(event->mimeData())) {
event->accept(); event->accept();
end(generateUseSnapping(event->modifiers())); end(generateUseSnapping(event->modifiers()));
commitTransaction(); m_rewriterTransaction.commit();
if (m_dragNode.isValid()) { if (m_dragNode.isValid()) {
QList<QmlItemNode> nodeList; QList<QmlItemNode> nodeList;
nodeList.append(m_dragNode); nodeList.append(m_dragNode);
view()->setSelectedModelNodes(toModelNodeList(nodeList)); view()->setSelectedModelNodes(toModelNodeList(nodeList));
} }
m_dragNode = ModelNode();
m_dragNode = QmlItemNode();
view()->changeToSelectionTool(); view()->changeToSelectionTool();
} }
} }
static ItemLibraryEntry itemLibraryEntryFromData(const QByteArray &data) static ItemLibraryEntry itemLibraryEntryFromMimeData(const QMimeData *mimeData)
{ {
QByteArray data = mimeData->data(QStringLiteral("application/vnd.bauhaus.itemlibraryinfo"));
QDataStream stream(data); QDataStream stream(data);
ItemLibraryEntry itemLibraryEntry; ItemLibraryEntry itemLibraryEntry;
@@ -291,16 +281,14 @@ static ItemLibraryEntry itemLibraryEntryFromData(const QByteArray &data)
return itemLibraryEntry; return itemLibraryEntry;
} }
void DragTool::dragEnterEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent * event) void DragTool::dragEnterEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent *event)
{ {
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") || if (canHandleMimeData(event->mimeData())) {
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
QList<Import> importToBeAddedList; QList<Import> importToBeAddedList;
m_blockMove = false; m_blockMove = false;
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo")) { if (hasItemLibraryInfo(event->mimeData())) {
view()->widgetInfo().widget->setFocus(); view()->widgetInfo().widget->setFocus();
m_Aborted = false; m_isAborted = false;
Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo").isEmpty());
} }
if (!m_rewriterTransaction.isValid()) { if (!m_rewriterTransaction.isValid()) {
@@ -310,10 +298,9 @@ void DragTool::dragEnterEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphi
} }
} }
void DragTool::dragLeaveEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent * event) void DragTool::dragLeaveEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent *event)
{ {
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") || if (canHandleMimeData(event->mimeData())) {
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
event->accept(); event->accept();
m_moveManipulator.end(); m_moveManipulator.end();
@@ -321,66 +308,60 @@ void DragTool::dragLeaveEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphi
if (m_dragNode.isValid()) if (m_dragNode.isValid())
m_dragNode.destroy(); m_dragNode.destroy();
commitTransaction(); m_rewriterTransaction.commit();
QList<QmlItemNode> nodeList; view()->clearSelectedModelNodes();
view()->setSelectedModelNodes(toModelNodeList(nodeList));
view()->changeToSelectionTool(); view()->changeToSelectionTool();
} }
} }
void DragTool::dragMoveEvent(const QList<QGraphicsItem*> &/*itemList*/, QGraphicsSceneDragDropEvent *event) static QString libraryResourceImageName(const QMimeData *mimeData)
{ {
if (m_blockMove) return QString::fromUtf8((mimeData->data(QStringLiteral("application/vnd.bauhaus.libraryresource"))));
return; }
if (m_Aborted) { void DragTool::createDragNode(const QMimeData *mimeData, const QPointF &scenePosition, const QList<QGraphicsItem*> &itemList)
event->ignore(); {
return; if (!m_dragNode.hasModelNode()) {
} FormEditorItem *targetContainerFormEditorItem = calculateTargetContainer(itemList);
if (targetContainerFormEditorItem) {
QmlItemNode targetContainerQmlItemNode;
if (targetContainerFormEditorItem)
targetContainerQmlItemNode = targetContainerFormEditorItem->qmlItemNode();
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") || if (hasItemLibraryInfo(mimeData))
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) { createQmlItemNode(itemLibraryEntryFromMimeData(mimeData), targetContainerQmlItemNode, scenePosition);
event->accept(); else if (hasLibraryResources(mimeData))
QPointF scenePos = event->scenePos(); createQmlItemNodeFromImage(libraryResourceImageName(mimeData), targetContainerQmlItemNode, scenePosition);
if (m_dragNode.isValid()) {
FormEditorItem *parentItem = calculateContainer(event->scenePos() + QPoint(2, 2));
if (!parentItem) { //if there is no parent any more - the use left the scene
end();
m_dragNode.destroy(); //delete the node then
return;
}
//move
move(event->scenePos());
} else {
//create new node if container
if (m_dragNode.modelNode().isValid())
return;
FormEditorItem *parentItem = calculateContainer(scenePos);
if (!parentItem)
return;
QmlItemNode parentNode;
if (parentItem)
parentNode = parentItem->qmlItemNode();
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo")) {
Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo").isEmpty());
ItemLibraryEntry itemLibraryEntry = itemLibraryEntryFromData(event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo"));
createQmlItemNode(itemLibraryEntry, parentNode, event->scenePos());
} else if (event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.libraryresource").isEmpty());
QString imageName = QString::fromUtf8((event->mimeData()->data("application/vnd.bauhaus.libraryresource")));
createQmlItemNodeFromImage(imageName, parentNode, event->scenePos());
} else Q_ASSERT(false);
m_blockMove = true; m_blockMove = true;
m_startPoint = event->scenePos(); m_startPoint = scenePosition;
QTimer::singleShot(10000, m_timerHandler.data(), SLOT(clearMoveDelay())); QTimer::singleShot(10000, m_timerHandler.data(), SLOT(clearMoveDelay()));
} }
} }
}
if (event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) { void DragTool::dragMoveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent *event)
{
if (!m_blockMove) {
if (!m_isAborted) {
if (canHandleMimeData(event->mimeData())) {
event->accept();
if (m_dragNode.isValid()) {
FormEditorItem *targetContainerItem = calculateTargetContainer(itemList);
if (targetContainerItem) {
move(event->scenePos(), itemList);
} else {
end();
m_dragNode.destroy();
}
} else {
createDragNode(event->mimeData(), event->scenePos(), itemList);
}
}
} else {
event->ignore();
}
} }
} }
@@ -396,25 +377,20 @@ void DragTool::end(Snapper::Snapping useSnapping)
clear(); clear();
} }
void DragTool::move(const QPointF &scenePos) void DragTool::move(const QPointF &scenePos, const QList<QGraphicsItem*> &itemList)
{ {
if (!m_movingItem) if (m_movingItem) {
return; FormEditorItem *containerItem = calculateTargetContainer(itemList, m_movingItem.data());
if (containerItem && m_movingItem->parentItem() &&
containerItem != m_movingItem->parentItem()) {
FormEditorItem *containerItem = calculateContainer(scenePos - QPoint(2, 2), m_movingItem.data()); m_moveManipulator.reparentTo(containerItem);
if (containerItem && m_movingItem->parentItem() && }
containerItem != m_movingItem->parentItem()) {
m_moveManipulator.reparentTo(containerItem); Snapper::Snapping useSnapping = Snapper::UseSnapping;
m_moveManipulator.update(scenePos, useSnapping, MoveManipulator::UseBaseState);
} }
//MoveManipulator::Snapping useSnapping = MoveManipulator::NoSnapping;
Snapper::Snapping useSnapping = Snapper::UseSnapping;
/* if (event->modifiers().testFlag(Qt::ControlModifier) != view()->isSnapButtonChecked())
useSnapping = MoveManipulator::UseSnapping;*/
m_moveManipulator.update(scenePos, useSnapping, MoveManipulator::UseBaseState);
} }
} }

View File

@@ -61,7 +61,7 @@ private:
class DragTool : public AbstractFormEditorTool class DragTool : public AbstractFormEditorTool
{ {
public: public:
DragTool(FormEditorView* editorView); DragTool(FormEditorView *editorView);
virtual ~DragTool(); virtual ~DragTool();
void mousePressEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent *event) QTC_OVERRIDE; void mousePressEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent *event) QTC_OVERRIDE;
@@ -71,10 +71,10 @@ public:
void hoverMoveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent *event) QTC_OVERRIDE; void hoverMoveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneMouseEvent *event) QTC_OVERRIDE;
void dropEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent * event) QTC_OVERRIDE; void dropEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent *event) QTC_OVERRIDE;
void dragEnterEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent * event) QTC_OVERRIDE; void dragEnterEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent *event) QTC_OVERRIDE;
void dragLeaveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent * event) QTC_OVERRIDE; void dragLeaveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent *event) QTC_OVERRIDE;
void dragMoveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent * event) QTC_OVERRIDE; void dragMoveEvent(const QList<QGraphicsItem*> &itemList, QGraphicsSceneDragDropEvent *event) QTC_OVERRIDE;
void keyPressEvent(QKeyEvent *event) QTC_OVERRIDE; void keyPressEvent(QKeyEvent *event) QTC_OVERRIDE;
void keyReleaseEvent(QKeyEvent *keyEvent) QTC_OVERRIDE; void keyReleaseEvent(QKeyEvent *keyEvent) QTC_OVERRIDE;
@@ -101,19 +101,16 @@ public:
protected: protected:
void abort(); void abort();
private:
void commitTransaction();
void createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, const QmlItemNode &parentNode, const QPointF &scenePos); void createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, const QmlItemNode &parentNode, const QPointF &scenePos);
void createQmlItemNodeFromImage(const QString &imageName, const QmlItemNode &parentNode, const QPointF &scenePos); void createQmlItemNodeFromImage(const QString &imageName, const QmlItemNode &parentNode, const QPointF &scenePos);
FormEditorItem* calculateContainer(const QPointF &point, FormEditorItem * currentItem = 0); FormEditorItem *calculateTargetContainer(const QList<QGraphicsItem*> &itemList, FormEditorItem *urrentItem = 0);
void begin(QPointF scenePos); void begin(QPointF scenePos);
void end(); void end();
void end(Snapper::Snapping useSnapping); void end(Snapper::Snapping useSnapping);
void move(const QPointF &scenePos); void move(const QPointF &scenePos, const QList<QGraphicsItem *> &itemList);
void createDragNode(const QMimeData *mimeData, const QPointF &scenePosition, const QList<QGraphicsItem *> &itemList);
private:
MoveManipulator m_moveManipulator; MoveManipulator m_moveManipulator;
SelectionIndicator m_selectionIndicator; SelectionIndicator m_selectionIndicator;
QPointer<FormEditorItem> m_movingItem; QPointer<FormEditorItem> m_movingItem;
@@ -122,7 +119,7 @@ private:
QScopedPointer<Internal::TimerHandler> m_timerHandler; QScopedPointer<Internal::TimerHandler> m_timerHandler;
bool m_blockMove; bool m_blockMove;
QPointF m_startPoint; QPointF m_startPoint;
bool m_Aborted; bool m_isAborted;
}; };