1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "itemcreatortool.h"
35 #include "formeditorscene.h"
36 #include "formeditorview.h"
39 #include <itemlibrarywidget.h>
41 #include <QApplication>
42 #include <QGraphicsSceneMouseEvent>
45 #include <variantproperty.h>
47 namespace QmlDesigner {
49 ItemCreatorTool::ItemCreatorTool(FormEditorView *editorView)
50 : AbstractFormEditorTool(editorView),
51 m_rubberbandSelectionManipulator(editorView->scene()->manipulatorLayerItem(), editorView)
58 ItemCreatorTool::~ItemCreatorTool()
62 void ItemCreatorTool::mousePressEvent(const QList<QGraphicsItem*> &/*itemList*/,
63 QGraphicsSceneMouseEvent *event)
65 if (event->button() == Qt::LeftButton)
66 m_rubberbandSelectionManipulator.begin(event->scenePos());
68 view()->deActivateItemCreator();
71 void ItemCreatorTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
72 QGraphicsSceneMouseEvent *event)
74 view()->setCursor(Qt::CrossCursor);
75 if (m_rubberbandSelectionManipulator.isActive()) {
76 m_rubberbandSelectionManipulator.update(event->scenePos());
82 void ItemCreatorTool::hoverMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
83 QGraphicsSceneMouseEvent * /*event*/)
87 void ItemCreatorTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
88 QGraphicsSceneMouseEvent *event)
90 if (event->button() == Qt::LeftButton) {
91 if (m_rubberbandSelectionManipulator.isActive()) {
92 QPointF mouseMovementVector = m_rubberbandSelectionManipulator.beginPoint() - event->scenePos();
93 if (mouseMovementVector.toPoint().manhattanLength() < QApplication::startDragDistance()) {
94 m_rubberbandSelectionManipulator.update(event->scenePos());
96 m_rubberbandSelectionManipulator.update(event->scenePos());
97 QRectF rect(m_rubberbandSelectionManipulator.beginPoint(), event->scenePos());
99 m_rubberbandSelectionManipulator.end();
100 view()->deActivateItemCreator();
104 view()->deActivateItemCreator();
108 void ItemCreatorTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &/*itemList*/,
109 QGraphicsSceneMouseEvent * /*event*/)
114 void ItemCreatorTool::keyPressEvent(QKeyEvent *event)
116 switch(event->key()) {
118 view()->deActivateItemCreator();
123 void ItemCreatorTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
128 void ItemCreatorTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
133 void ItemCreatorTool::clear()
135 m_rubberbandSelectionManipulator.clear();
138 void ItemCreatorTool::selectedItemsChanged(const QList<FormEditorItem*> &/*itemList*/)
142 void ItemCreatorTool::formEditorItemsChanged(const QList<FormEditorItem*> &/*itemList*/)
146 void ItemCreatorTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
150 void ItemCreatorTool::setItemString(const QString &itemString)
152 m_itemString = itemString;
155 FormEditorItem* ItemCreatorTool::calculateContainer(const QPointF &point)
157 QList<QGraphicsItem *> list = scene()->items(point);
158 foreach (QGraphicsItem *item, list) {
159 FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
161 return formEditorItem;
166 void ItemCreatorTool::createAtItem(const QRectF &rect)
168 QPointF pos = rect.topLeft();
170 QmlItemNode parentNode = view()->rootQmlItemNode();
171 if (!parentNode.isValid())
174 FormEditorItem *parentItem = calculateContainer(pos);
176 parentNode = parentItem->qmlItemNode();
177 pos = parentItem->mapFromScene(pos);
180 QStringList list = m_itemString.split(QLatin1Char('^'));
181 if (list.count() != 2)
183 if (list.first() == "item") {
184 RewriterTransaction transaction = view()->beginRewriterTransaction();
185 Q_ASSERT(false); //this code is out of date
186 ItemLibraryEntry itemLibraryEntry; // = view()->model()->metaInfo().itemLibraryInfo()->entry(list.at(1));
187 QmlItemNode newNode = view()->createQmlItemNode(itemLibraryEntry, pos, parentNode);
188 newNode.modelNode().variantProperty("width") = rect.width();
189 newNode.modelNode().variantProperty("height") = rect.height();
190 QList<QmlItemNode> nodeList;
191 nodeList.append(newNode);
192 view()->setSelectedQmlItemNodes(nodeList);
193 } else if (list.first() == "image") {
194 RewriterTransaction transaction = view()->beginRewriterTransaction();
195 QmlItemNode newNode = view()->createQmlItemNodeFromImage(list.at(1), pos, parentNode);
196 newNode.modelNode().variantProperty("width") = rect.width();
197 newNode.modelNode().variantProperty("height") = rect.height();
198 QList<QmlItemNode> nodeList;
199 nodeList.append(newNode);
200 view()->setSelectedQmlItemNodes(nodeList);