Krita/krita/pykrita/hotkeybrushes/hotkeybrushes.py

159 lines
6.3 KiB
Python
Raw Normal View History

2025-03-07 08:03:18 +01:00
import krita
from PyQt5.QtGui import QPixmap, QIcon
from . import uihotkeybrushes
class HotkeyBrushesExtension(krita.Extension):
def __init__(self, parent):
super(HotkeyBrushesExtension, self).__init__(parent)
self.actions = []
self.buttons = []
self.selectedPresets = []
# Indicates whether we want to activate the previous-selected brush
# on the second press of the shortcut
self.activatePrev = True
self.oldPreset = None
def setup(self):
self.readSettings()
def createActions(self, window):
action = window.createAction("hotkey_brushes", i18n("Hotkey Brushes"))
action.setToolTip(i18n("Assign brush presets to shortcuts."))
action.triggered.connect(self.initialize)
self.loadActions(window)
def initialize(self):
self.uihotkeybrushes = uihotkeybrushes.UIHotkeyBrushes()
self.uihotkeybrushes.initialize(self)
def readSettings(self):
self.selectedPresets = Application.readSetting(
"", "hotkeybrushes", "").split(',')
setting = Application.readSetting(
"", "hotkeybrushesActivatePrev2ndPress", "True")
# we should not get anything other than 'True' and 'False'
self.activatePrev = setting == 'True'
def writeSettings(self):
presets = []
for index, button in enumerate(self.buttons):
self.actions[index].preset = button.preset
presets.append(button.preset)
Application.writeSetting("", "hotkeybrushes", ','.join(map(str, presets)))
Application.writeSetting("", "hotkeybrushesActivatePrev2ndPress",
str(self.activatePrev))
def loadActions(self, window):
allPresets = Application.resources("preset")
# action = window.createAction(
# f"hotkey_reset_brush",
# f"Set Brush 100% Opacity and 100% Flow", "")
# action.setData({"payload": 1, "message": "Set 100% opacity + 100% flow"})
# action.triggered.connect(self.resetBrush)
# self.actions.append(action)
# for i, item in enumerate([5,10,15,20,30,40,50,60,70,80,90,100]):
# for z in "opacity flow".split(" "):
# action = window.createAction(
# f"hotkey_{z}_{item}",
# f"Set Brush {z.capitalize()} {item}%", "")
# action.setData({"type": z, "payload": item/100, "message": f"Set {item}% {z}"})
# action.triggered.connect(self.setBrushSetting)
# self.actions.append(action)
for index, item in enumerate(range( 1, 64 )):
action = window.createAction(
"hotkey_preset_" + str(item),
str(i18n("Activate Brush Preset {num}")).format(num=item), "")
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 resetBrush(self):
window = Application.activeWindow()
currentView = window.views()[0]
try:
currentPreset = window.views()[0].currentBrushPreset()
Application.activeWindow().activeView().showFloatingMessage(
"Preset Reset",
QIcon(QPixmap.fromImage(currentPreset.image())),
1000,
2
)
# Application.activeWindow().views()[0].showFloatingMessage( #TODO: ensure activeView message is displayed
# f"Set 100% opacity + 100% flow",
# QIcon(QPixmap.fromImage(currentPreset.image())),
# 1000,
# 2
# )
except Exception as e:
print(e)
currentView.setPaintingOpacity(1) #100%
currentView.setPaintingFlow(1) #100%
currentView.setCurrentBlendingMode("") # any string passed just defaults to "Normal" apparently
def setBrushSetting(self):
window = Application.activeWindow()
currentView = window.views()[0]
data = self.sender().data()
try:
currentPreset = window.views()[0].currentBrushPreset()
Application.activeWindow().activeView().showFloatingMessage(
data['message'],
QIcon(QPixmap.fromImage(currentPreset.image())),
1000,
2
)
# Application.activeWindow().views()[0].showFloatingMessage(
# data['message'],
# QIcon(QPixmap.fromImage(currentPreset.image())),
# 1000,
# 2
# )
except Exception as e:
print(e)
if data['type'] == "opacity":
currentView.setPaintingOpacity(data["payload"])
if data['type'] == "flow":
currentView.setPaintingFlow(data["payload"])
def activatePreset(self):
allPresets = Application.resources("preset")
window = Application.activeWindow()
if (window and len(window.views()) > 0
and self.sender().preset in allPresets):
currentPreset = window.views()[0].currentBrushPreset()
if (self.activatePrev
and self.sender().preset == currentPreset.name()):\
window.views()[0].activateResource(self.oldPreset)
else:
self.oldPreset = window.views()[0].currentBrushPreset()
window.views()[0].activateResource(
allPresets[self.sender().preset])
try:
currentPreset = window.views()[0].currentBrushPreset()
Application.activeWindow().activeView().showFloatingMessage(
f"{currentPreset.name()} Selected",
QIcon(QPixmap.fromImage(currentPreset.image())),
1000,
2
)
# Application.activeWindow().views()[0].showFloatingMessage(
# str(i18n("{}\nselected")).format(currentPreset.name()),
# QIcon(QPixmap.fromImage(currentPreset.image())),
# 1000,
# 1
# )
except Exception as e:
print(e)