Changeset View
Changeset View
Standalone View
Standalone View
plugins/python/hello/hello.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 | ||||
14 | | ||||
15 | """ | ||||
16 | This is a simple example of a Python script for Krita. | ||||
17 | It demonstrates how to set up a custom extension and a custom docker! | ||||
18 | """ | ||||
9 | 19 | | |||
10 | https://creativecommons.org/publicdomain/zero/1.0/legalcode | | |||
11 | ''' | | |||
12 | # | | |||
13 | # This is a simple example of a Python script for Krita. | | |||
14 | # It demonstrates how to set up a custom extension and a custom docker! | | |||
15 | # | | |||
16 | 20 | | |||
17 | from PyQt5.QtWidgets import QWidget, QLabel, QMessageBox | 21 | from PyQt5.QtWidgets import QWidget, QLabel, QMessageBox | ||
18 | from krita import (Krita, Extension, DockWidget, | 22 | from krita import Extension, DockWidget | ||
19 | DockWidgetFactory, DockWidgetFactoryBase) | | |||
20 | 23 | | |||
21 | 24 | | |||
22 | def hello(): | 25 | def hello(): | ||
23 | """ | 26 | """ | ||
24 | Show a test message box. | 27 | Show a test message box. | ||
25 | """ | 28 | """ | ||
26 | QMessageBox.information(QWidget(), i18n("Test"), i18n("Hello! This is Krita version %s") % Application.version()) | 29 | QMessageBox.information( | ||
30 | QWidget(), | ||||
31 | i18n("Test"), | ||||
32 | i18n("Hello! This is Krita version %s") % Application.version()) | ||||
27 | 33 | | |||
28 | 34 | | |||
29 | class HelloExtension(Extension): | 35 | class HelloExtension(Extension): | ||
30 | """ | 36 | """ | ||
31 | HelloExtension is a small example extension demonstrating basic Python scripting support in Krita! | 37 | HelloExtension is a small example extension demonstrating basic | ||
38 | Python scripting support in Krita! | ||||
32 | """ | 39 | """ | ||
33 | 40 | | |||
34 | def __init__(self, parent): | 41 | def __init__(self, parent): | ||
35 | """ | 42 | """ | ||
36 | Standard Krita Python extension constructor. | 43 | Standard Krita Python extension constructor. | ||
37 | Most of the initialization happens in :func:`setup` | 44 | Most of the initialization happens in :func:`setup` | ||
38 | 45 | | |||
39 | :param parent: Parent widget | 46 | :param parent: Parent widget | ||
40 | :type parent: :class:`QWidget` or None | 47 | :type parent: :class:`QWidget` or None | ||
41 | """ | 48 | """ | ||
42 | super(HelloExtension, self).__init__(parent) | 49 | super(HelloExtension, self).__init__(parent) | ||
43 | 50 | | |||
44 | def setup(self): | 51 | def setup(self): | ||
45 | pass | 52 | pass | ||
46 | 53 | | |||
47 | def createActions(self, window): | 54 | def createActions(self, window): | ||
48 | """ | 55 | """ | ||
49 | This is where most of the setup takes place! | 56 | This is where most of the setup takes place! | ||
50 | """ | 57 | """ | ||
51 | action = window.createAction("hello_python", i18n("Hello")) | 58 | action = window.createAction("hello_python", i18n("Hello")) | ||
52 | action.triggered.connect(hello) | 59 | action.triggered.connect(hello) | ||
53 | 60 | | |||
54 | 61 | | |||
55 | # Initialize and add the extension | | |||
56 | Scripter.addExtension(HelloExtension(Krita.instance())) | | |||
57 | | ||||
58 | | ||||
59 | class HelloDocker(DockWidget): | 62 | class HelloDocker(DockWidget): | ||
60 | """ | 63 | """ | ||
61 | The HelloDocker is an example of a simple Python-based docker. | 64 | The HelloDocker is an example of a simple Python-based docker. | ||
62 | """ | 65 | """ | ||
63 | 66 | | |||
64 | def __init__(self): | 67 | def __init__(self): | ||
65 | """ | 68 | """ | ||
66 | Constructs an instance of HelloDocker and the widget it contains | 69 | Constructs an instance of HelloDocker and the widget it contains | ||
Show All 10 Lines | |||||
77 | def canvasChanged(self, canvas): | 80 | def canvasChanged(self, canvas): | ||
78 | """ | 81 | """ | ||
79 | Override canvasChanged from :class:`DockWidget`. | 82 | Override canvasChanged from :class:`DockWidget`. | ||
80 | This gets called when the canvas changes. | 83 | This gets called when the canvas changes. | ||
81 | You can also access the active canvas via :func:`DockWidget.canvas` | 84 | You can also access the active canvas via :func:`DockWidget.canvas` | ||
82 | Parameter `canvas` can be null if the last document is closed | 85 | Parameter `canvas` can be null if the last document is closed | ||
83 | """ | 86 | """ | ||
84 | self._label.setText("HelloDocker: canvas changed") | 87 | self._label.setText("HelloDocker: canvas changed") | ||
85 | | ||||
86 | | ||||
87 | # Register the docker so Krita can use it! | | |||
88 | Application.addDockWidgetFactory(DockWidgetFactory("hello", DockWidgetFactoryBase.DockRight, HelloDocker)) | |