Skip to content

Commit

Permalink
Merge pull request #78 from hsorby/scene-fixes
Browse files Browse the repository at this point in the history
Fix resizing of graphics scene for macOS.
  • Loading branch information
hsorby authored Sep 28, 2023
2 parents e831efd + 808bd06 commit ae3aba4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/mapclient/core/workflow/workflowscene.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(self, manager):
self._items = {}
self._dependencyGraph = WorkflowDependencyGraph(self)
self._main_window = None
self._view_parameters = None
self._view_parameters = {'rect': QtCore.QRectF(0, 0, 1024, 880)}

def getViewParameters(self):
return self._view_parameters
Expand Down
4 changes: 4 additions & 0 deletions src/mapclient/view/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ def do_runtime_error(self, *a, **kw):
logger.error('{0}: {1}'.format(e.title, e.description))
ErrorDialog(e.title, e.description, self).exec()
return do_runtime_error


def is_light_mode():
return QtGui.QGuiApplication.styleHints().colorScheme() == QtCore.Qt.ColorScheme.Light
78 changes: 40 additions & 38 deletions src/mapclient/view/workflow/workflowgraphicsitems.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from mapclient.core.workflow.workflowscene import Connection
from mapclient.tools.annotation.annotationdialog import AnnotationDialog
from mapclient.tools.pmr.pmrdvcshelper import repositoryIsUpToDate
from mapclient.view.utils import is_light_mode


def _define_tooltip_for_triples(triples):
Expand All @@ -46,7 +47,7 @@ def __init__(self, sourceNode, destNode):
self._dest = weakref.ref(destNode)
self._sourcePoint = QtCore.QPointF()
self._destPoint = QtCore.QPointF()
self._pixmap = QtGui.QPixmap(':/workflow/images/cancel_256.png').scaled(16, 16, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self._pixmap = QtGui.QPixmap(':/workflow/images/cancel_256.png').scaled(16, 16, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.FastTransformation)
self._source().addArc(self)
self._dest().addArc(self)
self.setZValue(-1.5)
Expand Down Expand Up @@ -91,7 +92,7 @@ def paint(self, painter, option, widget):
if line.length() == 0.0:
return

painter.setPen(QtGui.QPen(QtCore.Qt.black, 1, QtCore.Qt.DashLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
painter.setPen(QtGui.QPen(QtCore.Qt.GlobalColor.black, 1, QtCore.Qt.PenStyle.DashLine, QtCore.Qt.PenCapStyle.RoundCap, QtCore.Qt.PenJoinStyle.RoundJoin))
painter.drawLine(line)

painter.drawPixmap(midPoint.x() - 8, midPoint.y() - 8, self._pixmap)
Expand All @@ -105,7 +106,7 @@ class Item(QtWidgets.QGraphicsItem):
def __init__(self):
QtWidgets.QGraphicsItem.__init__(self)

self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
self.setFlag(QtWidgets.QGraphicsItem.GraphicsItemFlag.ItemIsSelectable)

def setSelected(self, selected):
QtWidgets.QGraphicsItem.setSelected(self, selected)
Expand Down Expand Up @@ -158,9 +159,9 @@ def adjust(self):
return

sourceCentre = self._source().boundingRect().center()
destCentre = self._dest().boundingRect().center()
destinationCentre = self._dest().boundingRect().center()
line = QtCore.QLineF(self.mapFromItem(self._source(), sourceCentre.x(), sourceCentre.y()),
self.mapFromItem(self._dest(), destCentre.x(), destCentre.y()))
self.mapFromItem(self._dest(), destinationCentre.x(), destinationCentre.y()))
length = line.length()

if length == 0.0:
Expand Down Expand Up @@ -200,12 +201,13 @@ def paint(self, painter, option, widget):
if line.length() == 0.0:
return

brush = QtGui.QBrush(QtCore.Qt.black)
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)
# brush = QtGui.QBrush(QtCore.Qt.red)

brush_colour = QtCore.Qt.GlobalColor.black if is_light_mode() else QtCore.Qt.GlobalColor.gray
brush = QtGui.QBrush(brush_colour)
painter.setBrush(brush)

angle = math.acos(line.dx() / line.length())
Expand All @@ -214,20 +216,20 @@ def paint(self, painter, option, widget):

# Draw the arrows if there's enough room.
if line.dy() * line.dy() + line.dx() * line.dx() > 200 * self._arrowSize:
midPoint = (self._destPoint + self._sourcePoint) / 2
midPoint = QtCore.QPointF((self._destPoint + self._sourcePoint) / 2)

destArrowP1 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi / 3) * self._arrowSize)
destArrowP2 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize)
destination_arrow_p1 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi / 3) * self._arrowSize)
destination_arrow_p2 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize)

self._arrow.clear()
self._arrow.append(midPoint)
self._arrow.append(destArrowP1)
self._arrow.append(destArrowP2)
self._arrow.append(destination_arrow_p1)
self._arrow.append(destination_arrow_p2)
painter.drawPolygon(self._arrow)

painter.setPen(QtGui.QPen(brush, 1, QtCore.Qt.SolidLine, QtCore.Qt.RoundCap, QtCore.Qt.RoundJoin))
painter.setPen(QtGui.QPen(brush, 1, QtCore.Qt.PenStyle.SolidLine, QtCore.Qt.PenCapStyle.RoundCap, QtCore.Qt.PenJoinStyle.RoundJoin))
# painter.setPen(QtGui.QPen(QtCore.Qt.SolidLine))
painter.drawLine(line)

Expand All @@ -248,16 +250,16 @@ def __init__(self, metastep):
icon = QtGui.QImage(':/workflow/images/default_step_icon.png')

self._pixmap = QtGui.QPixmap.fromImage(icon) \
.scaled(self.Size, self.Size, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
.scaled(self.Size, self.Size, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.FastTransformation)

self._step_port_items = []
self._text = StepText(metastep.getStep().getName(), self)
self._updateTextIcon()

self._setToolTip()

self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
self.setFlag(QtWidgets.QGraphicsItem.ItemSendsGeometryChanges)
self.setFlag(QtWidgets.QGraphicsItem.GraphicsItemFlag.ItemIsMovable)
self.setFlag(QtWidgets.QGraphicsItem.GraphicsItemFlag.ItemSendsGeometryChanges)
self.setCacheMode(self.CacheMode.DeviceCoordinateCache)
self.setZValue(-1)

Expand Down Expand Up @@ -430,7 +432,7 @@ def paint(self, painter, option, widget):
# painter.drawPixmap(40, 40, self._configure_red)

def itemChange(self, change, value):
if change == QtWidgets.QGraphicsItem.ItemPositionChange and self.scene():
if change == QtWidgets.QGraphicsItem.GraphicsItemChange.ItemPositionChange and self.scene():
return self.scene().ensureItemInScene(self, value)
elif change == QtWidgets.QGraphicsItem.ItemPositionHasChanged:
for port_item in self._step_port_items:
Expand All @@ -452,7 +454,7 @@ class StepPort(QtWidgets.QGraphicsEllipseItem):

def __init__(self, port, parent):
super(StepPort, self).__init__(0, 0, 11, 11, parent=parent)
self.setBrush(QtCore.Qt.black)
self.setBrush(QtCore.Qt.GlobalColor.black)
self._port = port
self._connections = []
self._pixmap = QtGui.QPixmap(':/workflow/images/icon-port.png')
Expand Down Expand Up @@ -512,7 +514,7 @@ def removeArc(self, arc):
self._connections = [weakarc for weakarc in self._connections if weakarc() != arc]

def itemChange(self, change, value):
if change == QtWidgets.QGraphicsItem.ItemPositionHasChanged:
if change == QtWidgets.QGraphicsItem.GraphicsItemChange.ItemPositionHasChanged:
self._removeDeadwood()
for arc in self._connections:
arc().adjust()
Expand All @@ -525,7 +527,7 @@ class StepText(QtWidgets.QGraphicsTextItem):

def __init__(self, *args):
super().__init__(*args)
self._editable_flags = QtCore.Qt.TextEditable | QtCore.Qt.TextSelectableByMouse | QtCore.Qt.TextSelectableByKeyboard
self._editable_flags = QtCore.Qt.TextInteractionFlag.TextEditable | QtCore.Qt.TextInteractionFlag.TextSelectableByMouse | QtCore.Qt.TextInteractionFlag.TextSelectableByKeyboard
self.setTextInteractionFlags(self._editable_flags)
self._active_key = None
self._make_connections()
Expand All @@ -545,24 +547,24 @@ def focusOutEvent(self, event):

def mousePressEvent(self, event):
modifiers = QtWidgets.QApplication.keyboardModifiers()
if modifiers == QtCore.Qt.NoModifier:
if modifiers == QtCore.Qt.KeyboardModifier.NoModifier:
self.setTextInteractionFlags(self._editable_flags)
else:
self.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)
super().mousePressEvent(event)

def keyPressEvent(self, event):
self._active_key = event.key()
if self._active_key != QtCore.Qt.Key_Return and self._active_key != QtCore.Qt.Key_Enter:
if self._active_key != QtCore.Qt.Key.Key_Return and self._active_key != QtCore.Qt.Key.Key_Enter:
super().keyPressEvent(event)

def keyReleaseEvent(self, event):
super().keyReleaseEvent(event)
if self._active_key and self._active_key == event.key():
if self._active_key == QtCore.Qt.Key_Return or self._active_key == QtCore.Qt.Key_Enter:
if self._active_key == QtCore.Qt.Key.Key_Return or self._active_key == QtCore.Qt.Key.Key_Enter:
self._update_identifier()
self.clearFocus()
self.setTextInteractionFlags(QtCore.Qt.NoTextInteraction)
self.setTextInteractionFlags(QtCore.Qt.TextInteractionFlag.NoTextInteraction)

self._active_key = None

Expand All @@ -586,7 +588,7 @@ class MercurialIcon(QtWidgets.QGraphicsItem):
def __init__(self, *args, **kwargs):
super(MercurialIcon, self).__init__(*args, **kwargs)
self._hg_yellow = QtGui.QPixmap(':/workflow/images/modified_repo.png') \
.scaled(24, 24, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
.scaled(24, 24, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.FastTransformation)
self.setToolTip('The repository has been modified')

def paint(self, painter, option, widget):
Expand All @@ -608,8 +610,8 @@ class ConfigureIcon(QtWidgets.QGraphicsItem):
def __init__(self, *args, **kwargs):
super(ConfigureIcon, self).__init__(*args, **kwargs)
self._configured = False
self._configure_green = QtGui.QPixmap(':/workflow/images/configure_green.png').scaled(24, 24, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self._configure_red = QtGui.QPixmap(':/workflow/images/configure_red.png').scaled(24, 24, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
self._configure_green = QtGui.QPixmap(':/workflow/images/configure_green.png').scaled(24, 24, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.FastTransformation)
self._configure_red = QtGui.QPixmap(':/workflow/images/configure_red.png').scaled(24, 24, QtCore.Qt.AspectRatioMode.KeepAspectRatio, QtCore.Qt.TransformationMode.FastTransformation)
self.setToolTip('Configure the step')

def setConfigured(self, state):
Expand Down Expand Up @@ -662,13 +664,13 @@ def paint(self, painter, option, widget):
angle = Arc.TwoPi - angle
# Draw the arrows if there's enough room.
if line.dy() * line.dy() + line.dx() * line.dx() > 200 * self._arrowSize:
midPoint = (line.p1() + line.p2()) / 2
midPoint = QtCore.QPointF((line.p1() + line.p2()) / 2)

destArrowP1 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi / 3) * self._arrowSize)
destArrowP2 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize)
destination_arrow_p1 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi / 3) * self._arrowSize)
destination_arrow_p2 = midPoint + QtCore.QPointF(math.sin(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize,
math.cos(angle - Arc.Pi + Arc.Pi / 3) * self._arrowSize)

painter.setBrush(QtCore.Qt.black)
painter.setBrush(QtCore.Qt.GlobalColor.black)
# painter.drawPolygon(QtGui.QPolygonF([line.p1(), sourceArrowP1, sourceArrowP2]))
painter.drawPolygon(QtGui.QPolygonF([midPoint, destArrowP1, destArrowP2]))
painter.drawPolygon(QtGui.QPolygonF([midPoint, destination_arrow_p1, destination_arrow_p2]))
Loading

0 comments on commit ae3aba4

Please sign in to comment.