diff --git a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py index 0a3ff28681..7e44dd9b1f 100644 --- a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py @@ -1,20 +1,19 @@ from PyQt5.QtWidgets import QPushButton from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtCore import QSize class DropButton(QPushButton): def __init__(self, parent): super(DropButton, self).__init__(parent) self.presetChooser = None self.preset = None self.setFixedSize(64, 64) self.setIconSize(QSize(64, 64)) - def selectPreset(self): self.preset = self.presetChooser.currentPreset().name() self.setIcon(QIcon(QPixmap.fromImage(self.presetChooser.currentPreset().image()))) diff --git a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushes.py b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushes.py index f17ff5a47c..9bc1e6b19c 100644 --- a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushes.py +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushes.py @@ -1,23 +1,58 @@ -import sys -from PyQt5.QtGui import * -from PyQt5.QtWidgets import * -from krita import * -from tenbrushes import dropbutton, uitenbrushes +import krita +from tenbrushes import uitenbrushes -class TenBrushesExtension(Extension): +class TenBrushesExtension(krita.Extension): def __init__(self, parent): super(TenBrushesExtension, self).__init__(parent) + self.actions = [] + self.buttons = [] + self.selectedPresets = [] + def setup(self): action = Application.createAction("ten_brushes", "Ten Brushes") action.setToolTip("Assign ten brush presets to ten shortcuts.") action.triggered.connect(self.initialize) + self.readSettings() + self.loadActions() + def initialize(self): self.uitenbrushes = uitenbrushes.UITenBrushes() - self.uitenbrushes.initialize() + self.uitenbrushes.initialize(self) + + def readSettings(self): + self.selectedPresets = Application.readSetting("", "tenbrushes", "").split(',') + + def writeSettings(self): + presets = [] + + for index, button in enumerate(self.buttons): + self.actions[index].preset = button.preset + presets.append(button.preset) + Application.writeSetting("", "tenbrushes", ','.join(map(str, presets))) + + def loadActions(self): + allPresets = Application.resources("preset") + + for index, item in enumerate(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']): + action = Application.createAction("activate_preset_" + item, "Activate Brush Preset " + item) + action.setMenu("None") + action.triggered.connect(self.activatePreset) + + if index < len(self.selectedPresets) and self.selectedPresets[index] in allPresets: + action.preset = self.selectedPresets[index] + else: + action.preset = None + + self.actions.append(action) + + def activatePreset(self): + allPresets = Application.resources("preset") + if Application.activeWindow() and len(Application.activeWindow().views()) > 0 and self.sender().preset in allPresets: + Application.activeWindow().views()[0].activateResource(allPresets[self.sender().preset]) Scripter.addExtension(TenBrushesExtension(Application)) diff --git a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py index ce746142f4..9fdddbcbe4 100644 --- a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py @@ -1,17 +1,17 @@ from PyQt5.QtWidgets import QDialog class TenBrushesDialog(QDialog): def __init__(self, uitenbrushes, parent=None): super(TenBrushesDialog, self).__init__(parent) self.uitenbrushes = uitenbrushes def accept(self): - self.uitenbrushes.writeSettings() + self.uitenbrushes.tentbrushes.writeSettings() super(TenBrushesDialog, self).accept() def closeEvent(self, event): event.accept() diff --git a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/uitenbrushes.py b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/uitenbrushes.py index 1418e0e157..b85198d103 100644 --- a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/uitenbrushes.py +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/uitenbrushes.py @@ -1,93 +1,63 @@ from PyQt5.QtCore import Qt, QSize from PyQt5.QtGui import QPixmap, QIcon from PyQt5.QtWidgets import (QDialogButtonBox, QLabel, QVBoxLayout, QHBoxLayout) from tenbrushes import tenbrushesdialog, dropbutton import krita class UITenBrushes(object): def __init__(self): self.kritaInstance = krita.Krita.instance() self.mainDialog = tenbrushesdialog.TenBrushesDialog(self, self.kritaInstance.activeWindow().qwindow()) self.buttonBox = QDialogButtonBox(self.mainDialog) self.vbox = QVBoxLayout(self.mainDialog) self.hbox = QHBoxLayout(self.mainDialog) - self.actions = [] - self.buttons = [] self.buttonBox.accepted.connect(self.mainDialog.accept) self.buttonBox.rejected.connect(self.mainDialog.reject) self.buttonBox.setOrientation(Qt.Horizontal) self.buttonBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.presetChooser = krita.PresetChooser(self.mainDialog) - def initialize(self): - self.readSettings() + def initialize(self, tentbrushes): + self.tentbrushes = tentbrushes + + self.loadButtons() + + self.vbox.addLayout(self.hbox) + self.vbox.addWidget(self.presetChooser) + self.vbox.addWidget(self.buttonBox) + self.vbox.addWidget(QLabel("Select the brush preset, then click on the button you want to use to select the preset")) + + self.mainDialog.show() + self.mainDialog.activateWindow() + self.mainDialog.exec_() + + def loadButtons(self): + self.tentbrushes.buttons = [] allPresets = Application.resources("preset") for index, item in enumerate(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']): buttonLayout = QVBoxLayout() button = dropbutton.DropButton(self.mainDialog) button.setObjectName(item) button.clicked.connect(button.selectPreset) button.presetChooser = self.presetChooser - if self.actions[index] and self.actions[index].preset and self.actions[index].preset in allPresets: - p = allPresets[self.actions[index].preset]; + if self.tentbrushes.actions[index] and self.tentbrushes.actions[index].preset and self.tentbrushes.actions[index].preset in allPresets: + p = allPresets[self.tentbrushes.actions[index].preset]; button.preset = p.name() button.setIcon(QIcon(QPixmap.fromImage(p.image()))) buttonLayout.addWidget(button) label = QLabel("Ctrl+Alt+" + item) label.setAlignment(Qt.AlignHCenter) buttonLayout.addWidget(label) self.hbox.addLayout(buttonLayout) - self.buttons.append(button) - - self.vbox.addLayout(self.hbox) - self.vbox.addWidget(self.presetChooser) - self.vbox.addWidget(self.buttonBox) - self.vbox.addWidget(QLabel("Select the brush preset, then click on the button you want to use to select the preset")) - - self.mainDialog.show() - self.mainDialog.activateWindow() - self.mainDialog.exec_() - - def activatePreset(self): - allPresets = self.kritaInstance.resources("preset") - print("activatePreset", self.sender().preset) - if Application.activeWindow() and len(Application.activeWindow().views()) > 0 and self.sender().preset in allPresets: - Application.activeWindow().views()[0].activateResource(allPresets[self.sender().preset]) - - def readSettings(self): - # Read the ten selected brush presets from the settings - # That part can be a loadPresets method 43 - 58, but it really needs a refactoring - selectedPresets = self.kritaInstance.readSetting("", "tenbrushes", "").split(',') - allPresets = self.kritaInstance.resources("preset") - - # Setup up to ten actions and give them default shortcuts - for index, item in enumerate(['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']): - action = self.kritaInstance.createAction("activate_preset_" + item, "Activate Preset " + item) - #action.setVisible(False) - action.setMenu("None") - action.triggered.connect(self.activatePreset) - if index < len(selectedPresets) and selectedPresets[index] in allPresets: - action.preset = selectedPresets[index] - else: - action.preset = None - self.actions.append(action) - - def writeSettings(self): - i = 0 - presets = [] - for button in self.buttons: - self.actions[i].preset = button.preset - presets.append(button.preset) - i = i + 1 - self.kritaInstance.writeSetting("", "tenbrushes", ','.join(map(str, presets))) + self.tentbrushes.buttons.append(button)