Developer Documentation
graphicsScene.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 #ifndef VSI_GRAPHICSSCENE_HH
45 #define VSI_GRAPHICSSCENE_HH
46 
47 //== INCLUDES =================================================================
48 
49 #include <QGraphicsScene>
50 #include <QPointF>
51 #include <QDomDocument>
52 #include <QDomElement>
53 #include <QXmlQuery>
54 
55 //== FORWARDDECLARATIONS ======================================================
56 class QGraphicsRectItem;
57 class QMimeData;
58 
59 
60 //== NAMESPACES ===============================================================
61 
62 namespace VSI {
63 
64 //== FORWARDDECLARATIONS ======================================================
65 class Context;
66 class SceneElement;
67 class SceneTools;
68 class ElementArea;
69 class WayFind;
70 class ElementFunction;
71 class GraphicsView;
72 class Connection;
73 
74 
75 //== CLASS DEFINITION =========================================================
76 
80 class GraphicsScene : public QGraphicsScene
81 {
82  Q_OBJECT
83 
84  public:
85 
87  GraphicsScene (VSI::Context *_ctx, ElementFunction *_function = NULL);
88 
90  ~GraphicsScene ();
91 
93  GraphicsView *graphicsView () { return view_; };
94 
96  qreal getNewZ ();
97 
99  void moveElements (qreal _dx, qreal _dy, bool _selected = false);
100 
102  void scaleElements (qreal _delta);
103 
105  void scaleElements (qreal _delta, QPointF _center);
106 
108  void addElement (SceneElement *_element);
109 
111  bool removeElement (SceneElement *_element);
112 
114  void mouseMove (QPointF _pos);
115 
117  void mouseRelease (QPointF _pos, QGraphicsItem *_item);
118 
120  QRectF elementsBoundingRect ();
121 
123  ElementArea *elementArea () const { return elementArea_; };
124 
126  const QList<SceneElement *>& elements () const;
127 
129  QString generateCode (QString &errors, bool _codeOnly = false);
130 
132  WayFind *wayFind () { return wayFind_; };
133 
135  void saveToXml (QDomDocument &_doc, QDomElement &_root);
136 
138  void loadFromXml (QXmlQuery &_xml);
139 
141  ElementFunction *function () { return function_; };
142 
144  void setActiveConnection (Connection *_conn) { activeConnection_ = _conn; };
145 
146  signals:
148  void contentChanged ();
149 
150  public slots:
152  void contentChange ();
153 
155  void clearScene (bool _start = false);
156 
157  protected:
158 
159  // draw background
160  virtual void drawBackground(QPainter *_painter, const QRectF &_rect);
161 
162  // handle draging of elements from tool box
163  void dragEnterEvent (QGraphicsSceneDragDropEvent *_event);
164  void dragLeaveEvent (QGraphicsSceneDragDropEvent *_event);
165  void dragMoveEvent (QGraphicsSceneDragDropEvent *_event);
166  void dropEvent (QGraphicsSceneDragDropEvent *_event);
167 
168  // handle mouse for selection
169  void mousePressEvent (QGraphicsSceneMouseEvent *_event);
170  void mouseMoveEvent (QGraphicsSceneMouseEvent *_event);
171  void mouseReleaseEvent (QGraphicsSceneMouseEvent *_event);
172 
173  private slots:
174  // update all connections in the scene
175  void updateConnections ();
176 
177  // scene has been resized
178  void sceneResized (const QRectF &_rect);
179 
180  private:
181 
182  // Mark all inputs that are connected to the outputs of _from as valid (used during code generation)
183  QString updateConnections (SceneElement *_from, bool _isStart);
184 
185  // Checks for supported mimetype
186  bool validMimeData (const QMimeData *);
187 
188  // gets the element name from the mimetype data
189  QString mimeDataElementName (const QMimeData *);
190 
191  // gets the mouse hotspot from the mimetype data
192  QPoint mimeDataPoint (const QMimeData *);
193 
194  // load element from xml
195  void loadElement (QXmlQuery &_xml);
196 
197  // load connection from xml
198  void loadConnection (QXmlQuery &_xml);
199 
200  // returns all scene elements. Also all elements of sub-functions
201  QList<SceneElement *> getAllElements ();
202 
203  private:
204 
205  Context *ctx_;
206  ElementFunction *function_;
207  GraphicsView *view_;
208 
209  SceneElement *dndElement_;
210  SceneTools *tools_;
211 
212  ElementArea *elementArea_;
213 
214  QGraphicsRectItem *selection_;
215  QPointF selectionStart_;
216  bool selectionActive_;
217 
218  bool dontMoveStart_;
219  bool blockChanges_;
220 
221  Connection *activeConnection_;
222 
223  qreal currentZ_;
224 
225  WayFind *wayFind_;
226 };
227 
228 //=============================================================================
229 }
230 //=============================================================================
231 
232 #endif
233 
void scaleElements(qreal _delta)
Scale all elements with scaling center in the center of the scene.
const QList< SceneElement * > & elements() const
Scene elements.
void loadFromXml(QXmlQuery &_xml)
Load from xml.
void contentChange()
handle content changes
QString generateCode(QString &errors, bool _codeOnly=false)
Generate textual script code (_codeOnly = only pure code block)
GraphicsView * graphicsView()
Graphics view of the scene.
ElementArea * elementArea() const
Element area.
bool removeElement(SceneElement *_element)
Remove the element from the scene.
QRectF elementsBoundingRect()
Bounding rectangle of all scene elements.
void setActiveConnection(Connection *_conn)
Sets the active connection.
void moveElements(qreal _dx, qreal _dy, bool _selected=false)
Moves all elements.
void contentChanged()
Informs about content changes.
void mouseMove(QPointF _pos)
Redirect mouse movement to tools area.
void mouseRelease(QPointF _pos, QGraphicsItem *_item)
Redirect mouse release to tools area.
void addElement(SceneElement *_element)
Add the element to the scene.
GraphicsScene(VSI::Context *_ctx, ElementFunction *_function=NULL)
Constructor.
void clearScene(bool _start=false)
clear the whole scene (_start = keep start element)
qreal getNewZ()
Returns a new Z value that is above all elements.
WayFind * wayFind()
WayFind object.
~GraphicsScene()
Destructor.
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.