1# This script is licensed CC 0 1.0, so that you can learn from it.
2
3# ------ CC 0 1.0 ---------------
4
5# The person who associated a work with this deed has dedicated the
6# work to the public domain by waiving all of his or her rights to the
7# work worldwide under copyright law, including all related and
8# neighboring rights, to the extent allowed by law.
9
10# You can copy, modify, distribute and perform the work, even for
11# commercial purposes, all without asking permission.
12
13# https://creativecommons.org/publicdomain/zero/1.0/legalcode
14
15import krita
16from PyQt5.QtGui import QPixmap, QIcon
17from . import uitenbrushes
18
19
20class TenBrushesExtension(krita.Extension):
21
22    def __init__(self, parent):
23        super(TenBrushesExtension, self).__init__(parent)
24
25        self.actions = []
26        self.buttons = []
27        self.selectedPresets = []
28        # Indicates whether we want to activate the previous-selected brush
29        # on the second press of the shortcut
30        self.activatePrev = True
31        self.oldPreset = None
32
33    def setup(self):
34        self.readSettings()
35
36    def createActions(self, window):
37        action = window.createAction("ten_brushes", i18n("Ten Brushes"))
38        action.setToolTip(i18n("Assign ten brush presets to ten shortcuts."))
39        action.triggered.connect(self.initialize)
40        self.loadActions(window)
41
42    def initialize(self):
43        self.uitenbrushes = uitenbrushes.UITenBrushes()
44        self.uitenbrushes.initialize(self)
45
46    def readSettings(self):
47        self.selectedPresets = Application.readSetting(
48            "", "tenbrushes", "").split(',')
49        setting = Application.readSetting(
50            "", "tenbrushesActivatePrev2ndPress", "True")
51        # we should not get anything other than 'True' and 'False'
52        self.activatePrev = setting == 'True'
53
54    def writeSettings(self):
55        presets = []
56
57        for index, button in enumerate(self.buttons):
58            self.actions[index].preset = button.preset
59            presets.append(button.preset)
60        Application.writeSetting("", "tenbrushes", ','.join(map(str, presets)))
61        Application.writeSetting("", "tenbrushesActivatePrev2ndPress",
62                                 str(self.activatePrev))
63
64    def loadActions(self, window):
65        allPresets = Application.resources("preset")
66
67        for index, item in enumerate(['1', '2', '3', '4', '5',
68                                      '6', '7', '8', '9', '0']):
69            action = window.createAction(
70                "activate_preset_" + item,
71                str(i18n("Activate Brush Preset {num}")).format(num=item), "")
72            action.triggered.connect(self.activatePreset)
73
74            if (index < len(self.selectedPresets)
75                    and self.selectedPresets[index] in allPresets):
76                action.preset = self.selectedPresets[index]
77            else:
78                action.preset = None
79
80            self.actions.append(action)
81
82    def activatePreset(self):
83        allPresets = Application.resources("preset")
84        window = Application.activeWindow()
85        if (window and len(window.views()) > 0
86                and self.sender().preset in allPresets):
87            currentPreset = window.views()[0].currentBrushPreset()
88            if (self.activatePrev
89                    and self.sender().preset == currentPreset.name()):
90                window.views()[0].activateResource(self.oldPreset)
91            else:
92                self.oldPreset = window.views()[0].currentBrushPreset()
93                window.views()[0].activateResource(
94                    allPresets[self.sender().preset])
95
96        preset = window.views()[0].currentBrushPreset()
97        window.views()[0].showFloatingMessage(str(i18n("{}\nselected")).format(preset.name()),
98                                              QIcon(QPixmap.fromImage(preset.image())),
99                                              1000, 1)
100
101