mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #8176 (Expose configuration dialog for a plugin via API)
This commit is contained in:
parent
51314ce5d8
commit
67c9bec212
@ -80,6 +80,84 @@ class Plugin(object): # {{{
|
|||||||
'''
|
'''
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def config_widget(self):
|
||||||
|
'''
|
||||||
|
Implement this method and :meth:`save_settings` in your plugin to
|
||||||
|
use a custom configuration dialog, rather then relying on the simple
|
||||||
|
string based default customization.
|
||||||
|
|
||||||
|
This method, if implemented, must return a QWidget. The widget can have
|
||||||
|
an optional method validate() that takes no arguments and is called
|
||||||
|
immediately after the user clicks OK. Changes are applied if and only
|
||||||
|
if the method returns True.
|
||||||
|
'''
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def save_settings(self, config_widget):
|
||||||
|
'''
|
||||||
|
Save the settings specified by the user with config_widget.
|
||||||
|
|
||||||
|
:param config_widget: The widget returned by :meth:`config_widget`.
|
||||||
|
|
||||||
|
'''
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def do_user_config(self, parent=None):
|
||||||
|
'''
|
||||||
|
This method shows a configuration dialog for this plugin. It returns
|
||||||
|
True if the user clicks OK, False otherwise. The changes are
|
||||||
|
automatically applied.
|
||||||
|
'''
|
||||||
|
from PyQt4.Qt import QDialog, QDialogButtonBox, QVBoxLayout, \
|
||||||
|
QLabel, Qt, QLineEdit
|
||||||
|
config_dialog = QDialog(parent)
|
||||||
|
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.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 config_widget is not None:
|
||||||
|
v.addWidget(config_widget)
|
||||||
|
v.addWidget(button_box)
|
||||||
|
config_dialog.exec_()
|
||||||
|
|
||||||
|
if config_dialog.result() == QDialog.Accepted:
|
||||||
|
if hasattr(config_widget, 'validate'):
|
||||||
|
if config_widget.validate():
|
||||||
|
self.save_settings(config_widget)
|
||||||
|
else:
|
||||||
|
self.save_settings(config_widget)
|
||||||
|
else:
|
||||||
|
from calibre.customize.ui import plugin_customization, \
|
||||||
|
customize_plugin
|
||||||
|
help_text = self.customization_help(gui=True)
|
||||||
|
help_text = QLabel(help_text, config_dialog)
|
||||||
|
help_text.setWordWrap(True)
|
||||||
|
help_text.setTextInteractionFlags(Qt.LinksAccessibleByMouse
|
||||||
|
| Qt.LinksAccessibleByKeyboard)
|
||||||
|
help_text.setOpenExternalLinks(True)
|
||||||
|
v.addWidget(help_text)
|
||||||
|
sc = plugin_customization(self)
|
||||||
|
if not sc:
|
||||||
|
sc = ''
|
||||||
|
sc = sc.strip()
|
||||||
|
sc = QLineEdit(sc, config_dialog)
|
||||||
|
v.addWidget(sc)
|
||||||
|
v.addWidget(button_box)
|
||||||
|
config_dialog.exec_()
|
||||||
|
|
||||||
|
if config_dialog.result() == QDialog.Accepted:
|
||||||
|
sc = unicode(sc.text()).strip()
|
||||||
|
customize_plugin(self, sc)
|
||||||
|
|
||||||
|
return config_dialog.result()
|
||||||
|
|
||||||
def load_resources(self, names):
|
def load_resources(self, names):
|
||||||
'''
|
'''
|
||||||
If this plugin comes in a ZIP file (user added plugin), this method
|
If this plugin comes in a ZIP file (user added plugin), this method
|
||||||
|
@ -8,13 +8,12 @@ __docformat__ = 'restructuredtext en'
|
|||||||
import textwrap, os
|
import textwrap, os
|
||||||
|
|
||||||
from PyQt4.Qt import Qt, QModelIndex, QAbstractItemModel, QVariant, QIcon, \
|
from PyQt4.Qt import Qt, QModelIndex, QAbstractItemModel, QVariant, QIcon, \
|
||||||
QBrush, QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QLineEdit
|
QBrush
|
||||||
|
|
||||||
from calibre.gui2.preferences import ConfigWidgetBase, test_widget
|
from calibre.gui2.preferences import ConfigWidgetBase, test_widget
|
||||||
from calibre.gui2.preferences.plugins_ui import Ui_Form
|
from calibre.gui2.preferences.plugins_ui import Ui_Form
|
||||||
from calibre.customize.ui import initialized_plugins, is_disabled, enable_plugin, \
|
from calibre.customize.ui import initialized_plugins, is_disabled, enable_plugin, \
|
||||||
disable_plugin, customize_plugin, \
|
disable_plugin, plugin_customization, add_plugin, \
|
||||||
plugin_customization, add_plugin, \
|
|
||||||
remove_plugin
|
remove_plugin
|
||||||
from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files
|
from calibre.gui2 import NONE, error_dialog, info_dialog, choose_files
|
||||||
|
|
||||||
@ -189,49 +188,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
_('Plugin: %s does not need customization')%plugin.name).exec_()
|
_('Plugin: %s does not need customization')%plugin.name).exec_()
|
||||||
return
|
return
|
||||||
self.changed_signal.emit()
|
self.changed_signal.emit()
|
||||||
|
if plugin.do_user_config():
|
||||||
config_dialog = QDialog(self)
|
|
||||||
button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
|
|
||||||
v = QVBoxLayout(config_dialog)
|
|
||||||
|
|
||||||
button_box.accepted.connect(config_dialog.accept)
|
|
||||||
button_box.rejected.connect(config_dialog.reject)
|
|
||||||
config_dialog.setWindowTitle(_('Customize') + ' ' + plugin.name)
|
|
||||||
|
|
||||||
if hasattr(plugin, 'config_widget'):
|
|
||||||
config_widget = plugin.config_widget()
|
|
||||||
v.addWidget(config_widget)
|
|
||||||
v.addWidget(button_box)
|
|
||||||
config_dialog.exec_()
|
|
||||||
|
|
||||||
if config_dialog.result() == QDialog.Accepted:
|
|
||||||
if hasattr(config_widget, 'validate'):
|
|
||||||
if config_widget.validate():
|
|
||||||
plugin.save_settings(config_widget)
|
|
||||||
else:
|
|
||||||
plugin.save_settings(config_widget)
|
|
||||||
self._plugin_model.refresh_plugin(plugin)
|
|
||||||
else:
|
|
||||||
help_text = plugin.customization_help(gui=True)
|
|
||||||
help_text = QLabel(help_text, config_dialog)
|
|
||||||
help_text.setWordWrap(True)
|
|
||||||
help_text.setTextInteractionFlags(Qt.LinksAccessibleByMouse
|
|
||||||
| Qt.LinksAccessibleByKeyboard)
|
|
||||||
help_text.setOpenExternalLinks(True)
|
|
||||||
v.addWidget(help_text)
|
|
||||||
sc = plugin_customization(plugin)
|
|
||||||
if not sc:
|
|
||||||
sc = ''
|
|
||||||
sc = sc.strip()
|
|
||||||
sc = QLineEdit(sc, config_dialog)
|
|
||||||
v.addWidget(sc)
|
|
||||||
v.addWidget(button_box)
|
|
||||||
config_dialog.exec_()
|
|
||||||
|
|
||||||
if config_dialog.result() == QDialog.Accepted:
|
|
||||||
sc = unicode(sc.text()).strip()
|
|
||||||
customize_plugin(plugin, sc)
|
|
||||||
|
|
||||||
self._plugin_model.refresh_plugin(plugin)
|
self._plugin_model.refresh_plugin(plugin)
|
||||||
elif op == 'remove':
|
elif op == 'remove':
|
||||||
if remove_plugin(plugin):
|
if remove_plugin(plugin):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user