From d4fdd34d7405084b211f86c31407cbe151d6ab90 Mon Sep 17 00:00:00 2001 From: Charles Haley Date: Sun, 11 May 2025 11:15:58 +0100 Subject: [PATCH] If there is both an attribute validate_before_accept=True and a method validate() in the config_widget then calll validate() when the config dialog OK button is pressed. --- src/calibre/customize/__init__.py | 28 +++++++++++++++---- src/calibre/devices/kobo/kobotouch_config.py | 8 +++--- .../device_drivers/tabbed_device_config.py | 6 +++- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/calibre/customize/__init__.py b/src/calibre/customize/__init__.py index 568267b6be..52f1085d39 100644 --- a/src/calibre/customize/__init__.py +++ b/src/calibre/customize/__init__.py @@ -149,17 +149,35 @@ class Plugin: # {{{ from calibre.gui2 import gprefs + class ConfigDialog(QDialog): + + def __init__(self, parent, config_widget): + super().__init__(parent) + self.config_widget = config_widget + + def accept(self): + print('in accept') + if ((validate := getattr(self.config_widget, 'validate', None)) and + getattr(self.config_widget, 'validate_before_accept', False)): + print('have validate and validate_before_accept') + if not validate(): + return + print('accepting') + super().accept() + + try: + config_widget = self.config_widget() + except NotImplementedError: + config_widget = None + prefname = 'plugin config dialog:'+self.type + ':' + self.name - config_dialog = QDialog(parent) + + config_dialog = ConfigDialog(parent, config_widget) button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel) v = QVBoxLayout(config_dialog) button_box.accepted.connect(config_dialog.accept) button_box.rejected.connect(config_dialog.reject) config_dialog.setWindowTitle(_('Customize') + ' ' + self.name) - try: - config_widget = self.config_widget() - except NotImplementedError: - config_widget = None if isinstance(config_widget, tuple): from calibre.gui2 import warning_dialog diff --git a/src/calibre/devices/kobo/kobotouch_config.py b/src/calibre/devices/kobo/kobotouch_config.py index d4b6c280f3..4f01b2adbc 100644 --- a/src/calibre/devices/kobo/kobotouch_config.py +++ b/src/calibre/devices/kobo/kobotouch_config.py @@ -54,11 +54,13 @@ class KOBOTOUCHConfig(TabbedDeviceConfig): def __init__(self, device_settings, all_formats, supports_subdirs, must_read_metadata, supports_use_author_sort, - extra_customization_message, device, extra_customization_choices=None, parent=None): + extra_customization_message, device, extra_customization_choices=None, + parent=None): super().__init__(device_settings, all_formats, supports_subdirs, must_read_metadata, supports_use_author_sort, - extra_customization_message, device, extra_customization_choices, parent) + extra_customization_message, device, extra_customization_choices, parent, + validate_before_accept=True) self.device_settings = device_settings self.all_formats = all_formats @@ -570,7 +572,6 @@ class CollectionsGroupBox(DeviceOptionsGroupBox): self.use_collections_template_checkbox.clicked.connect(self.use_collections_template_checkbox_clicked) self.use_collections_columns_checkbox_clicked(device.get_pref('use_collections_columns')) self.use_collections_template_checkbox_clicked(device.get_pref('use_collections_template')) - self.collections_columns_edit.editingFinished.connect(self.validate_collections_columns) def validate(self): v = self.validate_collections_columns() @@ -590,7 +591,6 @@ class CollectionsGroupBox(DeviceOptionsGroupBox): error_dialog(self, _('Kobo configuration: Invalid collection column names'), '

'+_("Collection column names that don't exist in the library: {0}").format(s), show=True) - self.collections_columns_edit.setFocus(Qt.FocusReason.OtherFocusReason) return False return True diff --git a/src/calibre/gui2/device_drivers/tabbed_device_config.py b/src/calibre/gui2/device_drivers/tabbed_device_config.py index d8bae0d0ee..1fe7bbab1b 100644 --- a/src/calibre/gui2/device_drivers/tabbed_device_config.py +++ b/src/calibre/gui2/device_drivers/tabbed_device_config.py @@ -63,15 +63,19 @@ class TabbedDeviceConfig(QTabWidget): DeviceConfigTab, for each set of options. Within the tabs, group boxes, subclassed from DeviceOptionsGroupBox, are created to further group the options. The group boxes can be coded to support any control type and dependencies between them. + + Set validate_before_accept to True if you want validation() to be called + when OK is pressed ''' def __init__(self, device_settings, all_formats, supports_subdirs, must_read_metadata, supports_use_author_sort, extra_customization_message, device, - extra_customization_choices=None, parent=None): + extra_customization_choices=None, parent=None, validate_before_accept = False): QTabWidget.__init__(self, parent) self._device = weakref.ref(device) + self.validate_before_accept = validate_before_accept self.device_settings = device_settings self.all_formats = set(all_formats) self.supports_subdirs = supports_subdirs