2010-01-07 12:14:35 +01:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2010-01-07 12:14:35 +01:00
|
|
|
**
|
|
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
|
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
|
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "dragtool.h"
|
|
|
|
|
|
|
|
|
|
#include "formeditorscene.h"
|
|
|
|
|
#include "formeditorview.h"
|
|
|
|
|
#include "itemutilfunctions.h"
|
|
|
|
|
#include <customdraganddrop.h>
|
|
|
|
|
#include <metainfo.h>
|
2010-09-24 13:45:42 +02:00
|
|
|
#include <rewritingexception.h>
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
#include "resizehandleitem.h"
|
|
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
|
#include <QtDebug>
|
2010-09-24 13:45:42 +02:00
|
|
|
#include <QMessageBox>
|
2010-12-06 17:44:25 +01:00
|
|
|
#include <QTimer>
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
namespace QmlDesigner {
|
|
|
|
|
|
2010-12-06 17:44:25 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
void TimerHandler::clearMoveDelay()
|
|
|
|
|
{
|
|
|
|
|
m_dragTool->clearMoveDelay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
DragTool::DragTool(FormEditorView *editorView)
|
|
|
|
|
: AbstractFormEditorTool(editorView),
|
|
|
|
|
m_moveManipulator(editorView->scene()->manipulatorLayerItem(), editorView),
|
2010-12-06 17:44:25 +01:00
|
|
|
m_selectionIndicator(editorView->scene()->manipulatorLayerItem()),
|
|
|
|
|
m_timerHandler(new Internal::TimerHandler(this)),
|
|
|
|
|
m_blockMove(false)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
|
|
|
|
// view()->setCursor(Qt::SizeAllCursor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DragTool::~DragTool()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::clear()
|
|
|
|
|
{
|
|
|
|
|
m_moveManipulator.clear();
|
|
|
|
|
m_selectionIndicator.clear();
|
|
|
|
|
m_movingItem.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::mousePressEvent(const QList<QGraphicsItem*> &,
|
|
|
|
|
QGraphicsSceneMouseEvent *)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::mouseMoveEvent(const QList<QGraphicsItem*> &,
|
|
|
|
|
QGraphicsSceneMouseEvent *)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::hoverMoveEvent(const QList<QGraphicsItem*> &,
|
|
|
|
|
QGraphicsSceneMouseEvent * /*event*/)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::keyPressEvent(QKeyEvent *)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::keyReleaseEvent(QKeyEvent *)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DragTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
|
|
|
|
|
QGraphicsSceneMouseEvent *)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> & /*itemList*/,
|
|
|
|
|
QGraphicsSceneMouseEvent * /*event*/)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::itemsAboutToRemoved(const QList<FormEditorItem*> & /* removedItemList */)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::selectedItemsChanged(const QList<FormEditorItem*> &)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DragTool::updateMoveManipulator()
|
|
|
|
|
{
|
|
|
|
|
if (m_moveManipulator.isActive())
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::beginWithPoint(const QPointF &beginPoint)
|
|
|
|
|
{
|
|
|
|
|
m_movingItem = scene()->itemForQmlItemNode(m_dragNode);
|
|
|
|
|
|
|
|
|
|
m_moveManipulator.setItem(m_movingItem.data());
|
|
|
|
|
m_moveManipulator.begin(beginPoint);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-07 12:49:13 +02:00
|
|
|
void DragTool::createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, QmlItemNode parentNode, QPointF scenePos)
|
2010-01-07 12:14:35 +01:00
|
|
|
{
|
|
|
|
|
MetaInfo metaInfo = MetaInfo::global();
|
|
|
|
|
|
|
|
|
|
FormEditorItem *parentItem = scene()->itemForQmlItemNode(parentNode);
|
|
|
|
|
QPointF pos = parentItem->mapFromScene(scenePos);
|
|
|
|
|
|
2010-05-07 12:49:13 +02:00
|
|
|
m_dragNode = view()->createQmlItemNode(itemLibraryEntry, pos, parentNode);
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
Q_ASSERT(m_dragNode.modelNode().isValid());
|
|
|
|
|
|
|
|
|
|
QList<QmlItemNode> nodeList;
|
2010-12-08 16:10:32 +01:00
|
|
|
nodeList.append(m_dragNode);
|
2010-01-07 12:14:35 +01:00
|
|
|
m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
|
2010-05-18 16:48:57 +02:00
|
|
|
|
|
|
|
|
QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::hide();
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::createQmlItemNodeFromImage(const QString &imageName, QmlItemNode parentNode, QPointF scenePos)
|
|
|
|
|
{
|
2010-04-06 15:08:46 +02:00
|
|
|
if (!parentNode.isValid())
|
|
|
|
|
return;
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
MetaInfo metaInfo = MetaInfo::global();
|
|
|
|
|
|
|
|
|
|
FormEditorItem *parentItem = scene()->itemForQmlItemNode(parentNode);
|
|
|
|
|
QPointF pos = parentItem->mapFromScene(scenePos);
|
|
|
|
|
|
|
|
|
|
m_dragNode = view()->createQmlItemNodeFromImage(imageName, pos, parentNode);
|
|
|
|
|
|
|
|
|
|
QList<QmlItemNode> nodeList;
|
|
|
|
|
nodeList.append(m_dragNode);
|
|
|
|
|
m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
|
2010-05-18 16:48:57 +02:00
|
|
|
|
|
|
|
|
QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::hide();
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FormEditorItem* DragTool::calculateContainer(const QPointF &point, FormEditorItem * currentItem)
|
|
|
|
|
{
|
|
|
|
|
QList<QGraphicsItem *> list = scene()->items(point);
|
|
|
|
|
foreach (QGraphicsItem *item, list) {
|
|
|
|
|
FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
|
|
|
|
|
if (formEditorItem && formEditorItem != currentItem && formEditorItem->isContainer())
|
|
|
|
|
return formEditorItem;
|
|
|
|
|
}
|
2010-03-15 17:39:37 +01:00
|
|
|
|
2010-04-14 14:15:33 +02:00
|
|
|
if (scene()->rootFormEditorItem() && scene()->rootFormEditorItem()->boundingRect().adjusted(-100, -100, 100, 100).contains(point))
|
2010-03-15 17:39:37 +01:00
|
|
|
return scene()->rootFormEditorItem();
|
2010-01-07 12:14:35 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DragTool::formEditorItemsChanged(const QList<FormEditorItem*> & itemList)
|
|
|
|
|
{
|
|
|
|
|
if (m_movingItem && itemList.contains(m_movingItem.data())) {
|
|
|
|
|
QList<FormEditorItem*> updateItemList;
|
|
|
|
|
updateItemList.append(m_movingItem.data());
|
|
|
|
|
m_selectionIndicator.updateItems(updateItemList);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-10 22:46:18 +01:00
|
|
|
void DragTool::instancesCompleted(const QList<FormEditorItem*> &itemList)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-06 17:44:25 +01:00
|
|
|
void DragTool::clearMoveDelay()
|
|
|
|
|
{
|
|
|
|
|
m_blockMove = false;
|
|
|
|
|
if (m_dragNode.isValid())
|
2010-12-08 16:10:32 +01:00
|
|
|
beginWithPoint(m_startPoint);
|
2010-12-06 17:44:25 +01:00
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
void DragTool::dropEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
|
{
|
|
|
|
|
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") ||
|
|
|
|
|
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
|
|
|
|
|
event->accept();
|
|
|
|
|
end(event->scenePos());
|
|
|
|
|
//Q_ASSERT(m_token.isValid());
|
2010-09-24 13:45:42 +02:00
|
|
|
try {
|
|
|
|
|
m_rewriterTransaction.commit();
|
|
|
|
|
} catch (RewritingException &e) {
|
|
|
|
|
QMessageBox::warning(0, "Error", e.description());
|
|
|
|
|
}
|
2010-12-09 16:38:28 +01:00
|
|
|
if (m_dragNode.isValid()) {
|
|
|
|
|
QList<QmlItemNode> nodeList;
|
|
|
|
|
nodeList.append(m_dragNode);
|
|
|
|
|
view()->setSelectedQmlItemNodes(nodeList);
|
|
|
|
|
}
|
2010-01-07 12:14:35 +01:00
|
|
|
m_dragNode = ModelNode();
|
|
|
|
|
view()->changeToSelectionTool();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-09 16:39:15 +01:00
|
|
|
static ItemLibraryEntry itemLibraryEntryFromData(const QByteArray &data)
|
|
|
|
|
{
|
|
|
|
|
QDataStream stream(data);
|
|
|
|
|
|
|
|
|
|
ItemLibraryEntry itemLibraryEntry;
|
|
|
|
|
stream >> itemLibraryEntry;
|
|
|
|
|
|
|
|
|
|
return itemLibraryEntry;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
void DragTool::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
|
{
|
|
|
|
|
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") ||
|
|
|
|
|
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
|
2010-12-09 16:39:15 +01:00
|
|
|
|
|
|
|
|
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"));
|
|
|
|
|
if (!itemLibraryEntry.requiredImport().isEmpty()) {
|
|
|
|
|
const QString newImportUrl = itemLibraryEntry.requiredImport();
|
|
|
|
|
const QString newImportVersion = QString("%1.%2").arg(QString::number(itemLibraryEntry.majorVersion()), QString::number(itemLibraryEntry.minorVersion()));
|
|
|
|
|
Import newImport = Import::createLibraryImport(newImportUrl, newImportVersion);
|
|
|
|
|
|
|
|
|
|
if (!view()->model()->imports().contains(newImport)) {
|
|
|
|
|
view()->model()->addImport(newImport);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
if (!m_rewriterTransaction.isValid()) {
|
2010-03-31 12:59:16 +02:00
|
|
|
view()->clearSelectedModelNodes();
|
2010-01-07 12:14:35 +01:00
|
|
|
m_rewriterTransaction = view()->beginRewriterTransaction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::dragLeaveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
|
{
|
|
|
|
|
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") ||
|
|
|
|
|
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
|
|
|
|
|
event->accept();
|
|
|
|
|
if (m_dragNode.isValid())
|
|
|
|
|
m_dragNode.destroy();
|
|
|
|
|
end(event->scenePos());
|
2010-09-24 13:45:42 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
m_rewriterTransaction.commit();
|
|
|
|
|
} catch (RewritingException &e) {
|
|
|
|
|
QMessageBox::warning(0, "Error", e.description());
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::show();
|
|
|
|
|
QList<QmlItemNode> nodeList;
|
|
|
|
|
view()->setSelectedQmlItemNodes(nodeList);
|
|
|
|
|
view()->changeToSelectionTool();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
|
|
|
|
|
{
|
2010-12-06 17:44:25 +01:00
|
|
|
if (m_blockMove)
|
|
|
|
|
return;
|
2010-01-07 12:14:35 +01:00
|
|
|
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") ||
|
|
|
|
|
event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
|
|
|
|
|
event->accept();
|
|
|
|
|
QPointF scenePos = event->scenePos();
|
|
|
|
|
if (m_dragNode.isValid()) {
|
|
|
|
|
|
2010-05-18 16:48:57 +02:00
|
|
|
FormEditorItem *parentItem = calculateContainer(event->scenePos() + QPoint(2, 2));
|
2010-01-07 12:14:35 +01:00
|
|
|
if (!parentItem) { //if there is no parent any more - the use left the scene
|
|
|
|
|
end(event->scenePos());
|
|
|
|
|
QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::show();
|
2010-05-18 16:48:57 +02:00
|
|
|
m_dragNode.destroy(); //delete the node then
|
2010-01-07 12:14:35 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//move
|
|
|
|
|
move(event->scenePos());
|
|
|
|
|
} else {
|
|
|
|
|
//create new node if container
|
2010-09-24 13:45:42 +02:00
|
|
|
if (m_dragNode.modelNode().isValid())
|
|
|
|
|
return;
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
FormEditorItem *parentItem = calculateContainer(event->scenePos());
|
|
|
|
|
if (!parentItem)
|
|
|
|
|
return;
|
2010-12-06 17:44:25 +01:00
|
|
|
QmlItemNode parentNode;
|
2010-01-07 12:14:35 +01:00
|
|
|
if (parentItem)
|
|
|
|
|
parentNode = parentItem->qmlItemNode();
|
|
|
|
|
|
|
|
|
|
if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo")) {
|
|
|
|
|
Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo").isEmpty());
|
2010-05-07 12:49:13 +02:00
|
|
|
ItemLibraryEntry itemLibraryEntry = itemLibraryEntryFromData(event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo"));
|
|
|
|
|
createQmlItemNode(itemLibraryEntry, parentNode, event->scenePos());
|
2010-01-07 12:14:35 +01:00
|
|
|
} else if (event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
|
|
|
|
|
Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.libraryresource").isEmpty());
|
|
|
|
|
QString imageName = QString::fromLatin1((event->mimeData()->data("application/vnd.bauhaus.libraryresource")));
|
|
|
|
|
createQmlItemNodeFromImage(imageName, parentNode, event->scenePos());
|
|
|
|
|
} else Q_ASSERT(false);
|
2010-12-06 17:44:25 +01:00
|
|
|
m_blockMove = true;
|
2010-12-08 16:10:32 +01:00
|
|
|
m_startPoint = event->scenePos();
|
|
|
|
|
QTimer::singleShot(100, m_timerHandler.data(), SLOT(clearMoveDelay()));
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::end(QPointF scenePos)
|
|
|
|
|
{
|
|
|
|
|
m_moveManipulator.end(scenePos);
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DragTool::move(QPointF scenePos)
|
|
|
|
|
{
|
|
|
|
|
if (!m_movingItem)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
FormEditorItem *containerItem = calculateContainer(scenePos - QPoint(2, 2), m_movingItem.data());
|
|
|
|
|
if (containerItem &&
|
2010-04-23 11:14:30 +02:00
|
|
|
containerItem != m_movingItem->parentItem()) {
|
2010-01-07 12:14:35 +01:00
|
|
|
|
|
|
|
|
m_moveManipulator.reparentTo(containerItem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//MoveManipulator::Snapping useSnapping = MoveManipulator::NoSnapping;
|
|
|
|
|
MoveManipulator::Snapping useSnapping = MoveManipulator::UseSnapping;
|
|
|
|
|
/* if (event->modifiers().testFlag(Qt::ControlModifier) != view()->isSnapButtonChecked())
|
|
|
|
|
useSnapping = MoveManipulator::UseSnapping;*/
|
|
|
|
|
|
2010-05-26 16:03:38 +02:00
|
|
|
m_moveManipulator.update(scenePos, useSnapping, MoveManipulator::UseBaseState);
|
2010-01-07 12:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|