Changeset View
Changeset View
Standalone View
Standalone View
plugins/python/krita_script_starter/krita_script_starter.py
1 | """ | 1 | """ | ||
---|---|---|---|---|---|
2 | BBD's Krita script starter | 2 | BBD's Krita script starter | ||
3 | 3 | | |||
4 | This script does the boring stuff involved in creating a script for Krita. | 4 | This script does the boring stuff involved in creating a script for Krita. | ||
5 | it creates | 5 | it creates | ||
6 | * a directory for the script in the correct Krita resources subdirectory, | 6 | * a directory for the script in the correct Krita resources subdirectory, | ||
7 | * populates that directory with: | 7 | * populates that directory with: | ||
8 | -- a __init__.py file, | 8 | -- a __init__.py file, | ||
9 | -- a skeleton file for the script proper | 9 | -- a skeleton file for the script proper | ||
10 | -- a Manual.html file | 10 | -- a Manual.html file | ||
11 | * creates a .desktop file for the script | 11 | * creates a .desktop file for the script | ||
12 | It also: | 12 | It also: | ||
13 | * correctly imports the script proper nto __init__.py, creates | 13 | * correctly imports the script proper nto __init__.py, creates | ||
14 | * creates basic skeleton code depending on whether the script is intended | 14 | * creates basic skeleton code depending on whether the script is intended | ||
15 | to be an extension or a docker | 15 | to be an extension or a docker | ||
16 | * creates skeleton code in the Manual.html file | 16 | * creates skeleton code in the Manual.html file | ||
17 | * (optionally) automatically enables the script in the Krita menu | 17 | * (optionally) automatically enables the script in the Krita menu | ||
18 | 18 | | |||
19 | Script can be run from the command line. This can be used to | 19 | Script can be run from the command line. This can be used to | ||
20 | bootstrap the script into a Krita menu entry - create a new script | 20 | bootstrap the script into a Krita menu entry - create a new script | ||
21 | called Krita Script Starter, then copy the script (and the .ui file) | 21 | called Krita Script Starter, then copy the script (and the .ui file) | ||
22 | into the directory you have just created, overwriting the existing | 22 | into the directory you have just created, overwriting the existing | ||
23 | files. | 23 | files. | ||
24 | 24 | | |||
25 | BBD | 25 | BBD | ||
26 | 16 March 2018 | 26 | 16 March 2018 | ||
27 | """ | 27 | """ | ||
28 | 28 | | |||
29 | import os, sys | 29 | import os | ||
30 | import sys | ||||
30 | from PyQt5.QtCore import QStandardPaths, QSettings | 31 | from PyQt5.QtCore import QStandardPaths, QSettings | ||
31 | from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox | 32 | from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox | ||
32 | import PyQt5.uic as uic | 33 | import PyQt5.uic as uic | ||
33 | 34 | | |||
34 | try: | 35 | try: | ||
35 | import krita | 36 | import krita | ||
36 | CONTEXT_KRITA = True | 37 | CONTEXT_KRITA = True | ||
37 | Krita = Krita # to stop Eric ide complaining about unknown Krita | | |||
38 | EXTENSION = krita.Extension | 38 | EXTENSION = krita.Extension | ||
39 | 39 | | |||
40 | except ImportError: # script being run in testing environment without Krita | 40 | except ImportError: | ||
41 | # script being run in testing environment without Krita | ||||
41 | CONTEXT_KRITA = False | 42 | CONTEXT_KRITA = False | ||
42 | EXTENSION = QWidget | 43 | EXTENSION = QWidget | ||
43 | 44 | | |||
44 | #TESTING = True | 45 | # TESTING = True | ||
45 | TESTING = False | 46 | TESTING = False | ||
46 | 47 | | |||
47 | MAIN_KRITA_ID = "Krita Script Starter" | 48 | MAIN_KRITA_ID = "Krita Script Starter" | ||
48 | MAIN_KRITA_MENU_ENTRY = "Krita Script Starter" | 49 | MAIN_KRITA_MENU_ENTRY = "Krita Script Starter" | ||
49 | 50 | | |||
50 | SCRIPT_NAME = "script_name" | 51 | SCRIPT_NAME = "script_name" | ||
51 | SCRIPT_COMMENT = "script_comment" | 52 | SCRIPT_COMMENT = "script_comment" | ||
52 | KRITA_ID = "krita_id" | 53 | KRITA_ID = "krita_id" | ||
53 | LIBRARY_NAME = "library_name" | 54 | LIBRARY_NAME = "library_name" | ||
54 | MENU_ENTRY = "menu_entry" | 55 | MENU_ENTRY = "menu_entry" | ||
55 | SCRIPT_TYPE = "script_type" # extension v docker | 56 | SCRIPT_TYPE = "script_type" # extension v docker | ||
56 | PYTHON_FILE_NAME = "python_file" | 57 | PYTHON_FILE_NAME = "python_file" | ||
57 | CLASS_NAME = "class_name" | 58 | CLASS_NAME = "class_name" | ||
58 | 59 | | |||
59 | # from LIBRARY_NAME get: | 60 | # from LIBRARY_NAME get: | ||
60 | # the name of the directory | 61 | # the name of the directory | ||
61 | # the name of the main python file | 62 | # the name of the main python file | ||
62 | # the name of the class | 63 | # the name of the class | ||
63 | 64 | | |||
64 | SCRIPT_EXTENSION = "Extension" | 65 | SCRIPT_EXTENSION = "Extension" | ||
65 | SCRIPT_DOCKER = "Docker`" | 66 | SCRIPT_DOCKER = "Docker`" | ||
66 | 67 | | |||
67 | SCRIPT_SETTINGS = 'python' | 68 | SCRIPT_SETTINGS = 'python' | ||
68 | 69 | | |||
69 | UI_FILE = "bbdkss.ui" | 70 | UI_FILE = "bbdkss.ui" | ||
70 | 71 | | |||
72 | | ||||
71 | def load_ui(ui_file): | 73 | def load_ui(ui_file): | ||
72 | """ if this script has been distributed with a ui file in the same directory, | 74 | """If this script has been distributed with a ui file in the same | ||
73 | then find that directory (since it will likely be different from krita's current | 75 | directory, then find that directory (since it will likely be | ||
74 | working directory) and use that to load the ui file | 76 | different from krita's current working directory) and use that to | ||
77 | load the ui file. | ||||
75 | 78 | | |||
76 | return the loaded ui | 79 | return the loaded ui | ||
77 | """ | 80 | """ | ||
78 | abs_path = os.path.dirname(os.path.realpath(__file__)) | 81 | abs_path = os.path.dirname(os.path.realpath(__file__)) | ||
79 | ui_file = os.path.join(abs_path, UI_FILE) | 82 | ui_file = os.path.join(abs_path, UI_FILE) | ||
80 | return uic.loadUi(ui_file) | 83 | return uic.loadUi(ui_file) | ||
81 | 84 | | |||
82 | 85 | | |||
83 | DESKTOP_TEMPLATE = """[Desktop Entry] | 86 | DESKTOP_TEMPLATE = """[Desktop Entry] | ||
84 | Type=Service | 87 | Type=Service | ||
85 | ServiceTypes=Krita/PythonPlugin | 88 | ServiceTypes=Krita/PythonPlugin | ||
86 | X-KDE-Library={library_name} | 89 | X-KDE-Library={library_name} | ||
87 | X-Python-2-Compatible=false | 90 | X-Python-2-Compatible=false | ||
88 | X-Krita-Manual=Manual.html | 91 | X-Krita-Manual=Manual.html | ||
89 | Name={script_name} | 92 | Name={script_name} | ||
90 | Comment={script_comment} | 93 | Comment={script_comment} | ||
91 | """ | 94 | """ | ||
92 | 95 | | |||
93 | INIT_TEMPLATE = """from .{library_name} import *""" | 96 | INIT_TEMPLATE = """from .{library_name} import {class_name} | ||
94 | 97 | | |||
95 | EXTENSION_TEMPLATE = """#BBD's Krita Script Starter Feb 2018 | 98 | # And add the extension to Krita's list of extensions: | ||
99 | app = Krita.instance() | ||||
100 | # Instantiate your class: | ||||
101 | extension = {class_name}(parent=app) | ||||
102 | app.addExtension(extension) | ||||
103 | """ | ||||
104 | | ||||
105 | EXTENSION_TEMPLATE = """# BBD's Krita Script Starter Feb 2018 | ||||
96 | 106 | | |||
97 | from krita import Extension | 107 | from krita import Extension | ||
98 | 108 | | |||
99 | EXTENSION_ID = '{krita_id}' | 109 | EXTENSION_ID = '{krita_id}' | ||
100 | MENU_ENTRY = '{menu_entry}' | 110 | MENU_ENTRY = '{menu_entry}' | ||
101 | 111 | | |||
112 | | ||||
102 | class {class_name}(Extension): | 113 | class {class_name}(Extension): | ||
103 | 114 | | |||
104 | def __init__(self, parent): | 115 | def __init__(self, parent): | ||
105 | #Always initialise the superclass, This is necessary to create the underlying C++ object | 116 | # Always initialise the superclass. | ||
117 | # This is necessary to create the underlying C++ object | ||||
106 | super().__init__(parent) | 118 | super().__init__(parent) | ||
107 | 119 | | |||
108 | def setup(self): | 120 | def setup(self): | ||
109 | pass | 121 | pass | ||
110 | 122 | | |||
111 | def createActions(self, window): | 123 | def createActions(self, window): | ||
112 | action = window.createAction(EXTENSION_ID, MENU_ENTRY, "tools/scripts") | 124 | action = window.createAction(EXTENSION_ID, MENU_ENTRY, "tools/scripts") | ||
113 | # parameter 1 = the name that Krita uses to identify the action | 125 | # parameter 1 = the name that Krita uses to identify the action | ||
114 | # parameter 2 = the text to be added to the menu entry for this script | 126 | # parameter 2 = the text to be added to the menu entry for this script | ||
115 | # parameter 3 = location of menu entry | 127 | # parameter 3 = location of menu entry | ||
116 | action.triggered.connect(self.action_triggered) | 128 | action.triggered.connect(self.action_triggered) | ||
117 | | ||||
118 | def action_triggered(self): | | |||
119 | pass # your active code goes here. | | |||
120 | 129 | | |||
121 | # And add the extension to Krita's list of extensions: | 130 | def action_triggered(self): | ||
122 | app=Krita.instance() | 131 | pass # your active code goes here. | ||
123 | extension={class_name}(parent=app) #instantiate your class | | |||
124 | app.addExtension(extension) | | |||
125 | """ | 132 | """ | ||
126 | 133 | | |||
127 | DOCKER_TEMPLATE = """#BBD's Krita Script Starter Feb 2018 | 134 | DOCKER_TEMPLATE = """#BBD's Krita Script Starter Feb 2018 | ||
128 | from krita import DockWidget, DockWidgetFactory, DockWidgetFactoryBase | 135 | from krita import DockWidget, DockWidgetFactory, DockWidgetFactoryBase | ||
129 | 136 | | |||
130 | DOCKER_NAME = '{script_name}' | 137 | DOCKER_NAME = '{script_name}' | ||
131 | DOCKER_ID = '{krita_id}' | 138 | DOCKER_ID = '{krita_id}' | ||
132 | 139 | | |||
140 | | ||||
133 | class {class_name}(DockWidget): | 141 | class {class_name}(DockWidget): | ||
134 | 142 | | |||
135 | def __init__(self): | 143 | def __init__(self): | ||
136 | super().__init__() | 144 | super().__init__() | ||
137 | self.setWindowTitle(DOCKER_NAME) | 145 | self.setWindowTitle(DOCKER_NAME) | ||
138 | 146 | | |||
139 | def canvasChanged(self, canvas): | 147 | def canvasChanged(self, canvas): | ||
140 | pass | 148 | pass | ||
141 | 149 | | |||
150 | | ||||
142 | instance = Krita.instance() | 151 | instance = Krita.instance() | ||
143 | dock_widget_factory = DockWidgetFactory(DOCKER_ID, | 152 | dock_widget_factory = DockWidgetFactory(DOCKER_ID, | ||
144 | DockWidgetFactoryBase.DockRight, | 153 | DockWidgetFactoryBase.DockRight, | ||
145 | {class_name}) | 154 | {class_name}) | ||
146 | 155 | | |||
147 | instance.addDockWidgetFactory(dock_widget_factory) | 156 | instance.addDockWidgetFactory(dock_widget_factory) | ||
148 | """ | 157 | """ | ||
149 | 158 | | |||
150 | MANUAL_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> | 159 | MANUAL_TEMPLATE = """<?xml version="1.0" encoding="utf-8"?> | ||
151 | <!DOCTYPE html> | 160 | <!DOCTYPE html> | ||
152 | 161 | | |||
153 | <html xmlns="http://www.w3.org/1999/xhtml"> | 162 | <html xmlns="http://www.w3.org/1999/xhtml"> | ||
154 | <!--BBD's Krita Script Starter, Feb 2018 --> | 163 | <!--BBD's Krita Script Starter, Feb 2018 --> | ||
155 | <head><title>{script_name}</title> | 164 | <head><title>{script_name}</title> | ||
156 | </head> | 165 | </head> | ||
157 | <body> | 166 | <body> | ||
158 | <h3>{script_name}</h3> | 167 | <h3>{script_name}</h3> | ||
159 | Tell people about what your script does here. This is an html document so you can format it with html tags. | 168 | Tell people about what your script does here. | ||
169 | This is an html document so you can format it with html tags. | ||||
160 | <h3>Usage</h3> | 170 | <h3>Usage</h3> | ||
161 | Tell people how to use your script here. | 171 | Tell people how to use your script here. | ||
162 | 172 | | |||
163 | </body> | 173 | </body> | ||
164 | </html>""" | 174 | </html>""" | ||
165 | 175 | | |||
176 | | ||||
166 | class KritaScriptStarter(EXTENSION): | 177 | class KritaScriptStarter(EXTENSION): | ||
167 | 178 | | |||
168 | def __init__(self, parent): | 179 | def __init__(self, parent): | ||
169 | super().__init__(parent) | 180 | super().__init__(parent) | ||
170 | 181 | | |||
171 | def setup(self): | 182 | def setup(self): | ||
172 | self.script_abs_path = os.path.dirname(os.path.realpath(__file__)) | 183 | self.script_abs_path = os.path.dirname(os.path.realpath(__file__)) | ||
173 | self.ui_file = os.path.join(self.script_abs_path, UI_FILE) | 184 | self.ui_file = os.path.join(self.script_abs_path, UI_FILE) | ||
174 | self.ui = load_ui(self.ui_file) | 185 | self.ui = load_ui(self.ui_file) | ||
175 | 186 | | |||
176 | self.ui.e_name_of_script.textChanged.connect(self.name_change) | 187 | self.ui.e_name_of_script.textChanged.connect(self.name_change) | ||
177 | self.ui.cancel_button.clicked.connect(self.cancel) | 188 | self.ui.cancel_button.clicked.connect(self.cancel) | ||
178 | self.ui.create_button.clicked.connect(self.create) | 189 | self.ui.create_button.clicked.connect(self.create) | ||
179 | 190 | | |||
180 | app_data_location = QStandardPaths.AppDataLocation | 191 | app_data_location = QStandardPaths.AppDataLocation | ||
181 | target_directory = QStandardPaths.writableLocation(app_data_location) | 192 | target_directory = QStandardPaths.writableLocation(app_data_location) | ||
182 | if not CONTEXT_KRITA: | 193 | if not CONTEXT_KRITA: | ||
183 | target_directory = os.path.join(target_directory, "krita") | 194 | target_directory = os.path.join(target_directory, "krita") | ||
184 | target_directory = os.path.join(target_directory, "pykrita") | 195 | target_directory = os.path.join(target_directory, "pykrita") | ||
185 | self.target_directory = target_directory | 196 | self.target_directory = target_directory | ||
186 | 197 | | |||
187 | # if CONTEXT_KRITA: | 198 | def createActions(self, window): | ||
188 | # app = Krita.instance() | | |||
189 | # action = app.createAction(MAIN_KRITA_ID, MAIN_KRITA_MENU_ENTRY) | | |||
190 | # # parameter 1 = the name that Krita uses to identify the action | | |||
191 | # # parameter 2 = the text to be added to the menu entry for this script | | |||
192 | # action.triggered.connect(self.action_triggered) | | |||
193 | | ||||
194 | def createActions(self, window): | | |||
195 | """ Called by Krita to create actions.""" | 199 | """ Called by Krita to create actions.""" | ||
196 | action = window.createAction(MAIN_KRITA_ID, MAIN_KRITA_MENU_ENTRY, "tools/scripts") | 200 | action = window.createAction( | ||
197 | # parameter 1 = the name that Krita uses to identify the action | 201 | MAIN_KRITA_ID, MAIN_KRITA_MENU_ENTRY, "tools/scripts") | ||
202 | # parameter 1 = the name that Krita uses to identify the action | ||||
198 | # parameter 2 = the text to be added to the menu entry for this script | 203 | # parameter 2 = the text to be added to the menu entry for this script | ||
199 | # parameter 3 = location of menu entry | 204 | # parameter 3 = location of menu entry | ||
200 | action.triggered.connect(self.action_triggered) | 205 | action.triggered.connect(self.action_triggered) | ||
201 | 206 | | |||
202 | def action_triggered(self): | 207 | def action_triggered(self): | ||
203 | self.ui.show() | 208 | self.ui.show() | ||
204 | self.ui.activateWindow() | 209 | self.ui.activateWindow() | ||
205 | 210 | | |||
206 | def cancel(self): | 211 | def cancel(self): | ||
207 | self.ui.close() | 212 | self.ui.close() | ||
208 | 213 | | |||
209 | def create(self): | 214 | def create(self): | ||
210 | """Go ahead and create the relevant files. """ | 215 | """Go ahead and create the relevant files. """ | ||
211 | 216 | | |||
212 | if self.ui.e_name_of_script.text().strip() == "": | 217 | if self.ui.e_name_of_script.text().strip() == "": | ||
213 | #Don't create script with empty name | 218 | # Don't create script with empty name | ||
214 | return | 219 | return | ||
215 | 220 | | |||
216 | def full_dir(path): #convenience function | 221 | def full_dir(path): | ||
217 | return os.path.join(self.target_directory, path) | 222 | # convenience function | ||
218 | 223 | return os.path.join(self.target_directory, path) | |||
224 | | ||||
219 | # should already be done, but just in case: | 225 | # should already be done, but just in case: | ||
220 | self.calculate_file_names(self.ui.e_name_of_script.text()) | 226 | self.calculate_file_names(self.ui.e_name_of_script.text()) | ||
221 | 227 | | |||
222 | menu_entry = self.ui.e_menu_entry.text() | 228 | menu_entry = self.ui.e_menu_entry.text() | ||
223 | if menu_entry.strip() == "": | 229 | if menu_entry.strip() == "": | ||
224 | menu_entry = self.ui.e_name_of_script.text() | 230 | menu_entry = self.ui.e_name_of_script.text() | ||
225 | 231 | | |||
226 | comment = self.ui.e_comment.text() | 232 | comment = self.ui.e_comment.text() | ||
227 | if comment.strip()=="": | 233 | if comment.strip() == "": | ||
228 | comment = "Replace this text with your description" | 234 | comment = "Replace this text with your description" | ||
229 | 235 | | |||
230 | values = {SCRIPT_NAME : self.ui.e_name_of_script.text(), | 236 | values = { | ||
231 | SCRIPT_COMMENT : comment, | 237 | SCRIPT_NAME: self.ui.e_name_of_script.text(), | ||
232 | KRITA_ID : "pykrita_"+self.package_name, | 238 | SCRIPT_COMMENT: comment, | ||
233 | SCRIPT_TYPE :SCRIPT_DOCKER if self.ui.rb_docker.isChecked() else SCRIPT_EXTENSION, | 239 | KRITA_ID: "pykrita_" + self.package_name, | ||
234 | MENU_ENTRY:menu_entry, | 240 | SCRIPT_TYPE: SCRIPT_DOCKER if self.ui.rb_docker.isChecked() else SCRIPT_EXTENSION, # noqa: E501 | ||
235 | LIBRARY_NAME : self.package_name, | 241 | MENU_ENTRY: menu_entry, | ||
236 | CLASS_NAME: self.class_name | 242 | LIBRARY_NAME: self.package_name, | ||
243 | CLASS_NAME: self.class_name | ||||
237 | } | 244 | } | ||
238 | 245 | | |||
239 | try: | 246 | try: | ||
240 | # create package directory | 247 | # create package directory | ||
241 | package_directory = full_dir(self.package_name) | 248 | package_directory = full_dir(self.package_name) | ||
242 | # needs to be lowercase and no spaces | 249 | # needs to be lowercase and no spaces | ||
243 | os.mkdir(package_directory) | 250 | os.mkdir(package_directory) | ||
244 | except FileExistsError: | 251 | except FileExistsError: | ||
245 | pass # if package directory exists write into it, overwriting existing files. | 252 | # if package directory exists write into it, overwriting | ||
253 | # existing files. | ||||
254 | pass | ||||
246 | 255 | | |||
247 | # create desktop file | 256 | # create desktop file | ||
248 | fn = full_dir(self.desktop_fn) | 257 | fn = full_dir(self.desktop_fn) | ||
249 | with open(fn, 'w+t') as f: | 258 | with open(fn, 'w+t') as f: | ||
250 | f.write(DESKTOP_TEMPLATE.format(**values)) | 259 | f.write(DESKTOP_TEMPLATE.format(**values)) | ||
251 | 260 | | |||
252 | fn = full_dir(self.init_name) | 261 | fn = full_dir(self.init_name) | ||
253 | with open(fn, 'w+t') as f: | 262 | with open(fn, 'w+t') as f: | ||
254 | f.write(INIT_TEMPLATE.format(**values)) | 263 | f.write(INIT_TEMPLATE.format(**values)) | ||
255 | 264 | | |||
256 | # create main package file | 265 | # create main package file | ||
257 | fn = full_dir(self.package_file) | 266 | fn = full_dir(self.package_file) | ||
258 | 267 | | |||
259 | if self.ui.rb_docker.isChecked(): | 268 | if self.ui.rb_docker.isChecked(): | ||
260 | with open(fn, 'w+t') as f: | 269 | with open(fn, 'w+t') as f: | ||
261 | f.write(DOCKER_TEMPLATE.format(**values)) | 270 | f.write(DOCKER_TEMPLATE.format(**values)) | ||
262 | else: | 271 | else: | ||
263 | # Default to extension type | 272 | # Default to extension type | ||
264 | with open(fn, 'w+t') as f: | 273 | with open(fn, 'w+t') as f: | ||
265 | f.write(EXTENSION_TEMPLATE.format(**values)) | 274 | f.write(EXTENSION_TEMPLATE.format(**values)) | ||
266 | 275 | | |||
267 | # create manual file. | 276 | # create manual file. | ||
268 | fn = full_dir(self.manual_file) | 277 | fn = full_dir(self.manual_file) | ||
269 | with open(fn, 'w+t') as f: | 278 | with open(fn, 'w+t') as f: | ||
270 | f.write(MANUAL_TEMPLATE.format(**values)) | 279 | f.write(MANUAL_TEMPLATE.format(**values)) | ||
271 | # enable script in krita settings (!) | 280 | # enable script in krita settings (!) | ||
272 | 281 | | |||
273 | if self.ui.cb_enable_script.isChecked(): | 282 | if self.ui.cb_enable_script.isChecked(): | ||
274 | configPath = QStandardPaths.writableLocation(QStandardPaths.GenericConfigLocation) | 283 | configPath = QStandardPaths.writableLocation( | ||
275 | configPath = os.path.join(configPath, 'kritarc' ) | 284 | QStandardPaths.GenericConfigLocation) | ||
285 | configPath = os.path.join(configPath, 'kritarc') | ||||
276 | settings = QSettings(configPath, QSettings.IniFormat) | 286 | settings = QSettings(configPath, QSettings.IniFormat) | ||
277 | settings.beginGroup(SCRIPT_SETTINGS) | 287 | settings.beginGroup(SCRIPT_SETTINGS) | ||
278 | settings.setValue('enable_'+self.package_name, 'true') | 288 | settings.setValue('enable_'+self.package_name, 'true') | ||
279 | settings.endGroup() | 289 | settings.endGroup() | ||
280 | settings.sync() | 290 | settings.sync() | ||
281 | 291 | | |||
282 | # notify success | 292 | # notify success | ||
283 | # Assemble message | 293 | # Assemble message | ||
284 | title = "Krita Script files created" | 294 | title = "Krita Script files created" | ||
285 | message = [] | 295 | message = [] | ||
286 | message.append("<h3>Directory</h3>") | 296 | message.append("<h3>Directory</h3>") | ||
287 | message.append("Project files were created in the directory<p>%s"%self.target_directory) | 297 | message.append("Project files were created in the directory<p>%s" | ||
288 | message.append("<h3>Files Created</h3>The following files were created:<p>") | 298 | % self.target_directory) | ||
299 | message.append( | ||||
300 | "<h3>Files Created</h3>The following files were created:<p>") | ||||
289 | for f in self.files: | 301 | for f in self.files: | ||
290 | message.append("%s<p>"%f) | 302 | message.append("%s<p>" % f) | ||
291 | message.append("%s<p>"%self.manual_file) | 303 | message.append("%s<p>" % self.manual_file) | ||
292 | message.append("<h3>Location of script</h3>") | 304 | message.append("<h3>Location of script</h3>") | ||
293 | message.append("Open this file to edit your script:<p>") | 305 | message.append("Open this file to edit your script:<p>") | ||
294 | script_path = os.path.join(self.target_directory, self.package_file) | 306 | script_path = os.path.join(self.target_directory, self.package_file) | ||
295 | message.append("%s<p>"%script_path) | 307 | message.append("%s<p>" % script_path) | ||
296 | message.append("Open this file to edit your Manual:<p>") | 308 | message.append("Open this file to edit your Manual:<p>") | ||
297 | script_path = os.path.join(self.target_directory, self.manual_file) | 309 | script_path = os.path.join(self.target_directory, self.manual_file) | ||
298 | message.append("%s<p>"%script_path) | 310 | message.append("%s<p>" % script_path) | ||
299 | message.append("<h3>Record these locations</h3>") | 311 | message.append("<h3>Record these locations</h3>") | ||
300 | message.append("Make a note of these locations before you click ok.<p>") | 312 | message.append( | ||
313 | "Make a note of these locations before you click ok.<p>") | ||||
301 | message = "\n".join(message) | 314 | message = "\n".join(message) | ||
302 | 315 | | |||
303 | # Display message box | 316 | # Display message box | ||
304 | if CONTEXT_KRITA: | 317 | if CONTEXT_KRITA: | ||
305 | msgbox = QMessageBox() | 318 | msgbox = QMessageBox() | ||
306 | else: | 319 | else: | ||
307 | msgbox = QMessageBox(self) | 320 | msgbox = QMessageBox(self) | ||
308 | msgbox.setWindowTitle(title) | 321 | msgbox.setWindowTitle(title) | ||
309 | msgbox.setText(message) | 322 | msgbox.setText(message) | ||
310 | msgbox.setStandardButtons(QMessageBox.Ok) | 323 | msgbox.setStandardButtons(QMessageBox.Ok) | ||
311 | msgbox.setDefaultButton(QMessageBox.Ok) | 324 | msgbox.setDefaultButton(QMessageBox.Ok) | ||
312 | msgbox.setIcon(QMessageBox.Information) | 325 | msgbox.setIcon(QMessageBox.Information) | ||
313 | msgbox.exec() | 326 | msgbox.exec() | ||
314 | 327 | | |||
315 | self.ui.close() | 328 | self.ui.close() | ||
316 | 329 | | |||
317 | def name_change(self, text): | 330 | def name_change(self, text): | ||
318 | """ | 331 | """ | ||
319 | name of script has changed, | 332 | name of script has changed, | ||
320 | * calculate consequential names | 333 | * calculate consequential names | ||
321 | * update the e_files_display edit | 334 | * update the e_files_display edit | ||
322 | """ | 335 | """ | ||
323 | # text = self.e_name_of_script.text() | 336 | | ||
324 | text = text.strip() | 337 | text = text.strip() | ||
325 | if len(text) == 0 : | 338 | if len(text) == 0: | ||
326 | return | 339 | return | ||
327 | 340 | | |||
328 | self.calculate_file_names(text) | 341 | self.calculate_file_names(text) | ||
329 | edit_text = self.calculate_edit_text() | 342 | edit_text = self.calculate_edit_text() | ||
330 | self.ui.e_files_display.setText(edit_text) | 343 | self.ui.e_files_display.setText(edit_text) | ||
331 | 344 | | |||
332 | def calculate_file_names(self, text): | 345 | def calculate_file_names(self, text): | ||
333 | # name of class | 346 | # name of class | ||
334 | 347 | | |||
335 | spam = text.split(" ") | 348 | spam = text.split(" ") | ||
336 | for i, s in enumerate(spam): | 349 | for i, s in enumerate(spam): | ||
337 | s = s.strip() | 350 | s = s.strip() | ||
338 | s = s.lower() | 351 | s = s.lower() | ||
339 | try: | 352 | try: | ||
340 | spam[i] = s[0].upper()+s[1:] | 353 | spam[i] = s[0].upper()+s[1:] | ||
341 | except IndexError: | 354 | except IndexError: | ||
342 | continue | 355 | continue | ||
343 | self.class_name = "".join(spam) | 356 | self.class_name = "".join(spam) | ||
344 | 357 | | |||
345 | # Normalise library name | 358 | # Normalise library name | ||
346 | if TESTING: | 359 | if TESTING: | ||
347 | self.package_name = "bbdsss_"+text.lower().replace(" ","_") | 360 | self.package_name = "bbdsss_"+text.lower().replace(" ", "_") | ||
348 | else: | 361 | else: | ||
349 | self.package_name = text.lower().replace(" ","_") | 362 | self.package_name = text.lower().replace(" ", "_") | ||
350 | 363 | | |||
351 | # create desktop file | 364 | # create desktop file | ||
352 | self.desktop_fn = self.package_name+'.desktop' | 365 | self.desktop_fn = self.package_name+'.desktop' | ||
353 | 366 | | |||
354 | self.init_name = os.path.join(self.package_name, "__init__.py") | 367 | self.init_name = os.path.join(self.package_name, "__init__.py") | ||
355 | self.package_file = os.path.join(self.package_name, self.package_name+".py") | 368 | self.package_file = os.path.join( | ||
356 | self.manual_file = os.path.join(self.package_name, "Manual.html") | 369 | self.package_name, self.package_name + ".py") | ||
357 | self.files =[self.desktop_fn, self.init_name, | 370 | self.manual_file = os.path.join(self.package_name, "Manual.html") | ||
358 | self.package_name, self.package_file, | 371 | self.files = [self.desktop_fn, self.init_name, | ||
359 | self.manual_file] | 372 | self.package_name, self.package_file, | ||
373 | self.manual_file] | ||||
360 | 374 | | |||
361 | def calculate_edit_text(self): | 375 | def calculate_edit_text(self): | ||
362 | """ | 376 | """ | ||
363 | Determine if any of the intended files will collide with existing files. | 377 | Determine if any of the intended files will collide with existing files. | ||
364 | 378 | | |||
365 | """ | 379 | """ | ||
366 | conflict_template = """%s - FILE ALREADY EXISTS<p>""" | 380 | conflict_template = "%s - FILE ALREADY EXISTS<p>" | ||
367 | non_conflict_template = "%s<p>" | 381 | non_conflict_template = "%s<p>" | ||
368 | file_conflict = False | 382 | file_conflict = False | ||
369 | 383 | | |||
370 | html = ["<h3>Will create the following files:</h3>"] | 384 | html = ["<h3>Will create the following files:</h3>"] | ||
371 | for path in self.files: | 385 | for path in self.files: | ||
372 | target_file = os.path.join(self.target_directory, path) | 386 | target_file = os.path.join(self.target_directory, path) | ||
373 | if os.path.exists(path): | 387 | if os.path.exists(path): | ||
374 | html.append(conflict_template%target_file) | 388 | html.append(conflict_template % target_file) | ||
375 | file_conflict = True | 389 | file_conflict = True | ||
376 | else: | 390 | else: | ||
377 | html.append(non_conflict_template%target_file) | 391 | html.append(non_conflict_template % target_file) | ||
378 | 392 | | |||
379 | if file_conflict: | 393 | if file_conflict: | ||
380 | html.append("""<h2><span style="color:red;font-weight:bold"> | 394 | html.append("""<h2><span style="color:red;font-weight:bold"> | ||
381 | Warning:</span></h2><p> | 395 | Warning:</span></h2><p> | ||
382 | <span style="color:red;font-weight:bold"> | 396 | <span style="color:red;font-weight:bold"> | ||
383 | One or more of the files to be created already exists. | 397 | One or more of the files to be created already exists. | ||
384 | If you click "Create Script" those files will be deleted and new files | 398 | If you click "Create Script" those files will be deleted | ||
385 | created in their place.</span><p>""") | 399 | and new files created in their place.</span><p>""") | ||
386 | 400 | | |||
387 | return "\n".join(html) | 401 | return "\n".join(html) | ||
388 | 402 | | |||
389 | if __name__ == "__main__": | 403 | | ||
390 | # this includes when the script is run from the command line or | 404 | if __name__ == "__main__": | ||
405 | # this includes when the script is run from the command line or | ||||
391 | # from the Scripter plugin. | 406 | # from the Scripter plugin. | ||
392 | if CONTEXT_KRITA: | 407 | if CONTEXT_KRITA: | ||
393 | # scripter plugin | 408 | # scripter plugin | ||
394 | # give up - has the wrong context to create widgets etc. | 409 | # give up - has the wrong context to create widgets etc. | ||
395 | # maybe in the future change this. | 410 | # maybe in the future change this. | ||
396 | pass | 411 | pass | ||
397 | else: | 412 | else: | ||
398 | app = QApplication([]) | 413 | app = QApplication([]) | ||
399 | 414 | | |||
400 | extension = KritaScriptStarter(None) | 415 | extension = KritaScriptStarter(None) | ||
401 | extension.setup() | 416 | extension.setup() | ||
402 | extension.action_triggered() | 417 | extension.action_triggered() | ||
403 | sys.exit(app.exec_()) | 418 | sys.exit(app.exec_()) | ||
404 | | ||||
405 | elif CONTEXT_KRITA: | | |||
406 | # And add the extension to Krita's list of extensions: | | |||
407 | app=Krita.instance() | | |||
408 | extension=KritaScriptStarter(parent=app) #instantiate your class | | |||
409 | app.addExtension(extension) | | |||
410 | |