Browse Source

scroll view when moving blocks and tracks with mouse drag

master
Nils 3 years ago
parent
commit
417d3efd10
  1. 4
      qtgui/musicstructures.py
  2. 48
      qtgui/scorescene.py

4
qtgui/musicstructures.py

@ -63,7 +63,8 @@ class GuiBlockHandle(QtWidgets.QGraphicsRectItem):
self.setPen(self.trans)
self.setBrush(self.trans)
#self.setOpacity(0.4) #slightly fuller than background
self.setFlag(QtWidgets.QGraphicsItem.ItemIgnoresParentOpacity)
self.setFlag(QtWidgets.QGraphicsItem.ItemIgnoresParentOpacity, True)
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.setParentItem(parent)
self.setZValue(10) #This is the z value within GuiTrack
self.staticExportItem = staticExportItem
@ -197,6 +198,7 @@ class GuiTrack(QtWidgets.QGraphicsItem):
self.lengthInPixel = 0 # a cached value
self.createStaffLines() #no stafflines at all are too confusing.
self.ccPaths = {} # ccNumber0-127:PathItem. Empty for a new track. We only create ccPaths with the first ccBlock. Creation and handling is done in GuiScore, starting with syncCCsToBackend.
self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
self.nameGraphic = self.NameGraphic(self.staticExportItem["name"], parent = self)
self.staticItems.append(self.nameGraphic)

48
qtgui/scorescene.py

@ -55,6 +55,7 @@ class GuiScore(QtWidgets.QGraphicsScene):
self.duringTrackDragAndDrop = None #switched to a QGraphicsItem (e.g. GuiTrack) while a track is moved around by the mouse
self.duringBlockDragAndDrop = None #switched to a QGraphicsItem (e.g. GuiTrack) while a block is moved around by the mouse
self.mouseMoveEventBlocked = False #mouseMoveEvent and helper functions
self.conductor = Conductor(parentView = self.parentView)
self.addItem(self.conductor)
@ -325,7 +326,7 @@ class GuiScore(QtWidgets.QGraphicsScene):
self.updateSceneRect()
#Macro-Structure: Score / Track / Block Moving and Duplicating
#Hold the ALT Key to unlock the moving mode.super().keyPressEvent(event)
#Hold the shift Key to unlock the moving mode.super().keyPressEvent(event)
#No note-editing requires a mouse action, so the mouse is free for controlling other aspects.
#Like zooming or moving blocks around.
@ -395,27 +396,67 @@ class GuiScore(QtWidgets.QGraphicsScene):
super().mousePressEvent(event)
def _mmE_ensureCursorVisible(self, event):
"""Helper function for mouseMoveEvent.
centerOn and ensureVisible either crash or they recurse forever or they scroll exponentially
We must do it ourselves. Thanks, qt."""
if self.mouseMoveEventBlocked:
return #block the mouseMoveEvent recursion signal. If we scroll it sees the mouse as moving.
currentlyVisibleSceneRect = self.parentView.mapToScene(self.parentView.viewport().geometry()).boundingRect() #x, y top left corner, width, height
leftEdge = int(currentlyVisibleSceneRect.x())
rightEdge = int(leftEdge + currentlyVisibleSceneRect.width())
topEdge = int(currentlyVisibleSceneRect.y())
bottomEdge = int(topEdge + currentlyVisibleSceneRect.height())
x = int(event.scenePos().x())
y = int(event.scenePos().y())
if x < leftEdge:
self.mouseMoveEventBlocked = True #block the mouseMoveEvent recursion signal. If we scroll it sees the mouse as moving.
self.parentView.horizontalScrollBar().setValue(self.parentView.horizontalScrollBar().value() - (leftEdge - x))
elif x > rightEdge:
self.mouseMoveEventBlocked = True #block the mouseMoveEvent recursion signal. If we scroll it sees the mouse as moving.
self.parentView.horizontalScrollBar().setValue(self.parentView.horizontalScrollBar().value() + (x - rightEdge))
if y < topEdge:
self.mouseMoveEventBlocked = True #block the mouseMoveEvent recursion signal. If we scroll it sees the mouse as moving.
self.parentView.verticalScrollBar().setValue(self.parentView.verticalScrollBar().value() - (topEdge - y))
elif y > bottomEdge:
self.mouseMoveEventBlocked = True #block the mouseMoveEvent recursion signal. If we scroll it sees the mouse as moving.
self.parentView.verticalScrollBar().setValue(self.parentView.verticalScrollBar().value() + (y - bottomEdge))
self.mouseMoveEventBlocked = False
def mouseMoveEvent(self, event):
"""Catches certain mouse events for moving tracks and blocks.
Otherwise the event is propagated to the real QGraphicsItem.
Don't forget that an item needs to have the flag movable or selectable or else
it will not get mouseRelease or mouseMove events. MousePress always works.
Only active when mouse was pressed!
"""
super().mouseMoveEvent(event)
self._mmE_ensureCursorVisible(event) #Works with all buttons. Good.
if self.duringTrackDragAndDrop:
#X is locked for tracks.
#self._mmE_ensureCursorVisible(event)
x = self.duringTrackDragAndDrop.staticExportItem["guiPosStart"].x()
y = event.scenePos().y()
self.duringTrackDragAndDrop.setPos(x, y)
elif self.duringBlockDragAndDrop:
#GuiBlockHandles are children of a Track. Their Y position will be offset by the track when we move them around with the mouse.
#Which is the same the distance to the first track is on top. We need to substract that for a good look and feel.
#The actual later dropping of the block is handled with the mouse cursor coordinates, the moving colored block is just eyecandy.
#TODO: I want to scroll but this does not work because the item is wider than the viewport. It actually crashes qt
#self._mmE_ensureCursorVisible(event)
x = event.scenePos().x()
y = event.scenePos().y() - self.duringBlockDragAndDrop.parentGuiTrack.y()
self.duringBlockDragAndDrop.setPos(x, y)
#else:
else:
super().mouseMoveEvent(event)
# #this happens EVERY mouse move. Just idle mouse movement over the window.
@ -428,7 +469,6 @@ class GuiScore(QtWidgets.QGraphicsScene):
#self.parentView.unsetCursor() #While moving stuff the mouse-cursor is hidden. Reset.
#self.cursor.show() #Our own position cursor #TODO: why was that in here? It shows the cursor after a mouseclick, even in CC mode (it should not)
tempBlockDragAndDrop = self.duringBlockDragAndDrop
tempTrackDragAndDrop = self.duringTrackDragAndDrop
self.duringBlockDragAndDrop = None

Loading…
Cancel
Save