Krita Source Code Documentation
Loading...
Searching...
No Matches
buttons_settings_dialog.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, Qt
8 from PyQt6.QtGui import QIcon, QPixmap, QColor, QPen, QBrush, QPainter
9 from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QWidget, QScrollArea, QPushButton,
10 QToolButton, QLabel, QLineEdit, QComboBox, QDialogButtonBox,
11 QFrame, QWidget, QSizePolicy)
12except:
13 from PyQt5.QtCore import QSize, Qt
14 from PyQt5.QtGui import QIcon, QPixmap, QColor, QPen, QBrush, QPainter
15 from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QWidget, QScrollArea, QPushButton,
16 QToolButton, QLabel, QLineEdit, QComboBox, QDialogButtonBox,
17 QFrame, QWidget, QSizePolicy)
18from krita import Krita, PresetChooser, ManagedColor, FileDialog
19from .flow_layout import FlowLayout
20import copy
21from builtins import i18n
22
23INSTANCE = Krita.instance()
24
25LISTOFTOOLS = [
26 { "toolName": "", "toolIcon": "", "toolString": "" },
27 { "toolName": "InteractionTool", "toolIcon": "select", "toolString": "Select Shapes Tool" },
28 { "toolName": "SvgTextTool", "toolIcon": "draw-text", "toolString": "Text Tool" },
29 { "toolName": "PathTool", "toolIcon": "shape_handling", "toolString": "Edit Shapes Tool" },
30 { "toolName": "KarbonCalligraphyTool", "toolIcon": "calligraphy", "toolString": "Calligraphy" },
31 { "toolName": "KritaShape/KisToolBrush", "toolIcon": "krita_tool_freehand", "toolString": "Freehand Brush Tool" },
32 { "toolName": "KritaShape/KisToolLine", "toolIcon": "krita_tool_line", "toolString": "Line Tool" },
33 { "toolName": "KritaShape/KisToolRectangle", "toolIcon": "krita_tool_rectangle", "toolString": "Rectangle Tool" },
34 { "toolName": "KritaShape/KisToolEllipse", "toolIcon": "krita_tool_ellipse", "toolString": "Ellipse Tool" },
35 { "toolName": "KisToolPolygon", "toolIcon": "krita_tool_polygon", "toolString": "Polygon Tool" },
36 { "toolName": "KisToolPolyline", "toolIcon": "polyline", "toolString": "Polyline Tool" },
37 { "toolName": "KisToolPath", "toolIcon": "krita_draw_path", "toolString": "Bezier Curve Tool" },
38 { "toolName": "KisToolPencil", "toolIcon": "krita_tool_freehandvector", "toolString": "Freehand Path Tool" },
39 { "toolName": "KritaShape/KisToolDyna", "toolIcon": "krita_tool_dyna", "toolString": "Dynamic Brush Tool" },
40 { "toolName": "KritaShape/KisToolMultiBrush", "toolIcon": "krita_tool_multihand", "toolString": "Multibrush Tool" },
41 { "toolName": "KisToolTransform", "toolIcon": "krita_tool_transform", "toolString": "Transform Tool" },
42 { "toolName": "KritaTransform/KisToolMove", "toolIcon": "krita_tool_move", "toolString": "Move Tool" },
43 { "toolName": "KisToolCrop", "toolIcon": "tool_crop", "toolString": "Crop Tool" },
44 { "toolName": "KritaFill/KisToolGradient", "toolIcon": "krita_tool_gradient", "toolString": "Gradient Tool" },
45 { "toolName": "KritaSelected/KisToolColorSampler", "toolIcon": "krita_tool_color_sampler", "toolString": "Color Sampler" },
46 { "toolName": "KritaShape/KisToolLazyBrush", "toolIcon": "krita_tool_lazybrush", "toolString": "Colorize Mask Tool" },
47 { "toolName": "KritaShape/KisToolSmartPatch", "toolIcon": "krita_tool_smart_patch", "toolString": "Smart Patch Tool" },
48 { "toolName": "KritaFill/KisToolFill", "toolIcon": "krita_tool_color_fill", "toolString": "Fill Tool" },
49 { "toolName": "KisToolEncloseAndFill", "toolIcon": "krita_tool_enclose_and_fill", "toolString": "Enclose and Fill Tool" },
50 { "toolName": "KisAssistantTool", "toolIcon": "krita_tool_assistant", "toolString": "Assistant Tool" },
51 { "toolName": "KritaShape/KisToolMeasure", "toolIcon": "krita_tool_measure", "toolString": "Measurement Tool" },
52 { "toolName": "ToolReferenceImages", "toolIcon": "krita_tool_reference_images", "toolString": "Reference Images Tool" },
53 { "toolName": "KisToolSelectRectangular", "toolIcon": "tool_rect_selection", "toolString": "Rectangular Selection Tool" },
54 { "toolName": "KisToolSelectElliptical", "toolIcon": "tool_elliptical_selection", "toolString": "Elliptical Selection Tool" },
55 { "toolName": "KisToolSelectPolygonal", "toolIcon": "tool_polygonal_selection", "toolString": "Polygonal Selection Tool" },
56 { "toolName": "KisToolSelectOutline", "toolIcon": "tool_outline_selection", "toolString": "Freehand Selection Tool" },
57 { "toolName": "KisToolSelectContiguous", "toolIcon": "tool_contiguous_selection", "toolString": "Contiguous Selection Tool" },
58 { "toolName": "KisToolSelectSimilar", "toolIcon": "tool_similar_selection", "toolString": "Similar Color Selection Tool" },
59 { "toolName": "KisToolSelectPath", "toolIcon": "tool_path_selection", "toolString": "Bezier Curve Selection Tool" },
60 { "toolName": "KisToolSelectMagnetic", "toolIcon": "tool_magnetic_selection", "toolString": "Magnetic Selection Tool" },
61 { "toolName": "ZoomTool", "toolIcon": "tool_zoom", "toolString": "Zoom Tool" },
62 { "toolName": "PanTool", "toolIcon": "tool_pan", "toolString": "Pan Tool" }
63 ]
64
65LISTOFSIZES = [16, 22, 32, 48, 64]
66
67LISTOFICONMODES = [i18n("Custom icon"), i18n("Tool icon"), i18n("Brush preset icon")]
68
69class ButtonsSettingsDialog(QDialog):
70 def __init__(self, parent=None, buttonsContentList=[], sizeIndex=2, settingsButtonPosition=0):
71 super().__init__(parent)
72 self.setWindowTitle(i18n("Workflow Buttons settings"))
73 self.allBrushPresets = INSTANCE.resources('preset')
74
75 mainLayout = QVBoxLayout(self)
76
77 self.buttonsContentList = copy.deepcopy(buttonsContentList)
78 self.defaultButtonContent = { "iconMode": 0, "icon" : "", "tooltip": "", "toolIndex" : 0, "presetName" : "", "FGColorValues" : { "model":"","depth":"","components":[],"profile":"" }, "BGColorValues" : { "model":"","depth":"","components":[],"profile":"" }, "script" : "" }
79
80 # button ID starts at 1, 0 is no button selected when list is empty
82 # if list is not empty, start with last button selected
83 if len(self.buttonsContentList) > 0:
85
86 # list of custom buttons
87 buttonsScrollArea = QScrollArea(self)
88 self.buttonsWidget = QWidget(self)
90 buttonsScrollArea.setWidgetResizable(True)
91 buttonsScrollArea.setWidget(self.buttonsWidget)
92
93 mainLayout.addWidget(buttonsScrollArea)
94
95 self.sizeIndex = sizeIndex
96 self.globalButtonSize = QSize(LISTOFSIZES[self.sizeIndex], LISTOFSIZES[self.sizeIndex])
97
98 self.settingsButtonPosition = settingsButtonPosition
99
100 layoutForSelectorControls = QHBoxLayout()
101 controlsSize = QSize(22,22)
102
103 # label to show selected button id:
104 selectedButtonIDLabelTitle = QLabel(i18n("Selected button:"), self)
105 self.selectedButtonIDLabel = QLabel(str(self.selectedButtonID), self)
106 layoutForSelectorControls.addWidget(selectedButtonIDLabelTitle)
107 layoutForSelectorControls.addWidget(self.selectedButtonIDLabel)
108
109 spacer1 = QWidget(self)
110 spacer1.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
111 layoutForSelectorControls.addWidget(spacer1)
112
113 # button to add a custom button
114 addButtonButton = QToolButton(self)
115 addButtonButton.setIconSize(controlsSize)
116 addButtonButton.setIcon(INSTANCE.icon('addlayer'))
117 addButtonButton.setToolTip(i18n("Add a workflow button"))
118 addButtonButton.clicked.connect(self.addButtonaddButton)
119 layoutForSelectorControls.addWidget(addButtonButton)
120
121 # buttons to move selected button to left/right
122 moveButtonLeftButton = QToolButton(self)
123 moveButtonLeftButton.setIconSize(controlsSize)
124 moveButtonLeftButton.setIcon(INSTANCE.icon('arrow-left'))
125 moveButtonLeftButton.setToolTip(i18n("Move selected workflow button to the left"))
126 moveButtonLeftButton.clicked.connect(self.moveButtonLeftmoveButtonLeft)
127 layoutForSelectorControls.addWidget(moveButtonLeftButton)
128 moveButtonRightButton = QToolButton(self)
129 moveButtonRightButton.setIconSize(controlsSize)
130 moveButtonRightButton.setIcon(INSTANCE.icon('arrow-right'))
131 moveButtonRightButton.setToolTip(i18n("Move selected workflow button to the right"))
132 moveButtonRightButton.clicked.connect(self.moveButtonRightmoveButtonRight)
133 layoutForSelectorControls.addWidget(moveButtonRightButton)
134
135 # button to delete selected button
136 deleteButtonButton = QToolButton(self)
137 deleteButtonButton.setIconSize(controlsSize)
138 deleteButtonButton.setIcon(INSTANCE.icon('deletelayer'))
139 deleteButtonButton.setToolTip(i18n("Delete selected workflow button"))
140 deleteButtonButton.clicked.connect(self.deleteButtondeleteButton)
141 layoutForSelectorControls.addWidget(deleteButtonButton)
142 mainLayout.addLayout(layoutForSelectorControls)
143
144 # set selected button's icon mode
145 layoutForIconMode = QHBoxLayout()
146 iconModeLabel = QLabel(i18n("Select icon mode:"), self)
147 layoutForIconMode.addWidget(iconModeLabel)
148 self.iconModeSelector = QComboBox(self)
151 layoutForIconMode.addWidget(self.iconModeSelector)
152
153 spacer2 = QWidget(self)
154 spacer2.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
155 layoutForIconMode.addWidget(spacer2)
156
157 mainLayout.addLayout(layoutForIconMode)
158
159 # set selected button's custom icon
160 layoutForIconSelection = QHBoxLayout()
161
162 addIconLabel = QLabel(i18n("Select a custom icon:"), self)
163 layoutForIconSelection.addWidget(addIconLabel)
164 self.iconPathInput = QLineEdit(self)
165 self.iconPathInput.setToolTip(i18n("Custom icon path"))
166 self.iconPathInput.editingFinished.connect(self.iconPathChangediconPathChanged)
167 layoutForIconSelection.addWidget(self.iconPathInput)
168
169 iconPathDialogButton = QPushButton(i18n("..."), self)
170 iconPathDialogButton.setToolTip(i18n("Select the custom icon"))
171 iconPathDialogButton.clicked.connect(self.selectIconselectIcon)
172 layoutForIconSelection.addWidget(iconPathDialogButton)
173
174 mainLayout.addLayout(layoutForIconSelection)
175
176 # set custom button's tooltip
177 layoutForToolTip = QHBoxLayout()
178 toolTipLabel = QLabel(i18n("Enter a Tooltip:"), self)
179 layoutForToolTip.addWidget(toolTipLabel)
180 self.toolTipInput = QLineEdit(self)
181 self.toolTipInput.setToolTip(i18n("Tooltip text"))
182 self.toolTipInput.editingFinished.connect(self.toolTipChangedtoolTipChanged)
183 layoutForToolTip.addWidget(self.toolTipInput)
184
185 mainLayout.addLayout(layoutForToolTip)
186
187 # set custom button's' toolName
188 layoutForToolSelector = QHBoxLayout()
189 toolSelectorLabel = QLabel(i18n("Select a tool:"), self)
190 layoutForToolSelector.addWidget(toolSelectorLabel)
191 self.toolSelector = QComboBox(self)
192 self.populateToolList()
193 self.toolSelector.activated.connect(self.toolChangedtoolChanged)
194 layoutForToolSelector.addWidget(self.toolSelector)
195
196 spacer3 = QWidget(self)
197 spacer3.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
198 layoutForToolSelector.addWidget(spacer3)
199
200 mainLayout.addLayout(layoutForToolSelector)
201
202 # set custom button's presetName
203 layoutForPresetSelector = QHBoxLayout()
204 presetSelectorLabel = QLabel(i18n("Select a brush preset:"), self)
205 layoutForPresetSelector.addWidget(presetSelectorLabel)
206 self.presetSelectorInput = QLineEdit(self)
207 self.presetSelectorInput.setToolTip(i18n("Brush preset name"))
208 self.presetSelectorInput.editingFinished.connect(self.presetChangedpresetChanged)
209 layoutForPresetSelector.addWidget(self.presetSelectorInput)
210
211 presetDialogButton = QPushButton(i18n("..."), self)
212 presetDialogButton.setToolTip(i18n("Select the brush preset"))
213 presetDialogButton.clicked.connect(self.selectPresetselectPreset)
214 layoutForPresetSelector.addWidget(presetDialogButton)
215
216 mainLayout.addLayout(layoutForPresetSelector)
217
218 # set custom button's FGColorValues
219 layoutForFGColorInput = QHBoxLayout()
220 FGColorLabel = QLabel(i18n("Select foreground color:"), self)
221 layoutForFGColorInput.addWidget(FGColorLabel)
222 FGColorInputSelector = QPushButton(i18n("Load current foreground color"), self)
223 FGColorInputSelector.clicked.connect(self.selectFGColorselectFGColor)
224 layoutForFGColorInput.addWidget(FGColorInputSelector)
225 FGColorClear = QPushButton(i18n("Clear foreground color"), self)
226 FGColorClear.clicked.connect(self.clearFGColorclearFGColor)
227 layoutForFGColorInput.addWidget(FGColorClear)
228
229 spacer4 = QWidget(self)
230 spacer4.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
231 layoutForFGColorInput.addWidget(spacer4)
232
233 mainLayout.addLayout(layoutForFGColorInput)
234
235 colorInfoToolTip = i18n("Color model ; depth ; profile ; components")
236
237 layoutForFGColorInfo = QHBoxLayout()
238 self.FGColorPreview = SelectedColorPreview(self, self.defaultButtonContent["FGColorValues"])
239 layoutForFGColorInfo.addWidget(self.FGColorPreview)
240 self.FGColorInfoLabel = QLabel(self)
241 self.FGColorInfoLabel.setToolTip(colorInfoToolTip)
242 layoutForFGColorInfo.addWidget(self.FGColorInfoLabel)
243
244 spacer5 = QWidget(self)
245 spacer5.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
246 layoutForFGColorInfo.addWidget(spacer5)
247
248 mainLayout.addLayout(layoutForFGColorInfo)
249
250 # set custom button's BGColorValues
251 layoutForBGColorInput = QHBoxLayout()
252 BGColorLabel = QLabel(i18n("Select background color:"), self)
253 layoutForBGColorInput.addWidget(BGColorLabel)
254 BGColorInputSelector = QPushButton(i18n("Load current background color"), self)
255 BGColorInputSelector.clicked.connect(self.selectBGColorselectBGColor)
256 layoutForBGColorInput.addWidget(BGColorInputSelector)
257 BGColorClear = QPushButton(i18n("Clear background color"), self)
258 BGColorClear.clicked.connect(self.clearBGColorclearBGColor)
259 layoutForBGColorInput.addWidget(BGColorClear)
260
261 spacer6 = QWidget(self)
262 spacer6.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
263 layoutForBGColorInput.addWidget(spacer6)
264
265 mainLayout.addLayout(layoutForBGColorInput)
266
267 layoutForBGColorInfo = QHBoxLayout()
268 self.BGColorPreview = SelectedColorPreview(self, self.defaultButtonContent["BGColorValues"])
269 layoutForBGColorInfo.addWidget(self.BGColorPreview)
270 self.BGColorInfoLabel = QLabel(self)
271 self.BGColorInfoLabel.setToolTip(colorInfoToolTip)
272 layoutForBGColorInfo.addWidget(self.BGColorInfoLabel)
273
274 spacer7 = QWidget(self)
275 spacer7.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
276 layoutForBGColorInfo.addWidget(spacer7)
277
278 mainLayout.addLayout(layoutForBGColorInfo)
279
280 # set custom button's extra script
281 layoutForScriptSelection = QHBoxLayout()
282
283 scriptLabel = QLabel(i18n("Select a script file:"), self)
284 layoutForScriptSelection.addWidget(scriptLabel)
285 self.scriptPathInput = QLineEdit(self)
286 self.scriptPathInput.setToolTip(i18n("Script path"))
287 self.scriptPathInput.editingFinished.connect(self.scriptPathChangedscriptPathChanged)
288 layoutForScriptSelection.addWidget(self.scriptPathInput)
289
290 scriptPathDialogButton = QPushButton(i18n("..."), self)
291 scriptPathDialogButton.setToolTip(i18n("Select the script"))
292 scriptPathDialogButton.clicked.connect(self.selectScriptselectScript)
293 layoutForScriptSelection.addWidget(scriptPathDialogButton)
294
295 mainLayout.addLayout(layoutForScriptSelection)
296
297 # spacer for global controls
298 spacerLine = QFrame(self)
299 spacerLine.setFrameShape(QFrame.Shape.HLine)
300 spacerLine.setFrameShadow(QFrame.Shadow.Sunken)
301 mainLayout.addWidget(spacerLine)
302
303 # button's size selector
304 layoutForButtonsSize = QHBoxLayout()
305 buttonsSizeLabel = QLabel(i18n("Buttons size:"), self)
306 self.buttonsSizeSelector = QComboBox(self)
307 self.populateSizeList()
308 self.buttonsSizeSelector.setCurrentIndex(self.sizeIndex)
310 layoutForButtonsSize.addWidget(buttonsSizeLabel)
311 layoutForButtonsSize.addWidget(self.buttonsSizeSelector)
312
313 spacer8 = QWidget(self)
314 spacer8.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
315 layoutForButtonsSize.addWidget(spacer8)
316
317 mainLayout.addLayout(layoutForButtonsSize)
318
319 # layout for settings button position option
320 layoutForSettingsButtonOption = QHBoxLayout()
321 settingsButtonOptionLabel = QLabel(i18n("Settings button position:"), self)
322 self.settingsButtonPositionSelector = QComboBox(self)
323 self.settingsButtonPositionSelector.insertItem(0, i18n("Bottom bar"))
324 self.settingsButtonPositionSelector.insertItem(1, i18n("Inline"))
327 layoutForSettingsButtonOption.addWidget(settingsButtonOptionLabel)
328 layoutForSettingsButtonOption.addWidget(self.settingsButtonPositionSelector)
329
330 spacer9 = QWidget(self)
331 spacer9.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
332 layoutForSettingsButtonOption.addWidget(spacer9)
333
334 mainLayout.addLayout(layoutForSettingsButtonOption)
335
336 # main dialog's default buttons
337 layoutForBottom = QHBoxLayout()
338 buttonBox = QDialogButtonBox(self)
339 buttonBox.setStandardButtons( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
340 buttonBox.accepted.connect(self.accept)
341 buttonBox.rejected.connect(self.reject)
342 layoutForBottom.addWidget(buttonBox)
343 mainLayout.addLayout(layoutForBottom)
344
345 self.populateButtons()
346 self.resize(500, 500)
347 # end of __init__
348
350 listIndex = -1
351 for mode in LISTOFICONMODES:
352 listIndex += 1
353 self.iconModeSelector.insertItem(listIndex,str(LISTOFICONMODES[listIndex]) + " ")
354 # extra " " to workaround possible cut of last letter...
355
357 toolNumber = 0
358 # Workaround to avoid passing an empty string to i18n(), which triggers a warning.
359 self.toolSelector.insertItem(toolNumber,
360 INSTANCE.icon(LISTOFTOOLS[toolNumber]["toolIcon"]),
361 LISTOFTOOLS[toolNumber]["toolString"])
362 for tool in LISTOFTOOLS[1:]:
363 toolNumber += 1
364 self.toolSelector.insertItem(toolNumber,
365 INSTANCE.icon(LISTOFTOOLS[toolNumber]["toolIcon"]),
366 i18n(LISTOFTOOLS[toolNumber]["toolString"]))
367
369 listIndex = -1
370 for size in LISTOFSIZES:
371 listIndex += 1
372 self.buttonsSizeSelector.insertItem(listIndex,str(LISTOFSIZES[listIndex]))
373
374 def createButton(self, buttonToCreate, buttonID):
375 # print("create button with buttonID: " + str(buttonID))
376 if buttonToCreate["iconMode"] == 0:
377 buttonIcon = QIcon(buttonToCreate["icon"])
378 elif buttonToCreate["iconMode"] == 1 and buttonToCreate["toolIndex"] != 0:
379 buttonIcon = INSTANCE.icon(LISTOFTOOLS[buttonToCreate["toolIndex"]]["toolIcon"])
380 elif buttonToCreate["iconMode"] == 2 and buttonToCreate["presetName"] != "":
381 brushPreset = self.allBrushPresets[buttonToCreate["presetName"]]
382 buttonIcon = QIcon(QPixmap.fromImage(brushPreset.image()))
383 else:
384 buttonIcon = QIcon()
385 button = CustomButtonForSettings(self.buttonsWidget, buttonID)
386 button.setIconSize(self.globalButtonSize)
387 button.setIcon(buttonIcon)
388 button.clicked.connect(lambda state, x=button.buttonID: self.selectButton(x))
389 self.buttonsLayout.addWidget(button)
390
391 def clearButtons(self):
392 # print("clearButtons called")
393 for widget in self.buttonsWidget.children():
394 if isinstance(widget, CustomButtonForSettings):
395 widget.deleteLater()
396
398 # print("populateButtons called")
399 buttonID = 0
400 for buttonToCreate in self.buttonsContentList:
401 buttonID += 1
402 # print("create button id " + str(buttonID))
403 self.createButton(buttonToCreate, buttonID)
405
406 def refreshButtons(self):
407 # print("refreshButtons called")
408 self.clearButtons()
409 self.populateButtons()
410
412 # print("updateDialogFields called")
413 self.selectedButtonIDLabel.setText(str(self.selectedButtonID))
414 # add a line here for new dialog fields...
415 if self.selectedButtonID > 0:
416 self.iconModeSelector.setCurrentIndex(self.buttonsContentList[self.selectedButtonID - 1]["iconMode"])
417 self.iconPathInput.setText(self.buttonsContentList[self.selectedButtonID - 1]["icon"])
418 self.toolTipInput.setText(self.buttonsContentList[self.selectedButtonID - 1]["tooltip"])
419 self.toolSelector.setCurrentIndex(self.buttonsContentList[self.selectedButtonID - 1]["toolIndex"])
420 self.presetSelectorInput.setText(self.buttonsContentList[self.selectedButtonID - 1]["presetName"])
421 self.setFGColorInfoLabel(self.buttonsContentList[self.selectedButtonID - 1]["FGColorValues"])
422 self.setBGColorInfoLabel(self.buttonsContentList[self.selectedButtonID - 1]["BGColorValues"])
423 self.scriptPathInput.setText(self.buttonsContentList[self.selectedButtonID - 1]["script"])
424
425 def selectButton(self, buttonID):
426 # print("selectButton " + str(buttonID))
427 self.selectedButtonID = buttonID
428 for widget in self.buttonsWidget.children():
429 if isinstance(widget, CustomButtonForSettings):
430 if widget.buttonID == self.selectedButtonID:
431 widget.selectButton()
432 else:
433 widget.deselectButton()
434 self.updateDialogFields()
435
436 def addButton(self):
437 # print("addButton pressed")
438 buttonID = len(self.buttonsContentList) + 1
439 self.selectedButtonID = buttonID
440 buttonContent = copy.deepcopy(self.defaultButtonContent)
441 self.buttonsContentList.append(buttonContent)
442 self.createButton(self.defaultButtonContent, buttonID)
443 self.refreshButtons()
444
445 def iconModeChanged(self, iconModeIndex):
446 if self.selectedButtonID < 1:
447 return
448 self.buttonsContentList[self.selectedButtonID - 1]["iconMode"] = iconModeIndex
449 self.refreshButtons()
450
451 def selectIcon(self):
452 # print("select icon dialog started")
453 if self.selectedButtonID < 1:
454 return
455
456 dialog = FileDialog(self)
457 dialog.setImageFilters() # all supported image formats
458 selectedFile = dialog.filename()
459 if selectedFile:
460 self.iconPathInput.setText(selectedFile)
462
464 # print("icon path edited...")
465 if self.selectedButtonID < 1:
466 return
467 self.buttonsContentList[self.selectedButtonID - 1]["icon"] = self.iconPathInput.text()
468 self.refreshButtons()
469
470 def toolTipChanged(self):
471 # print("tooltip edited...")
472 if self.selectedButtonID < 1:
473 return
474 self.buttonsContentList[self.selectedButtonID - 1]["tooltip"] = self.toolTipInput.text()
475
476 def toolChanged(self, toolIndex):
477 # print("tool changed")
478 if self.selectedButtonID < 1:
479 return
480 self.buttonsContentList[self.selectedButtonID - 1]["toolIndex"] = toolIndex
481 if self.iconModeSelector.currentIndex() == 1:
482 self.refreshButtons()
483
484 def selectPreset(self):
485 # print("select preset dialog started")
486 if self.selectedButtonID < 1:
487 return
488 dialog = QDialog(self)
489 dialogLayout = QVBoxLayout(dialog)
490 presetChooser = PresetChooser(dialog)
491 dialogLayout.addWidget(presetChooser)
492 buttonBox = QDialogButtonBox(dialog)
493 buttonBox.setStandardButtons( QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
494 buttonBox.accepted.connect(dialog.accept)
495 buttonBox.rejected.connect(dialog.reject)
496 dialogLayout.addWidget(buttonBox)
497
498 dialog.exec()
499 if dialog.result() == 1 and presetChooser.currentPreset():
500 self.presetSelectorInput.setText(presetChooser.currentPreset().name())
502
503 def presetChanged(self):
504 # print("icon path edited...")
505 if self.selectedButtonID < 1:
506 return
507 self.buttonsContentList[self.selectedButtonID - 1]["presetName"] = self.presetSelectorInput.text()
508 if self.iconModeSelector.currentIndex() == 2:
509 self.refreshButtons()
510
511 def setFGColorInfoLabel(self, values):
512 colorInfoLabel = i18n("Color info: ")
513 colorModel = values["model"]
514 if colorModel == "":
515 self.FGColorInfoLabel.setText(colorInfoLabel)
516 else:
517 colorDepth = values["depth"]
518 colorProfile = values["profile"]
519 colorComponents = values["components"]
520 shortenedComponents = [ "%.3f" % component for component in colorComponents ]
521 separator = " ; "
522 self.FGColorInfoLabel.setText(colorInfoLabel + colorModel + separator + colorDepth + separator + colorProfile + separator + str(shortenedComponents))
523 self.FGColorPreview.setColor(values)
524
525 def selectFGColor(self):
526 if self.selectedButtonID < 1:
527 return
528 # print("FGColor edited...")
529 savedColor = { "model":"", "depth":"", "components":[] }
530 currentFGColor = INSTANCE.activeWindow().activeView().foregroundColor()
531 savedColor["model"] = currentFGColor.colorModel()
532 savedColor["depth"] = currentFGColor.colorDepth()
533 savedColor["components"] = currentFGColor.components()
534 savedColor["profile"] = currentFGColor.colorProfile()
535 self.buttonsContentList[self.selectedButtonID - 1]["FGColorValues"] = savedColor
536 self.setFGColorInfoLabel(savedColor)
537
538 def clearFGColor(self):
539 if self.selectedButtonID < 1:
540 return
541 self.buttonsContentList[self.selectedButtonID - 1]["FGColorValues"] = self.defaultButtonContent["FGColorValues"]
542 self.setFGColorInfoLabel(self.buttonsContentList[self.selectedButtonID - 1]["FGColorValues"])
543
544 def setBGColorInfoLabel(self, values):
545 colorInfoLabel = i18n("Color info: ")
546 colorModel = values["model"]
547 if colorModel == "":
548 self.BGColorInfoLabel.setText(colorInfoLabel)
549 else:
550 colorDepth = values["depth"]
551 colorProfile = values["profile"]
552 colorComponents = values["components"]
553 shortenedComponents = [ "%.3f" % component for component in colorComponents ]
554 separator = " ; "
555 self.BGColorInfoLabel.setText(colorInfoLabel + colorModel + separator + colorDepth + separator + colorProfile + separator + str(shortenedComponents))
556 self.BGColorPreview.setColor(values)
557
558 def selectBGColor(self):
559 if self.selectedButtonID < 1:
560 return
561 # print("BGColor edited...")
562 savedColor = { "model":"", "depth":"", "components":[] }
563 currentBGColor = INSTANCE.activeWindow().activeView().backgroundColor()
564 savedColor["model"] = currentBGColor.colorModel()
565 savedColor["depth"] = currentBGColor.colorDepth()
566 savedColor["components"] = currentBGColor.components()
567 savedColor["profile"] = currentBGColor.colorProfile()
568 self.buttonsContentList[self.selectedButtonID - 1]["BGColorValues"] = savedColor
569 self.setBGColorInfoLabel(savedColor)
570
571 def clearBGColor(self):
572 if self.selectedButtonID < 1:
573 return
574 self.buttonsContentList[self.selectedButtonID - 1]["BGColorValues"] = self.defaultButtonContent["BGColorValues"]
575 self.setBGColorInfoLabel(self.buttonsContentList[self.selectedButtonID - 1]["BGColorValues"])
576
577 def selectScript(self):
578 # print("select script dialog started")
579 if self.selectedButtonID < 1:
580 return
581 dialog = FileDialog(self)
582 dialog.setNameFilter(i18n("Script files ") + "(*.py)")
583 selectedFile = dialog.filename()
584
585 if selectedFile:
586 self.scriptPathInput.setText(selectedFile)
588
590 # print("icon path edited...")
591 if self.selectedButtonID < 1:
592 return
593 self.buttonsContentList[self.selectedButtonID - 1]["script"] = self.scriptPathInput.text()
594
596 # print("clear all dialog fields...")
597 # add a line here for new dialog fields...
598 self.iconModeSelector.setCurrentIndex(0)
599 self.selectedButtonIDLabel.setText("0")
600 self.iconPathInput.setText("")
601 self.toolTipInput.setText("")
602 self.toolSelector.setCurrentIndex(0)
603 self.presetSelectorInput.setText("")
604 self.setFGColorInfoLabel(self.defaultButtonContent["FGColorValues"])
605 self.setBGColorInfoLabel(self.defaultButtonContent["BGColorValues"])
606 self.scriptPathInput.setText("")
607
608 def deleteButton(self):
609 # print("delete button " + str(self.selectedButtonID))
610 if self.selectedButtonID < 1 :
611 return
612 del self.buttonsContentList[self.selectedButtonID - 1]
613 self.selectedButtonID -= 1
614 if len(self.buttonsContentList) < 1:
615 self.selectedButtonID = 0
616 self.refreshButtons()
617 self.clearDialogFields()
618 return
619 if self.selectedButtonID == 0:
620 self.selectedButtonID = 1
621 self.refreshButtons()
622
623 def moveButtonLeft(self):
624 # print("moveLeft button " + str(self.selectedButtonID))
625 if self.selectedButtonID <= 1:
626 return
627 self.buttonsContentList.insert(self.selectedButtonID - 2, self.buttonsContentList.pop(self.selectedButtonID - 1))
628 self.selectedButtonID -= 1
629 self.refreshButtons()
630
632 # print("moveRight button " + str(self.selectedButtonID))
633 if self.selectedButtonID >= len(self.buttonsContentList):
634 return
635 self.buttonsContentList.insert(self.selectedButtonID, self.buttonsContentList.pop(self.selectedButtonID - 1))
636 self.selectedButtonID += 1
637 self.refreshButtons()
638
639 def buttonsSizeChanged(self, sizeIndex):
640 # print("size changed")
641 self.sizeIndex = sizeIndex
642 self.globalButtonSize = QSize(LISTOFSIZES[sizeIndex], LISTOFSIZES[sizeIndex])
643 if self.selectedButtonID > 0:
644 self.refreshButtons()
645
646 def settingsButtonPositionChanged(self, position):
647 # print("settings button position changed")
648 self.settingsButtonPosition = position
649
650
651class CustomButtonForSettings(QToolButton):
652 # Class to define the custom buttons inside the dialog
653 def __init__(self, parent=None, buttonID=-1):
654 super().__init__(parent)
655 self.buttonID = buttonID
656 self.highlightSquare = selectedButtonHighlight(self, self.height())
657
658 def resizeEvent(self, event):
659 self.highlightSquare.setMinimumSize(QSize(self.height(), self.height()))
660
661 def selectButton(self):
662 self.highlightSquare.setVisible(True)
663
664 def deselectButton(self):
665 self.highlightSquare.setVisible(False)
666
667
669 # Class to define the highlight for selected button
670 def __init__(self, parent=None, size=32):
671 super().__init__(parent)
672 self.colorPen = self.palette().highlight().color()
673 self.colorBrush = self.palette().highlight().color()
674 self.colorBrush.setAlpha(64)
675 self.setMinimumSize(QSize(size,size))
676
677 def paintEvent(self, event):
678 painter = QPainter(self)
679 painter.setPen(QPen(self.colorPen, 4, Qt.PenStyle.SolidLine))
680 painter.setBrush(QBrush(self.colorBrush, Qt.BrushStyle.SolidPattern))
681 painter.drawRect(2, 2, self.minimumSize().width() - 3, self.minimumSize().height() - 4)
682
683
684class SelectedColorPreview(QWidget):
685 # Class to define the widget used to visualize selected FG and BG colors
686 def __init__(self, parent=None, colorInfo={}):
687 super().__init__(parent)
688 self.setMinimumSize(32, 32)
689 self.canvas = INSTANCE.activeWindow().activeView().canvas()
690 self.colorInfo = {}
691 self.color = QColor(0,0,0,0)
692 self.emptyColor = QColor(0,0,0,0)
693 self.outlineColor = self.palette().text().color()
694 self.setColor(colorInfo)
695
696 def paintEvent(self, event):
697 painter = QPainter(self)
698 painter.setPen(QPen(self.outlineColor, 1, Qt.PenStyle.SolidLine))
699 painter.setBrush(QBrush(self.color, Qt.BrushStyle.SolidPattern))
700 painter.drawRect(1, 1, 30, 30)
701
703 if(self.colorInfo["model"] != ""):
704 managedColor = ManagedColor(self.colorInfo["model"],
705 self.colorInfo["depth"],
706 self.colorInfo["profile"])
707 colorComponents = self.colorInfo["components"]
708 managedColor.setComponents(colorComponents)
709 self.color = managedColor.colorForCanvas(self.canvas)
710 else:
711 self.color = self.emptyColor
712
713 def setColor(self, colorInfo):
714 self.colorInfo = colorInfo
716 self.update()
static Krita * instance()
instance retrieve the singleton instance of the Application object.
Definition Krita.cpp:390
__init__(self, parent=None, buttonsContentList=[], sizeIndex=2, settingsButtonPosition=0)