From d61ff39ff0c6ec6491f04b2924393e90bf3f4cd5 Mon Sep 17 00:00:00 2001 From: Nils <> Date: Tue, 22 Feb 2022 20:19:12 +0100 Subject: [PATCH] Hover status bar events for the pianos --- qtgui/horizontalpiano.py | 54 ++++++++++++++++++++++++++++++++-------- qtgui/instrument.py | 2 ++ qtgui/verticalpiano.py | 48 ++++++++++++++++++++++++----------- 3 files changed, 79 insertions(+), 25 deletions(-) diff --git a/qtgui/horizontalpiano.py b/qtgui/horizontalpiano.py index 53425e9..154a306 100644 --- a/qtgui/horizontalpiano.py +++ b/qtgui/horizontalpiano.py @@ -183,12 +183,14 @@ class _HorizontalPianoScene(QtWidgets.QGraphicsScene): for keyPitch, keyObject in self.allKeys.items(): if keyPitch in instrumentStatus["keyLabels"]: - self.numberLabels[keyPitch].setLabel(instrumentStatus["keyLabels"][keyPitch]) #can be overwritten by keyswitch label. otherwise on any key, no matter if deactivated or not + self.numberLabels[keyPitch].setLabel(instrumentStatus["keyLabels"][keyPitch], keyswitch=False) #can be overwritten by keyswitch label. otherwise on any key, no matter if deactivated or not if keyPitch in instrumentStatus["keySwitches"]: opcode, keyswitchLabel = instrumentStatus["keySwitches"][keyPitch] - self.numberLabels[keyPitch].setLabel(keyswitchLabel) + keyObject.setPlayable(True) keyObject.setKeySwitch(True) + self.numberLabels[keyPitch].setLabel(keyswitchLabel, keyswitch=True) + elif keyPitch in instrumentStatus["playableKeys"]: keyObject.setPlayable(True) #else: @@ -301,25 +303,32 @@ class NumberLabel(QtWidgets.QGraphicsSimpleTextItem): self.setEnabled(False) #Ignored? self.setAcceptedMouseButtons(QtCore.Qt.NoButton) - blackKey = number % 12 in (1, 3, 6, 8, 10) - if blackKey: - self.setBrush(QtGui.QColor("white")) - self.labelItem.setBrush(QtGui.QColor("white")) + self.blackKey = number % 12 in (1, 3, 6, 8, 10) + if self.blackKey: self.setScale(0.9) self.labelItem.setRotation(90) self.labelItem.setPos(15,15) else: - self.setBrush(QtGui.QColor("black")) - self.labelItem.setBrush(QtGui.QColor("black")) self.setScale(1) self.labelItem.setRotation(-90) self.labelItem.setPos(-5,0) - def setLabel(self, label:str): + self.setTextColor(blackKey=self.blackKey, keyswitch=False) + + def setTextColor(self, blackKey:bool, keyswitch:bool): + if blackKey and not keyswitch: + self.setBrush(QtGui.QColor("white")) + self.labelItem.setBrush(QtGui.QColor("white")) + else: + self.setBrush(QtGui.QColor("black")) + self.labelItem.setBrush(QtGui.QColor("black")) + + def setLabel(self, label:str, keyswitch=False): """Each key can have an optional text label for keyswitches, percussion names etc. Use with empty string to reset to just the midi pitch number.""" self.currentLabel = label self.labelItem.setText(label) + self.setTextColor(blackKey=self.blackKey, keyswitch=keyswitch) class BlackKey(QtWidgets.QGraphicsRectItem): @@ -327,9 +336,10 @@ class BlackKey(QtWidgets.QGraphicsRectItem): super().__init__(0, 0, WIDTH * 0.8, HEIGHT) #x, y, w, h self.parentScene = parentScene self.pitch = pitch + self.state = False self.setPen(QtGui.QPen(QtCore.Qt.NoPen)) self.setBrush(QtGui.QColor("black")) - self.setEnabled(False) + self.setAcceptHoverEvents(True) self.setAcceptedMouseButtons(QtCore.Qt.NoButton) self.decorationOverlay = QtWidgets.QGraphicsRectItem(0, 0, WIDTH * 0.8, HEIGHT) #x, y, w, h @@ -350,6 +360,7 @@ class BlackKey(QtWidgets.QGraphicsRectItem): def setPlayable(self, state:bool): c = QtGui.QColor() + self.state = state if state: c.setNamedColor("#0c0c0c") else: @@ -357,8 +368,17 @@ class BlackKey(QtWidgets.QGraphicsRectItem): c.setNamedColor("#444444") self.setBrush(c) + def hoverEnterEvent(self, event): + if self.state: + l = self.parentScene.numberLabels[self.pitch].currentLabel + if l: + self.parentScene.parentView.mainWindow.statusBar().showMessage(f"[{self.pitch}] {l}") + + def hoverLeaveEvent(self, event): + self.parentScene.parentView.mainWindow.statusBar().showMessage("") def setKeySwitch(self, state:bool): + self.state = state if state: self.decorationOverlay.show() self.decorationOverlay.setBrush(QtGui.QColor("orange")) @@ -376,9 +396,10 @@ class WhiteKey(QtWidgets.QGraphicsRectItem): super().__init__(0, 0, WIDTH, HEIGHT) #x, y, w, h self.parentScene = parentScene self.pitch = pitch + self.state = False self.setPen(QtGui.QPen(QtCore.Qt.NoPen)) self.setBrush(QtGui.QColor("white")) - self.setEnabled(False) + self.setAcceptHoverEvents(True) self.setAcceptedMouseButtons(QtCore.Qt.NoButton) self.decorationOverlay = QtWidgets.QGraphicsRectItem(0, 0, WIDTH, HEIGHT) #x, y, w, h @@ -397,8 +418,18 @@ class WhiteKey(QtWidgets.QGraphicsRectItem): self.highlight.hide() + def hoverEnterEvent(self, event): + if self.state: + l = self.parentScene.numberLabels[self.pitch].currentLabel + if l: + self.parentScene.parentView.mainWindow.statusBar().showMessage(f"[{self.pitch}] {l}") + + def hoverLeaveEvent(self, event): + self.parentScene.parentView.mainWindow.statusBar().showMessage("") + def setPlayable(self, state:bool): c = QtGui.QColor() + self.state = state if state: c.setNamedColor("#fdfdff") else: @@ -407,6 +438,7 @@ class WhiteKey(QtWidgets.QGraphicsRectItem): self.setBrush(c) def setKeySwitch(self, state:bool): + self.state = state if state: self.decorationOverlay.show() self.decorationOverlay.setBrush(QtGui.QColor("orange")) diff --git a/qtgui/instrument.py b/qtgui/instrument.py index bc51261..394c280 100644 --- a/qtgui/instrument.py +++ b/qtgui/instrument.py @@ -432,6 +432,7 @@ class GuiInstrument(QtWidgets.QTreeWidgetItem): self.mixSendDial.valueChanged.connect(self._sendVolumeChangeToEngine) self.mixSendDial.enterEvent = lambda ev: self.parentTreeController.parentMainWindow.statusBar().showMessage((QtCore.QCoreApplication.translate("Instrument", "Use mousewheel to change the instruments mixSend for the stereo mixer ouput. Right click to (un)mute mixer-send."))) self.mixSendDial.leaveEvent = lambda ev: self.parentTreeController.parentMainWindow.statusBar().showMessage("") + self.leaveEvent = lambda ev: self.parentTreeController.parentMainWindow.statusBar().showMessage("") self.mixSendDial.contextMenuEvent = self._mixSendDialContextMenuEvent self.mixSendDial.setEnabled(False) #default is off, so we don't send mixSend changes for an unloaded instrument @@ -439,6 +440,7 @@ class GuiInstrument(QtWidgets.QTreeWidgetItem): #self.toggleSwitch.toggled.connect(lambda c: print('toggled', c)) #triggered by engine callback as well self.toggleSwitch.setAutoFillBackground(True) #otherwise conflicts with setItemWidget self.toggleSwitch.clicked.connect(self.instrumentSwitchOnViaGui) + self.toggleSwitch.leaveEvent = lambda ev: self.parentTreeController.parentMainWindow.statusBar().showMessage("") #self.toggleSwitch.pressed.connect(lambda: print('pressed')) #literal mouse down. #self.toggleSwitch.released.connect(lambda: print('released')) #We cannot add the ToggleSwitch Widget here. diff --git a/qtgui/verticalpiano.py b/qtgui/verticalpiano.py index f06558e..29aa6ac 100644 --- a/qtgui/verticalpiano.py +++ b/qtgui/verticalpiano.py @@ -60,6 +60,7 @@ class VerticalPiano(QtWidgets.QGraphicsView): self.centerOn(0, 64*STAFFLINEGAP) + class _VerticalPianoScene(QtWidgets.QGraphicsScene): """Most of this is copy paste from piano grid""" @@ -126,7 +127,7 @@ class _VerticalPianoScene(QtWidgets.QGraphicsScene): bk.setPos(0, (127-i) * STAFFLINEGAP) #Various purpose color keys. They are opaque and are on top of white/black keys - ck = ColorKey(self, QtGui.QColor("cyan"), blackKey) + ck = ColorKey(self, i, QtGui.QColor("cyan"), blackKey) self.addItem(ck) self.colorKeys[i] = ck ck.setPos(0, (127-i) * STAFFLINEGAP) @@ -193,10 +194,11 @@ class _VerticalPianoScene(QtWidgets.QGraphicsScene): keyObject.show() if keyPitch in instrumentStatus["keyLabels"]: - self.numberLabels[keyPitch].setLabel(instrumentStatus["keyLabels"][keyPitch]) #can be overwritten by keyswitch label. otherwise on any key, no matter if deactivated or not + self.numberLabels[keyPitch].setLabel(instrumentStatus["keyLabels"][keyPitch], keyswitch=False) #can be overwritten by keyswitch label. otherwise on any key, no matter if deactivated or not if keyPitch in instrumentStatus["keySwitches"]: opcode, keyswitchLabel = instrumentStatus["keySwitches"][keyPitch] - self.numberLabels[keyPitch].setLabel(keyswitchLabel) + self.numberLabels[keyPitch].setLabel(keyswitchLabel, keyswitch=True) + keyObject.setPlayable(True) keyObject.setBrush(QtGui.QColor("orange")) else: #self.numberLabels[keyPitch].hide() @@ -287,31 +289,33 @@ class _VerticalPianoScene(QtWidgets.QGraphicsScene): class NumberLabel(QtWidgets.QGraphicsSimpleTextItem): - def __init__(self, parentGrid, number:int): + def __init__(self, parentPiano, number:int): super().__init__() - self.parentGrid = parentGrid + self.parentPiano = parentPiano self.number = number self.currentLabel = "" self.setText(str(number)) self.setFlag(QtWidgets.QGraphicsItem.ItemIgnoresParentOpacity, True) self.setAcceptedMouseButtons(QtCore.Qt.NoButton) self.setScale(1) - blackKey = number % 12 in (1, 3, 6, 8, 10) - if blackKey: + self.blackKey = number % 12 in (1, 3, 6, 8, 10) + + def setTextColor(self, blackKey:bool, keyswitch:bool): + if blackKey and not keyswitch: self.setBrush(QtGui.QColor("white")) else: self.setBrush(QtGui.QColor("black")) - def setLabel(self, label:str): + def setLabel(self, label:str, keyswitch=False): """Each key can have an optional text label for keyswitches, percussion names etc. Use with empty string to reset to just the midi pitch number.""" self.currentLabel = label self.setText(f"{self.number} {label}") - + self.setTextColor(self.blackKey, keyswitch) class Highlight(QtWidgets.QGraphicsRectItem): - def __init__(self, parentGrid): + def __init__(self, parentPiano): super().__init__(0, 0, WIDTH, STAFFLINEGAP) #x, y, w, h self.setEnabled(False) #Not clickable, still visible. self.setAcceptedMouseButtons(QtCore.Qt.NoButton) @@ -322,17 +326,24 @@ class Highlight(QtWidgets.QGraphicsRectItem): class ColorKey(QtWidgets.QGraphicsRectItem): - def __init__(self, parentGrid, color:QtGui.QColor, blackKey:bool): + """These are the actual, playable, keys. And key switches""" + + def __init__(self, parentPiano, pitch, color:QtGui.QColor, blackKey:bool): super().__init__(0, 0, WIDTH, STAFFLINEGAP) #x, y, w, h - self.setEnabled(False) #Not clickable, still visible. + self.parentPiano = parentPiano + self.pitch = pitch + self.setEnabled(True) #Not clickable, still visible. self.setAcceptedMouseButtons(QtCore.Qt.NoButton) + self.setAcceptHoverEvents(True) self.setFlag(QtWidgets.QGraphicsItem.ItemIgnoresParentOpacity, True) self.setBrush(color) + self.state = False self.blackKey = blackKey self.hide() def setPlayable(self, state:bool): c = QtGui.QColor() + self.state = state if state: if self.blackKey: c.setNamedColor("#0c0c0c") @@ -347,11 +358,20 @@ class ColorKey(QtWidgets.QGraphicsRectItem): self.setBrush(c) + def hoverEnterEvent(self, event): + if self.state: + l = self.parentPiano.numberLabels[self.pitch].currentLabel + if l: + self.parentPiano.parentView.mainWindow.statusBar().showMessage(f"[{self.pitch}] {l}") + + def hoverLeaveEvent(self, event): + self.parentPiano.parentView.mainWindow.statusBar().showMessage("") + class BlackKey(QtWidgets.QGraphicsRectItem): - def __init__(self, parentGrid): + def __init__(self, parentPiano): super().__init__(0, 0, WIDTH, STAFFLINEGAP) #x, y, w, h - self.parentGrid = parentGrid + self.parentPiano = parentPiano self.setPen(QtGui.QPen(QtCore.Qt.NoPen)) c = QtGui.QColor() c.setNamedColor("#444444")