OpenWalnut  1.5.0dev
WQtNetworkSceneLayout.cpp
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 #include "core/common/WLogger.h"
26 
27 #include "WQtNetworkEditor.h"
28 #include "WQtNetworkScene.h"
29 #include "WQtNetworkItem.h"
30 #include "WQtNetworkItemGrid.h"
31 
32 #include "WQtNetworkSceneLayout.h"
33 
35  m_grid( grid )
36 {
37  // initialize members
38 }
39 
41 {
42  // cleanup
43 }
44 
46 {
47  // do we have a default?
50  ModuleDefaultCoordinates::const_iterator defaultCoord = r->get().find( item->getModule()->getUUID() );
51  if( defaultCoord == r->get().end() )
52  {
53  // NO. No default found. Do naive placement to next free column.
54  // add in first row of the next free column
55  int nextX = m_grid->getFirstFreeColumn();
56  m_grid->setItem( item, nextX, 0 );
57  }
58  else
59  {
60  // default found. Place there and mark item as already layed out
61  m_grid->setItem( item, ( *defaultCoord ).second );
62 
63  ModuleDefaultFlags::const_iterator defaultFlags = rflags->get().find( item->getModule()->getUUID() );
64  if( defaultFlags != rflags->get().end() )
65  {
66  item->setLayedOut( ( *defaultFlags ).second.m_layedOut );
67  item->setManuallyPlaced( ( *defaultFlags ).second.m_manuallyLayedOut );
68  }
69  else // no default flag found.
70  {
71  item->setLayedOut( );
72  }
73  }
74 }
75 
77 {
78  // remove from grid
79  m_grid->removeItem( item );
80 }
81 
83 {
84  // was layouted earlier?
85  if( inItem->wasLayedOut() || inItem->wasManuallyPlaced() )
86  {
87  return;
88  }
89 
90  // get out item position
91  QPoint out = m_grid->whereIs( outItem );
92  QPoint in = m_grid->whereIs( outItem );
93 
94  // first guess: y + 1
95  QPoint target( out.x(), out.y() + 1 );
96 
97  // maybe the x position is already occupied?
98  while( m_grid->isOccupied( target ) && ( target != in ) )
99  {
100  target.rx()++;
101  }
102 
103  // move there
104  m_grid->moveItem( inItem, target );
105  inItem->setLayedOut();
106 }
107 
109 {
110  // leave item on its current position.
111 }
112 
113 void WQtNetworkSceneLayout::snapTemporarily( WQtNetworkItem* item, QPointF worldCoords, bool noPhysicalMove )
114 {
115  // the grid knows where to snap:
116  QPoint cell = m_grid->findNearestCell( worldCoords );
117  QPoint oldCell = m_grid->whereIs( item );
118 
119  if( oldCell != cell )
120  {
121  bool isUsed = m_grid->isOccupied( cell );
122  m_grid->highlightCell( cell, isUsed ? QColor( 230, 40, 40, 128 ) : QColor( 90, 220, 90, 128 ) );
123  }
124  else
125  {
126  // explicitly turn of highlight
128  }
129  if( !noPhysicalMove )
130  {
131  m_grid->physicalMoveTo( item, cell, false );
132  }
133 }
134 
135 void WQtNetworkSceneLayout::snapAccept( WQtNetworkItem* item, QPointF worldCoords )
136 {
137  // disable highlight
139 
140  // something changed?
141  QPoint newCell = m_grid->findNearestCell( worldCoords );
142  QPoint oldCell = m_grid->whereIs( item );
143 
144  if( oldCell != newCell )
145  {
146  // user moved it somewhere. Mark as already positioned. (But only if start and end cell are different)
147  item->setManuallyPlaced();
148  }
149 
150  // move in layout
151  if( !m_grid->moveItem( item, newCell ) )
152  {
153  m_grid->physicalMoveTo( item, oldCell, true );
154  }
155 }
156 
158 {
159  m_grid->blendIn();
160 }
161 
163 {
164  m_grid->blendOut();
165 }
166 
168 {
169  return m_grid->boundingRect();
170 }
171 
173 {
174  return m_grid;
175 }
176 
178 {
180  w->get()[ module->getUUID() ] = coord;
181 }
182 
183 void WQtNetworkSceneLayout::setModuleDefaultFlags( WModule::SPtr module, bool layedOut, bool manuallyLayedOut )
184 {
186  w->get()[ module->getUUID() ].m_layedOut = layedOut;
187  w->get()[ module->getUUID() ].m_manuallyLayedOut = manuallyLayedOut;
188 }
189 
std::shared_ptr< WModule > SPtr
Shared pointer to a WModule.
Definition: WModule.h:106
Implement a virtual grid for placing QGraphicsItems.
bool setItem(QGraphicsItem *item, int col, int row)
Set the specified item to the specified position.
virtual QRectF boundingRect() const
The bounding area of the item.
virtual QPoint whereIs(QGraphicsItem *item)
Find the position of the specified item.
bool moveItem(QGraphicsItem *item, int col, int row)
Move item from its current position to the specified one.
virtual QPoint findNearestCell(QPointF worldSpace)
Find the nearest cell matching the specified world coordinates.
virtual void physicalMoveTo(QGraphicsItem *item, int col, int row, bool animate=true)
Move the item to the position physically.
void blendOut()
Allows blending out the underlaying layout structure.
void blendIn()
Allows blending in the underlaying layout structure.
void highlightCell(QPoint cell, QColor color)
Highlights the specified cell.
void removeItem(int col, int row)
Remove the item at the given position.
int getFirstFreeColumn() const
Return the index of the first empty column.
bool isOccupied(int col, int row)
Is there an element at the given position?
This class represents a WModule as QGraphicsItem and contains a reference to its in- and outports.
void setLayedOut(bool layedOut=true)
Marks the item as already layed out.
void setManuallyPlaced(bool manual=true)
Mark item as manually placed.
std::shared_ptr< WModule > getModule()
Get the WModule represented by this object.
bool wasLayedOut() const
Checks whether the item was layed out earlier.
bool wasManuallyPlaced() const
Item was placed by hand.
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.
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.
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.
The scene containing the whole graph.
This class provides a common interface for thread-safe access to associative containers (set,...
ReadTicket getReadTicket() const
Returns a ticket to get read access to the contained data.
WriteTicket getWriteTicket(bool suppressNotify=false) const
Returns a ticket to get write access to the contained data.