Krita Source Code Documentation
Loading...
Searching...
No Matches
palette_docker.py
Go to the documentation of this file.
1# Description: A Python based docker that allows you to edit KPL color
2# palettes.
3
4# By Wolthera(originally)
5
6# SPDX-License-Identifier: CC0-1.0
7
8# @package palette_docker
9
10
11# Importing the relevant dependencies:
12try:
13 from PyQt6.QtGui import QPixmap, QIcon, QPainter, QBrush, QPalette
14 from PyQt6.QtWidgets import (
15 QWidget, QVBoxLayout, QHBoxLayout, QComboBox, QTabWidget,
16 QLineEdit, QSpinBox, QDialogButtonBox, QToolButton, QDialog,
17 QPlainTextEdit, QCompleter, QMenu)
18 from PyQt6.QtCore import Qt, pyqtSlot
19 from PyQt6.QtGui import QAction
20except:
21 from PyQt5.QtGui import QPixmap, QIcon, QPainter, QBrush, QPalette
22 from PyQt5.QtWidgets import (
23 QWidget, QVBoxLayout, QHBoxLayout, QComboBox, QAction, QTabWidget,
24 QLineEdit, QSpinBox, QDialogButtonBox, QToolButton, QDialog,
25 QPlainTextEdit, QCompleter, QMenu)
26 from PyQt5.Qt import Qt, pyqtSlot
27from krita import DockWidget, Palette, PaletteView
28from builtins import i18n, i18nc, Application
29
30# import the exporters
31from . import (
32 palette_exporter_gimppalette,
33 palette_exporter_inkscapeSVG,
34 palette_sortColors,
35)
36
37
38class PaletteDocker(DockWidget):
39 # Init the docker
40
41 def __init__(self):
42 super(PaletteDocker, self).__init__()
43 # make base-widget and layout
44 widget = QWidget()
45 layout = QVBoxLayout()
46 buttonLayout = QHBoxLayout()
47 widget.setLayout(layout)
48 self.setWindowTitle(i18n("Python Palette Docker"))
49
50 # Make a combobox and add palettes
51 self.cmb_palettes = QComboBox()
52 allPalettes = Application.resources("palette")
53 for palette_name in allPalettes:
54 self.cmb_palettes.addItem(palette_name)
55 self.cmb_palettes.model().sort(0)
56
57 if len(allPalettes.keys()) > 0:
58 self.currentPalette = Palette(list(allPalettes.values())[0])
59 else:
60 self.currentPalette = None
61
62 self.cmb_palettes.currentTextChanged.connect(self.slot_paletteChangedslot_paletteChanged)
63 layout.addWidget(self.cmb_palettes) # add combobox to the layout
64 self.paletteView = PaletteView()
65 self.paletteView.setPalette(self.currentPalette)
66 layout.addWidget(self.paletteView)
67 self.paletteView.entrySelectedForeGround.connect(
69
70 self.colorComboBox = QComboBox()
71 self.colorList = list()
72 buttonLayout.addWidget(self.colorComboBox)
73 self.bnSetColor = QToolButton()
74 self.bnSetColor.setText(i18n("Set"))
76 buttonLayout.addWidget(self.bnSetColor)
77
78 self.addEntry = QAction(self)
79 self.addEntry.setIconText(i18n("+"))
80 self.addEntry.triggered.connect(self.slot_add_entryslot_add_entry)
81 self.addGroup = QAction(self)
82 self.addGroup.triggered.connect(self.slot_add_groupslot_add_group)
83 self.addGroup.setText(i18nc("Group as Color Group in a Palette", "Add Group"))
84 self.addGroup.setIconText(str("\U0001F4C2"))
85 self.removeEntry = QAction(self)
86 self.removeEntry.setText(i18n("Remove Entry"))
87 self.removeEntry.setIconText("-")
89 addEntryButton = QToolButton()
90 addEntryButton.setDefaultAction(self.addEntry)
91 buttonLayout.addWidget(addEntryButton)
92 addGroupButton = QToolButton()
93 addGroupButton.setDefaultAction(self.addGroup)
94 buttonLayout.addWidget(addGroupButton)
95 removeEntryButton = QToolButton()
96 removeEntryButton.setDefaultAction(self.removeEntry)
97 buttonLayout.addWidget(removeEntryButton)
98
99 # QActions
100 self.extra = QToolButton()
101 self.editPaletteData = QAction(self)
102 self.editPaletteData.setText(i18n("Edit Palette Settings"))
104 self.extra.setDefaultAction(self.editPaletteData)
105 buttonLayout.addWidget(self.extra)
106 self.actionMenu = QMenu()
107 self.exportToGimp = QAction(self)
108 self.exportToGimp.setText(i18n("Export as GIMP Palette File"))
110 self.exportToInkscape = QAction(self)
111 self.exportToInkscape.setText(
112 i18n("Export as Inkscape SVG with Swatches"))
113 self.exportToInkscape.triggered.connect(
115 self.sortColors = QAction(self)
116 self.sortColors.setText(i18n("Sort Colors"))
117 self.sortColors.triggered.connect(self.slot_sort_colorsslot_sort_colors)
118 self.actionMenu.addAction(self.editPaletteData)
119 self.actionMenu.addAction(self.exportToGimp)
120 self.actionMenu.addAction(self.exportToInkscape)
121 self.actionMenu.addAction(self.sortColors)
122
123 self.extra.setMenu(self.actionMenu)
124
125 layout.addLayout(buttonLayout)
126 self.slot_fill_combobox()
127 self.setWidget(widget) # add widget to the docker
128
129 def slot_paletteChanged(self, name):
130 allPalettes = Application.resources("palette")
131 if len(allPalettes) > 0 and name in allPalettes:
132 self.currentPalette = Palette(
133 Application.resources("palette")[name])
134 self.paletteView.setPalette(self.currentPalette)
135 self.slot_fill_combobox()
136
137 @pyqtSlot('Swatch')
138 def slot_swatchSelected(self, entry):
139 if (self.canvas()) is not None:
140 if (self.canvas().view()) is not None:
141 name = entry.name()
142 if len(entry.id()) > 0:
143 name = entry.id() + " - " + entry.name()
144 if len(name) > 0:
145 if name in self.colorList:
146 self.colorComboBox.setCurrentIndex(
147 self.colorList.index(name))
148 color = entry.color()
149 self.canvas().view().setForeGroundColor(color)
150
152 '''A function for making a combobox with the available colors. We use
153 QCompleter on the colorComboBox so that people can type in the
154 name of a color to select it. This is useful for people with
155 carefully made palettes where the colors are named properly,
156 which makes it easier for them to find colors.
157 '''
158
159 if self.currentPalette is None:
160 return
161 self.colorComboBox.clear()
162 self.colorList = list()
163 palette = self.currentPalette
164 for i in range(palette.colorsCountTotal()):
165 entry = palette.entryByIndex(i)
166 color = entry.color().colorForCanvas(self.canvas())
167 colorSquare = QPixmap(12, 12)
168 if entry.spotColor() is True:
169 img = colorSquare.toImage()
170 circlePainter = QPainter()
171 img.fill(self.colorComboBox.palette().color(QPalette.ColorRole.Base))
172 circlePainter.begin(img)
173 brush = QBrush(Qt.BrushStyle.SolidPattern)
174 brush.setColor(color)
175 circlePainter.setBrush(brush)
176 circlePainter.pen().setWidth(0)
177 circlePainter.drawEllipse(0, 0, 11, 11)
178 circlePainter.end()
179 colorSquare = QPixmap.fromImage(img)
180 else:
181 colorSquare.fill(color)
182 name = entry.name()
183 if len(entry.id()) > 0:
184 name = entry.id() + " - " + entry.name()
185 self.colorList.append(name)
186 self.colorComboBox.addItem(QIcon(colorSquare), name)
187 self.colorComboBox.setEditable(True)
188 self.colorComboBox.setInsertPolicy(QComboBox.InsertPolicy.NoInsert)
189 self.colorComboBox.completer().setCompletionMode(
190 QCompleter.CompletionMode.PopupCompletion)
191 self.colorComboBox.completer().setCaseSensitivity(Qt.CaseSensitivity.CaseInsensitive)
192 self.colorComboBox.completer().setFilterMode(Qt.MatchFlag.MatchContains)
193
195 if self.currentPalette is not None:
196 entry = self.currentPalette.entryByIndex(
197 self.colorComboBox.currentIndex())
199
200 def slot_add_entry(self):
201 if (self.canvas()) is not None:
202 if (self.canvas().view()) is not None:
203 color = self.canvas().view().foregroundColor()
204 success = self.paletteView.addEntryWithDialog(color)
205 if success is True:
206 self.slot_fill_combobox()
207
208 def slot_add_group(self):
209 success = self.paletteView.addGroupWithDialog()
210 if success is True:
211 self.slot_fill_combobox()
212
214 success = self.paletteView.removeSelectedEntryWithDialog()
215 if success is True:
216 self.slot_fill_combobox()
217
219 '''A function for giving a gui to edit palette metadata... I also
220 want this to be the way to edit the settings of the palette
221 docker.
222 '''
223
224 dialog = QDialog(self)
225 tabWidget = QTabWidget()
226 dialog.setWindowTitle(i18n("Edit Palette Data"))
227 dialog.setLayout(QVBoxLayout())
228 dialog.layout().addWidget(tabWidget)
229 paletteWidget = QWidget()
230 paletteWidget.setLayout(QVBoxLayout())
231 tabWidget.addTab(paletteWidget, i18n("Palette Data"))
232 paletteName = QLineEdit()
233 paletteName.setText(self.cmb_palettes.currentText())
234 paletteWidget.layout().addWidget(paletteName)
235 paletteColumns = QSpinBox()
236 paletteColumns.setValue(self.currentPalette.columnCount())
237 paletteWidget.layout().addWidget(paletteColumns)
238 paletteComment = QPlainTextEdit()
239 paletteComment.appendPlainText(self.currentPalette.comment())
240 paletteWidget.layout().addWidget(paletteComment)
241 buttons = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
242 dialog.layout().addWidget(buttons)
243 buttons.accepted.connect(dialog.accept)
244 # buttons.rejected.connect(dialog.reject())
245
246 if dialog.exec() == QDialog.DialogCode.Accepted:
247 Resource = Application.resources("palette")[
248 self.cmb_palettes.currentText()]
249 Resource.setName(paletteName.text())
250 self.currentPalette = Palette(Resource)
251 self.currentPalette.setColumnCount(paletteColumns.value())
252 self.paletteView.setPalette(self.currentPalette)
253 self.slot_fill_combobox()
254 self.currentPalette.setComment(paletteComment.toPlainText())
255 self.currentPalette.save()
256
266 colorSorter = palette_sortColors.sortColors(
267 self.cmb_palettes.currentText())
268 self.paletteView.setPalette(colorSorter.palette())
269
270 def canvasChanged(self, canvas):
271 self.cmb_palettes.clear()
272 allPalettes = Application.resources("palette")
273 for palette_name in allPalettes:
274 self.cmb_palettes.addItem(palette_name)
275 self.cmb_palettes.model().sort(0)
276
277 if self.currentPalette is None and len(allPalettes.keys()) > 0:
278 self.currentPalette = Palette(list(allPalettes.values())[0])
void setName(QString value)
Definition Resource.cpp:95
rgba palette[MAX_PALETTE]
Definition palette.c:35