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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 #ifndef VSI_GRAPHICSSCENE_HH
51 #define VSI_GRAPHICSSCENE_HH
52 
53 //== INCLUDES =================================================================
54 
55 #include <QGraphicsScene>
56 #include <QPointF>
57 #include <QDomDocument>
58 #include <QDomElement>
59 #include <QXmlQuery>
60 
61 //== FORWARDDECLARATIONS ======================================================
62 class QGraphicsRectItem;
63 class QMimeData;
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 namespace VSI {
69 
70 //== FORWARDDECLARATIONS ======================================================
71 class Context;
72 class SceneElement;
73 class SceneTools;
74 class ElementArea;
75 class WayFind;
76 class ElementFunction;
77 class GraphicsView;
78 class Connection;
79 
80 
81 //== CLASS DEFINITION =========================================================
82 
86 class GraphicsScene : public QGraphicsScene
87 {
88  Q_OBJECT
89 
90  public:
91 
93  GraphicsScene (VSI::Context *_ctx, ElementFunction *_function = NULL);
94 
96  ~GraphicsScene ();
97 
99  GraphicsView *graphicsView () { return view_; };
100 
102  qreal getNewZ ();
103 
105  void moveElements (qreal _dx, qreal _dy, bool _selected = false);
106 
108  void scaleElements (qreal _delta);
109 
111  void scaleElements (qreal _delta, QPointF _center);
112 
114  void addElement (SceneElement *_element);
115 
117  bool removeElement (SceneElement *_element);
118 
120  void mouseMove (QPointF _pos);
121 
123  void mouseRelease (QPointF _pos, QGraphicsItem *_item);
124 
126  QRectF elementsBoundingRect ();
127 
129  ElementArea *elementArea () const { return elementArea_; };
130 
132  const QList<SceneElement *>& elements () const;
133 
135  QString generateCode (QString &errors, bool _codeOnly = false);
136 
138  WayFind *wayFind () { return wayFind_; };
139 
141  void saveToXml (QDomDocument &_doc, QDomElement &_root);
142 
144  void loadFromXml (QXmlQuery &_xml);
145 
147  ElementFunction *function () { return function_; };
148 
150  void setActiveConnection (Connection *_conn) { activeConnection_ = _conn; };
151 
152  signals:
154  void contentChanged ();
155 
156  public slots:
158  void contentChange ();
159 
161  void clearScene (bool _start = false);
162 
163  protected:
164 
165  // draw background
166  virtual void drawBackground(QPainter *_painter, const QRectF &_rect);
167 
168  // handle draging of elements from tool box
169  void dragEnterEvent (QGraphicsSceneDragDropEvent *_event);
170  void dragLeaveEvent (QGraphicsSceneDragDropEvent *_event);
171  void dragMoveEvent (QGraphicsSceneDragDropEvent *_event);
172  void dropEvent (QGraphicsSceneDragDropEvent *_event);
173 
174  // handle mouse for selection
175  void mousePressEvent (QGraphicsSceneMouseEvent *_event);
176  void mouseMoveEvent (QGraphicsSceneMouseEvent *_event);
177  void mouseReleaseEvent (QGraphicsSceneMouseEvent *_event);
178 
179  private slots:
180  // update all connections in the scene
181  void updateConnections ();
182 
183  // scene has been resized
184  void sceneResized (const QRectF &_rect);
185 
186  private:
187 
188  // Mark all inputs that are connected to the outputs of _from as valid (used during code generation)
189  QString updateConnections (SceneElement *_from, bool _isStart);
190 
191  // Checks for supported mimetype
192  bool validMimeData (const QMimeData *);
193 
194  // gets the element name from the mimetype data
195  QString mimeDataElementName (const QMimeData *);
196 
197  // gets the mouse hotspot from the mimetype data
198  QPoint mimeDataPoint (const QMimeData *);
199 
200  // load element from xml
201  void loadElement (QXmlQuery &_xml);
202 
203  // load connection from xml
204  void loadConnection (QXmlQuery &_xml);
205 
206  // returns all scene elements. Also all elements of sub-functions
207  QList<SceneElement *> getAllElements ();
208 
209  private:
210 
211  Context *ctx_;
212  ElementFunction *function_;
213  GraphicsView *view_;
214 
215  SceneElement *dndElement_;
216  SceneTools *tools_;
217 
218  ElementArea *elementArea_;
219 
220  QGraphicsRectItem *selection_;
221  QPointF selectionStart_;
222  bool selectionActive_;
223 
224  bool dontMoveStart_;
225  bool blockChanges_;
226 
227  Connection *activeConnection_;
228 
229  qreal currentZ_;
230 
231  WayFind *wayFind_;
232 };
233 
234 //=============================================================================
235 }
236 //=============================================================================
237 
238 #endif
239 
QString generateCode(QString &errors, bool _codeOnly=false)
Generate textual script code (_codeOnly = only pure code block)
void loadFromXml(QXmlQuery &_xml)
Load from xml.
ElementArea * elementArea() const
Element area.
GraphicsScene(VSI::Context *_ctx, ElementFunction *_function=NULL)
Constructor.
void scaleElements(qreal _delta)
Scale all elements with scaling center in the center of the scene.
void addElement(SceneElement *_element)
Add the element to the scene.
void saveToXml(QDomDocument &_doc, QDomElement &_root)
Save to xml.
QRectF elementsBoundingRect()
Bounding rectangle of all scene elements.
void contentChanged()
Informs about content changes.
GraphicsView * graphicsView()
Graphics view of the scene.
~GraphicsScene()
Destructor.
void contentChange()
handle content changes
qreal getNewZ()
Returns a new Z value that is above all elements.
void moveElements(qreal _dx, qreal _dy, bool _selected=false)
Moves all elements.
void mouseMove(QPointF _pos)
Redirect mouse movement to tools area.
const QList< SceneElement * > & elements() const
Scene elements.
void mouseRelease(QPointF _pos, QGraphicsItem *_item)
Redirect mouse release to tools area.
void setActiveConnection(Connection *_conn)
Sets the active connection.
void clearScene(bool _start=false)
clear the whole scene (_start = keep start element)
WayFind * wayFind()
WayFind object.
bool removeElement(SceneElement *_element)
Remove the element from the scene.