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