File indexing completed on 2024-12-22 04:16:28

0001 # SPDX-License-Identifier: CC0-1.0
0002 
0003 import krita
0004 from PyQt5.QtGui import QPixmap, QIcon
0005 from . import uitenbrushes
0006 
0007 
0008 class TenBrushesExtension(krita.Extension):
0009 
0010     def __init__(self, parent):
0011         super(TenBrushesExtension, self).__init__(parent)
0012 
0013         self.buttons = []
0014         self.selectedPresets = []
0015         self.actionToIndex = {}
0016         # Indicates whether we want to activate the previous-selected brush
0017         # on the second press of the shortcut
0018         self.activatePrev = True
0019         # Indicates whether we want to select the freehand brush tool
0020         # on the press of a preset shortcut
0021         self.autoBrush = False
0022         self.oldPreset = None
0023 
0024     def setup(self):
0025         self.readSettings()
0026 
0027     def createActions(self, window):
0028         action = window.createAction("ten_brushes", i18n("Ten Brushes"))
0029         action.setToolTip(i18n("Assign ten brush presets to ten shortcuts."))
0030         action.triggered.connect(self.initialize)
0031         self.loadActions(window)
0032 
0033     def initialize(self):
0034         self.uitenbrushes = uitenbrushes.UITenBrushes()
0035         self.uitenbrushes.initialize(self)
0036 
0037     def readSettings(self):
0038         allPresets = Application.resources("preset")
0039 
0040         self.selectedPresets = Application.readSetting("", "tenbrushes", "").split(',')
0041 
0042         # in Krita 4.x we used to replace spaces in preset names with
0043         # underscores, which has changed in Krita 5.x. Here we just
0044         # try hard to load the legacy preset
0045 
0046         for index, preset in enumerate(self.selectedPresets):
0047             for name in [preset, preset.replace('_', ' ')]:
0048                 if name in allPresets:
0049                     if name != preset:
0050                         self.selectedPresets[index] = name
0051                     break
0052 
0053         setting = Application.readSetting("", "tenbrushesActivatePrev2ndPress", "True")
0054         # we should not get anything other than 'True' and 'False'
0055         self.activatePrev = setting == 'True'
0056 
0057         setting = Application.readSetting(
0058             "", "tenbrushesAutoBrushOnPress", "False")
0059         self.autoBrush = setting == 'True'
0060 
0061     def writeSettings(self):
0062         presets = []
0063 
0064         for index, button in enumerate(self.buttons):
0065             presets.append(button.preset)
0066             self.selectedPresets.insert(index, button.preset)
0067 
0068         Application.writeSetting("", "tenbrushes", ','.join(map(str, presets)))
0069         Application.writeSetting("", "tenbrushesActivatePrev2ndPress",
0070                                  str(self.activatePrev))
0071         Application.writeSetting("", "tenbrushesAutoBrushOnPress",
0072                                  str(self.autoBrush))
0073 
0074     def loadActions(self, window):
0075         allPresets = Application.resources("preset")
0076 
0077         for index, item in enumerate(['1', '2', '3', '4', '5',
0078                                       '6', '7', '8', '9', '0']):
0079             action = window.createAction(
0080                 "activate_preset_" + item,
0081                 str(i18n("Activate Brush Preset {num}")).format(num=item), "")
0082             action.triggered.connect(self.activatePreset)
0083             self.actionToIndex[action.objectName()] = index;
0084 
0085     def activatePreset(self):
0086         allPresets = Application.resources("preset")
0087         window = Application.activeWindow()
0088 
0089         presetIndex = self.actionToIndex[self.sender().objectName()]
0090         preset = self.selectedPresets[presetIndex] if len(self.selectedPresets) > presetIndex else None
0091 
0092         if (window and len(window.views()) > 0
0093                 and preset
0094                 and preset in allPresets):
0095             currentPreset = window.views()[0].currentBrushPreset()
0096 
0097             if self.autoBrush:
0098                 Krita.instance().action('KritaShape/KisToolBrush').trigger()
0099 
0100             if (self.activatePrev
0101                     and preset == currentPreset.name()):
0102                 window.views()[0].activateResource(self.oldPreset)
0103             else:
0104                 self.oldPreset = window.views()[0].currentBrushPreset()
0105                 window.views()[0].activateResource(allPresets[preset])
0106 
0107         preset = window.views()[0].currentBrushPreset()
0108         window.activeView().showFloatingMessage(str(i18n("{}\nselected")).format(preset.name()),
0109                                               QIcon(QPixmap.fromImage(preset.image())),
0110                                               1000, 1)
0111