diff --git a/plugins/python/krita_script_starter/krita_script_starter.py b/plugins/python/krita_script_starter/krita_script_starter.py index 1cc92aaacc..e812af4d31 100644 --- a/plugins/python/krita_script_starter/krita_script_starter.py +++ b/plugins/python/krita_script_starter/krita_script_starter.py @@ -1,426 +1,419 @@ """ BBD's Krita script starter This script does the boring stuff involved in creating a script for Krita. it creates * a directory for the script in the correct Krita resources subdirectory, * populates that directory with: -- a __init__.py file, -- a skeleton file for the script proper -- a Manual.html file * creates a .desktop file for the script It also: * correctly imports the script proper nto __init__.py, creates * creates basic skeleton code depending on whether the script is intended to be an extension or a docker * creates skeleton code in the Manual.html file * (optionally) automatically enables the script in the Krita menu Script can be run from the command line. This can be used to bootstrap the script into a Krita menu entry - create a new script called Krita Script Starter, then copy the script (and the .ui file) into the directory you have just created, overwriting the existing files. BBD 16 March 2018 """ import os import sys from PyQt5.QtCore import QStandardPaths, QSettings from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox import PyQt5.uic as uic try: import krita CONTEXT_KRITA = True EXTENSION = krita.Extension except ImportError: # script being run in testing environment without Krita CONTEXT_KRITA = False EXTENSION = QWidget # TESTING = True TESTING = False MAIN_KRITA_ID = "Krita Script Starter" MAIN_KRITA_MENU_ENTRY = "Krita Script Starter" SCRIPT_NAME = "script_name" SCRIPT_COMMENT = "script_comment" KRITA_ID = "krita_id" LIBRARY_NAME = "library_name" MENU_ENTRY = "menu_entry" SCRIPT_TYPE = "script_type" # extension v docker PYTHON_FILE_NAME = "python_file" CLASS_NAME = "class_name" # from LIBRARY_NAME get: # the name of the directory # the name of the main python file # the name of the class SCRIPT_EXTENSION = "Extension" SCRIPT_DOCKER = "Docker`" SCRIPT_SETTINGS = 'python' UI_FILE = "bbdkss.ui" def load_ui(ui_file): """If this script has been distributed with a ui file in the same directory, then find that directory (since it will likely be different from krita's current working directory) and use that to load the ui file. return the loaded ui """ abs_path = os.path.dirname(os.path.realpath(__file__)) ui_file = os.path.join(abs_path, UI_FILE) return uic.loadUi(ui_file) DESKTOP_TEMPLATE = """[Desktop Entry] Type=Service ServiceTypes=Krita/PythonPlugin X-KDE-Library={library_name} X-Python-2-Compatible=false X-Krita-Manual=Manual.html Name={script_name} Comment={script_comment} """ INIT_TEMPLATE_EXTENSION = """from .{library_name} import {class_name} # And add the extension to Krita's list of extensions: app = Krita.instance() # Instantiate your class: extension = {class_name}(parent = app) app.addExtension(extension) """ INIT_TEMPLATE_DOCKER = """from .{library_name} import {class_name} """ EXTENSION_TEMPLATE = """# BBD's Krita Script Starter Feb 2018 from krita import Extension EXTENSION_ID = '{krita_id}' MENU_ENTRY = '{menu_entry}' class {class_name}(Extension): def __init__(self, parent): # Always initialise the superclass. # This is necessary to create the underlying C++ object super().__init__(parent) def setup(self): pass def createActions(self, window): action = window.createAction(EXTENSION_ID, MENU_ENTRY, "tools/scripts") # parameter 1 = the name that Krita uses to identify the action # parameter 2 = the text to be added to the menu entry for this script # parameter 3 = location of menu entry action.triggered.connect(self.action_triggered) def action_triggered(self): pass # your active code goes here. """ DOCKER_TEMPLATE = """#BBD's Krita Script Starter Feb 2018 from krita import DockWidget, DockWidgetFactory, DockWidgetFactoryBase DOCKER_NAME = '{script_name}' DOCKER_ID = '{krita_id}' class {class_name}(DockWidget): def __init__(self): super().__init__() self.setWindowTitle(DOCKER_NAME) def canvasChanged(self, canvas): pass instance = Krita.instance() dock_widget_factory = DockWidgetFactory(DOCKER_ID, DockWidgetFactoryBase.DockRight, {class_name}) instance.addDockWidgetFactory(dock_widget_factory) """ MANUAL_TEMPLATE = """
%s" % self.target_directory) message.append( "
") for f in self.files: message.append("%s
" % f) message.append("%s
" % self.manual_file) message.append("
") script_path = os.path.join(self.target_directory, self.package_file) message.append("%s
" % script_path) message.append("Open this file to edit your Manual:
") script_path = os.path.join(self.target_directory, self.manual_file) message.append("%s
" % script_path) message.append("
") message = "\n".join(message) # Display message box if CONTEXT_KRITA: msgbox = QMessageBox() else: msgbox = QMessageBox(self) msgbox.setWindowTitle(title) msgbox.setText(message) msgbox.setStandardButtons(QMessageBox.Ok) msgbox.setDefaultButton(QMessageBox.Ok) msgbox.setIcon(QMessageBox.Information) msgbox.exec() self.ui.close() def name_change(self, text): """ name of script has changed, * calculate consequential names * update the e_files_display edit """ text = text.strip() if len(text) == 0: return self.calculate_file_names(text) edit_text = self.calculate_edit_text() self.ui.e_files_display.setText(edit_text) def calculate_file_names(self, text): # name of class spam = text.split(" ") for i, s in enumerate(spam): s = s.strip() s = s.lower() try: spam[i] = s[0].upper()+s[1:] except IndexError: continue self.class_name = "".join(spam) # Normalise library name if TESTING: self.package_name = "bbdsss_"+text.lower().replace(" ", "_") else: self.package_name = text.lower().replace(" ", "_") # create desktop file self.desktop_fn = self.package_name+'.desktop' self.init_name = os.path.join(self.package_name, "__init__.py") self.package_file = os.path.join( self.package_name, self.package_name + ".py") self.manual_file = os.path.join(self.package_name, "Manual.html") self.files = [self.desktop_fn, self.init_name, self.package_name, self.package_file, self.manual_file] def calculate_edit_text(self): """ Determine if any of the intended files will collide with existing files. """ conflict_template = "%s - FILE ALREADY EXISTS
" non_conflict_template = "%s
" file_conflict = False html = ["
One or more of the files to be created already exists. If you click "Create Script" those files will be deleted and new files created in their place.
""") return "\n".join(html) if __name__ == "__main__": # this includes when the script is run from the command line or # from the Scripter plugin. if CONTEXT_KRITA: # scripter plugin # give up - has the wrong context to create widgets etc. # maybe in the future change this. pass else: app = QApplication([]) extension = KritaScriptStarter(None) extension.setup() extension.action_triggered() sys.exit(app.exec_())