mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Merge pull request #248 from glutanimate/addon-onconfigupdated
Extend add-on API with setConfigUpdatedAction
This commit is contained in:
commit
cbf3240203
1 changed files with 21 additions and 7 deletions
|
@ -204,6 +204,7 @@ When loading '%(name)s':
|
|||
######################################################################
|
||||
|
||||
_configButtonActions = {}
|
||||
_configUpdatedActions = {}
|
||||
|
||||
def addonConfigDefaults(self, dir):
|
||||
path = os.path.join(self.addonsFolder(dir), "config.json")
|
||||
|
@ -227,6 +228,9 @@ When loading '%(name)s':
|
|||
def configAction(self, addon):
|
||||
return self._configButtonActions.get(addon)
|
||||
|
||||
def configUpdatedAction(self, addon):
|
||||
return self._configUpdatedActions.get(addon)
|
||||
|
||||
# Add-on Config API
|
||||
######################################################################
|
||||
|
||||
|
@ -246,6 +250,10 @@ When loading '%(name)s':
|
|||
addon = self.addonFromModule(module)
|
||||
self._configButtonActions[addon] = fn
|
||||
|
||||
def setConfigUpdatedAction(self, module, fn):
|
||||
addon = self.addonFromModule(module)
|
||||
self._configUpdatedActions[addon] = fn
|
||||
|
||||
def writeConfig(self, module, conf):
|
||||
addon = self.addonFromModule(module)
|
||||
meta = self.addonMeta(addon)
|
||||
|
@ -457,12 +465,12 @@ class ConfigEditor(QDialog):
|
|||
restore = self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults)
|
||||
restore.clicked.connect(self.onRestoreDefaults)
|
||||
self.updateHelp()
|
||||
self.updateText()
|
||||
self.updateText(self.conf)
|
||||
self.show()
|
||||
|
||||
def onRestoreDefaults(self):
|
||||
self.conf = self.mgr.addonConfigDefaults(self.addon)
|
||||
self.updateText()
|
||||
default_conf = self.mgr.addonConfigDefaults(self.addon)
|
||||
self.updateText(default_conf)
|
||||
|
||||
def updateHelp(self):
|
||||
txt = self.mgr.addonConfigHelp(self.addon)
|
||||
|
@ -471,17 +479,23 @@ class ConfigEditor(QDialog):
|
|||
else:
|
||||
self.form.scrollArea.setVisible(False)
|
||||
|
||||
def updateText(self):
|
||||
def updateText(self, conf):
|
||||
self.form.editor.setPlainText(
|
||||
json.dumps(self.conf,sort_keys=True,indent=4, separators=(',', ': ')))
|
||||
json.dumps(conf,sort_keys=True,indent=4, separators=(',', ': ')))
|
||||
|
||||
def accept(self):
|
||||
txt = self.form.editor.toPlainText()
|
||||
try:
|
||||
self.conf = json.loads(txt)
|
||||
new_conf = json.loads(txt)
|
||||
except Exception as e:
|
||||
showInfo(_("Invalid configuration: ") + repr(e))
|
||||
return
|
||||
|
||||
self.mgr.writeConfig(self.addon, self.conf)
|
||||
if new_conf != self.conf:
|
||||
self.mgr.writeConfig(self.addon, new_conf)
|
||||
# does the add-on define an action to be fired?
|
||||
act = self.mgr.configUpdatedAction(self.addon)
|
||||
if act:
|
||||
act(new_conf)
|
||||
|
||||
super().accept()
|
||||
|
|
Loading…
Reference in a new issue