Skip to content

Commit

Permalink
Merge pull request #98 from hsorby/main
Browse files Browse the repository at this point in the history
Improve workflow scaling
  • Loading branch information
hsorby authored Oct 8, 2023
2 parents d0688b5 + 838450d commit 35d7d19
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 90 deletions.
1 change: 1 addition & 0 deletions src/mapclient/core/managers/undomanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
along with MAP Client. If not, see <http://www.gnu.org/licenses/>..
"""


class UndoManager(object):
"""
This class is the undo redo manager for multiple undo stacks. It is a
Expand Down
7 changes: 4 additions & 3 deletions src/mapclient/core/managers/workflowmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ def is_restricted(location):

return is_workflow_in_use(location)

def load(self, location):
def load(self, location, scene_rect=QtCore.QPointF(0, 0)):
"""
Open a workflow from the given location.
:param location:
:param scene_rect: Rectangle of the scene rect to load the workflow into.
"""
if location is None:
raise WorkflowError('No location given to open Workflow.')
Expand All @@ -227,7 +228,7 @@ def load(self, location):
self.set_location(location)
if self._scene.is_loadable(wf):
if mark_workflow_in_use(location):
self._scene.load_state(wf)
self._scene.load_state(wf, scene_rect)
else:
logger.warning('Workflow is already in use.')
raise WorkflowError('Workflow is already in use.')
Expand All @@ -251,7 +252,7 @@ def load(self, location):

if self._scene.is_loadable(wf):
if mark_workflow_in_use(location):
self._scene.load_state(wf)
self._scene.load_state(wf, scene_rect)
else:
logger.warning('Workflow is already in use.')
raise WorkflowError('Workflow is already in use.')
Expand Down
10 changes: 6 additions & 4 deletions src/mapclient/core/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,12 @@ def _log_event(self, event):
]
}

response = requests.post(self._base_url, json=event_data)

if not response.ok:
logger.info(f"Event response: {event['name']} - {response.status_code}")
try:
response = requests.post(self._base_url, json=event_data)
if not response.ok:
logger.info(f"Event response: {event['name']} - {response.status_code}")
except requests.ConnectionError:
logger.info(f"Event logging failed: {event['name']}")


metrics_logger = MetricsLogger()
Expand Down
12 changes: 7 additions & 5 deletions src/mapclient/core/workflow/workflowscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

class WorkflowScene(object):
"""
This is the authoratative model for the workflow scene.
This is the authoritative model for the workflow scene.
"""

def __init__(self, manager):
Expand All @@ -39,7 +39,8 @@ def __init__(self, manager):
self._items = {}
self._dependencyGraph = WorkflowDependencyGraph(self)
self._main_window = None
self._view_parameters = {'rect': QtCore.QRectF(0, 0, 1024, 880)}
self._default_view_rect = QtCore.QRectF(0, 0, 1024, 880)
self._view_parameters = None

def getViewParameters(self):
return self._view_parameters
Expand Down Expand Up @@ -184,19 +185,20 @@ def doStepReport(self, ws):

return report

def load_state(self, ws):
def load_state(self, ws, scene_rect):
self.clear()
ws.beginGroup('view')
loaded_view_parameters = {
'scale': float(ws.value('scale', '1.0')),
'rect': ws.value('rect', self._view_parameters['rect']),
'rect': ws.value('rect', self._default_view_rect),
'transform': ws.value('transform')
}
ws.endGroup()

# Scale the WorkflowScene view-parameters:
current_rect = self._view_parameters['rect']
current_rect = scene_rect
loaded_rect = loaded_view_parameters['rect']

scale_factor = loaded_view_parameters['scale']
if scale_factor != 1.0:
current_rect.setWidth(current_rect.width() / scale_factor)
Expand Down
9 changes: 9 additions & 0 deletions src/mapclient/core/workflow/workflowutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from PySide6 import QtCore


def convert_to_parameterised_position(bounding_rect, raw_position, offset):
return QtCore.QPointF(raw_position.x() / (bounding_rect.width() - offset.x()), raw_position.y() / (bounding_rect.height() - offset.y()))


def revert_parameterised_position(bounding_rect, parameterised_position, offset):
return QtCore.QPointF((bounding_rect.width() - offset.x()) * parameterised_position.x(), (bounding_rect.height() - offset.y()) * parameterised_position.y())
36 changes: 25 additions & 11 deletions src/mapclient/view/workflow/workflowgraphicsitems.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from mapclient.core.annotations import PROVIDES_ANNOTATIONS, USES_ANNOTATIONS, ANNOTATION_BASE
from mapclient.core.workflow.workflowscene import Connection
from mapclient.core.workflow.workflowutils import convert_to_parameterised_position
from mapclient.tools.annotation.annotationdialog import AnnotationDialog
from mapclient.tools.pmr.pmrdvcshelper import repositoryIsUpToDate
from mapclient.view.utils import is_light_mode
Expand Down Expand Up @@ -244,6 +245,7 @@ class Node(Item):
def __init__(self, metastep):
Item.__init__(self)

self._margin = 2.0
self._metastep = metastep
icon = self._metastep.getStep().getIcon()
if not icon:
Expand All @@ -253,6 +255,7 @@ def __init__(self, metastep):
.scaled(self.Size, self.Size, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.FastTransformation)

self._step_port_items = []
self._parameterised_pos = QtCore.QPointF(0, 0)
self._text = StepText(metastep.getStep().getName(), self)
self._updateTextIcon()

Expand Down Expand Up @@ -366,17 +369,26 @@ def updateDVCSIcon(self):
"""
:TODO: Update this for setting/saving output/input for step to repository
"""
if self._metastep._step.getIdentifier():
if self._metastep.getStep().getIdentifier():
if repositoryIsUpToDate(self._getStepLocation()):
self._modified_item.hide()
else:
self._modified_item.show()
else:
self._modified_item.hide()

def setPos(self, pos):
def setPos(self, pos, modify_parameterised=True):
super(Node, self).setPos(pos)
self.scene().workflowScene().setItemPos(self._metastep, pos)
scene = self.scene()
if modify_parameterised:
self._parameterised_pos = convert_to_parameterised_position(scene.sceneRect(), pos, self.offset())
self._metastep.setPos(pos)

def set_parameterised_pos(self, parameterised_pos):
self._parameterised_pos = parameterised_pos

def parameterised_pos(self):
return self._parameterised_pos

def type(self):
return Node.Type
Expand Down Expand Up @@ -414,15 +426,17 @@ def showStepName(self, show):
def metaItem(self):
return self._metastep

def offset(self):
return QtCore.QPointF(self._pixmap.width(), self._pixmap.height())

def boundingRect(self):
adjust = 2.0
return QtCore.QRectF(-adjust, -adjust,
self._pixmap.width() + 2 * adjust,
self._pixmap.height() + 2 * adjust)
return QtCore.QRectF(-self._margin, -self._margin,
self._pixmap.width() + 2 * self._margin,
self._pixmap.height() + 2 * self._margin)

def paint(self, painter, option, widget):
if option.state & QtWidgets.QStyle.State_Selected: # or self.selected:
painter.setBrush(QtCore.Qt.darkGray)
if option.state & QtWidgets.QStyle.StateFlag.State_Selected: # or self.selected:
painter.setBrush(QtCore.Qt.GlobalColor.darkGray)
painter.drawRoundedRect(self.boundingRect(), 5, 5)

# super(Node, self).paint(painter, option, widget)
Expand All @@ -433,8 +447,8 @@ def paint(self, painter, option, widget):

def itemChange(self, change, value):
if change == QtWidgets.QGraphicsItem.GraphicsItemChange.ItemPositionChange and self.scene():
return self.scene().ensureItemInScene(self, value)
elif change == QtWidgets.QGraphicsItem.ItemPositionHasChanged:
return self.scene().ensure_item_in_scene(self, value)
elif change == QtWidgets.QGraphicsItem.GraphicsItemChange.ItemPositionHasChanged:
for port_item in self._step_port_items:
port_item.itemChange(change, value)

Expand Down
39 changes: 20 additions & 19 deletions src/mapclient/view/workflow/workflowgraphicsscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,31 @@ def updateModel(self):

self._previousSelection = self.selectedItems()

def ensureItemInScene(self, item, newPos):
def ensure_item_in_scene(self, item, new_pos):
if self._is_ready:
bRect = item.boundingRect()
xp1 = bRect.x() + newPos.x()
yp1 = bRect.y() + newPos.y()
xp2 = bRect.x() + bRect.width() + newPos.x()
yp2 = bRect.y() + bRect.height() + newPos.y()
xp1 = bRect.x() + new_pos.x()
yp1 = bRect.y() + new_pos.y()
xp2 = xp1 + bRect.width()
yp2 = yp1 + bRect.height()
bRect.setCoords(xp1, yp1, xp2, yp2)
offset = item.offset()
rect = self.sceneRect()
if not rect.contains(bRect):
x1 = max(bRect.left(), rect.left()) + 2.0 # plus bounding rectangle adjust
x2 = min(bRect.x() + bRect.width(), rect.x() + rect.width()) - bRect.width() + 2.0
y1 = max(bRect.top(), rect.top()) + 2.0 # plus bounding rectangle adjust
y2 = min(bRect.bottom(), rect.bottom()) - bRect.height() + 2.0
if newPos.x() != x1:
newPos.setX(x1)
elif newPos.x() != x2:
newPos.setX(x2)
if newPos.y() != y1:
newPos.setY(y1)
elif newPos.y() != y2:
newPos.setY(y2)

return newPos
x1 = max(bRect.left(), rect.left())
y1 = max(bRect.top(), rect.top())
x2 = min(bRect.right(), rect.right()) - offset.x()
y2 = min(bRect.bottom(), rect.bottom()) - offset.y()
if new_pos.x() <= x1:
new_pos.setX(x1)
elif new_pos.x() >= x2:
new_pos.setX(x2)
if new_pos.y() <= y1:
new_pos.setY(y1)
elif new_pos.y() >= y2:
new_pos.setY(y2)

return new_pos

def clear(self):
QtWidgets.QGraphicsScene.clear(self)
Expand Down
Loading

0 comments on commit 35d7d19

Please sign in to comment.