Changeset View
Changeset View
Standalone View
Standalone View
plugins/python/exportlayers/uiexportlayers.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 | from . import exportlayersdialog | 15 | from . import exportlayersdialog | ||
13 | from PyQt5.QtCore import Qt | 16 | from PyQt5.QtCore import Qt | ||
14 | from PyQt5.QtWidgets import (QFormLayout, QListWidget, QHBoxLayout, | 17 | from PyQt5.QtWidgets import (QFormLayout, QListWidget, QHBoxLayout, | ||
Context not available. | |||||
17 | QMessageBox, QFileDialog, QCheckBox, QSpinBox, | 20 | QMessageBox, QFileDialog, QCheckBox, QSpinBox, | ||
18 | QComboBox) | 21 | QComboBox) | ||
19 | import os | 22 | import os | ||
20 | import errno | | |||
21 | import krita | 23 | import krita | ||
22 | 24 | | |||
23 | 25 | | |||
Context not available. | |||||
36 | self.widgetDocuments = QListWidget() | 38 | self.widgetDocuments = QListWidget() | ||
37 | self.directoryTextField = QLineEdit() | 39 | self.directoryTextField = QLineEdit() | ||
38 | self.directoryDialogButton = QPushButton(i18n("...")) | 40 | self.directoryDialogButton = QPushButton(i18n("...")) | ||
39 | self.exportFilterLayersCheckBox = QCheckBox(i18n("Export filter layers")) | 41 | self.exportFilterLayersCheckBox = QCheckBox( | ||
42 | i18n("Export filter layers")) | ||||
40 | self.batchmodeCheckBox = QCheckBox(i18n("Export in batchmode")) | 43 | self.batchmodeCheckBox = QCheckBox(i18n("Export in batchmode")) | ||
41 | self.ignoreInvisibleLayersCheckBox = QCheckBox(i18n("Ignore invisible layers")) | 44 | self.ignoreInvisibleLayersCheckBox = QCheckBox( | ||
45 | i18n("Ignore invisible layers")) | ||||
42 | self.xResSpinBox = QSpinBox() | 46 | self.xResSpinBox = QSpinBox() | ||
43 | self.yResSpinBox = QSpinBox() | 47 | self.yResSpinBox = QSpinBox() | ||
44 | self.formatsComboBox = QComboBox() | 48 | self.formatsComboBox = QComboBox() | ||
45 | 49 | | |||
46 | self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) | 50 | self.buttonBox = QDialogButtonBox( | ||
51 | QDialogButtonBox.Ok | QDialogButtonBox.Cancel) | ||||
47 | 52 | | |||
48 | self.kritaInstance = krita.Krita.instance() | 53 | self.kritaInstance = krita.Krita.instance() | ||
49 | self.documentsList = [] | 54 | self.documentsList = [] | ||
Context not available. | |||||
57 | self.buttonBox.rejected.connect(self.mainDialog.close) | 62 | self.buttonBox.rejected.connect(self.mainDialog.close) | ||
58 | 63 | | |||
59 | self.mainDialog.setWindowModality(Qt.NonModal) | 64 | self.mainDialog.setWindowModality(Qt.NonModal) | ||
60 | self.widgetDocuments.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents) | 65 | self.widgetDocuments.setSizeAdjustPolicy( | ||
66 | QAbstractScrollArea.AdjustToContents) | ||||
61 | 67 | | |||
62 | def initialize(self): | 68 | def initialize(self): | ||
63 | self.loadDocuments() | 69 | self.loadDocuments() | ||
Context not available. | |||||
82 | self.resolutionLayout.addWidget(self.yResSpinBox) | 88 | self.resolutionLayout.addWidget(self.yResSpinBox) | ||
83 | 89 | | |||
84 | self.formLayout.addRow(i18n("Documents:"), self.documentLayout) | 90 | self.formLayout.addRow(i18n("Documents:"), self.documentLayout) | ||
85 | self.formLayout.addRow(i18n("Initial directory:"), self.directorySelectorLayout) | 91 | self.formLayout.addRow( | ||
92 | i18n("Initial directory:"), self.directorySelectorLayout) | ||||
86 | self.formLayout.addRow(i18n("Export options:"), self.optionsLayout) | 93 | self.formLayout.addRow(i18n("Export options:"), self.optionsLayout) | ||
87 | self.formLayout.addRow(i18n("Resolution:"), self.resolutionLayout) | 94 | self.formLayout.addRow(i18n("Resolution:"), self.resolutionLayout) | ||
88 | self.formLayout.addRow(i18n("Images extensions:"), self.formatsComboBox) | 95 | self.formLayout.addRow( | ||
96 | i18n("Images extensions:"), self.formatsComboBox) | ||||
89 | 97 | | |||
90 | self.line = QFrame() | 98 | self.line = QFrame() | ||
91 | self.line.setFrameShape(QFrame.HLine) | 99 | self.line.setFrameShape(QFrame.HLine) | ||
Context not available. | |||||
104 | def loadDocuments(self): | 112 | def loadDocuments(self): | ||
105 | self.widgetDocuments.clear() | 113 | self.widgetDocuments.clear() | ||
106 | 114 | | |||
107 | self.documentsList = [document for document in self.kritaInstance.documents() if document.fileName()] | 115 | self.documentsList = [ | ||
116 | document for document in self.kritaInstance.documents() | ||||
117 | if document.fileName() | ||||
118 | ] | ||||
108 | 119 | | |||
109 | for document in self.documentsList: | 120 | for document in self.documentsList: | ||
110 | self.widgetDocuments.addItem(document.fileName()) | 121 | self.widgetDocuments.addItem(document.fileName()) | ||
Context not available. | |||||
113 | self.loadDocuments() | 124 | self.loadDocuments() | ||
114 | 125 | | |||
115 | def confirmButton(self): | 126 | def confirmButton(self): | ||
116 | selectedPaths = [item.text() for item in self.widgetDocuments.selectedItems()] | 127 | selectedPaths = [ | ||
117 | selectedDocuments = [document for document in self.documentsList for path in selectedPaths if path == document.fileName()] | 128 | item.text() for item in self.widgetDocuments.selectedItems()] | ||
129 | selectedDocuments = [ | ||||
130 | document for document in self.documentsList | ||||
131 | for path in selectedPaths if path == document.fileName() | ||||
132 | ] | ||||
118 | 133 | | |||
119 | self.msgBox = QMessageBox(self.mainDialog) | 134 | self.msgBox = QMessageBox(self.mainDialog) | ||
120 | if not selectedDocuments: | 135 | if not selectedDocuments: | ||
Context not available. | |||||
128 | 143 | | |||
129 | def mkdir(self, directory): | 144 | def mkdir(self, directory): | ||
130 | target_directory = self.directoryTextField.text() + directory | 145 | target_directory = self.directoryTextField.text() + directory | ||
131 | if os.path.exists(target_directory) and os.path.isdir(target_directory): | 146 | if (os.path.exists(target_directory) | ||
147 | and os.path.isdir(target_directory)): | ||||
132 | return | 148 | return | ||
133 | 149 | | |||
134 | try: | 150 | try: | ||
Context not available. | |||||
139 | def export(self, document): | 155 | def export(self, document): | ||
140 | Application.setBatchmode(self.batchmodeCheckBox.isChecked()) | 156 | Application.setBatchmode(self.batchmodeCheckBox.isChecked()) | ||
141 | 157 | | |||
142 | documentName = document.fileName() if document.fileName() else 'Untitled' | 158 | documentName = document.fileName() if document.fileName() else 'Untitled' # noqa: E501 | ||
143 | fileName, extension = os.path.splitext(os.path.basename(documentName)) | 159 | fileName, extension = os.path.splitext(os.path.basename(documentName)) | ||
144 | self.mkdir('/' + fileName) | 160 | self.mkdir('/' + fileName) | ||
145 | 161 | | |||
146 | self._exportLayers(document.rootNode(), self.formatsComboBox.currentText(), '/' + fileName) | 162 | self._exportLayers( | ||
163 | document.rootNode(), | ||||
164 | self.formatsComboBox.currentText(), | ||||
165 | '/' + fileName) | ||||
147 | Application.setBatchmode(True) | 166 | Application.setBatchmode(True) | ||
148 | 167 | | |||
149 | def _exportLayers(self, parentNode, fileFormat, parentDir): | 168 | def _exportLayers(self, parentNode, fileFormat, parentDir): | ||
Context not available. | |||||
155 | if node.type() == 'grouplayer': | 174 | if node.type() == 'grouplayer': | ||
156 | newDir = os.path.join(parentDir, node.name()) | 175 | newDir = os.path.join(parentDir, node.name()) | ||
157 | self.mkdir(newDir) | 176 | self.mkdir(newDir) | ||
158 | elif not self.exportFilterLayersCheckBox.isChecked() and 'filter' in node.type(): | 177 | elif (not self.exportFilterLayersCheckBox.isChecked() | ||
178 | and 'filter' in node.type()): | ||||
159 | continue | 179 | continue | ||
160 | elif self.ignoreInvisibleLayersCheckBox.isChecked() and not node.visible(): | 180 | elif (self.ignoreInvisibleLayersCheckBox.isChecked() | ||
181 | and not node.visible()): | ||||
161 | continue | 182 | continue | ||
162 | else: | 183 | else: | ||
163 | nodeName = node.name() | 184 | nodeName = node.name() | ||
Context not available. | |||||
167 | elif '[png]' in nodeName: | 188 | elif '[png]' in nodeName: | ||
168 | _fileFormat = 'png' | 189 | _fileFormat = 'png' | ||
169 | 190 | | |||
170 | layerFileName = '{0}{1}/{2}.{3}'.format(self.directoryTextField.text(), | 191 | layerFileName = '{0}{1}/{2}.{3}'.format( | ||
171 | parentDir, node.name(), _fileFormat) | 192 | self.directoryTextField.text(), | ||
193 | parentDir, node.name(), _fileFormat) | ||||
172 | node.save(layerFileName, self.xResSpinBox.value(), | 194 | node.save(layerFileName, self.xResSpinBox.value(), | ||
173 | self.yResSpinBox.value(), krita.InfoObject()) | 195 | self.yResSpinBox.value(), krita.InfoObject()) | ||
174 | 196 | | |||
Context not available. | |||||
176 | self._exportLayers(node, fileFormat, newDir) | 198 | self._exportLayers(node, fileFormat, newDir) | ||
177 | 199 | | |||
178 | def _selectDir(self): | 200 | def _selectDir(self): | ||
179 | directory = QFileDialog.getExistingDirectory(self.mainDialog, i18n("Select a Folder"), os.path.expanduser("~"), QFileDialog.ShowDirsOnly) | 201 | directory = QFileDialog.getExistingDirectory( | ||
202 | self.mainDialog, | ||||
203 | i18n("Select a Folder"), | ||||
204 | os.path.expanduser("~"), | ||||
205 | QFileDialog.ShowDirsOnly) | ||||
180 | self.directoryTextField.setText(directory) | 206 | self.directoryTextField.setText(directory) | ||
181 | 207 | | |||
182 | def _setResolution(self, index): | 208 | def _setResolution(self, index): | ||
Context not available. |