Skip to content

Commit 5b98103

Browse files
committed
1.Fixed broken placement of nodes in view, 2.Warning instead of crash upon attempt to move a node which doesnt exist.
1 parent 28f1cac commit 5b98103

6 files changed

Lines changed: 49 additions & 22 deletions

File tree

plugins/gui/include/gui/graph_widget/contexts/graph_context.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,10 @@ namespace hal
325325
}
326326

327327
/**
328-
* Move node to antother grid location
329-
*/
330-
void moveNodeAction(const QPoint& from, const QPoint& to);
328+
* Update placement of nodes already existing in view
329+
* @param plc - the placement hash (Node -> QPoint)
330+
*/
331+
void updatePlacement(const GridPlacement& plc);
331332

332333
/**
333334
* Returns whether the scene is in an updating process (i.e. layouter process) or not.

plugins/gui/include/gui/graph_widget/layouters/graph_layouter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ namespace hal
284284

285285
void dumpNodePositions(const QPoint& search) const;
286286

287+
void updatePlacement(const GridPlacement& plc);
288+
287289
void setNodePosition(const Node& n, const QPoint& p);
288290
void swapNodePositions(const Node& n1, const Node& n2);
289291
void removeNodeFromMaps(const Node& n);

plugins/gui/include/gui/gui_def.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ namespace hal
144144
GridPlacement(const QHash<hal::Node,QPoint>& data) : QHash<hal::Node,QPoint>(data) {;}
145145

146146
void setGatePosition(u32 gateId, std::pair<int,int>p, bool swap = false) {
147-
QPoint pos = QPoint(gatePosition(gateId)->first, gatePosition(gateId)->second); //position of current gate to move
147+
std::pair<int,int>* posPtr = gatePosition(gateId);
148+
if (!posPtr)
149+
{
150+
log_warning("gui", "Gate id {} cannot be moved, not found in current placement", gateId);
151+
return;
152+
}
153+
QPoint pos = QPoint(posPtr->first, posPtr->second); //position of current gate to move
148154
hal::Node nd = key(QPoint(p.first, p.second)); //find the node in the destination
149155

150156
if(!nd.isNull() && !swap) //if the destination placement is not available
@@ -159,8 +165,15 @@ namespace hal
159165
else
160166
operator[](hal::Node(gateId,hal::Node::Gate)) = QPoint(p.first,p.second);
161167
}
168+
162169
void setModulePosition(u32 moduleId, std::pair<int,int>p, bool swap = false){
163-
QPoint pos = QPoint(modulePosition(moduleId)->first, modulePosition(moduleId)->second);
170+
std::pair<int,int>* posPtr = modulePosition(moduleId);
171+
if (!posPtr)
172+
{
173+
log_warning("gui", "Module id {} cannot be moved, not found in current placement", moduleId);
174+
return;
175+
}
176+
QPoint pos = QPoint(posPtr->first, posPtr->second); //position of current module to move
164177
hal::Node nd = key(QPoint(p.first, p.second));
165178

166179
if(!nd.isNull() && !swap)
@@ -174,11 +187,13 @@ namespace hal
174187
}
175188
else
176189
operator[](hal::Node(moduleId,hal::Node::Module)) = QPoint(p.first,p.second);};
190+
177191
std::pair<int,int>* gatePosition(u32 gateId) const
178192
{
179193
auto it = constFind(hal::Node(gateId,hal::Node::Gate));
180194
return (it == constEnd() ? nullptr : new std::pair<int,int>(it->x(),it->y()));
181195
}
196+
182197
std::pair<int,int>* modulePosition(u32 moduleId) const
183198
{
184199
auto it = constFind(hal::Node(moduleId,hal::Node::Module));

plugins/gui/src/graph_widget/contexts/graph_context.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,12 +533,9 @@ namespace hal
533533
if (mParentWidget) mParentWidget->storeViewport();
534534
}
535535

536-
void GraphContext::moveNodeAction(const QPoint& from, const QPoint& to)
536+
void GraphContext::updatePlacement(const GridPlacement& plc)
537537
{
538-
const QMap<QPoint,Node> nodeMap = mLayouter->positionToNodeMap();
539-
auto it = nodeMap.find(from);
540-
if (it==nodeMap.constEnd()) return;
541-
mLayouter->setNodePosition(it.value(),to);
538+
mLayouter->updatePlacement(plc);
542539
scheduleSceneUpdate();
543540
}
544541

plugins/gui/src/graph_widget/layouters/graph_layouter.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,29 @@ namespace hal
107107
return mPositionToNodeMap;
108108
}
109109

110+
void GraphLayouter::updatePlacement(const GridPlacement& plc)
111+
{
112+
// Keep track which positions are occupied by new nodes, possible values:
113+
// -1 : position no longer occupied
114+
// 1 : position which was not occupied before now occupied
115+
// 0 : old position taken by new node
116+
QHash<QPoint,int> positionOccupied;
117+
118+
for (auto it = plc.constBegin(); it != plc.constEnd(); ++it)
119+
{
120+
auto jt = mNodeToPositionMap.find(it.key());
121+
if (jt == mNodeToPositionMap.end()) continue;
122+
--positionOccupied[jt.value()]; // old value from mNodePositionMap
123+
++positionOccupied[it.value()]; // new value from plc
124+
jt.value() = it.value();
125+
mPositionToNodeMap[it.value()] = it.key();
126+
}
127+
128+
for (auto rpit = positionOccupied.begin(); rpit != positionOccupied.end(); ++rpit)
129+
if (rpit.value() < 0)
130+
mPositionToNodeMap.remove(rpit.key());
131+
}
132+
110133
void GraphLayouter::setNodePosition(const Node& n, const QPoint& p)
111134
{
112135
if (mNodeToPositionMap.contains(n))

plugins/gui/src/user_action/action_move_node.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,7 @@ namespace hal
160160
}
161161

162162
LayoutLocker llock;
163-
ctx->clear();
164-
165-
QSet<u32> modIds;
166-
QSet<u32> gateIds;
167-
168-
for (Node node : mGridPlacement.keys())
169-
{
170-
if(node.isModule()) modIds.insert(node.id());
171-
else gateIds.insert(node.id());
172-
}
173-
174-
ctx->add(modIds, gateIds, PlacementHint(mGridPlacement));
163+
ctx->updatePlacement(mGridPlacement);
175164
ctx->scheduleSceneUpdate();
176165

177166
return UserAction::exec();

0 commit comments

Comments
 (0)