File indexing completed on 2024-12-22 04:16:28
0001 # SPDX-License-Identifier: CC0-1.0 0002 0003 import sys 0004 import krita 0005 from PyQt5.QtGui import QIcon 0006 from . import uitenscripts 0007 0008 if sys.version_info[0] > 2: 0009 import importlib 0010 else: 0011 import imp 0012 0013 0014 class TenScriptsExtension(krita.Extension): 0015 0016 def __init__(self, parent): 0017 super(TenScriptsExtension, self).__init__(parent) 0018 0019 self.scripts = [] 0020 self.actionToIndex = {} 0021 self.indexToAction = {} 0022 0023 def setup(self): 0024 self.readSettings() 0025 0026 def createActions(self, window): 0027 action = window.createAction("ten_scripts", i18n("Ten Scripts")) 0028 action.setToolTip(i18n("Assign ten scripts to ten shortcuts.")) 0029 action.triggered.connect(self.initialize) 0030 self.loadActions(window) 0031 0032 def initialize(self): 0033 self.uitenscripts = uitenscripts.UITenScripts() 0034 self.uitenscripts.initialize(self) 0035 0036 def readSettings(self): 0037 self.scripts = Application.readSetting( 0038 "tenscripts", "scripts", "").split(',') 0039 0040 def writeSettings(self): 0041 0042 saved_scripts = self.uitenscripts.saved_scripts() 0043 self.scripts = saved_scripts 0044 0045 Application.writeSetting( 0046 "tenscripts", "scripts", ','.join(map(str, saved_scripts))) 0047 0048 def loadActions(self, window): 0049 for index, item in enumerate(['1', '2', '3', '4', '5', 0050 '6', '7', '8', '9', '10']): 0051 action = window.createAction( 0052 "execute_script_" + item, 0053 str(i18n("Execute Script {num}")).format(num=item), 0054 "") 0055 action.triggered.connect(self._executeScript) 0056 self.actionToIndex[action.objectName()] = index 0057 self.indexToAction[index] = action.objectName() 0058 0059 def _executeScript(self): 0060 scriptIndex = self.actionToIndex[self.sender().objectName()] 0061 script = self.scripts[scriptIndex] if len(self.scripts) > scriptIndex else None 0062 window = Application.activeWindow() 0063 0064 if script: 0065 try: 0066 if sys.version_info[0] > 2: 0067 spec = importlib.util.spec_from_file_location( 0068 "users_script", script) 0069 users_module = importlib.util.module_from_spec(spec) 0070 spec.loader.exec_module(users_module) 0071 else: 0072 users_module = imp.load_source("users_script", script) 0073 0074 if (hasattr(users_module, 'main') 0075 and callable(users_module.main)): 0076 users_module.main() 0077 0078 window.activeView().showFloatingMessage( 0079 str(i18n("Script {0} executed")).format(script), QIcon(), 1500, 2) 0080 0081 except Exception as e: 0082 window.activeView().showFloatingMessage(str(e), QIcon(), 2000, 1) 0083 else: 0084 window.activeView().showFloatingMessage( 0085 str(i18n(("You did not assign a script to that action"))), QIcon(), 1500, 2)