Krita Source Code Documentation
Loading...
Searching...
No Matches
workflow_buttons.py
Go to the documentation of this file.
1# SPDX-FileCopyrightText: 2024 Timothée Giet <animtim@gmail.com>
2# Authors:
3# Timothée Giet <animtim@gmail.com>
4# SPDX-License-Identifier: GPL-3.0-or-later
5
6try:
7 from PyQt6.QtCore import QSize
8 from PyQt6.QtGui import QIcon, QPixmap, QAction
9 from PyQt6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QScrollArea, QToolButton, QPushButton, QToolBar, QSizePolicy
10except:
11 from PyQt5.QtCore import QSize
12 from PyQt5.QtGui import QIcon, QPixmap
13 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QScrollArea, QToolButton, QPushButton, QToolBar, QAction, QSizePolicy
14from krita import Krita, DockWidget, DockWidgetFactory, DockWidgetFactoryBase, ManagedColor
15from builtins import i18n, i18nc, Application
16from .flow_layout import FlowLayout
17from .buttons_settings_dialog import LISTOFTOOLS, LISTOFSIZES, ButtonsSettingsDialog
18import ast
19
20DOCKER_ID = 'pykrita_workflow_buttons'
21INSTANCE = Krita.instance()
22
23class WorkflowButtons(DockWidget):
24 def __init__(self):
25 super().__init__()
26 self.setWindowTitle(i18nc("@title:window", "Workflow Buttons"))
27 self.sizeIndex = 2
28 self.globalButtonSize = QSize(LISTOFSIZES[self.sizeIndex], LISTOFSIZES[self.sizeIndex])
30
31 # all widget and layout setup of the docker
32 mainWidget = QWidget(self)
33 self.setWidget(mainWidget)
34 mainLayout = QVBoxLayout(mainWidget)
35 mainLayout.setSpacing(2)
36 mainLayout.setContentsMargins(0, 0, 0, 0)
37
38 buttonsScrollArea = QScrollArea(mainWidget)
39 buttonsScrollArea.setMinimumSize(self.globalButtonSize)
40 self.buttonsWidget = QWidget(buttonsScrollArea)
41 buttonsScrollArea.setWidgetResizable(True)
42 buttonsScrollArea.setWidget(self.buttonsWidget)
43
44 mainLayout.addWidget(buttonsScrollArea)
45
47 self.buttonsLayout.setSpacing(2)
48 self.buttonsLayout.setContentsMargins(0, 0, 0, 0)
49
50 self.bottomLayout = QHBoxLayout()
51 self.bottomLayout.setSpacing(2)
52 self.bottomLayout.setContentsMargins(0, 0, 0, 0)
53 mainLayout.addLayout(self.bottomLayout)
54
55 barSpacer = QWidget(mainWidget)
56 barSpacer.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Ignored)
57 self.bottomLayout.addWidget(barSpacer)
58
59 self.bottomBarButton = QToolButton(mainWidget)
60 self.bottomBarButton.setAutoRaise(True)
61 self.bottomBarButton.setIconSize(QSize(22,22))
62
63 self.settingsAction = QAction(self)
64 self.settingsAction.setIcon(INSTANCE.icon('view-choose-22'))
65 self.settingsAction.setToolTip(i18n("Open workflow buttons settings dialog"))
67
68 self.bottomBarButton.setDefaultAction(self.settingsAction)
69 self.bottomLayout.addWidget(self.bottomBarButton)
70
71 # Inline edit button setup
72 self.editButton = QToolButton(self.buttonsWidget)
73 self.editButton.setIconSize(self.globalButtonSize)
74 self.editButton.setDefaultAction(self.settingsAction)
75 self.buttonsLayout.addWidget(self.editButton)
76
78 self.readSettings()
80 self.populateButtons()
81
82 def canvasChanged(self, canvas):
83 pass
84
85 def readSettings(self):
86 savedSize = Application.readSetting("workflowbuttons", "buttonsSize", "")
87 if savedSize:
88 self.sizeIndex = int(savedSize)
89 self.refreshButtonsSize()
90 savedList = Application.readSetting("workflowbuttons", "buttons", "")
91 if savedList:
92 self.buttonsContentList = ast.literal_eval(savedList)
93 savedSettingsButtonPosition = Application.readSetting("workflowbuttons", "settingsButtonPosition", "")
94 if savedSettingsButtonPosition:
95 self.settingsButtonPosition = int(savedSettingsButtonPosition)
96
97 def writeSettings(self):
98 Application.writeSetting("workflowbuttons", "buttonsSize", str(self.sizeIndex))
99 Application.writeSetting("workflowbuttons", "buttons", str(self.buttonsContentList))
100 Application.writeSetting("workflowbuttons", "settingsButtonPosition", str(self.settingsButtonPosition))
101
103 if self.settingsButtonPosition == 0:
104 self.bottomBarButton.setVisible(True)
105 self.editButton.setVisible(False)
106 elif self.settingsButtonPosition == 1:
107 self.bottomBarButton.setVisible(False)
108 self.editButton.setVisible(True)
109
110 def clearButtons(self):
111 for widget in self.buttonsWidget.children():
112 if isinstance(widget, CustomButton):
113 widget.deleteLater()
114
116 buttonID = -1
117 allBrushPresets = INSTANCE.resources('preset')
118 for buttonToCreate in self.buttonsContentList:
119 buttonID += 1
120 if buttonToCreate["iconMode"] == 0:
121 buttonIcon = QIcon(buttonToCreate["icon"])
122 elif buttonToCreate["iconMode"] == 1 and buttonToCreate["toolIndex"] != 0:
123 buttonIcon = INSTANCE.icon(LISTOFTOOLS[buttonToCreate["toolIndex"]]["toolIcon"])
124 elif buttonToCreate["iconMode"] == 2 and buttonToCreate["presetName"] != "":
125 brushPreset = allBrushPresets[buttonToCreate["presetName"]]
126 buttonIcon = QIcon(QPixmap.fromImage(brushPreset.image()))
127 else:
128 buttonIcon = QIcon()
129 button = CustomButton(self.buttonsWidget, buttonID, buttonToCreate)
130 button.setIconSize(self.globalButtonSize)
131 button.setIcon(buttonIcon)
132 self.buttonsLayout.addWidget(button)
133
134 def refreshButtons(self):
135 self.clearButtons()
136 self.populateButtons()
137
140 newDialog.exec()
141 if newDialog.result() == 1:
142 self.buttonsContentList = newDialog.buttonsContentList
143 self.sizeIndex = newDialog.sizeIndex
144 self.settingsButtonPosition = newDialog.settingsButtonPosition
146 self.refreshButtonsSize()
147 self.writeSettings()
148 self.refreshButtons()
149
151 self.globalButtonSize = QSize(LISTOFSIZES[self.sizeIndex], LISTOFSIZES[self.sizeIndex])
152 self.editButton.setIconSize(self.globalButtonSize)
153
154class CustomButton(QToolButton):
155 # Class to define the custom buttons inside the docker
156 def __init__(self, parent=None, buttonID=-1, values={}):
157 super().__init__(parent)
158 self.buttonID = buttonID
159 self.values = values
160 self.clicked.connect(self.triggeredFunctiontriggeredFunction)
161 if self.values["tooltip"]:
162 self.setToolTip(values["tooltip"])
163
165 currentView = INSTANCE.activeWindow().activeView()
166 # select given tool
167 if self.values["toolIndex"] != "":
168 toolIndex = int(self.values["toolIndex"])
169 if(toolIndex > 0 and toolIndex < len(LISTOFTOOLS)):
170 INSTANCE.action(LISTOFTOOLS[int(self.values["toolIndex"])]["toolName"]).trigger()
171
172 # get list of all presets
173 if self.values["presetName"] != "":
174 allBrushPresets = INSTANCE.resources('preset')
175 # find given preset by name
176 brushPreset = allBrushPresets[self.values["presetName"]]
177 # select the brush preset
178 currentView.setCurrentBrushPreset(brushPreset)
179
180 # set given FGColor
181 if self.values["FGColorValues"]["model"] != "":
182 colorToSelect = ManagedColor(self.values["FGColorValues"]["model"],
183 self.values["FGColorValues"]["depth"],
184 self.values["FGColorValues"]["profile"])
185 colorComponents = self.values["FGColorValues"]["components"]
186 colorToSelect.setComponents(colorComponents)
187 currentView.setForeGroundColor(colorToSelect)
188
189 # set given BGColor
190 if self.values["BGColorValues"]["model"] != "":
191 colorToSelect = ManagedColor(self.values["BGColorValues"]["model"],
192 self.values["BGColorValues"]["depth"],
193 self.values["BGColorValues"]["profile"])
194 colorComponents = self.values["BGColorValues"]["components"]
195 colorToSelect.setComponents(colorComponents)
196 currentView.setBackGroundColor(colorToSelect)
197
198 # run extra script
199 if self.values["script"] != "":
200 scriptPath = self.values["script"]
201 exec(open(scriptPath).read())
202
203
204dock_widget_factory = DockWidgetFactory(DOCKER_ID,
205 DockWidgetFactoryBase.DockPosition.DockRight,
206 WorkflowButtons)
207
208INSTANCE.addDockWidgetFactory(dock_widget_factory)
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:396
__init__(self, parent=None, buttonID=-1, values={})