Music production session manager https://www.laborejo.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

101 lines
4.6 KiB

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Copyright 2021, Nils Hilbricht, Germany ( https://www.hilbricht.net )
This file is part of the Laborejo Software Suite ( https://www.laborejo.org ),
This application is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import logging; logger = logging.getLogger(__name__); logger.info("import")
#Standard Library
from datetime import timedelta
#Third Party
from PyQt5 import QtCore, QtGui, QtWidgets
#Engine
import engine.api as api
class JackTransportControls(object):
"""Singleton object for code-structure reasons"""
def __init__(self, mainWindow):
self.mainWindow = mainWindow
self.ui = self.mainWindow.ui
self.visible = None
self.currentMaximumInSeconds = self.ui.jackTransportMaxTime.value() * 60 #widget is in minutes
self.resolutionFactor = 10 #Around 10 seems to be the maximum. The progressbar will still "Jump" above that.
self.ui.jackTransportPlayPause.setCheckable(True)
self.ui.jackTransportPlayPause.setText("")
self.ui.jackTransportPlayPause.setIcon(QtGui.QIcon(':playpause.png'))
self.ui.jackTransportRewind.setText("")
self.ui.jackTransportRewind.setIcon(QtGui.QIcon(':tostart.png'))
api.callbacks.sessionOpenReady.append(self.show)
api.callbacks.sessionClosed.append(self.hide)
api.callbacks.setPlaybackSeconds.append(self._react_playbackSeconds)
self.ui.jackTransportMaxTime.valueChanged.connect(lambda v: setattr(self, "currentMaximumInSeconds", v*60))
self.ui.jackTransportPlayPause.clicked.connect(api.jackClient.playPause)
self.ui.jackTransportRewind.clicked.connect(api.jackClient.rewind)
#Maximum Resolution of 100 is not enough. It is too "steppy"
self.ui.jackTransportTimeline.setMaximum(self.ui.jackTransportTimeline.maximum() * self.resolutionFactor)
self.ui.jackTransportTimeline.reset()
self.ui.jackTransportTimeline.mousePressEvent = self._timelineMousePressEvent
self.ui.jackTransportTimeline.mouseMoveEvent = self._timelineMouseMoveEvent
self.hide()
def hide(self, *args):
self.ui.jackTransportControls.hide()
self.visible = False
def show(self, *args):
self.ui.jackTransportControls.show()
self.visible = True
def _react_playbackSeconds(self, seconds, isTransportRunning):
if self.visible: #optimisation
realPercent = seconds / self.currentMaximumInSeconds
progressPercent = realPercent * 100 * self.resolutionFactor #100 because that is 0.xy -> xy% )
rogressPercent = int(progressPercent)
prettyTime = str(timedelta(seconds=int(seconds))) #timedelta without int will print microseconds
self.ui.jackTransportTimeline.setValue(progressPercent)
if isTransportRunning:
self.ui.jackTransportPlayPause.setChecked(True)
self.ui.jackTransportTimeline.setFormat("" + prettyTime) #we don't use the Qt format substitutions. We just set a fixed value.
else:
self.ui.jackTransportPlayPause.setChecked(False)
self.ui.jackTransportTimeline.setFormat("" + prettyTime)
def _timelineMousePressEvent(self, event):
"""The positions are in pixels. If the window is different we get different numbers.
Normalize first."""
percentage = event.x() / self.ui.jackTransportTimeline.width() #x is relative to the widget. same as localPos().x()
seconds = percentage * self.currentMaximumInSeconds
api.jackClient.seek(seconds)
def _timelineMouseMoveEvent(self, event):
"""We did not set any qt-flags on this widget so mouseMoveEvent only works when the left
mouse button is down, and not just simply by hovering. Which is exactly what we want:
dragging"""
percentage = event.x() / self.ui.jackTransportTimeline.width() #x is relative to the widget. same as localPos().x()
seconds = percentage * self.currentMaximumInSeconds
api.jackClient.seek(seconds)