Skip to content

Commit bcfd7af

Browse files
committed
Fix errors when MIDI plugin not installed/loaded
1 parent 4c90177 commit bcfd7af

2 files changed

Lines changed: 54 additions & 25 deletions

File tree

lisp/plugins/controller/protocols/midi.py

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,6 @@
3535
from lisp.core.plugin import PluginNotLoadedError
3636
from lisp.plugins.controller.common import LayoutAction, tr_layout_action
3737
from lisp.plugins.controller.protocol import Protocol
38-
from lisp.plugins.midi.midi_utils import (
39-
MIDI_MSGS_NAME,
40-
midi_data_from_msg,
41-
midi_msg_from_data,
42-
midi_from_dict,
43-
midi_from_str,
44-
MIDI_MSGS_SPEC,
45-
MIDI_ATTRS_SPEC,
46-
PortDirection,
47-
)
48-
from lisp.plugins.midi.widgets import MIDIPatchCombo, MIDIMessageEditDialog
4938
from lisp.ui.qdelegates import (
5039
CueActionDelegate,
5140
EnumComboBoxDelegate,
@@ -55,6 +44,21 @@
5544
from lisp.ui.settings.pages import CuePageMixin, SettingsPage
5645
from lisp.ui.ui_utils import translate
5746

47+
try:
48+
from lisp.plugins.midi.midi_utils import (
49+
MIDI_MSGS_NAME,
50+
midi_data_from_msg,
51+
midi_msg_from_data,
52+
midi_from_dict,
53+
midi_from_str,
54+
MIDI_MSGS_SPEC,
55+
MIDI_ATTRS_SPEC,
56+
PortDirection,
57+
)
58+
from lisp.plugins.midi.widgets import MIDIPatchCombo, MIDIMessageEditDialog
59+
except ImportError:
60+
midi_from_str = lambda *_: None
61+
5862

5963
logger = logging.getLogger(__name__)
6064

@@ -76,6 +80,16 @@ def __init__(self, actionDelegate, **kwargs):
7680

7781
self.midiModel = MidiModel()
7882

83+
try:
84+
self.__midi = get_plugin("Midi")
85+
except PluginNotLoadedError:
86+
self.setEnabled(False)
87+
self.midiNotInstalledMessage = QLabel()
88+
self.midiNotInstalledMessage.setAlignment(Qt.AlignCenter)
89+
self.midiGroup.layout().addWidget(self.midiNotInstalledMessage)
90+
self.retranslateUi()
91+
return
92+
7993
self.midiView = MidiView(actionDelegate, parent=self.midiGroup)
8094
self.midiView.setModel(self.midiModel)
8195
self.midiGroup.layout().addWidget(self.midiView, 0, 0, 1, 2)
@@ -117,12 +131,13 @@ def __init__(self, actionDelegate, **kwargs):
117131
self.retranslateUi()
118132

119133
self._defaultAction = None
120-
try:
121-
self.__midi = get_plugin("Midi")
122-
except PluginNotLoadedError:
123-
self.setEnabled(False)
124134

125135
def retranslateUi(self):
136+
if hasattr(self, "midiNotInstalledMessage"):
137+
self.midiNotInstalledMessage.setText(
138+
translate("ControllerSettings", "MIDI plugin not installed"))
139+
return
140+
126141
self.addButton.setText(translate("ControllerSettings", "Add"))
127142
self.removeButton.setText(translate("ControllerSettings", "Remove"))
128143

@@ -262,9 +277,16 @@ def __init__(self):
262277
translate("ControllerMidiSettings", "Action"),
263278
]
264279
)
265-
self.__midi = get_plugin("Midi")
280+
try:
281+
self.__midi = get_plugin("Midi")
282+
if not self.__midi.is_loaded():
283+
self.__midi = None
284+
except PluginNotLoadedError:
285+
self.__midi = None
266286

267287
def appendMessage(self, patch_id, message, action):
288+
if not self.__midi:
289+
return
268290
data = midi_data_from_msg(message)
269291
data.extend((None,) * (3 - len(data)))
270292
self.appendRow(patch_id, message.type, *data, action)
@@ -354,8 +376,13 @@ class Midi(Protocol):
354376

355377
def __init__(self):
356378
super().__init__()
357-
# Install callback for new MIDI messages
358-
get_plugin("Midi").received.connect(self.__new_message)
379+
try:
380+
# Install callback for new MIDI messages
381+
midi = get_plugin("Midi")
382+
if midi.is_loaded():
383+
midi.received.connect(self.__new_message)
384+
except PluginNotLoadedError:
385+
pass
359386

360387
def __new_message(self, patch_id, message):
361388
if hasattr(message, "velocity"):

lisp/plugins/midi/widgets.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ def __init__(self, direction: PortDirection, *args, **kwargs):
4747
super().__init__(*args, **kwargs)
4848
self.__direction = direction
4949
self.__midi = get_plugin("Midi")
50-
for patch_id in self._patches():
51-
self.addItem("", patch_id)
50+
if self.__midi.is_loaded():
51+
for patch_id in self._patches():
52+
self.addItem("", patch_id)
5253

5354
def _patches(self):
5455
if self.__direction is PortDirection.Input:
@@ -61,11 +62,12 @@ def _patch_name(self, patch_id):
6162
return self.__midi.output_name_formatted(patch_id)
6263

6364
def retranslateUi(self):
64-
for patch_id, device_name in self._patches().items():
65-
self.setItemText(
66-
self.findData(patch_id),
67-
self._patch_name(patch_id)
68-
)
65+
if self.__midi.is_loaded():
66+
for patch_id, device_name in self._patches().items():
67+
self.setItemText(
68+
self.findData(patch_id),
69+
self._patch_name(patch_id)
70+
)
6971

7072

7173
class MIDIMessageEdit(QWidget):

0 commit comments

Comments
 (0)