Krita Source Code Documentation
Loading...
Searching...
No Matches
tenbrushes.py
Go to the documentation of this file.
1# SPDX-License-Identifier: CC0-1.0
2
3from krita import Krita, Extension
4from builtins import i18n, Application
5try:
6 from PyQt6.QtGui import QPixmap, QIcon
7except:
8 from PyQt5.QtGui import QPixmap, QIcon
9from . import uitenbrushes
10
11
12class TenBrushesExtension(Extension):
13
14 def __init__(self, parent):
15 super(TenBrushesExtension, self).__init__(parent)
16
17 self.buttons = []
19 self.actionToIndex = {}
20 # Indicates whether we want to activate the previous-selected brush
21 # on the second press of the shortcut
22 self.activatePrev = True
23 # Indicates whether we want to select the freehand brush tool
24 # on the press of a preset shortcut
25 self.autoBrush = False
26 # Indicates whether to show an on-canvas message when changing preset
27 self.showMessage = True
28 self.oldPreset = None
29
30 def setup(self):
31 self.readSettings()
32
33 def createActions(self, window):
34 action = window.createAction("ten_brushes", i18n("Ten Brushes"))
35 action.setToolTip(i18n("Assign ten brush presets to ten shortcuts."))
36 action.triggered.connect(self.initializeinitialize)
37 self.loadActions(window)
38
43 def readSettings(self):
44 allPresets = Application.resources("preset")
45
46 self.selectedPresets = Application.readSetting("", "tenbrushes", "").split(',')
47
48 # in Krita 4.x we used to replace spaces in preset names with
49 # underscores, which has changed in Krita 5.x. Here we just
50 # try hard to load the legacy preset
51
52 for index, preset in enumerate(self.selectedPresets):
53 for name in [preset, preset.replace('_', ' ')]:
54 if name in allPresets:
55 if name != preset:
56 self.selectedPresets[index] = name
57 break
58
59 setting = Application.readSetting("", "tenbrushesActivatePrev2ndPress", "True")
60 # we should not get anything other than 'True' and 'False'
61 self.activatePrev = setting == 'True'
62
63 setting = Application.readSetting(
64 "", "tenbrushesAutoBrushOnPress", "False")
65 self.autoBrush = setting == 'True'
66
67 setting = Application.readSetting(
68 "", "tenbrushesShowMessage", "True")
69 self.showMessage = setting == 'True'
70
71 def writeSettings(self):
72 presets = []
73
74 for index, button in enumerate(self.buttons):
75 presets.append(button.preset)
76 self.selectedPresets.insert(index, button.preset)
77
78 Application.writeSetting("", "tenbrushes", ','.join(map(str, presets)))
79 Application.writeSetting("", "tenbrushesActivatePrev2ndPress",
80 str(self.activatePrev))
81 Application.writeSetting("", "tenbrushesAutoBrushOnPress",
82 str(self.autoBrush))
83 Application.writeSetting("", "tenbrushesShowMessage",
84 str(self.showMessage))
85
86 def loadActions(self, window):
87 allPresets = Application.resources("preset")
88
89 for index, item in enumerate(['1', '2', '3', '4', '5',
90 '6', '7', '8', '9', '0']):
91 action = window.createAction(
92 "activate_preset_" + item,
93 str(i18n("Activate Brush Preset {num}")).format(num=item), "")
94 action.triggered.connect(self.activatePresetactivatePreset)
95 self.actionToIndex[action.objectName()] = index;
96
97 def activatePreset(self):
98 allPresets = Application.resources("preset")
99 window = Application.activeWindow()
100
101 presetIndex = self.actionToIndex[self.sender().objectName()]
102 preset = self.selectedPresets[presetIndex] if len(self.selectedPresets) > presetIndex else None
103
104 if (window and len(window.views()) > 0
105 and preset
106 and preset in allPresets):
107 currentPreset = window.views()[0].currentBrushPreset()
108
109 if self.autoBrush:
110 Krita.instance().action('KritaShape/KisToolBrush').trigger()
111
112 if (self.activatePrev
113 and preset == currentPreset.name()):
114 window.views()[0].activateResource(self.oldPreset)
115 else:
116 self.oldPreset = window.views()[0].currentBrushPreset()
117 window.views()[0].activateResource(allPresets[preset])
118
119 if self.showMessage:
120 preset = window.views()[0].currentBrushPreset()
121 window.activeView().showFloatingMessage(str(i18n("{}\nselected")).format(preset.name()),
122 QIcon(QPixmap.fromImage(preset.image())),
123 1000, 1)
124
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390