summaryrefslogtreecommitdiffstats
path: root/share/extensions/jessyink_key_bindings.py
blob: 2d28a95a90663b17ce83cabb42066ddcf082a3eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env python
#
# Copyright 2008, 2009 Hannes Hochreiner
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.
#
"""Effect to add key bindings to jessyInk slide show"""

import inkex
from inkex import Group, Script
from jessyink_install import JessyInkMixin

KEY_CODES = (
    "LEFT",
    "RIGHT",
    "DOWN",
    "UP",
    "HOME",
    "END",
    "ENTER",
    "SPACE",
    "PAGE_UP",
    "PAGE_DOWN",
    "ESCAPE",
)


class KeyBindings(JessyInkMixin, inkex.EffectExtension):
    """Add key bindings to slide show"""

    modes = ("slide", "index", "drawing")

    def set_options(self, namespace, opt_str, value):
        """Sort through all the options and combine them"""
        slot, action = opt_str.split("_", 1)
        keycodes = getattr(namespace, f"{slot}KeyCodes", {})
        charcodes = getattr(namespace, f"{slot}CharCodes", {})
        if value:
            for val in value.split(","):
                val = val.strip()
                if val in KEY_CODES:
                    keycodes[val + "_KEY"] = self.actions[slot][action]
                elif len(val) == 1:
                    charcodes[val] = self.actions[slot][action]
        setattr(namespace, f"{slot}KeyCodes", keycodes)
        setattr(namespace, f"{slot}CharCodes", charcodes)

    actions = {
        "slide": {
            "export": "slideUpdateExportLayer();",
            "addSlide": "slideAddSlide(activeSlide);",
            "resetTimer": "slideResetTimer();",
            "setDuration": "slideQueryDuration();",
            "backWithEffects": "dispatchEffects(-1);",
            "nextWithEffects": "dispatchEffects(1);",
            "backWithoutEffects": "skipEffects(-1);",
            "nextWithoutEffects": "skipEffects(1);",
            "switchToIndexMode": "toggleSlideIndex();",
            "switchToDrawingMode": "slideSwitchToDrawingMode();",
            "toggleProgressBar": "slideToggleProgressBarVisibility();",
            "firstSlide": "slideSetActiveSlide(0);",
            "lastSlide": "slideSetActiveSlide(slides.length - 1);",
        },
        "drawing": {
            "undo": "drawingUndo();",
            "switchToSlideMode": "drawingSwitchToSlideMode();",
            "pathWidthDefault": "drawingResetPathWidth();",
            "pathWidth1": "drawingSetPathWidth(1.0);",
            "pathWidth3": "drawingSetPathWidth(3.0);",
            "pathWidth5": "drawingSetPathWidth(5.0);",
            "pathWidth7": "drawingSetPathWidth(7.0);",
            "pathWidth9": "drawingSetPathWidth(9.0);",
            "pathColourBlue": 'drawingSetPathColour("blue");',
            "pathColourCyan": 'drawingSetPathColour("cyan");',
            "pathColourGreen": 'drawingSetPathColour("green");',
            "pathColourBlack": 'drawingSetPathColour("black");',
            "pathColourMagenta": 'drawingSetPathColour("magenta");',
            "pathColourOrange": 'drawingSetPathColour("orange");',
            "pathColourRed": 'drawingSetPathColour("red");',
            "pathColourWhite": 'drawingSetPathColour("white");',
            "pathColourYellow": 'drawingSetPathColour("yellow");',
        },
        "index": {
            "selectSlideToLeft": "indexSetPageSlide(activeSlide - 1);",
            "selectSlideToRight": "indexSetPageSlide(activeSlide + 1);",
            "selectSlideAbove": "indexSetPageSlide(activeSlide - INDEX_COLUMNS);",
            "selectSlideBelow": "indexSetPageSlide(activeSlide + INDEX_COLUMNS);",
            "previousPage": "indexSetPageSlide(activeSlide - INDEX_COLUMNS * INDEX_COLUMNS);",
            "nextPage": "indexSetPageSlide(activeSlide + INDEX_COLUMNS * INDEX_COLUMNS);",
            "firstSlide": "indexSetPageSlide(0);",
            "lastSlide": "indexSetPageSlide(slides.length - 1);",
            "switchToSlideMode": "toggleSlideIndex();",
            "decreaseNumberOfColumns": "indexDecreaseNumberOfColumns();",
            "increaseNumberOfColumns": "indexIncreaseNumberOfColumns();",
            "setNumberOfColumnsToDefault": "indexResetNumberOfColumns();",
        },
    }

    def add_arguments(self, pars):
        pars.add_argument("--tab")
        for slot, actions in self.actions.items():
            for action in actions:
                pars.add_argument(f"--{slot}_{action}")

    def effect(self):
        self.is_installed()

        for name in list(self.options.__dict__):
            if "_" in name:
                self.set_options(self.options, name, self.options.__dict__.pop(name))

        # Remove old master slide property
        for node in self.svg.xpath(
            "//svg:g[@jessyink:customKeyBindings='customKeyBindings']"
        ):
            node.delete()

        # Set custom key bindings.
        node_text = """function getCustomKeyBindingsSub()
{
    var keyDict = new Object();
    keyDict[SLIDE_MODE] = new Object();
    keyDict[INDEX_MODE] = new Object();
    keyDict[DRAWING_MODE] = new Object();
"""

        for key, value in self.options.slideKeyCodes.items():
            node_text += f"    keyDict[SLIDE_MODE][{key}] = function() {{ {value} }};\n"

        for key, value in self.options.drawingKeyCodes.items():
            node_text += (
                f"    keyDict[DRAWING_MODE][{key}] = function() {{ {value} }};\n"
            )

        for key, value in self.options.indexKeyCodes.items():
            node_text += f"    keyDict[INDEX_MODE][{key}] = function() {{ {value} }};\n"

        # Set custom char bindings.
        node_text += """    return keyDict;
}

function getCustomCharBindingsSub()
{
    var charDict = new Object();
    charDict[SLIDE_MODE] = new Object();
    charDict[INDEX_MODE] = new Object();
    charDict[DRAWING_MODE] = new Object();
"""

        for key, value in self.options.slideCharCodes.items():
            node_text += (
                f'    charDict[SLIDE_MODE]["{key}"] = function() {{ {value} }};\n'
            )

        for key, value in self.options.drawingCharCodes.items():
            node_text += (
                f'    charDict[DRAWING_MODE]["{key}"] = function() {{ {value} }};\n'
            )

        for key, value in self.options.indexCharCodes.items():
            node_text += (
                f'    charDict[INDEX_MODE]["{key}"] = function() {{ {value} }};\n'
            )

        node_text += "    return charDict;" + "\n"
        node_text += "}" + "\n"

        # Create new script node
        group = self.svg.add(Group())
        script = group.add(Script())
        script.text = node_text
        group.set("jessyink:customKeyBindings", "customKeyBindings")
        group.set(
            "onload",
            "this.getCustomCharBindings = function() { "
            "return getCustomCharBindingsSub(); }; "
            "this.getCustomKeyBindings = function() { return getCustomKeyBindingsSub(); };",
        )


if __name__ == "__main__":
    KeyBindings().run()