OSDN Git Service

473b3303fb574163a94fd0700048ff981a3b810d
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qmldesigner / components / formeditor / itemcreatortool.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "itemcreatortool.h"
35 #include "formeditorscene.h"
36 #include "formeditorview.h"
37
38 #include <metainfo.h>
39 #include <itemlibrarywidget.h>
40
41 #include <QApplication>
42 #include <QGraphicsSceneMouseEvent>
43 #include <QtDebug>
44 #include <QClipboard>
45 #include <variantproperty.h>
46
47 namespace QmlDesigner {
48
49 ItemCreatorTool::ItemCreatorTool(FormEditorView *editorView)
50     : AbstractFormEditorTool(editorView),
51     m_rubberbandSelectionManipulator(editorView->scene()->manipulatorLayerItem(), editorView)
52
53 {
54
55 }
56
57
58 ItemCreatorTool::~ItemCreatorTool()
59 {
60 }
61
62 void ItemCreatorTool::mousePressEvent(const QList<QGraphicsItem*> &/*itemList*/,
63                                     QGraphicsSceneMouseEvent *event)
64 {
65     if (event->button() == Qt::LeftButton)
66         m_rubberbandSelectionManipulator.begin(event->scenePos());
67     else
68         view()->deActivateItemCreator();
69 }
70
71 void ItemCreatorTool::mouseMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
72                                    QGraphicsSceneMouseEvent *event)
73 {
74     view()->setCursor(Qt::CrossCursor);
75     if (m_rubberbandSelectionManipulator.isActive()) {
76         m_rubberbandSelectionManipulator.update(event->scenePos());
77     } else {
78
79     }
80 }
81
82 void ItemCreatorTool::hoverMoveEvent(const QList<QGraphicsItem*> &/*itemList*/,
83                         QGraphicsSceneMouseEvent * /*event*/)
84 {
85 }
86
87 void ItemCreatorTool::mouseReleaseEvent(const QList<QGraphicsItem*> &/*itemList*/,
88                                       QGraphicsSceneMouseEvent *event)
89 {
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());
95             } else {
96                 m_rubberbandSelectionManipulator.update(event->scenePos());
97                 QRectF rect(m_rubberbandSelectionManipulator.beginPoint(), event->scenePos());
98                 createAtItem(rect);
99                 m_rubberbandSelectionManipulator.end();
100                 view()->deActivateItemCreator();
101             }
102         }
103     } else {
104         view()->deActivateItemCreator();
105     }
106 }
107
108 void ItemCreatorTool::mouseDoubleClickEvent(const QList<QGraphicsItem*> &/*itemList*/,
109                                           QGraphicsSceneMouseEvent * /*event*/)
110 {
111
112 }
113
114 void ItemCreatorTool::keyPressEvent(QKeyEvent *event)
115 {
116     switch(event->key()) {
117         case Qt::Key_Escape:
118             view()->deActivateItemCreator();
119             break;
120     }
121 }
122
123 void ItemCreatorTool::keyReleaseEvent(QKeyEvent * /*keyEvent*/)
124 {
125
126 }
127
128 void ItemCreatorTool::itemsAboutToRemoved(const QList<FormEditorItem*> &/*itemList*/)
129 {
130
131 }
132
133 void ItemCreatorTool::clear()
134 {
135     m_rubberbandSelectionManipulator.clear();
136 }
137
138 void ItemCreatorTool::selectedItemsChanged(const QList<FormEditorItem*> &/*itemList*/)
139 {
140 }
141
142 void ItemCreatorTool::formEditorItemsChanged(const QList<FormEditorItem*> &/*itemList*/)
143 {
144 }
145
146 void ItemCreatorTool::instancesCompleted(const QList<FormEditorItem*> &/*itemList*/)
147 {
148 }
149
150 void ItemCreatorTool::setItemString(const QString &itemString)
151 {
152     m_itemString = itemString;
153 }
154
155 FormEditorItem* ItemCreatorTool::calculateContainer(const QPointF &point)
156 {
157     QList<QGraphicsItem *> list = scene()->items(point);
158     foreach (QGraphicsItem *item, list) {
159          FormEditorItem *formEditorItem = FormEditorItem::fromQGraphicsItem(item);
160          if (formEditorItem)
161              return formEditorItem;
162     }
163     return 0;
164 }
165
166 void ItemCreatorTool::createAtItem(const QRectF &rect)
167 {
168     QPointF pos = rect.topLeft();
169
170     QmlItemNode parentNode = view()->rootQmlItemNode();
171     if (!parentNode.isValid())
172         return;
173
174     FormEditorItem *parentItem = calculateContainer(pos);
175     if (parentItem) {
176         parentNode = parentItem->qmlItemNode();
177         pos = parentItem->mapFromScene(pos);
178     }
179
180     QStringList list = m_itemString.split(QLatin1Char('^'));
181     if (list.count() != 2)
182         return;
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);
201     }
202 }
203
204 } //QmlDesigner