Changeset View
Changeset View
Standalone View
Standalone View
plugins/python/plugin_importer/plugin_importer_extension.py
- This file was added.
1 | # Copyright (c) 2019 Rebecca Breu <rebecca@rbreu.de> | ||||
---|---|---|---|---|---|
2 | | ||||
3 | # This file is part of Krita. | ||||
4 | | ||||
5 | # Krita is free software: you can redistribute it and/or modify | ||||
6 | # it under the terms of the GNU General Public License as published by | ||||
7 | # the Free Software Foundation, either version 3 of the License, or | ||||
8 | # (at your option) any later version. | ||||
9 | | ||||
10 | # Krita is distributed in the hope that it will be useful, | ||||
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | # GNU General Public License for more details. | ||||
14 | | ||||
15 | # You should have received a copy of the GNU General Public License | ||||
16 | # along with Krita. If not, see <https://www.gnu.org/licenses/>. | ||||
17 | | ||||
18 | import os | ||||
19 | | ||||
20 | import krita | ||||
21 | | ||||
22 | from PyQt5.QtCore import QStandardPaths | ||||
23 | from PyQt5.QtWidgets import QFileDialog, QMessageBox | ||||
24 | | ||||
25 | from .plugin_importer import PluginImporter, PluginImportError | ||||
26 | | ||||
27 | | ||||
28 | class PluginImporterExtension(krita.Extension): | ||||
29 | | ||||
30 | def __init__(self, parent): | ||||
31 | super().__init__(parent) | ||||
32 | self.parent = parent | ||||
33 | | ||||
34 | def setup(self): | ||||
35 | pass | ||||
36 | | ||||
37 | def createActions(self, window): | ||||
38 | action = window.createAction( | ||||
39 | 'plugin_importer', | ||||
40 | i18n('Import Python Plugin...'), | ||||
41 | 'tools/scripts') | ||||
42 | action.triggered.connect(self.import_plugin) | ||||
43 | | ||||
44 | def confirm_overwrite(self, plugin): | ||||
45 | reply = QMessageBox.question( | ||||
46 | self.parent.activeWindow().qwindow(), | ||||
47 | i18n('Overwrite Plugin'), | ||||
48 | i18n('The plugin "%s" already exists. Overwrite it?') % ( | ||||
49 | plugin['ui_name']), | ||||
50 | QMessageBox.Yes | QMessageBox.No) | ||||
51 | return reply == QMessageBox.Yes | ||||
52 | | ||||
53 | def get_success_text(self, plugins): | ||||
54 | txt = [ | ||||
55 | '<p>', | ||||
56 | i18n('The following plugins were imported:'), | ||||
57 | '</p>', | ||||
58 | '<ul>' | ||||
59 | ] | ||||
60 | for plugin in plugins: | ||||
61 | txt.append('<li>%s</li>' % plugin['ui_name']) | ||||
62 | | ||||
63 | txt.append('</ul>') | ||||
64 | txt.append('<p>') | ||||
65 | txt.append(i18n( | ||||
66 | 'Please restart Krita and activate the plugins in ' | ||||
67 | '<em>Settings -> Configure Krita -> ' | ||||
68 | 'Python Plugin Manager</em>.')) | ||||
69 | txt.append('</p>') | ||||
70 | return ('\n').join(txt) | ||||
71 | | ||||
72 | def get_resources_dir(self): | ||||
73 | return QStandardPaths.writableLocation( | ||||
74 | QStandardPaths.AppDataLocation) | ||||
75 | | ||||
76 | def import_plugin(self): | ||||
77 | zipfile = QFileDialog.getOpenFileName( | ||||
78 | self.parent.activeWindow().qwindow(), | ||||
79 | i18n('Import Plugin'), | ||||
80 | os.path.expanduser('~'), | ||||
81 | '%s (*.zip)' % i18n('Zip Archives'), | ||||
82 | )[0] | ||||
83 | | ||||
84 | if not zipfile: | ||||
85 | return | ||||
86 | | ||||
87 | try: | ||||
88 | imported = PluginImporter( | ||||
89 | zipfile, | ||||
90 | self.get_resources_dir(), | ||||
91 | self.confirm_overwrite | ||||
92 | ).import_all() | ||||
93 | except PluginImportError as e: | ||||
94 | msg = '<p>%s</p><pre>%s</pre>' % ( | ||||
95 | i18n('Error during import:'), str(e)) | ||||
96 | QMessageBox.warning( | ||||
97 | self.parent.activeWindow().qwindow(), | ||||
98 | i18n('Error'), | ||||
99 | msg) | ||||
100 | return | ||||
101 | | ||||
102 | if imported: | ||||
103 | QMessageBox.information( | ||||
104 | self.parent.activeWindow().qwindow(), | ||||
105 | i18n('Import successful'), | ||||
106 | self.get_success_text(imported)) |