OpenWalnut  1.5.0dev
WQtNetworkSceneLayout.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WQTNETWORKSCENELAYOUT_H
26 #define WQTNETWORKSCENELAYOUT_H
27 
28 #include <map>
29 #include <memory>
30 #include <string>
31 #include <utility>
32 
33 
34 #include "core/common/WSharedAssociativeContainer.h"
35 
36 class WQtNetworkEditor;
37 class WQtNetworkScene;
38 class WQtNetworkItem;
39 class WQtNetworkItemGrid;
40 
41 /**
42  * Layout manager class for a QGraphicsScene. As the QGraphicsScene itself does not provide virtual methods for item addition and removal, we
43  * need to write a separate layout class.
44  */
46 {
47 public:
48  /**
49  * Convenience typedef for a std::shared_ptr< WQtNetworkSceneLayout >.
50  */
51  typedef std::shared_ptr< WQtNetworkSceneLayout > SPtr;
52 
53  /**
54  * Convenience typedef for a std::shared_ptr< const WQtNetworkSceneLayout >.
55  */
56  typedef std::shared_ptr< const WQtNetworkSceneLayout > ConstSPtr;
57 
58  /**
59  * Default constructor.
60  *
61  * \param scene the scene to manage
62  * \param grid the grid to work in
63  */
65 
66  /**
67  * Destructor.
68  */
69  virtual ~WQtNetworkSceneLayout();
70 
71  /**
72  * Add an item to the layout
73  *
74  * \param item the item to add.
75  */
76  virtual void addItem( WQtNetworkItem* item );
77 
78  /**
79  * Remove the item from the layout
80  *
81  * \param item the item to remove
82  */
83  virtual void removeItem( WQtNetworkItem* item );
84 
85  /**
86  * Two module got connected.
87  *
88  * \param outItem the output module
89  * \param inItem the input module
90  */
91  virtual void connection( WQtNetworkItem* outItem, WQtNetworkItem* inItem );
92 
93  /**
94  * Two modules got disconnected.
95  *
96  * \param outItem the output module
97  * \param inItem the input module
98  */
99  void disconnection( WQtNetworkItem* outItem, WQtNetworkItem* inItem );
100 
101  /**
102  * Snap the item to the nearest layout element. The coordinates specified are in world space. The snapping is temporary. You can undo it or
103  * accept it later to actually modify layout.
104  *
105  * \param item the item
106  * \param worldCoords world space coordinates
107  * \param noPhysicalMove disable movement of item if you handle it for yourself
108  */
109  void snapTemporarily( WQtNetworkItem* item, QPointF worldCoords, bool noPhysicalMove = false );
110 
111  /**
112  * Accept the current item position and update layout accordingly.
113  *
114  * \param item the item
115  * \param worldCoords world space coordinates
116  */
117  void snapAccept( WQtNetworkItem* item, QPointF worldCoords );
118 
119  /**
120  * Allows blending in the underlaying layout structure.
121  */
122  void blendIn();
123 
124  /**
125  * Allows blending out the underlaying layout structure.
126  */
127  void blendOut();
128 
129  /**
130  * Return the bounding box of this layout.
131  *
132  * \return the bb
133  */
134  QRectF getBoundingBox();
135 
136  /**
137  * Get the grid used for the layout.
138  *
139  * \return the grid.
140  */
141  WQtNetworkItemGrid* getGrid() const;
142 
143  /**
144  * Set a default position for a given module. The layouter will position a module at the given coordinate now, or in future.
145  *
146  * \param module the module
147  * \param coord the coordinate
148  */
149  void setModuleDefaultPosition( WModule::SPtr module, QPoint coord );
150 
151  /**
152  * Set a default flag combination for the given module. The layouter might use them.
153  *
154  * \param module the module
155  * \param layedOut flag if the item was layed out already.
156  * \param manuallyLayedOut flag if the item was manually layed out.
157  */
158  void setModuleDefaultFlags( WModule::SPtr module, bool layedOut, bool manuallyLayedOut );
159 
160 protected:
161 private:
162  WQtNetworkItemGrid* m_grid; //!< we use a grid to place the items
163 
164  /**
165  * Map between module UUID and network coord
166  */
167  typedef std::map< std::string, QPoint > ModuleDefaultCoordinates;
168 
169  /**
170  * Some flags used in the layouter as defaults.
171  */
172  struct LayoutFlags
173  {
174  /**
175  * Automatically layed out
176  */
178 
179  /**
180  * Manual layout done
181  */
183  };
184 
185  /**
186  * Map between module UUID and network flags
187  */
188  typedef std::map< std::string, LayoutFlags > ModuleDefaultFlags;
189 
190  /**
191  * The type inside the map
192  */
193  typedef std::pair< std::string, QPoint > ModuleDefaultCoordinatesItem;
194 
195  /**
196  * The mapping of network coordinates for each module. This is wrapped by a thread save WSharedObject, since the loader might run in a thread
197  * that is not the GUI thread.
198  */
200 
201  /**
202  * The mapping of network flags for each module. This is wrapped by a thread save WSharedObject, since the loader might run in a thread
203  * that is not the GUI thread.
204  */
206 };
207 
208 #endif // WQTNETWORKSCENELAYOUT_H
209 
std::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:106
Container widget to hold the WQtNetworkScene.
Implement a virtual grid for placing QGraphicsItems.
This class represents a WModule as QGraphicsItem and contains a reference to its in- and outports.
Layout manager class for a QGraphicsScene.
std::map< std::string, LayoutFlags > ModuleDefaultFlags
Map between module UUID and network flags.
virtual void addItem(WQtNetworkItem *item)
Add an item to the layout.
WSharedAssociativeContainer< ModuleDefaultCoordinates > m_moduleDefaultCoords
The mapping of network coordinates for each module.
void snapAccept(WQtNetworkItem *item, QPointF worldCoords)
Accept the current item position and update layout accordingly.
void blendOut()
Allows blending out the underlaying layout structure.
std::pair< std::string, QPoint > ModuleDefaultCoordinatesItem
The type inside the map.
WSharedAssociativeContainer< ModuleDefaultFlags > m_moduleDefaultFlags
The mapping of network flags for each module.
WQtNetworkSceneLayout(WQtNetworkScene *scene, WQtNetworkItemGrid *grid)
Default constructor.
WQtNetworkItemGrid * m_grid
we use a grid to place the items
virtual void connection(WQtNetworkItem *outItem, WQtNetworkItem *inItem)
Two module got connected.
virtual void removeItem(WQtNetworkItem *item)
Remove the item from the layout.
void blendIn()
Allows blending in the underlaying layout structure.
virtual ~WQtNetworkSceneLayout()
Destructor.
void setModuleDefaultFlags(WModule::SPtr module, bool layedOut, bool manuallyLayedOut)
Set a default flag combination for the given module.
WQtNetworkItemGrid * getGrid() const
Get the grid used for the layout.
std::map< std::string, QPoint > ModuleDefaultCoordinates
Map between module UUID and network coord.
std::shared_ptr< const WQtNetworkSceneLayout > ConstSPtr
Convenience typedef for a std::shared_ptr< const WQtNetworkSceneLayout >.
void disconnection(WQtNetworkItem *outItem, WQtNetworkItem *inItem)
Two modules got disconnected.
void setModuleDefaultPosition(WModule::SPtr module, QPoint coord)
Set a default position for a given module.
QRectF getBoundingBox()
Return the bounding box of this layout.
void snapTemporarily(WQtNetworkItem *item, QPointF worldCoords, bool noPhysicalMove=false)
Snap the item to the nearest layout element.
std::shared_ptr< WQtNetworkSceneLayout > SPtr
Convenience typedef for a std::shared_ptr< WQtNetworkSceneLayout >.
The scene containing the whole graph.
Some flags used in the layouter as defaults.
bool m_manuallyLayedOut
Manual layout done.
bool m_layedOut
Automatically layed out.