Skip to content

Commit

Permalink
Merge pull request #3461 from GNS3/fix/3441
Browse files Browse the repository at this point in the history
Fix nodes are not snapped to the grid at the moment of creation
  • Loading branch information
grossmj authored May 8, 2023
2 parents ea4a7f2 + 8042c9e commit ab6e0ce
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gns3/items/drawing_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def delete(self, skip_controller=False):

def itemChange(self, change, value):

if change == QtWidgets.QGraphicsItem.ItemPositionChange and self.isActive() and self._main_window.uiSnapToGridAction.isChecked():
if change == QtWidgets.QGraphicsItem.ItemPositionChange and self._main_window.uiSnapToGridAction.isChecked():
grid_size = self._graphics_view.drawingGridSize()
mid_x = self.boundingRect().width() / 2
value.setX((grid_size * round((value.x() + mid_x) / grid_size)) - mid_x)
Expand Down
5 changes: 4 additions & 1 deletion gns3/items/node_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def __init__(self, node):
if node.initialized():
self.createdSlot(node.id())

if self._main_window.uiSnapToGridAction.isChecked():
self.setPos(QtCore.QPointF(self._node.x() + 0.1, self._node.y()))

def updateNode(self):
"""
Sync change to the node
Expand Down Expand Up @@ -466,7 +469,7 @@ def itemChange(self, change, value):
:param value: value of the change
"""

if change == QtWidgets.QGraphicsItem.ItemPositionChange and self.isActive() and self._main_window.uiSnapToGridAction.isChecked():
if change == QtWidgets.QGraphicsItem.ItemPositionChange and self._main_window.uiSnapToGridAction.isChecked():
grid_size = self._main_window.uiGraphicsView.nodeGridSize()
mid_x = self.boundingRect().width() / 2
value.setX((grid_size * round((value.x() + mid_x) / grid_size)) - mid_x)
Expand Down
2 changes: 2 additions & 0 deletions tests/items/test_line_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def test_toSvg_negative_y(project, controller):

def test_fromSvg(project, controller):
line = LineItem(project=project)
line._main_window.uiSnapToGridAction.isChecked = lambda: False
line.setPos(50, 84)
line.fromSvg('<svg height="150" width="250"><line x1="0" y1="0" x2="250" y2="150" stroke-width="5" stroke="#0000ff" stroke-dasharray="5, 25, 25" /></svg>')
assert line.line().x1() == 0
Expand All @@ -75,6 +76,7 @@ def test_fromSvg(project, controller):

def test_fromSvg_top_direction(project, controller):
line = LineItem(project=project)
line._main_window.uiSnapToGridAction.isChecked = lambda: False
line.setPos(50, 84)
line.fromSvg('<svg height="150" width="250"><line x1="0" y1="150" x2="250" y2="0" stroke-width="5" stroke="#0000ff" stroke-dasharray="5, 25, 25" /></svg>')
assert line.line().x1() == 0
Expand Down

0 comments on commit ab6e0ce

Please sign in to comment.