dragtool.cpp 13.6 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
** 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.
23
**
hjk's avatar
hjk committed
24
25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30
31
32
33
34
35

#include "dragtool.h"

#include "formeditorscene.h"
#include "formeditorview.h"
#include <customdraganddrop.h>
#include <metainfo.h>
36
#include <rewritingexception.h>
37
38

#include <QGraphicsSceneMouseEvent>
hjk's avatar
hjk committed
39
#include <QDebug>
40
#include <QMimeData>
41
#include <QMessageBox>
Thomas Hartmann's avatar
Thomas Hartmann committed
42
#include <QTimer>
43
44
45

namespace QmlDesigner {

Thomas Hartmann's avatar
Thomas Hartmann committed
46
47
48
49
50
51
52
53
namespace Internal {
void TimerHandler::clearMoveDelay()
{
    m_dragTool->clearMoveDelay();
}

}

54
55
56
DragTool::DragTool(FormEditorView *editorView)
    : AbstractFormEditorTool(editorView),
    m_moveManipulator(editorView->scene()->manipulatorLayerItem(), editorView),
Thomas Hartmann's avatar
Thomas Hartmann committed
57
58
    m_selectionIndicator(editorView->scene()->manipulatorLayerItem()),
    m_timerHandler(new Internal::TimerHandler(this)),
59
60
    m_blockMove(false),
    m_Aborted(false)
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{
}


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*/)
{

}

95
void DragTool::keyPressEvent(QKeyEvent *event)
96
{
97
98
99
    if (event->key() == Qt::Key_Escape) {
        abort();
        event->accept();
100
        commitTransaction();
101
102
        view()->changeToSelectionTool();
    }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
}

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);
}

149
void DragTool::createQmlItemNode(const ItemLibraryEntry &itemLibraryEntry, QmlItemNode parentNode, QPointF scenePos)
150
151
152
153
154
155
{
    MetaInfo metaInfo = MetaInfo::global();

    FormEditorItem *parentItem = scene()->itemForQmlItemNode(parentNode);
    QPointF pos = parentItem->mapFromScene(scenePos);

156
    m_dragNode = QmlItemNode::createQmlItemNode(view(), itemLibraryEntry, pos, parentNode);
157
158
159
160

    Q_ASSERT(m_dragNode.modelNode().isValid());

    QList<QmlItemNode> nodeList;
Orgad Shaneh's avatar
Orgad Shaneh committed
161
    nodeList.append(m_dragNode);
162
163
164
165
166
    m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
}

void DragTool::createQmlItemNodeFromImage(const QString &imageName, QmlItemNode parentNode, QPointF scenePos)
{
167
168
169
    if (!parentNode.isValid())
        return;

170
171
172
173
174
    MetaInfo metaInfo = MetaInfo::global();

    FormEditorItem *parentItem = scene()->itemForQmlItemNode(parentNode);
    QPointF pos = parentItem->mapFromScene(scenePos);

175
    m_dragNode = QmlItemNode::createQmlItemNodeFromImage(view(), imageName, pos, parentNode);
176
177
178
179
180
181

    QList<QmlItemNode> nodeList;
    nodeList.append(m_dragNode);
    m_selectionIndicator.setItems(scene()->itemsForQmlItemNodes(nodeList));
}

182
183
184
185
186
187
188
static inline bool isAncestorOf(FormEditorItem *formEditorItem, FormEditorItem *newParentItem)
{
    if (formEditorItem && newParentItem)
        return formEditorItem->isAncestorOf(newParentItem);
    return false;
}

189
190
191
192
193
FormEditorItem* DragTool::calculateContainer(const QPointF &point, FormEditorItem * currentItem)
{
    QList<QGraphicsItem *> list = scene()->items(point);
    foreach (QGraphicsItem *item, list) {
         FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
194
195
         if (formEditorItem && formEditorItem != currentItem && formEditorItem->isContainer()
             && !isAncestorOf(currentItem, formEditorItem))
196
197
             return formEditorItem;
    }
198

199
    if (scene()->rootFormEditorItem())
200
        return scene()->rootFormEditorItem();
201
202
203
    return 0;
}

204
205
206
207
208
209
210
211
212
 QList<Import> DragTool::missingImportList(const ItemLibraryEntry &itemLibraryEntry)
{
    QList<Import> importToBeAddedList;

    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);

213
        if (itemLibraryEntry.majorVersion() == -1 && itemLibraryEntry.minorVersion() == -1)
214
            newImport = Import::createFileImport(newImportUrl, QString());
215
        else
216
217
218
219
220
            newImport = Import::createLibraryImport(newImportUrl, newImportVersion);
    }
    return importToBeAddedList;
}

221
222
223
224
225
226
227
228
229
230

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);
    }
}

231
232
void DragTool::instancesCompleted(const QList<FormEditorItem*> &itemList)
{
233
234
235
    foreach (FormEditorItem* item, itemList)
        if (item->qmlItemNode() == m_dragNode)
            clearMoveDelay();
236
    QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::hide();
237
238
}

239
240
241
242
243
void DragTool::instancesParentChanged(const QList<FormEditorItem *> &itemList)
{
    m_moveManipulator.synchronizeInstanceParent(itemList);
}

244
245
246
void DragTool::instancePropertyChange(const QList<QPair<ModelNode, PropertyName> > & /*propertyList*/)
{
}
247

Thomas Hartmann's avatar
Thomas Hartmann committed
248
249
void DragTool::clearMoveDelay()
{
250
251
    if (!m_blockMove)
        return;
Thomas Hartmann's avatar
Thomas Hartmann committed
252
253
    m_blockMove = false;
    if  (m_dragNode.isValid())
254
        beginWithPoint(m_startPoint);
Thomas Hartmann's avatar
Thomas Hartmann committed
255
}
256

257
258
259
260
261
262
263
264
265
266
267
void DragTool::abort()
{
    if (m_Aborted)
        return;

    m_Aborted = true;

    if (m_dragNode.isValid())
        m_dragNode.destroy();

    QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::hide();
268
}
269

270
271
272
void DragTool::commitTransaction()
{
    try {
273
        m_rewriterTransaction.commit();
274
275
276
    } catch (RewritingException &e) {
        QMessageBox::warning(0, "Error", e.description());
    }
277
278
}

279
280
281
282
283
void DragTool::dropEvent(QGraphicsSceneDragDropEvent * event)
{
    if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") ||
       event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
        event->accept();
284
285
        end(generateUseSnapping(event->modifiers()));

286
287
        commitTransaction();

288
289
290
        if (m_dragNode.isValid()) {
            QList<QmlItemNode> nodeList;
            nodeList.append(m_dragNode);
291
            view()->setSelectedModelNodes(toModelNodeList(nodeList));
292
        }
293
294
295
296
297
        m_dragNode = ModelNode();
        view()->changeToSelectionTool();
    }
}

298
299
300
301
302
303
304
305
306
307
static ItemLibraryEntry itemLibraryEntryFromData(const QByteArray &data)
{
    QDataStream stream(data);

    ItemLibraryEntry itemLibraryEntry;
    stream >> itemLibraryEntry;

    return itemLibraryEntry;
}

308
309
310
311
void DragTool::dragEnterEvent(QGraphicsSceneDragDropEvent * event)
{
    if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo") ||
        event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
312
        QList<Import> importToBeAddedList;
313
        m_blockMove = false;
314
        if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo")) {
315
            view()->widgetInfo().widget->setFocus();
316
            m_Aborted = false;
317
318
            Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo").isEmpty());

319
320
            importToBeAddedList = missingImportList(
                        itemLibraryEntryFromData(event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo")));
321
322
        }

323
324
        view()->model()->changeImports(importToBeAddedList, QList<Import>());

325
        if (!m_rewriterTransaction.isValid()) {
326
            view()->clearSelectedModelNodes();
327
328
329
330
331
332
333
334
335
336
337
338
            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();
339
340
341

        m_moveManipulator.end();
        clear();
342

343
        commitTransaction();
344

345
346
        QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::show();
        QList<QmlItemNode> nodeList;
347
        view()->setSelectedModelNodes(toModelNodeList(nodeList));
348
349
350
351
352
353
        view()->changeToSelectionTool();
    }
}

void DragTool::dragMoveEvent(QGraphicsSceneDragDropEvent * event)
{
Thomas Hartmann's avatar
Thomas Hartmann committed
354
355
    if (m_blockMove)
        return;
356
357
358
359
360

    if (m_Aborted) {
        event->ignore();
        return;
    }
361
362
    if (QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::isAnimated())
        return;
363
364
365
366
367
368
    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()) {

369
            FormEditorItem *parentItem = calculateContainer(event->scenePos() + QPoint(2, 2));
370
            if (!parentItem) {      //if there is no parent any more - the use left the scene
371
                end();
372
                QmlDesignerItemLibraryDragAndDrop::CustomDragAndDrop::show();
373
                m_dragNode.destroy(); //delete the node then
374
375
376
377
378
379
                return;
            }
            //move
            move(event->scenePos());
        } else {
            //create new node  if container
380
381
            if (m_dragNode.modelNode().isValid())
                return;
382

383
            FormEditorItem *parentItem = calculateContainer(scenePos);
384
385
            if (!parentItem)
                return;
Thomas Hartmann's avatar
Thomas Hartmann committed
386
            QmlItemNode parentNode;
387
388
389
390
391
            if (parentItem)
                parentNode = parentItem->qmlItemNode();

            if (event->mimeData()->hasFormat("application/vnd.bauhaus.itemlibraryinfo")) {
                Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo").isEmpty());
392
393
                ItemLibraryEntry itemLibraryEntry = itemLibraryEntryFromData(event->mimeData()->data("application/vnd.bauhaus.itemlibraryinfo"));
                createQmlItemNode(itemLibraryEntry, parentNode, event->scenePos());
394
395
            } else if (event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
                Q_ASSERT(!event->mimeData()->data("application/vnd.bauhaus.libraryresource").isEmpty());
396
                QString imageName = QString::fromUtf8((event->mimeData()->data("application/vnd.bauhaus.libraryresource")));
397
398
                createQmlItemNodeFromImage(imageName, parentNode, event->scenePos());
            } else Q_ASSERT(false);
Thomas Hartmann's avatar
Thomas Hartmann committed
399
            m_blockMove = true;
400
            m_startPoint = event->scenePos();
401
            QTimer::singleShot(10000, m_timerHandler.data(), SLOT(clearMoveDelay()));
402
403
        }
    }
404

405
406
407
408
    if (event->mimeData()->hasFormat("application/vnd.bauhaus.libraryresource")) {
    }
}

409
410
411
412
413
414
415
void  DragTool::end()
{
    m_moveManipulator.end();
    clear();
}

void DragTool::end(Snapper::Snapping useSnapping)
416
{
417
    m_moveManipulator.end(useSnapping);
418
419
420
421
422
423
424
425
426
    clear();
}

void  DragTool::move(QPointF scenePos)
{
    if (!m_movingItem)
        return;

    FormEditorItem *containerItem = calculateContainer(scenePos - QPoint(2, 2), m_movingItem.data());
427
    if (containerItem && m_movingItem->parentItem() &&
428
       containerItem != m_movingItem->parentItem()) {
429
430
431
432
433

        m_moveManipulator.reparentTo(containerItem);
    }

    //MoveManipulator::Snapping useSnapping = MoveManipulator::NoSnapping;
434
    Snapper::Snapping useSnapping = Snapper::UseSnapping;
435
436
437
   /* if (event->modifiers().testFlag(Qt::ControlModifier) != view()->isSnapButtonChecked())
        useSnapping = MoveManipulator::UseSnapping;*/

438
    m_moveManipulator.update(scenePos, useSnapping, MoveManipulator::UseBaseState);
439
440
441
442
}


}