1# This script is licensed CC 0 1.0, so that you can learn from it. 2 3# ------ CC 0 1.0 --------------- 4 5# The person who associated a work with this deed has dedicated the 6# work to the public domain by waiving all of his or her rights to the 7# work worldwide under copyright law, including all related and 8# neighboring rights, to the extent allowed by law. 9 10# You can copy, modify, distribute and perform the work, even for 11# commercial purposes, all without asking permission. 12 13# https://creativecommons.org/publicdomain/zero/1.0/legalcode 14 15import sys 16 17from PyQt5.QtWidgets import QMessageBox 18import krita 19from . import uitenscripts 20 21if sys.version_info[0] > 2: 22 import importlib 23else: 24 import imp 25 26 27class TenScriptsExtension(krita.Extension): 28 29 def __init__(self, parent): 30 super(TenScriptsExtension, self).__init__(parent) 31 32 self.actions = [] 33 self.scripts = [] 34 35 def setup(self): 36 self.readSettings() 37 38 def createActions(self, window): 39 action = window.createAction("ten_scripts", i18n("Ten Scripts")) 40 action.setToolTip(i18n("Assign ten scripts to ten shortcuts.")) 41 action.triggered.connect(self.initialize) 42 self.loadActions(window) 43 44 def initialize(self): 45 self.uitenscripts = uitenscripts.UITenScripts() 46 self.uitenscripts.initialize(self) 47 48 def readSettings(self): 49 self.scripts = Application.readSetting( 50 "tenscripts", "scripts", "").split(',') 51 52 def writeSettings(self): 53 saved_scripts = self.uitenscripts.saved_scripts() 54 55 for index, script in enumerate(saved_scripts): 56 self.actions[index].script = script 57 58 Application.writeSetting( 59 "tenscripts", "scripts", ','.join(map(str, saved_scripts))) 60 61 def loadActions(self, window): 62 for index, item in enumerate(['1', '2', '3', '4', '5', 63 '6', '7', '8', '9', '10']): 64 action = window.createAction( 65 "execute_script_" + item, 66 str(i18n("Execute Script {num}")).format(num=item), 67 "") 68 action.script = None 69 action.triggered.connect(self._executeScript) 70 71 if index < len(self.scripts): 72 action.script = self.scripts[index] 73 74 self.actions.append(action) 75 76 def _executeScript(self): 77 script = self.sender().script 78 if script: 79 try: 80 if sys.version_info[0] > 2: 81 spec = importlib.util.spec_from_file_location( 82 "users_script", script) 83 users_module = importlib.util.module_from_spec(spec) 84 spec.loader.exec_module(users_module) 85 else: 86 users_module = imp.load_source("users_script", script) 87 88 if (hasattr(users_module, 'main') 89 and callable(users_module.main)): 90 users_module.main() 91 92 self.showMessage( 93 str(i18n("Script {0} executed")).format(script)) 94 except Exception as e: 95 self.showMessage(str(e)) 96 else: 97 self.showMessage( 98 i18n("You did not assign a script to that action")) 99 100 def showMessage(self, message): 101 self.msgBox = QMessageBox(Application.activeWindow().qwindow()) 102 self.msgBox.setText(message) 103 self.msgBox.exec_() 104