-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpluginpanel.py
311 lines (241 loc) · 13.1 KB
/
pluginpanel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env python
import json
from pathlib import Path
from importlib import import_module
from logging import getLogger
from PySide6.QtGui import QAction, QActionGroup, QFontMetrics, QCursor
from PySide6.QtCore import Qt, QObject, Signal
from PySide6.QtWidgets import QSizePolicy, QLayout, QMessageBox
logger = getLogger(__name__)
class PluginPanel (QObject):
# signal from this panel or relayed from plugins
signal_update_image_view = Signal()
signal_restore_image_settings = Signal()
# signals relayed from plugins to the main window
signal_update_mouse_cursor = Signal(QCursor)
signal_select_image_by_tczindex = Signal(int, int, int)
signal_focus_graphics_view = Signal()
def __init__ (self, ui, parent = None):
logger.debug("Plugin panel being intialized.")
super().__init__(parent)
self.ui = ui
self.plugin_instance_dict = {}
self.plugin_package = 'plugin'
self.default_plugin = 'base'
self.plugin_folder = str(Path(__file__).parent.parent.joinpath(self.plugin_package))
logger.debug(f"Plugin folder {self.plugin_folder}")
self.default_instance = None
self.current_instance = None
self.load_plugins()
logger.debug(f"Plugins loaded {self.plugin_instance_dict}.")
plugin_name = list(self.plugin_instance_dict.values())[0].plugin_name
self.switch_plugin(plugin_name)
logger.debug(f"Plugin switched to {plugin_name}.")
self.connect_signals_to_slots()
logger.debug("Plugin panel signal connected.")
def load_plugins (self):
load_failed = []
self.actgroup_plugin = QActionGroup(self.ui.menu_plugin)
instance_list = []
module_name_list = [x.stem for x in Path(self.plugin_folder).iterdir() if not x.name.startswith("_")]
for module_name in module_name_list:
try:
module = import_module(name = '{0}.{1}'.format(self.plugin_package, module_name))
if module.priority < 0:
continue
instance = getattr(module, module.class_name)()
except:
load_failed.append(str(module_name))
# good module
instance.priority = module.priority
instance.plugin_name = module.plugin_name
instance_list.append(instance)
# make sure to load at least one plugin
module = import_module(name = f"{self.plugin_package}.{self.default_plugin}")
instance = getattr(module, module.class_name)()
instance.priority = 0
instance.plugin_name = module.plugin_name
logger.debug(f"Default plugin loaded: {module}")
self.default_instance = getattr(module, module.class_name)()
if len(instance_list) == 0:
instance_list = [self.default_instance]
# sort instances by priority values
logger.debug(f"Initial plugin instance list: {instance_list}")
instance_list = sorted(instance_list, key = lambda x: x.priority)
logger.debug(f"Sorted plugin instance list: {instance_list}")
# menu
for instance in instance_list:
action = QAction(instance.plugin_name, self.ui.menu_plugin, checkable = True, checked = False)
self.ui.menu_plugin.addAction(action)
self.actgroup_plugin.addAction(action)
self.actgroup_plugin.setExclusive(True)
# combobox
self.ui.combo_plugin_name.blockSignals(True)
self.ui.combo_plugin_name.clear()
self.ui.combo_plugin_name.addItems([x.plugin_name for x in instance_list])
self.ui.combo_plugin_name.blockSignals(False)
# store plugin instances to a list
self.plugin_instance_dict = {x.plugin_name: x for x in instance_list}
if len(load_failed) > 0:
self.show_message(f"Plugin error", "Failed to load: {', '.join(load_failed)}")
def connect_signals_to_slots(self):
self.actgroup_plugin.triggered.connect(self.slot_switch_plugin_by_action)
self.ui.combo_plugin_name.currentTextChanged.connect(self.slot_switch_plugin_by_name)
def switch_plugin (self, plugin_name = None):
plugin_name = self.select_plugin_instance(plugin_name).plugin_name
logger.debug(f"Switching plugin from {self.current_instance} to {plugin_name}.")
if self.current_instance is not None:
if plugin_name == self.current_instance.plugin_name:
return
# disconnect the old class
self.current_instance.signal_update_image_view.disconnect()
self.current_instance.signal_update_mouse_cursor.disconnect()
self.current_instance.signal_select_image_by_tczindex.disconnect()
self.current_instance.signal_focus_graphics_view.disconnect()
self.current_instance.signal_records_updated.disconnect()
logger.debug(f"Signals of the current instance {self.current_instance} disconnected.")
# connect a new class
self.current_instance = self.plugin_instance_dict.get(plugin_name, self.default_instance)
logger.debug(f"New current instance {self.current_instance} set to the variable.")
self.update_labels()
logger.debug("Labels updated.")
self.update_plugin_widgets()
logger.debug("Plugin widgets updated.")
self.current_instance.signal_update_image_view.connect(self.slot_update_image_view)
self.current_instance.signal_update_mouse_cursor.connect(self.slot_update_mouse_cursor)
self.current_instance.signal_select_image_by_tczindex.connect(self.slot_select_image_by_tczindex)
self.current_instance.signal_focus_graphics_view.connect(self.slot_focus_graphics_view)
self.current_instance.signal_records_updated.connect(self.slot_records_updated)
logger.debug(f"Signals of the new instance {self.current_instance} connected.")
# update menu
menu_action_candidates = [x for x in self.ui.menu_plugin.actions() if x.text() == self.current_instance.plugin_name]
if len(menu_action_candidates) > 0:
self.ui.menu_plugin.blockSignals(True)
menu_action_candidates[0].setChecked(True)
self.ui.menu_plugin.blockSignals(False)
logger.debug(f"Plugin menu checked for {menu_action_candidates[0]}.")
# update the combo box
self.ui.combo_plugin_name.blockSignals(True)
self.ui.combo_plugin_name.setCurrentText(self.current_instance.plugin_name)
self.ui.combo_plugin_name.blockSignals(False)
logger.debug(f"Plugin combo box checked for {self.current_instance.plugin_name}.")
self.signal_update_image_view.emit()
logger.debug(f"Plugin switched to {self.current_instance.plugin_name}.")
def update_plugin_widgets (self):
self.recurse_for_widgets(self.ui.vlayout_plugin, lambda x: x.deleteLater(), lambda x: x.deleteLater())
self.current_instance.init_widgets(self.ui.vlayout_plugin)
self.current_instance.connect_signals_to_slots()
self.recurse_for_widgets(self.ui.vlayout_plugin,
lambda x: x.setSizeConstraint(QLayout.SetMinimumSize),
lambda x: x.setSizePolicy(QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Minimum))
def recurse_for_widgets (self, object, func_layout, func_widget):
for index in reversed(range(object.count())):
if object.itemAt(index).widget() is None:
self.recurse_for_widgets(object.itemAt(index).layout(), func_layout, func_widget)
func_layout(object.itemAt(index).layout())
else:
func_widget(object.itemAt(index).widget())
def update_labels (self):
flag = '*' if self.current_instance.is_records_modified() else ''
if self.current_instance.records_filename is None:
text = f"File: {flag}(None)"
else:
text = f"File: {flag}{Path(self.current_instance.records_filename).name}"
fontmetrics = QFontMetrics(self.ui.label_records_filename.font())
elidedtext = fontmetrics.elidedText(text, Qt.TextElideMode.ElideRight, self.ui.label_records_filename.width())
self.ui.label_records_filename.setText(elidedtext)
if text != elidedtext:
self.ui.label_records_filename.setToolTip(text)
def select_plugin_instance (self, plugin_name = None):
if plugin_name is None:
plugin_instance = self.current_instance
else:
plugin_instance = self.plugin_instance_dict.get(plugin_name, self.default_instance)
return plugin_instance
def load_records (self, records_filename, plugin_name = None):
try:
with open(records_filename, 'r') as f:
records_dict = json.load(f)
except:
logger.error(f"Records file cannot be opened for the initial check: {records_filename}.")
self.show_message()
records_plugin_name = records_dict.get("summary", {}).get('plugin_name', None)
logger.debug(f"Records being loaded are created by {records_plugin_name}")
plugin_instance = self.select_plugin_instance(plugin_name)
if records_plugin_name != plugin_instance.plugin_name:
mbox = QMessageBox()
mbox.setWindowTitle("Continue loading records?")
mbox.setText(f"Records created by {records_plugin_name}, not by {plugin_instance.plugin_name}. Continue?")
mbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
mbox.setDefaultButton(QMessageBox.No)
result = mbox.exec()
if result == QMessageBox.No:
return
else:
self.switch_plugin(records_plugin_name)
plugin_instance = self.select_plugin_instance(plugin_name)
try:
plugin_instance.load_records(records_filename)
except Exception as exception:
self.show_message(title = "Record loading error", message = str(exception))
# labels not updated if the target plugin is not current
self.update_labels()
self.signal_restore_image_settings.emit()
self.signal_update_image_view.emit()
def save_records (self, records_filename, plugin_name = None, additional_settings = {}):
plugin_instance = self.select_plugin_instance(plugin_name)
try:
plugin_instance.save_records(records_filename, settings = additional_settings)
except Exception as exception:
self.show_message(title = "Record saving error", message = str(exception))
# labels not updated if the target plugin is not current
self.update_labels()
def clear_records (self, plugin_name = None):
plugin_instance = self.select_plugin_instance(plugin_name)
plugin_instance.clear_records()
# labels not updated if the target plugin is not current
self.update_labels()
def is_records_modified (self, plugin_name = None):
plugin_instance = self.select_plugin_instance(plugin_name)
return plugin_instance.is_records_modified()
def records_filename_filter_list (self, plugin_name = None):
plugin_instance = self.select_plugin_instance(plugin_name)
return [f"{key} ({" ".join(value)})" for key, value in plugin_instance.file_types.items()]
def notify_plugins_stack_updated (self, stack):
for instance in self.plugin_instance_dict.values():
instance.update_stack_reference(stack)
def notify_plugin_focus_recovery (self):
self.current_instance.notice_focus_recovery()
def plugin_records_dict (self, plugin_name = None):
plugin_instance = self.select_plugin_instance(plugin_name)
return plugin_instance.records_dict
def suggest_records_filename (self, image_filename, plugin_name = None):
plugin_instance = self.select_plugin_instance(plugin_name)
return plugin_instance.suggest_filename(image_filename)
def plugin_records_filename (self, plugin_name = None):
plugin_instance = self.select_plugin_instance(plugin_name)
return plugin_instance.records_filename
def show_message (self, title = "No title", message = "Default message."):
mbox = QMessageBox()
mbox.setWindowTitle(title)
mbox.setText(message)
mbox.setStandardButtons(QMessageBox.Ok)
mbox.exec()
def slot_switch_plugin_by_action (self, action):
self.switch_plugin(action.text())
def slot_switch_plugin_by_name (self, plugin_name):
self.switch_plugin(plugin_name)
def slot_plugin_help (self):
self.show_message(title = "Quick help", message = self.current_instance.help_message())
self.current_instance.notice_focus_recovery()
def slot_update_image_view (self):
self.signal_update_image_view.emit()
def slot_update_mouse_cursor (self, cursor):
self.signal_update_mouse_cursor.emit(cursor)
def slot_select_image_by_tczindex (self, *tcz_index):
self.signal_select_image_by_tczindex.emit(*tcz_index)
def slot_focus_graphics_view (self):
self.signal_focus_graphics_view.emit()
def slot_records_updated (self):
self.update_labels()
self.signal_update_image_view.emit()