summaryrefslogtreecommitdiff
path: root/normal.py
blob: 66d61db318d058de54ae1f9a4e1b187d78f39ff3 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from Xlib import X, XK

from clipboard import copy, paste
from constants import TARGET
from vim import open_vim
from inkscape import distribute, ids_from_xml
import text
import styles

# Set of pressed keys
pressed = set()


# This is a list of received events that haven't been handled yet.
# Only when the user releases a key, the script knows what it should do.
# Then it either discards the preceding events, or replays them
events = []

def event_to_string(self, event):
    mods = []
    if event.state & X.ShiftMask:
        mods.append('Shift')

    if event.state & X.ControlMask:
        mods.append('Control')

    keycode = event.detail
    keysym = self.disp.keycode_to_keysym(keycode, 0)
    char = XK.keysym_to_string(keysym)

    return ''.join(mod + '+' for mod in mods) + (char if char else '?')


def replay(self):
    for e in events:
        self.inkscape.send_event(e, propagate=True)

    self.disp.flush()
    self.disp.sync()

def normal_mode(self, event, char):
    events.append(event)

    if event.type == X.KeyPress and char:
        pressed.add(event_to_string(self, event))
        return

    if event.type != X.KeyRelease:
        return

    handled = False
    if len(pressed) > 1:
        paste_style(self, pressed)
        handled = True
    elif len(pressed) == 1:
        # Get the only element in pressed
        ev = next(iter(pressed))
        handled = handle_single_key(self, ev)

    # replay events to Inkscape if we couldn't handle them
    if not handled:
        replay(self)

    events.clear()
    pressed.clear()

# TODO: add key for distributing and grouping maybe
def handle_single_key(self, ev):
    if ev == 't':
        # Vim mode
        open_vim(self, compile_latex=True)
    elif ev == 'Shift+t':
        # Vim mode prerendered
        open_vim(self, compile_latex=False)
    elif ev == 'a':
        # Add objects mode
        self.mode = styles.object_mode
    elif ev == 'Shift+a':
        # Save objects mode
        styles.save_object_mode(self)
    elif ev == 's':
        # Apply style mode
        self.mode = styles.style_mode
    elif ev == 'Shift+s':
        # Save style mode
        styles.save_style_mode(self)
    elif ev == 'w':
        # Pencil
        self.press('p')
    elif ev == 'x':
        # Snap
        self.press('percent', X.ShiftMask)
    elif ev == 'f':
        # Bezier
        self.press('b')
    elif ev == 'b':
        # Node
        self.press('n')
    elif ev == 'z':
        # Undo
        self.press('z', X.ControlMask)
    elif ev == 'Shift+z':
        # Delete
        self.press('Delete')
    elif ev == 'd':
        # distribute horizontally
        self.press('s', X.ControlMask)
        self.press('c', X.ControlMask)
        clipboard = paste()
        distribute(ids_from_xml(clipboard), "hgap")
        self.press('r', X.ControlMask)
    elif ev == 'Shift+d':
        # distribute vertically
        self.press('s', X.ControlMask)
        self.press('c', X.ControlMask)
        clipboard = paste()
        distribute(ids_from_xml(clipboard), "vgap")
        self.press('r', X.ControlMask)
    elif ev == 'v':
        self.press('t', X.ControlMask | X.Mod1Mask)
    elif ev == 'Shift+v':
        self.press('h', X.ControlMask | X.Mod1Mask)
    elif ev == 'g':
        self.press('g', X.ControlMask)
    elif ev == 'Shift+g':
        self.press('g', X.ControlMask | X.ShiftMask)
    elif ev == '`':
        # Disabled mode
        self.press('t')
        self.mode = text.text_mode
    else:
        # Not handled
        return False
    return True

def paste_style(self, combination):
    """

    This creates the style depending on the combination of keys.

    """

    # Stolen from TikZ
    pt = 1.327 # pixels
    w = 0.4 * pt
    thick_width = 0.8 * pt
    very_thick_width = 1.2 * pt

    style = {
        'stroke-opacity': 1
    }

    if {'s', 'a', 'd', 'g', 'r', 'x', 'e'} & combination:
        style['stroke'] = 'black'
        style['stroke-width'] = w
        style['marker-end'] = 'none'
        style['marker-start'] = 'none'
        style['stroke-dasharray'] = 'none'
    else:
        style['stroke'] = 'none'

    if 'g' in combination:
        w = thick_width
        style['stroke-width'] = w

    if 'r' in combination:
        w = very_thick_width
        style['stroke-width'] = w

    if 'a' in combination:
        style['marker-end'] = f'url(#marker-arrow-{w})'

    if 'x' in combination:
        style['marker-start'] = f'url(#marker-arrow-{w})'
        style['marker-end'] = f'url(#marker-arrow-{w})'

    if 'd' in combination:
        style['stroke-dasharray'] = f'{w},{2*pt}'

    if 'e' in combination:
        style['stroke-dasharray'] = f'{3*pt},{3*pt}'

    if 'f' in combination:
        style['fill'] = 'black'
        style['fill-opacity'] = 0.12

    if 'b' in combination:
        style['fill'] = 'black'
        style['fill-opacity'] = 1

    if 'w' in combination:
        style['fill'] = 'white'
        style['fill-opacity'] = 1

    if {'f', 'b', 'w'} & combination:
        style['marker-end'] = 'none'
        style['marker-start'] = 'none'

    if not {'f', 'b', 'w'} & combination:
        style['fill'] = 'none'
        style['fill-opacity'] = 1

    if style['fill'] == 'none' and style['stroke'] == 'none':
        return

    # Start creation of the svg.
    # Later on, we'll write this svg to the clipboard, and send Ctrl+Shift+V to
    # Inkscape, to paste this style.

    svg = '''
          <?xml version="1.0" encoding="UTF-8" standalone="no"?>
          <svg>
          '''
    # If a marker is applied, add its definition to the clipboard
    # Arrow styles stolen from tikz
    if ('marker-end' in style and style['marker-end'] != 'none') or \
            ('marker-start' in style and style['marker-start'] != 'none'):
        svg += f'''
                <defs id="marker-defs">
                <marker
                    id="marker-arrow-{w}"
                    viewBox="0 0 10 10"
                    refX="5" refY="5"
                    markerWidth="6" markerHeight="6"
                    orient="auto-start-reverse">
                    <path d="M 0 0 L 10 5 L 0 10 z" />
                </marker>
                </defs>
                '''
# TODO test if the new arrow actually works
# <marker
# id="marker-arrow-{w}"
# orient="auto-start-reverse"
# refY="0" refX="0"
# markerHeight="1.690" markerWidth="0.911">
#   <g transform="scale({(8.40 * w + 3.87)/(4.5*w)})">
#     <path
#        d="M -1.55415,2.0722 C -1.42464,1.29512 0,0.1295 0.38852,0 0,-0.1295 -1.42464,-1.29512 -1.55415,-2.0722"
#        style="fill:none;stroke:#000000;stroke-width:{0.6};stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1"
#        inkscape:connector-curvature="0" />
#    </g>
# </marker>

    style_string = ';'.join('{}: {}'.format(key, value)
                            for key, value in sorted(style.items(), key=lambda x: x[0])
                           )
    svg += f'<inkscape:clipboard style="{style_string}" /></svg>'

    copy(svg, target=TARGET)
    self.press('v', X.ControlMask | X.ShiftMask)