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 self.bottomBar = QToolBar(self)
56 self.bottomBar.setIconSize(QSize(22,22))
57 self.bottomBar.setStyleSheet("QToolBar{spacing:0px; margin:0px;}")
58 barSpacer = QWidget(self.bottomBar)
59 barSpacer.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
60 self.bottomBar.addWidget(barSpacer)
61 self.bottomLayout.addWidget(self.bottomBar)
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 self.bottomBar.addAction(self.settingsAction)
68
69 # Inline edit button setup
70 self.editButton = QToolButton(self.buttonsWidget)
71 self.editButton.setIconSize(self.globalButtonSize)
72 self.editButton.setDefaultAction(self.settingsAction)
73 self.buttonsLayout.addWidget(self.editButton)
74
76 self.readSettings()
78 self.populateButtons()
79
80 def canvasChanged(self, canvas):
81 pass
82
83 def readSettings(self):
84 savedSize = Application.readSetting("workflowbuttons", "buttonsSize", "")
85 if savedSize:
86 self.sizeIndex = int(savedSize)
87 self.refreshButtonsSize()
88 savedList = Application.readSetting("workflowbuttons", "buttons", "")
89 if savedList:
90 self.buttonsContentList = ast.literal_eval(savedList)
91 savedSettingsButtonPosition = Application.readSetting("workflowbuttons", "settingsButtonPosition", "")
92 if savedSettingsButtonPosition:
93 self.settingsButtonPosition = int(savedSettingsButtonPosition)
94
95 def writeSettings(self):
96 Application.writeSetting("workflowbuttons", "buttonsSize", str(self.sizeIndex))
97 Application.writeSetting("workflowbuttons", "buttons", str(self.buttonsContentList))
98 Application.writeSetting("workflowbuttons", "settingsButtonPosition", str(self.settingsButtonPosition))
99
101 if self.settingsButtonPosition == 0:
102 self.bottomBar.setVisible(True)
103 self.editButton.setVisible(False)
104 elif self.settingsButtonPosition == 1:
105 self.bottomBar.setVisible(False)
106 self.editButton.setVisible(True)
107
108 def clearButtons(self):
109 for widget in self.buttonsWidget.children():
110 if isinstance(widget, CustomButton):
111 widget.deleteLater()
112
114 buttonID = -1
115 allBrushPresets = INSTANCE.resources('preset')
116 for buttonToCreate in self.buttonsContentList:
117 buttonID += 1
118 if buttonToCreate["iconMode"] == 0:
119 buttonIcon = QIcon(buttonToCreate["icon"])
120 elif buttonToCreate["iconMode"] == 1 and buttonToCreate["toolIndex"] != 0:
121 buttonIcon = INSTANCE.icon(LISTOFTOOLS[buttonToCreate["toolIndex"]]["toolIcon"])
122 elif buttonToCreate["iconMode"] == 2 and buttonToCreate["presetName"] != "":
123 brushPreset = allBrushPresets[buttonToCreate["presetName"]]
124 buttonIcon = QIcon(QPixmap.fromImage(brushPreset.image()))
125 else:
126 buttonIcon = QIcon()
127 button = CustomButton(self.buttonsWidget, buttonID, buttonToCreate)
128 button.setIconSize(self.globalButtonSize)
129 button.setIcon(buttonIcon)
130 self.buttonsLayout.addWidget(button)
131
132 def refreshButtons(self):
133 self.clearButtons()
134 self.populateButtons()
135
138 newDialog.exec()
139 if newDialog.result() == 1:
140 self.buttonsContentList = newDialog.buttonsContentList
141 self.sizeIndex = newDialog.sizeIndex
142 self.settingsButtonPosition = newDialog.settingsButtonPosition
144 self.refreshButtonsSize()
145 self.writeSettings()
146 self.refreshButtons()
147
149 self.globalButtonSize = QSize(LISTOFSIZES[self.sizeIndex], LISTOFSIZES[self.sizeIndex])
150 self.editButton.setIconSize(self.globalButtonSize)
151
152class CustomButton(QToolButton):
153 # Class to define the custom buttons inside the docker
154 def __init__(self, parent=None, buttonID=-1, values={}):
155 super().__init__(parent)
156 self.buttonID = buttonID
157 self.values = values
159 if self.values["tooltip"]:
160 self.setToolTip(values["tooltip"])
161
163 currentView = INSTANCE.activeWindow().activeView()
164 # select given tool
165 if self.values["toolIndex"] != "":
166 toolIndex = int(self.values["toolIndex"])
167 if(toolIndex > 0 and toolIndex < len(LISTOFTOOLS)):
168 INSTANCE.action(LISTOFTOOLS[int(self.values["toolIndex"])]["toolName"]).trigger()
169
170 # get list of all presets
171 if self.values["presetName"] != "":
172 allBrushPresets = INSTANCE.resources('preset')
173 # find given preset by name
174 brushPreset = allBrushPresets[self.values["presetName"]]
175 # select the brush preset
176 currentView.setCurrentBrushPreset(brushPreset)
177
178 # set given FGColor
179 if self.values["FGColorValues"]["model"] != "":
180 colorToSelect = ManagedColor(self.values["FGColorValues"]["model"],
181 self.values["FGColorValues"]["depth"],
182 self.values["FGColorValues"]["profile"])
183 colorComponents = self.values["FGColorValues"]["components"]
184 colorToSelect.setComponents(colorComponents)
185 currentView.setForeGroundColor(colorToSelect)
186
187 # set given BGColor
188 if self.values["BGColorValues"]["model"] != "":
189 colorToSelect = ManagedColor(self.values["BGColorValues"]["model"],
190 self.values["BGColorValues"]["depth"],
191 self.values["BGColorValues"]["profile"])
192 colorComponents = self.values["BGColorValues"]["components"]
193 colorToSelect.setComponents(colorComponents)
194 currentView.setBackGroundColor(colorToSelect)
195
196 # run extra script
197 if self.values["script"] != "":
198 scriptPath = self.values["script"]
199 exec(open(scriptPath).read())
200
201
202dock_widget_factory = DockWidgetFactory(DOCKER_ID,
203 DockWidgetFactoryBase.DockPosition.DockRight,
204 WorkflowButtons)
205
206INSTANCE.addDockWidgetFactory(dock_widget_factory)
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390
__init__(self, parent=None, buttonID=-1, values={})