Snap to grid when moving playhead.

time
Ben Niemann 2020-02-20 12:14:46 +01:00
parent 402191ffec
commit d87f862581
3 changed files with 18 additions and 14 deletions

View File

@ -743,7 +743,6 @@ class PianoRollTrackEditor(
effectiveGridYSize, setEffectiveGridYSize, effectiveGridYSizeChanged = slots.slot(
int, 'effectiveGridYSize', default=15)
hoverPitch, setHoverPitch, hoverPitchChanged = slots.slot(int, 'hoverPitch', default=-1)
snapToGrid, setSnapToGrid, snapToGridChanged = slots.slot(bool, 'snapToGrid', default=True)
currentChannel, setCurrentChannel, currentChannelChanged = slots.slot(
int, 'currentChannel', default=0)
showKeys, setShowKeys, showKeysChanged = slots.slot(
@ -1160,19 +1159,6 @@ class PianoRollTrackEditor(
self.__y_scrollbar.setRange(0, max(0, self.gridHeight() - self.height()))
self.__y_scrollbar.setPageStep(self.height())
def shouldSnap(self, evt: QtGui.QMouseEvent) -> bool:
return self.snapToGrid() and not evt.modifiers() & Qt.ShiftModifier
def snapTime(self, time: audioproc.MusicalTime) -> audioproc.MusicalTime:
grid_time = (
audioproc.MusicalTime(0, 1)
+ self.gridStep() * int(round(float(time / self.gridStep()))))
time_x = int(time * self.scaleX())
grid_x = int(grid_time * self.scaleX())
if abs(time_x - grid_x) <= 10:
return grid_time
return time
def resizeEvent(self, evt: QtGui.QResizeEvent) -> None:
super().resizeEvent(evt)

View File

@ -129,6 +129,8 @@ class TimeLine(
if self.__move_time:
x = evt.pos().x() + self.xOffset()
current_time = min(self.xToTime(x), self.projectEndTime())
if self.shouldSnap(evt):
current_time = self.snapTime(current_time)
self.__player_state.setCurrentTime(current_time)
evt.accept()
return
@ -140,6 +142,8 @@ class TimeLine(
self.__move_time = False
x = evt.pos().x() + self.xOffset()
current_time = min(self.xToTime(x), self.projectEndTime())
if self.shouldSnap(evt):
current_time = self.snapTime(current_time)
self.call_async(
self.project_client.update_player_state(
self.__player_id,

View File

@ -94,6 +94,7 @@ class ScaledTimeMixin(ui_base.ProjectMixin, QObjectMixin):
class ContinuousTimeMixin(ScaledTimeMixin, slots.SlotContainer):
additionalXOffset, setAdditionalXOffset, additionalXOffsetChanged = slots.slot(
int, 'additionalXOffset', default=0)
snapToGrid, setSnapToGrid, snapToGridChanged = slots.slot(bool, 'snapToGrid', default=True)
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
@ -127,6 +128,19 @@ class ContinuousTimeMixin(ScaledTimeMixin, slots.SlotContainer):
def gridStep(self) -> audioproc.MusicalDuration:
return self.__grid_step
def shouldSnap(self, evt: QtGui.QMouseEvent) -> bool:
return self.snapToGrid() and not evt.modifiers() & Qt.ShiftModifier
def snapTime(self, time: audioproc.MusicalTime) -> audioproc.MusicalTime:
grid_time = (
audioproc.MusicalTime(0, 1)
+ self.gridStep() * int(round(float(time / self.gridStep()))))
time_x = int(time * self.scaleX())
grid_x = int(grid_time * self.scaleX())
if abs(time_x - grid_x) <= 10:
return grid_time
return time
def renderTimeGrid(
self, painter: QtGui.QPainter, rect: QtCore.QRect, *, show_numbers: bool = False
) -> None: