diff --git a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py index 0f9f40bd69..0a3ff28681 100644 --- a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/dropbutton.py @@ -1,17 +1,20 @@ from PyQt5.QtWidgets import QPushButton -from PyQt5.QtGui import QPixmap +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 0abd8bdbc6..f17ff5a47c 100644 --- a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushes.py +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushes.py @@ -1,115 +1,23 @@ import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from krita import * -from tenbrushes import dropbutton +from tenbrushes import dropbutton, uitenbrushes class TenBrushesExtension(Extension): def __init__(self, parent): - super().__init__(parent) - self.buttons = [] - self.actions = [] - - # def setup(self): - # action = Application.createAction("ten_brushes", "Ten Brushes") - # action.setToolTip("Assign ten brush presets to ten shortcuts.") - # action.triggered.connect(self.initialize) - # - # def initialize(self): - # self.uitenbrushes = uitenbrushes.UITenBrushes() - # self.uitenbrushes.initialize() + super(TenBrushesExtension, self).__init__(parent) def setup(self): - print('setup tenbrushes') action = Application.createAction("ten_brushes", "Ten Brushes") action.setToolTip("Assign ten brush presets to ten shortcuts.") - action.triggered.connect(self.showDialog) - - # 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 = Application.readSetting("", "tenbrushes", "").split(',') - allPresets = Application.resources("preset") - # Setup up to ten actions and give them default shortcuts - j = 0 - self.actions = [] - for i in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']: - action = Application.createAction("activate_preset_" + i, "Activate Preset " + i) - #action.setVisible(False) - action.setMenu("None") - action.triggered.connect(self.activatePreset) - if j < len(selectedPresets) and selectedPresets[j] in allPresets: - action.preset = selectedPresets[j] - else: - action.preset = None - self.actions.append(action) - j = j + 1 - - def activatePreset(self): - allPresets = Application.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 showDialog(self): - #it can be in an initialize method 68 - 111, but isolating loadButtons part - self.dialog = QDialog(Application.activeWindow().qwindow()) - - self.buttonBox = QDialogButtonBox(self.dialog) - self.buttonBox.setOrientation(QtCore.Qt.Horizontal) - self.buttonBox.setStandardButtons(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) - self.buttonBox.accepted.connect(self.accept) - self.buttonBox.rejected.connect(self.dialog.reject) - - vbox = QVBoxLayout(self.dialog) - hbox = QHBoxLayout(self.dialog) - - self.presetChooser = PresetChooser(self.dialog) - - # loadButtons method 82 - 103 - allPresets = Application.resources("preset") - j = 0 - self.buttons = [] - for i in ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']: - buttonBox = QVBoxLayout() - button = dropbutton.DropButton(self.dialog) - button.setObjectName(i) - button.clicked.connect(button.selectPreset) - button.presetChooser = self.presetChooser - - if self.actions[j] and self.actions[j].preset and self.actions[j].preset in allPresets: - p = allPresets[self.actions[j].preset]; - button.preset = p.name() - button.setIcon(QIcon(QPixmap.fromImage(p.image()))) - - buttonBox.addWidget(button) - label = QLabel("Ctrl+Alt+" + i) - label.setAlignment(Qt.AlignHCenter) - buttonBox.addWidget(label) - hbox.addLayout(buttonBox) - self.buttons.append(button) - j = j + 1 - - vbox.addLayout(hbox) - vbox.addWidget(self.presetChooser) - vbox.addWidget(self.buttonBox) - vbox.addWidget(QLabel("Select the brush preset, then click on the button you want to use to select the preset")) - - self.dialog.show() - self.dialog.activateWindow() - self.dialog.exec_() + action.triggered.connect(self.initialize) + def initialize(self): + self.uitenbrushes = uitenbrushes.UITenBrushes() + self.uitenbrushes.initialize() - def accept(self): - # we can be a writeSettings and readSettings - i = 0 - presets = [] - for button in self.buttons: - self.actions[i].preset = button.preset - presets.append(button.preset) - i = i + 1 - Application.writeSetting("", "tenbrushes", ','.join(map(str, presets))) - self.dialog.accept() Scripter.addExtension(TenBrushesExtension(Application)) diff --git a/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py new file mode 100644 index 0000000000..ce746142f4 --- /dev/null +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/tenbrushesdialog.py @@ -0,0 +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() + + 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 new file mode 100644 index 0000000000..1418e0e157 --- /dev/null +++ b/plugins/extensions/pykrita/plugin/plugins/tenbrushes/uitenbrushes.py @@ -0,0 +1,93 @@ +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() + + 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]; + 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)))