File indexing completed on 2025-01-26 04:11:38

0001 # Description: A Python based docker that allows you to edit KPL color
0002 # palettes.
0003 
0004 # By Wolthera(originally)
0005 
0006 # SPDX-License-Identifier: CC0-1.0
0007 
0008 # @package palette_docker
0009 
0010 
0011 # Importing the relevant dependencies:
0012 from PyQt5.QtWidgets import (
0013     QWidget, QVBoxLayout, QHBoxLayout, QComboBox, QAction, QTabWidget,
0014     QLineEdit, QSpinBox, QDialogButtonBox, QToolButton, QDialog,
0015     QPlainTextEdit, QCompleter, QMenu)
0016 from PyQt5.Qt import Qt, pyqtSlot
0017 from krita import DockWidget, Palette, PaletteView
0018 
0019 # import the exporters
0020 from . import (
0021     palette_exporter_gimppalette,
0022     palette_exporter_inkscapeSVG,
0023     palette_sortColors,
0024 )
0025 
0026 
0027 class PaletteDocker(DockWidget):
0028     # Init the docker
0029 
0030     def __init__(self):
0031         super(PaletteDocker, self).__init__()
0032         # make base-widget and layout
0033         widget = QWidget()
0034         layout = QVBoxLayout()
0035         buttonLayout = QHBoxLayout()
0036         widget.setLayout(layout)
0037         self.setWindowTitle(i18n("Python Palette Docker"))
0038 
0039         # Make a combobox and add palettes
0040         self.cmb_palettes = QComboBox()
0041         allPalettes = Application.resources("palette")
0042         for palette_name in allPalettes:
0043             self.cmb_palettes.addItem(palette_name)
0044             self.cmb_palettes.model().sort(0)
0045 
0046         if len(allPalettes.keys()) > 0:
0047             self.currentPalette = Palette(list(allPalettes.values())[0])
0048         else:
0049             self.currentPalette = None
0050 
0051         self.cmb_palettes.currentTextChanged.connect(self.slot_paletteChanged)
0052         layout.addWidget(self.cmb_palettes)  # add combobox to the layout
0053         self.paletteView = PaletteView()
0054         self.paletteView.setPalette(self.currentPalette)
0055         layout.addWidget(self.paletteView)
0056         self.paletteView.entrySelectedForeGround.connect(
0057             self.slot_swatchSelected)
0058 
0059         self.colorComboBox = QComboBox()
0060         self.colorList = list()
0061         buttonLayout.addWidget(self.colorComboBox)
0062         self.bnSetColor = QToolButton()
0063         self.bnSetColor.setText(i18n("Set"))
0064         self.bnSetColor.clicked.connect(self.slot_get_color_from_combobox)
0065         buttonLayout.addWidget(self.bnSetColor)
0066 
0067         self.addEntry = QAction(self)
0068         self.addEntry.setIconText(i18n("+"))
0069         self.addEntry.triggered.connect(self.slot_add_entry)
0070         self.addGroup = QAction(self)
0071         self.addGroup.triggered.connect(self.slot_add_group)
0072         self.addGroup.setText(i18nc("Group as Color Group in a Palette", "Add Group"))
0073         self.addGroup.setIconText(str("\U0001F4C2"))
0074         self.removeEntry = QAction(self)
0075         self.removeEntry.setText(i18n("Remove Entry"))
0076         self.removeEntry.setIconText("-")
0077         self.removeEntry.triggered.connect(self.slot_remove_entry)
0078         addEntryButton = QToolButton()
0079         addEntryButton.setDefaultAction(self.addEntry)
0080         buttonLayout.addWidget(addEntryButton)
0081         addGroupButton = QToolButton()
0082         addGroupButton.setDefaultAction(self.addGroup)
0083         buttonLayout.addWidget(addGroupButton)
0084         removeEntryButton = QToolButton()
0085         removeEntryButton.setDefaultAction(self.removeEntry)
0086         buttonLayout.addWidget(removeEntryButton)
0087 
0088         # QActions
0089         self.extra = QToolButton()
0090         self.editPaletteData = QAction(self)
0091         self.editPaletteData.setText(i18n("Edit Palette Settings"))
0092         self.editPaletteData.triggered.connect(self.slot_edit_palette_data)
0093         self.extra.setDefaultAction(self.editPaletteData)
0094         buttonLayout.addWidget(self.extra)
0095         self.actionMenu = QMenu()
0096         self.exportToGimp = QAction(self)
0097         self.exportToGimp.setText(i18n("Export as GIMP Palette File"))
0098         self.exportToGimp.triggered.connect(self.slot_export_to_gimp_palette)
0099         self.exportToInkscape = QAction(self)
0100         self.exportToInkscape.setText(
0101             i18n("Export as Inkscape SVG with Swatches"))
0102         self.exportToInkscape.triggered.connect(
0103             self.slot_export_to_inkscape_svg)
0104         self.sortColors = QAction(self)
0105         self.sortColors.setText(i18n("Sort Colors"))
0106         self.sortColors.triggered.connect(self.slot_sort_colors)
0107         self.actionMenu.addAction(self.editPaletteData)
0108         self.actionMenu.addAction(self.exportToGimp)
0109         self.actionMenu.addAction(self.exportToInkscape)
0110         # self.actionMenu.addAction(self.sortColors)
0111 
0112         self.extra.setMenu(self.actionMenu)
0113 
0114         layout.addLayout(buttonLayout)
0115         self.slot_fill_combobox()
0116         self.setWidget(widget)        # add widget to the docker
0117 
0118     def slot_paletteChanged(self, name):
0119         allPalettes = Application.resources("palette")
0120         if len(allPalettes) > 0 and name in allPalettes:
0121             self.currentPalette = Palette(
0122                 Application.resources("palette")[name])
0123             self.paletteView.setPalette(self.currentPalette)
0124             self.slot_fill_combobox()
0125 
0126     @pyqtSlot('KisSwatch')
0127     def slot_swatchSelected(self, entry):
0128         if (self.canvas()) is not None:
0129             if (self.canvas().view()) is not None:
0130                 name = entry.name()
0131                 if len(entry.id) > 0:
0132                     name = entry.id() + " - " + entry.name()
0133                 if len(name) > 0:
0134                     if name in self.colorList:
0135                         self.colorComboBox.setCurrentIndex(
0136                             self.colorList.index(name))
0137                 color = self.currentPalette.colorForEntry(entry)
0138                 self.canvas().view().setForeGroundColor(color)
0139 
0140     def slot_fill_combobox(self):
0141         '''A function for making a combobox with the available colors. We use
0142         QCompleter on the colorComboBox so that people can type in the
0143         name of a color to select it. This is useful for people with
0144         carefully made palettes where the colors are named properly,
0145         which makes it easier for them to find colors.
0146         '''
0147 
0148         if self.currentPalette is None:
0149             pass
0150         self.colorComboBox.clear()
0151         self.colorList = list()
0152 #        palette = self.currentPalette
0153 #        for info in palette.infoList():
0154 #            entry = info.swatch
0155 #            color = palette.colorForEntry(entry).colorForCanvas(self.canvas())
0156 #            colorSquare = QPixmap(12, 12)
0157 #            if entry.spotColor() is True:
0158 #                img = colorSquare.toImage()
0159 #                circlePainter = QPainter()
0160 #                img.fill(self.colorComboBox.palette().color(QPalette.Base))
0161 #                circlePainter.begin(img)
0162 #                brush = QBrush(Qt.SolidPattern)
0163 #                brush.setColor(color)
0164 #                circlePainter.setBrush(brush)
0165 #                circlePainter.pen().setWidth(0)
0166 #                circlePainter.drawEllipse(0, 0, 11, 11)
0167 #                circlePainter.end()
0168 #                colorSquare = QPixmap.fromImage(img)
0169 #            else:
0170 #                colorSquare.fill(color)
0171 #            name = entry.name()
0172 #            if len(entry.id()) > 0:
0173 #                name = entry.id() + " - " + entry.name()
0174 #            self.colorList.append(name)
0175 #            self.colorComboBox.addItem(QIcon(colorSquare), name)
0176         self.colorComboBox.setEditable(True)
0177         self.colorComboBox.setInsertPolicy(QComboBox.NoInsert)
0178         self.colorComboBox.completer().setCompletionMode(
0179             QCompleter.PopupCompletion)
0180         self.colorComboBox.completer().setCaseSensitivity(False)
0181         self.colorComboBox.completer().setFilterMode(Qt.MatchContains)
0182 
0183     def slot_get_color_from_combobox(self):
0184         if self.currentPalette is not None:
0185             entry = self.currentPalette.colorSetEntryByIndex(
0186                 self.colorComboBox.currentIndex())
0187             self.slot_swatchSelected(entry)
0188 
0189     def slot_add_entry(self):
0190         if (self.canvas()) is not None:
0191             if (self.canvas().view()) is not None:
0192                 color = self.canvas().view().foregroundColor()
0193                 success = self.paletteView.addEntryWithDialog(color)
0194                 if success is True:
0195                     self.slot_fill_combobox()
0196 
0197     def slot_add_group(self):
0198         success = self.paletteView.addGroupWithDialog()
0199         if success is True:
0200             self.slot_fill_combobox()
0201 
0202     def slot_remove_entry(self):
0203         success = self.paletteView.removeSelectedEntryWithDialog()
0204         if success is True:
0205             self.slot_fill_combobox()
0206 
0207     def slot_edit_palette_data(self):
0208         '''A function for giving a gui to edit palette metadata... I also
0209         want this to be the way to edit the settings of the palette
0210         docker.
0211         '''
0212 
0213         dialog = QDialog(self)
0214         tabWidget = QTabWidget()
0215         dialog.setWindowTitle(i18n("Edit Palette Data"))
0216         dialog.setLayout(QVBoxLayout())
0217         dialog.layout().addWidget(tabWidget)
0218         paletteWidget = QWidget()
0219         paletteWidget.setLayout(QVBoxLayout())
0220         tabWidget.addTab(paletteWidget, i18n("Palette Data"))
0221         paletteName = QLineEdit()
0222         paletteName.setText(self.cmb_palettes.currentText())
0223         paletteWidget.layout().addWidget(paletteName)
0224         paletteColumns = QSpinBox()
0225         paletteColumns.setValue(self.currentPalette.columnCount())
0226         paletteWidget.layout().addWidget(paletteColumns)
0227         paletteComment = QPlainTextEdit()
0228         paletteComment.appendPlainText(self.currentPalette.comment())
0229         paletteWidget.layout().addWidget(paletteComment)
0230         buttons = QDialogButtonBox(QDialogButtonBox.Ok)
0231         dialog.layout().addWidget(buttons)
0232         buttons.accepted.connect(dialog.accept)
0233         # buttons.rejected.connect(dialog.reject())
0234 
0235         if dialog.exec_() == QDialog.Accepted:
0236             Resource = Application.resources("palette")[
0237                 self.cmb_palettes.currentText()]
0238             Resource.setName(paletteName.text())
0239             self.currentPalette = Palette(Resource)
0240             self.currentPalette.setColumnCount(paletteColumns.value())
0241             self.paletteView.setPalette(self.currentPalette)
0242             self.slot_fill_combobox()
0243             self.currentPalette.setComment(paletteComment.toPlainText())
0244             self.currentPalette.save()
0245 
0246     def slot_export_to_gimp_palette(self):
0247         palette_exporter_gimppalette.gimpPaletteExporter(
0248             self.cmb_palettes.currentText())
0249 
0250     def slot_export_to_inkscape_svg(self):
0251         palette_exporter_inkscapeSVG.inkscapeSVGExporter(
0252             self.cmb_palettes.currentText())
0253 
0254     def slot_sort_colors(self):
0255         colorSorter = palette_sortColors.sortColors(
0256             self.cmb_palettes.currentText())
0257         self.paletteView.setPalette(colorSorter.palette())
0258 
0259     def canvasChanged(self, canvas):
0260         self.cmb_palettes.clear()
0261         allPalettes = Application.resources("palette")
0262         for palette_name in allPalettes:
0263             self.cmb_palettes.addItem(palette_name)
0264             self.cmb_palettes.model().sort(0)
0265 
0266         if self.currentPalette is None and len(allPalettes.keys()) > 0:
0267             self.currentPalette = Palette(list(allPalettes.values())[0])