1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 #pragma once
26 
27 #include <qmlitemnode.h>
28 #include "abstractformeditortool.h"
29 
30 #include <QGraphicsScene>
31 #include <QPointer>
32 #include <QHash>
33 #include <QElapsedTimer>
34 
35 QT_BEGIN_NAMESPACE
36 class QGraphicsSceneMouseEvent;
37 QT_END_NAMESPACE
38 
39 namespace QmlDesigner {
40 
41 class FormEditorWidget;
42 class FormEditorItem;
43 class FormEditorView;
44 class LayerItem;
45 
46 class QMLDESIGNERCORE_EXPORT FormEditorScene : public QGraphicsScene
47 {
48     Q_OBJECT
49 
50     friend FormEditorItem;
51     friend FormEditorView;
52 
53 public:
54 
55     enum ItemType {
56         Default,
57         Flow,
58         FlowAction,
59         FlowTransition,
60         FlowDecision,
61         FlowWildcard
62     };
63 
64     FormEditorScene(FormEditorWidget *widget, FormEditorView *editorView);
65     ~FormEditorScene() override;
66     FormEditorItem *addFormEditorItem(const QmlItemNode &qmlItemNode, ItemType type = Default);
67 
68     FormEditorItem* itemForQmlItemNode(const QmlItemNode &qmlItemNode) const;
69 
70     QList<FormEditorItem*> itemsForQmlItemNodes(const QList<QmlItemNode> &nodeList) const;
71     QList<FormEditorItem*> allFormEditorItems() const;
72 
73     void updateAllFormEditorItems();
74 
75     void setupScene();
76     void resetScene();
77 
78     double canvasWidth() const;
79     double canvasHeight() const;
80 
81     void synchronizeTransformation(FormEditorItem *item);
82     void synchronizeParent(const QmlItemNode &qmlItemNode);
83     void synchronizeOtherProperty(FormEditorItem *item, const QByteArray &propertyName);
84 
85     FormEditorItem* calulateNewParent(FormEditorItem *widget);
86     LayerItem* manipulatorLayerItem() const;
87     LayerItem* formLayerItem() const;
88     FormEditorView *editorView() const;
89 
90     FormEditorItem *rootFormEditorItem() const;
91 
92     void reparentItem(const QmlItemNode &node, const QmlItemNode &newParent);
93 
94     void clearFormEditorItems();
95 
96     void highlightBoundingRect(FormEditorItem *formEditorItem);
97 
98     void setShowBoundingRects(bool show);
99     bool showBoundingRects() const;
100 
101     bool annotationVisibility() const;
102     void setAnnotationVisibility(bool status);
103 
104 protected:
105     bool event(QEvent *event) override;
106     void dropEvent(QGraphicsSceneDragDropEvent * event) override;
107     void dragEnterEvent(QGraphicsSceneDragDropEvent * event) override;
108     void dragLeaveEvent(QGraphicsSceneDragDropEvent * event) override;
109     void dragMoveEvent(QGraphicsSceneDragDropEvent * event) override;
110     void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
111     void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
112     void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
113     void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
114 
115     void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
116     void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
117     void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
118 
119     void keyPressEvent(QKeyEvent *keyEvent) override;
120     void keyReleaseEvent(QKeyEvent *keyEvent) override;
121 
122     void focusOutEvent(QFocusEvent *focusEvent) override;
123     void focusInEvent(QFocusEvent *focusEvent) override;
124 
125 private:
126     QList<QGraphicsItem *> removeLayerItems(const QList<QGraphicsItem *> &itemList);
127     QList<QGraphicsItem *> itemsAt(const QPointF &pos);
128 
129     AbstractFormEditorTool* currentTool() const;
130     void removeItemFromHash(FormEditorItem*);
131 
132     FormEditorView *m_editorView;
133     AbstractFormEditorTool *m_currentTool;
134     QHash<QmlItemNode, FormEditorItem*> m_qmlItemNodeItemHash;
135     QPointer<LayerItem> m_formLayerItem;
136     QPointer<LayerItem> m_manipulatorLayerItem;
137     ModelNode m_dragNode;
138     bool m_showBoundingRects;
139     bool m_annotationVisibility;
140     QElapsedTimer m_usageTimer;
141 };
142 
143 } // namespace QmlDesigner
144