mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 06:22:22 -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 = {}
|
_configButtonActions = {}
|
||||||
|
_configUpdatedActions = {}
|
||||||
|
|
||||||
def addonConfigDefaults(self, dir):
|
def addonConfigDefaults(self, dir):
|
||||||
path = os.path.join(self.addonsFolder(dir), "config.json")
|
path = os.path.join(self.addonsFolder(dir), "config.json")
|
||||||
|
@ -227,6 +228,9 @@ When loading '%(name)s':
|
||||||
def configAction(self, addon):
|
def configAction(self, addon):
|
||||||
return self._configButtonActions.get(addon)
|
return self._configButtonActions.get(addon)
|
||||||
|
|
||||||
|
def configUpdatedAction(self, addon):
|
||||||
|
return self._configUpdatedActions.get(addon)
|
||||||
|
|
||||||
# Add-on Config API
|
# Add-on Config API
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
|
@ -246,6 +250,10 @@ When loading '%(name)s':
|
||||||
addon = self.addonFromModule(module)
|
addon = self.addonFromModule(module)
|
||||||
self._configButtonActions[addon] = fn
|
self._configButtonActions[addon] = fn
|
||||||
|
|
||||||
|
def setConfigUpdatedAction(self, module, fn):
|
||||||
|
addon = self.addonFromModule(module)
|
||||||
|
self._configUpdatedActions[addon] = fn
|
||||||
|
|
||||||
def writeConfig(self, module, conf):
|
def writeConfig(self, module, conf):
|
||||||
addon = self.addonFromModule(module)
|
addon = self.addonFromModule(module)
|
||||||
meta = self.addonMeta(addon)
|
meta = self.addonMeta(addon)
|
||||||
|
@ -457,12 +465,12 @@ class ConfigEditor(QDialog):
|
||||||
restore = self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults)
|
restore = self.form.buttonBox.button(QDialogButtonBox.RestoreDefaults)
|
||||||
restore.clicked.connect(self.onRestoreDefaults)
|
restore.clicked.connect(self.onRestoreDefaults)
|
||||||
self.updateHelp()
|
self.updateHelp()
|
||||||
self.updateText()
|
self.updateText(self.conf)
|
||||||
self.show()
|
self.show()
|
||||||
|
|
||||||
def onRestoreDefaults(self):
|
def onRestoreDefaults(self):
|
||||||
self.conf = self.mgr.addonConfigDefaults(self.addon)
|
default_conf = self.mgr.addonConfigDefaults(self.addon)
|
||||||
self.updateText()
|
self.updateText(default_conf)
|
||||||
|
|
||||||
def updateHelp(self):
|
def updateHelp(self):
|
||||||
txt = self.mgr.addonConfigHelp(self.addon)
|
txt = self.mgr.addonConfigHelp(self.addon)
|
||||||
|
@ -471,17 +479,23 @@ class ConfigEditor(QDialog):
|
||||||
else:
|
else:
|
||||||
self.form.scrollArea.setVisible(False)
|
self.form.scrollArea.setVisible(False)
|
||||||
|
|
||||||
def updateText(self):
|
def updateText(self, conf):
|
||||||
self.form.editor.setPlainText(
|
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):
|
def accept(self):
|
||||||
txt = self.form.editor.toPlainText()
|
txt = self.form.editor.toPlainText()
|
||||||
try:
|
try:
|
||||||
self.conf = json.loads(txt)
|
new_conf = json.loads(txt)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
showInfo(_("Invalid configuration: ") + repr(e))
|
showInfo(_("Invalid configuration: ") + repr(e))
|
||||||
return
|
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()
|
super().accept()
|
||||||
|
|
Loading…
Reference in a new issue