-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSType.py
835 lines (760 loc) · 31.8 KB
/
VSType.py
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
import sys, re, os.path
from PyQt4 import QtGui, QtCore, QtSvg, QtNetwork
from xml.etree import ElementTree as ET
from Tkinter import Tk # standard QT clipboard works strange with VNC client
import math
import svg.path
import euclid
min_itlv_threshold = 1.0/16
curvature_threshold = 0.005
def curvature( curve, pos):
x_d = 3 * ( (1-pos) ** 2 * ( curve.control1 - curve.start).real + \
2 * (1-pos) * pos * ( curve.control2 - curve.control1).real + \
pos ** 2 * ( curve.end - curve.control2).real)
y_d = 3 * ( (1-pos) ** 2 * ( curve.control1 - curve.start).imag + \
2 * (1-pos) * pos * ( curve.control2 - curve.control1).imag + \
pos ** 2 * ( curve.end - curve.control2).imag)
x_dd = 6 * ( (1-pos) * ( curve.start - 2 * curve.control1 + curve.control2).real + \
pos * ( curve.end - 2 * curve.control2 + curve.control1).real)
y_dd = 6 * ( (1-pos) * ( curve.start - 2 * curve.control1 + curve.control2).imag + \
pos * ( curve.end - 2 * curve.control2 + curve.control1).imag)
curvature = abs( x_d * y_dd - y_d * x_dd) / math.sqrt( ( x_d * x_d + y_d * y_d) ** 3)
return curvature
def cover_with_points( curve, curv_thr):
poss = []
curv = curvature( curve, 0.5)
if ( curv > curv_thr ):
poss.extend( cover_with_poss( curve, 0.0, 0.5, curv_thr))
poss.extend( cover_with_poss( curve, 0.5, 1.0, curv_thr))
else:
poss.append( 0.5)
# convert poss to points
points = []
for pos in poss:
point = curve.point( pos)
points.append( euclid.Point2( point.real, point.imag))
return points
def cover_with_poss( curve, start, end, curv_thr):
poss = []
curv = curvature( curve, ( end + start) / 2) * ( end - start)
if ( curv > curv_thr and ( end - start) > min_itlv_threshold ):
poss.extend( cover_with_poss( curve, start, ( end + start) / 2, curv_thr))
poss.extend( cover_with_poss( curve, ( end + start) / 2, end, curv_thr))
else:
poss.append( ( end + start) / 2)
return poss
class SingleEntity( QtGui.QApplication):
def __init__( self, argv, id):
QtGui.QApplication.__init__( self, argv)
self.my_mem = QtCore.QSharedMemory( self)
self.my_mem.setKey( id)
if self.my_mem.attach():
self.is_running = True
else:
self.is_running = False
if not self.my_mem.create( 1):
raise RuntimeError( 'SingleApplication.__init__: can\'t create memory')
def isRunning(self):
return self.is_running
class path_w_rect( QtGui.QGraphicsPathItem):
def __init__( self, tr_rect):
QtGui.QGraphicsPathItem.__init__( self)
self.tr_rect = tr_rect
def rect( self):
return self.tr_rect
def setRect( self, tr_rect):
self.tr_rect = tr_rect
class bound_polygon():
def __init__( self, arr):
self.arr = arr
self.cache_rect = None
def contains( self, point2):
# point 2 is assumed of type euclid.Point2
# put point into origin to simplify code but it is not optimal
pov_arr = []
rcross = 0
for i in range( 0, len( self.arr)):
pov_arr.append( self.arr[ i] - point2)
# cycle over all edges
for i in range( -1, len( pov_arr) - 1): # it is possible in python to get arr[-1] which is just last element but not a[len(a)]
# if edge includes X axis
if ( pov_arr[ i ].y > 0 and pov_arr[ i + 1 ].y <= 0 or pov_arr[ i + 1 ].y > 0 and pov_arr[ i ].y <= 0 ):
# find x coordiante of intersection
x = float( pov_arr[ i ].x * pov_arr[ i + 1 ].y - pov_arr[ i + 1 ].x * pov_arr[ i ].y) / \
( pov_arr[ i + 1 ].y - pov_arr[ i ].y)
if ( x > 0 ):
rcross += 1
if ( rcross % 2 == 1 ):
return True
else:
return False
def rect( self):
# caching
if ( None != self.cache_rect ):
return self.cache_rect
# protection
if ( len( self.arr) <= 0 ):
self.cache_rect = QtCore.QRectF( 0,0,0,0)
return self.cache_rect
# initial value
tr_point = self.arr[ 0 ]
min_x = tr_point.x
max_x = tr_point.x
min_y = tr_point.y
max_y = tr_point.y
# process the rest
for vect in self.arr:
tr_vect = vect
if min_x > tr_vect.x:
min_x = tr_vect.x
if max_x < tr_vect.x:
max_x = tr_vect.x
if min_y > tr_vect.y:
min_y = tr_vect.y
if max_y < tr_vect.y:
max_y = tr_vect.y
# form the output
self.cache_rect = QtCore.QRectF(min_x,min_y,max_x-min_x,max_y-min_y)
return self.cache_rect
def get_graphics_item( self):
item = path_w_rect( self.rect())
item.setPath( self.get_path())
return item
def get_path( self):
path = QtGui.QPainterPath()
path.moveTo( QtCore.QPoint( self.arr[0].x, self.arr[0].y))
for idx in range( 1, len( self.arr)):
path.lineTo( QtCore.QPoint( self.arr[idx].x, self.arr[idx].y))
path.lineTo( QtCore.QPoint( self.arr[0].x, self.arr[0].y))
return path
class svg_parser:
def __init__(self, file_name):
self.rect_dict = {}
self.href_dict = {}
svg_root = ET.parse(file_name)
self.parse_root(svg_root)
def get_transform(self, attrib):
# fill with identity matrix
ret = euclid.Matrix3()
# find attribute
if 'transform' in attrib.keys():
tr_str = attrib['transform']
else:
return ret
# split
spl_str = re.findall(r'(\s*\w+\([^\)]*\)\s*)', tr_str)
# iterate over all transformations
for loc_str in spl_str:
# matrix
match = re.search(r'\s*matrix\(([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+)'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))\)\s*', loc_str)
if match:
tr_a = float(match.group(1))
tr_b = float(match.group(4))
tr_c = float(match.group(7))
tr_d = float(match.group(10))
tr_e = float(match.group(13))
tr_f = float(match.group(16))
loc_mtx = euclid.Matrix3()
loc_mtx.a = tr_a
loc_mtx.b = tr_c
loc_mtx.c = tr_e
loc_mtx.e = tr_b
loc_mtx.f = tr_d
loc_mtx.g = tr_f
ret = ret * loc_mtx
# translate
match = re.search(r'\s*translate\(([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+)'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))?\)\s*', loc_str)
if match:
tsl_x = float(match.group(1))
tsl_y = 0.0 if match.group(4) == None else float(match.group(4))
loc_mtx = euclid.Matrix3()
loc_mtx.c = tsl_x
loc_mtx.g = tsl_y
ret = ret * loc_mtx
# scale
match = re.search(r'\s*scale\(([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+)'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))?\)\s*', loc_str)
if match:
scl_x = float(match.group(1))
scl_y = scl_x if match.group(4)==None else float(match.group(4))
loc_mtx = euclid.Matrix3()
loc_mtx.a = scl_x
loc_mtx.f = scl_y
ret = ret * loc_mtx
# rotate
match = re.search(r'\s*rotate\(([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+)'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))?'+\
r'((\s*,\s*|\s+)([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+))?\)\s*', loc_str)
if match:
rot_an = float(match.group(1))
rot_an = rot_an/180*math.pi
loc_mtxa = euclid.Matrix3()
loc_mtxa.a = math.cos(rot_an)
loc_mtxa.b = -math.sin(rot_an)
loc_mtxa.f = math.cos(rot_an)
loc_mtxa.e = math.sin(rot_an)
rot_x = 0.0 if match.group(4) == None else float(match.group(4))
rot_y = 0.0 if match.group(7) == None else float(match.group(7))
loc_mtx = euclid.Matrix3()
loc_mtx.c = rot_x
loc_mtx.g = rot_y
ret = ret * loc_mtx * loc_mtxa
loc_mtx.c = -rot_x
loc_mtx.g = -rot_y
ret = ret * loc_mtx
# skewX
match = re.search(r'\s*skewX\(([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+)\)\s*', loc_str)
if match:
rot_an = float(match.group(1))
rot_an = rot_an/180*math.pi
loc_mtxa = euclid.Matrix3()
loc_mtxa.b = math.tan(rot_an)
ret = ret * loc_mtxa
# skewY
match = re.search(r'\s*skewY\(([-+]?\d+|[-+]?\d+[.]|'+\
r'[-+]?[.]\d+|[-+]?\d+[.]\d+)\)\s*', loc_str)
if match:
rot_an = float(match.group(1))
rot_an = rot_an/180*math.pi
loc_mtxa = euclid.Matrix3()
loc_mtxa.e = math.tan(rot_an)
ret = ret * loc_mtxa
return ret
def transform_dicts(self, tr_mtx, hrefs, rects):
r_hrefs = dict()
r_rects = dict()
for key in hrefs.keys():
r_hrefs[key] = self.transform_points(tr_mtx, hrefs[key])
for key in rects.keys():
r_rects[key] = self.transform_points(tr_mtx, rects[key])
return (r_hrefs, r_rects)
def parse_root(self, svg_root):
root = svg_root.getroot()
(hrefs, rects) = self.parse_item(root)
for key in hrefs.keys():
if len(hrefs[key]) > 1:
self.href_dict[ bound_polygon( hrefs[ key]) ] = key
for key in rects.keys():
if len(rects[key]) > 1:
self.rect_dict[ bound_polygon( rects[ key]) ] = key
def parse_item(self, item):
hrefs = dict()
rects = dict()
tr_mtx = self.get_transform(item.attrib)
# first check if there is a link
if (re.search(r'\{.*\}a$', item.tag)):
href = None
for attr in item.attrib.keys():
if re.search(r'\{.*\}href$', attr):
href = item.attrib[attr]
if (href != None):
points = self.get_points(item, False)
hrefs[href] = points
return self.transform_dicts(tr_mtx, hrefs, rects)
# handle onckick attribute if available
onclick_str = None
if 'onclick' in item.attrib.keys():
match = re.search(r'[^(]*\(\'([^\']*)\'\)', item.attrib['onclick'])
if match and len(match.groups()) == 1:
onclick_str = match.group(1)
if onclick_str != None:
print 'parse_item: onclick detected %s' % onclick_str
points = self.get_points(item, False)
rects[onclick_str] = points
return self.transform_dicts(tr_mtx, hrefs, rects)
# conventional way
for loc_item in item:
(loc_hrefs, loc_rects) = self.parse_item(loc_item)
# merge
for key in loc_hrefs.keys():
if key in hrefs.keys():
hrefs[key].extend(loc_hrefs[key])
else:
hrefs[key] = loc_hrefs[key]
for key in loc_rects.keys():
if key in rects.keys():
rects[key].extend(loc_rects[key])
else:
rects[key] = loc_rects[key]
# transform and return
return self.transform_dicts(tr_mtx, hrefs, rects)
def transform_points(self, g_mtx, arr):
# initial value
ret_arr = []
# process the rest
for vect in arr:
ret_arr.append(g_mtx*vect)
# output
return ret_arr
def get_points(self, item, do_tr):
if (do_tr):
tr_mtx = self.get_transform(item.attrib)
else:
tr_mtx = euclid.Matrix3()
points = []
# scan yourself
if (re.search(r'\{.*\}rect$', item.tag)):
points = self.get_rect_points(item)
elif (re.search(r'\{.*\}path$', item.tag)):
points = self.get_path_points(item)
# scan childs
for loc_item in item:
points.extend(self.get_points(loc_item, True))
# transform everything
return self.transform_points(tr_mtx, points)
def get_rect_points(self, item):
rect_x = float(item.attrib['x'])
rect_y = float(item.attrib['y'])
rect_w = float(item.attrib['width'])
rect_h = float(item.attrib['height'])
vect2tr = [euclid.Point2(rect_x, rect_y), \
euclid.Point2(rect_x+rect_w, rect_y), \
euclid.Point2(rect_x+rect_w, rect_y+rect_h), \
euclid.Point2(rect_x, rect_y+rect_h)]
return vect2tr
def get_path_points(self, item):
d_str = item.attrib['d']
svg_path = svg.path.parse_path( d_str)
vect2str = []
after_last_idx = len( svg_path) - 2
for idx in range( 0, after_last_idx + 1):
path_item = svg_path[ idx ]
if isinstance( path_item, svg.path.Line):
if ( 0 == idx ):
vect2str.append( euclid.Point2( path_item.start.real, path_item.start.imag))
if ( after_last_idx != idx ):
vect2str.append( euclid.Point2( path_item.end.real, path_item.end.imag))
elif isinstance( path_item, svg.path.CubicBezier):
if ( 0 == idx ):
vect2str.append( euclid.Point2( path_item.start.real, path_item.start.imag))
points = []
points = cover_with_points( path_item, curvature_threshold)
vect2str.extend( points)
if ( after_last_idx != idx ):
vect2str.append( euclid.Point2( path_item.end.real, path_item.end.imag))
else:
print( 'unknown type %s' % type( path_item))
return vect2str
def click(self, hit_point):
# Rectangles under references
for href in self.href_dict.keys():
if href.contains(hit_point):
print "Href rect hit:return %s" % self.href_dict[href]
return [self.href_dict[href], 'href']
# Common rectangles
for rect in self.rect_dict.keys():
if rect.contains(hit_point):
print "Rect hit:return %s" % self.rect_dict[rect]
return [self.rect_dict[rect], 'rect']
return None
def nav(self, cur_rect, direction):
pt = cur_rect.center()
min_detected = None
href_rects = self.href_dict.keys()
if (href_rects):
href_rects.extend(self.rect_dict.keys())
rects = href_rects
else:
rects = self.rect_dict.keys()
if not(rects):
print 'nav: No rects detected at all - stand still'
return
if direction == 'left':
for rect in rects:
pt2 = rect.rect().center()
dx = pt2.x() - pt.x()
dy = pt2.y() - pt.y()
if dx*dx + dy*dy < 10:
continue
if dx < 0 and abs(dy) < abs(dx):
if min_detected == None:
min_dist = dx*dx + dy*dy
min_detected = rect
elif min_dist > dx*dx + dy*dy:
min_dist = dx*dx + dy*dy
min_detected = rect
elif direction == 'up':
for rect in rects:
pt2 = rect.rect().center()
dx = pt2.x() - pt.x()
dy = pt2.y() - pt.y()
if dx*dx + dy*dy < 10:
continue
if dy < 0 and abs(dx) < abs(dy):
if min_detected == None:
min_dist = dx*dx + dy*dy
min_detected = rect
elif min_dist > dx*dx + dy*dy:
min_dist = dx*dx + dy*dy
min_detected = rect
elif direction == 'right':
for rect in rects:
pt2 = rect.rect().center()
dx = pt2.x() - pt.x()
dy = pt2.y() - pt.y()
if dx*dx + dy*dy < 10:
continue
if dx > 0 and abs(dy) < dx:
if min_detected == None:
min_dist = dx*dx + dy*dy
min_detected = rect
elif min_dist > dx*dx + dy*dy:
min_dist = dx*dx + dy*dy
min_detected = rect
elif direction == 'down':
for rect in rects:
pt2 = rect.rect().center()
dx = pt2.x() - pt.x()
dy = pt2.y() - pt.y()
if dx*dx + dy*dy < 10:
continue
if dy > 0 and abs(dx) < dy:
if min_detected == None:
min_dist = dx*dx + dy*dy
min_detected = rect
elif min_dist > dx*dx + dy*dy:
min_dist = dx*dx + dy*dy
min_detected = rect
else:
print 'nav: Unknown direction %s - stand still' % direction
return cur_rect
if min_detected != None:
return min_detected
else:
print 'nav: No rects detected to step - stand still'
return cur_rect
class cursor_animation(QtGui.QGraphicsItemAnimation):
def __init__(self, svg_item):
QtGui.QGraphicsItemAnimation.__init__(self)
self.color = QtGui.QColor(0,0,255) # initial state
self.pen = QtGui.QPen(self.color)
self.pen.setWidth(2)
self.svg_item = svg_item
self.tl = QtCore.QTimeLine(1000)
self.tl.setFrameRange(0, 100)
self.tl.setLoopCount(5)
self.setTimeLine(self.tl)
self.connect(self.tl, QtCore.SIGNAL('finished()'), \
self.finished_callback)
def setItem(self, item_to_set):
item_to_set.setPen(self.pen)
QtGui.QGraphicsItemAnimation.setItem(self, item_to_set)
def afterAnimationStep(self, step):
self.color.setRgb(0,int(255*(0.5-abs(step-0.5))*2),\
int(255*abs(step-0.5)*2))
self.pen.setColor(self.color)
self.item().setPen(self.pen)
def finished_callback(self):
self.color.setRgb(0,0,255,0)
self.pen.setColor(self.color)
self.item().setPen(self.pen)
class svg_scene(QtGui.QGraphicsScene):
def __init__(self):
QtGui.QGraphicsScene.__init__(self)
self.cursor = None
def addCursor(self, item):
cursor = cursor_animation(item)
# first of all find if there is something to embrace with cursor
if len(cursor.svg_item.parser.href_dict.keys()) != 0:
rect = cursor.svg_item.parser.href_dict.keys()[0]
elif len(cursor.svg_item.parser.rect_dict.keys()) != 0:
rect = cursor.svg_item.parser.rect_dict.keys()[0]
else:
print 'nothing to embrace with cursor'
return
self.cursor = cursor
self.cursor_item = rect.get_graphics_item()
self.addItem(self.cursor_item)
self.cursor.setItem(self.cursor_item)
def focusInEvent(self, evt):
QtGui.QGraphicsScene.focusInEvent(self, evt)
if (self.cursor):
self.cursor.tl.stop()
self.cursor.tl.start()
def focusOutEvent(self, evt):
QtGui.QGraphicsScene.focusOutEvent(self, evt)
if (self.cursor):
self.cursor.tl.stop()
self.cursor.finished_callback()
def setCallback(self):
if (self.cursor):
self.cursor.tl.start()
def unsetCallback(self):
if (self.cursor):
self.cursor.tl.stop()
def keyCallback(self, e):
if self.cursor == None:
return
cur_rect = self.cursor_item.rect()
new_rect = cur_rect
intercepted = True
if e.key() == QtCore.Qt.Key_H:
new_rect = self.cursor.svg_item.parser.nav(cur_rect, 'left')
elif e.key() == QtCore.Qt.Key_J:
new_rect = self.cursor.svg_item.parser.nav(cur_rect, 'down')
elif e.key() == QtCore.Qt.Key_K:
new_rect = self.cursor.svg_item.parser.nav(cur_rect, 'up')
elif e.key() == QtCore.Qt.Key_L:
new_rect = self.cursor.svg_item.parser.nav(cur_rect, 'right')
elif e.key() == QtCore.Qt.Key_Space:
self.cursor.svg_item.mouseHit(cur_rect.center())
else:
intercepted = False
if intercepted:
self.cursor.tl.stop()
if ( new_rect != cur_rect ):
self.cursor_item.setPath( new_rect.get_path())
self.cursor_item.setRect( new_rect.rect())
self.cursor.setItem(self.cursor_item)
self.cursor.tl.start()
class svg_item(QtSvg.QGraphicsSvgItem):
def __init__(self, file_name):
QtSvg.QGraphicsSvgItem.__init__(self, file_name)
self.parser = svg_parser(file_name)
def mousePressEvent(self, event):
hit_point = event.pos()
self.mouseHit(hit_point)
event.accept()
def mouseHit(self, hit_point):
ret = self.parser.click( euclid.Point2(hit_point.x(), hit_point.y()))
if ret:
(ret_str, ret_type) = ret
if ret_type == 'rect':
self.emit(QtCore.SIGNAL("rect_hit"), ret_str)
elif ret_type == 'href':
self.emit(QtCore.SIGNAL("href_hit"), ret_str)
else:
print 'Unknown click type %s' % ret_type
class line_edit(QtGui.QLineEdit):
def __init__(self, parent):
QtGui.QLineEdit.__init__(self, parent)
def focusInEvent(self, event):
QtGui.QLineEdit.focusInEvent(self, event)
self.emit(QtCore.SIGNAL('focusInSignal(QString)'),\
self.displayText())
class spacekeyFilter(QtCore.QObject):
def __init__(self, obj):
QtCore.QObject.__init__(self, obj)
self.bypass = obj
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Space:
self.bypass.keyPressEvent(event)
return True
return False
class tabkeyFilter(QtCore.QObject):
def __init__(self, obj):
QtCore.QObject.__init__(self, obj)
self.bypass = obj
self.signal = QtCore.SIGNAL("tab_key_hit")
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Tab:
self.emit(self.signal)
return True
return False
class app_view(QtGui.QGraphicsView):
def __init__(self):
QtGui.QGraphicsView.__init__(self)
# needed for obtaining clipboard
if os.name == 'nt':
self.tk = Tk()
self.tk.withdraw()
elif os.name != 'posix':
raise RuntimeError('Unsupported os.name: %s' % os.name)
# scenes dictionary and history
self.oldScene = None
self.scenes = {}
self.scenes_history = []
# show splash screen
self.show_scene('resources/splash.svg')
# buttons
sk_filter = spacekeyFilter(self)
open_but = QtGui.QPushButton(QtGui.QIcon('resources/open-file.png'),\
'', self)
open_but.setGeometry(0,0,20,20)
open_but.installEventFilter(sk_filter)
back_but = QtGui.QPushButton(QtGui.QIcon('resources/go-back.png'),\
'', self)
back_but.setGeometry(20,0,20,20)
back_but.installEventFilter(sk_filter)
reload_but = QtGui.QPushButton(QtGui.QIcon('resources/reload.png'),\
'', self)
reload_but.setGeometry(40,0,20,20)
reload_but.installEventFilter(sk_filter)
clear_but = QtGui.QPushButton(QtGui.QIcon('resources/clean-up.png'),\
'', self)
clear_but.setGeometry(60,0,20,20)
clear_but.installEventFilter(sk_filter)
# buttons callbacks
self.connect(open_but, QtCore.SIGNAL('clicked()'), \
self.open_but_callback)
self.connect(back_but, QtCore.SIGNAL('clicked()'), \
self.back_but_callback)
self.connect(reload_but, QtCore.SIGNAL('clicked()'), \
self.reload_but_callback)
self.connect(clear_but, QtCore.SIGNAL('clicked()'), \
self.clear_but_callback)
# line edit
self.lineedit = line_edit(self)
self.lineedit.setGeometry(80,0,200,20)
self.connect(self.lineedit, QtCore.SIGNAL('textEdited(QString)'), \
self.set_clipboard)
self.connect(self.lineedit, \
QtCore.SIGNAL('focusInSignal(QString)'), \
self.set_clipboard)
# File load dialog
self.file_open_dlg = QtGui.QFileDialog()
# Ctrl+z and Ctrl+y bypass
shortcut = QtGui.QShortcut(QtGui.QKeySequence('Ctrl+Z'), self)
self.connect(shortcut, QtCore.SIGNAL('activated()'), \
self.ctrl_z_callback)
shortcut = QtGui.QShortcut(QtGui.QKeySequence('Ctrl+Y'), self)
self.connect(shortcut, QtCore.SIGNAL('activated()'), \
self.ctrl_y_callback)
def setScene(self, scene):
if self.oldScene != None:
self.oldScene.unsetCallback()
QtGui.QGraphicsView.setScene(self, scene)
self.oldScene = scene
scene.setCallback()
def show_scene(self, file_name):
print 'Request to open %s' % file_name
file_name = self._show_scene(file_name)
if file_name:
self.scenes_history.append(file_name)
def _show_scene(self, file_name):
# canonize first
file_name = os.path.abspath(file_name)
try:
# check if it should open from scratch
if not(file_name in self.scenes.keys()):
# create scene
scene = svg_scene()
# item
item = svg_item(file_name)
item.setPos(QtCore.QPointF(0,0))
self.connect(item, QtCore.SIGNAL('rect_hit'), \
self.append_clipboard)
self.connect(item, QtCore.SIGNAL('href_hit'), \
self.follow_link)
scene.addItem(item)
# cursor item
scene.addCursor(item)
# filter to intercept Tab
tk_filter = tabkeyFilter(self)
scene.installEventFilter(tk_filter)
self.connect(tk_filter, QtCore.SIGNAL('tab_key_hit'), \
self.tabkey_hit_callback)
# add to scenes array
self.scenes[file_name] = scene
# look up
scene = self.scenes[file_name]
# show
self.setScene(scene)
except(IOError):
return None
return file_name
def open_but_callback(self):
file_name = self.file_open_dlg.getOpenFileName(self,\
'Open .svg file ...','','')
file_name = str(file_name)
self.show_scene(file_name)
def back_but_callback(self):
if len(self.scenes_history) > 1:
self.scenes_history.pop(-1)
self._show_scene(self.scenes_history[-1])
def reload_but_callback(self):
file_to_reload = self.scenes_history.pop(-1)
# the only thing to left is to wipe out from scenes
# before call to show again
del self.scenes[file_to_reload]
self.show_scene(file_to_reload)
def follow_link(self, href):
curr_path = (os.path.split(self.scenes_history[-1]))[0]
self.show_scene(os.path.abspath(curr_path + '/' + href))
def clear_but_callback(self):
self.lineedit.setText('')
self.set_clipboard('')
def ctrl_z_callback(self):
self.lineedit.undo()
def ctrl_y_callback(self):
self.lineedit.redo()
def tabkey_hit_callback(self):
self.lineedit.setFocus()
# called by lineedit which is not updated in the case
def set_clipboard(self, line2set):
if os.name == 'nt':
self.tk.clipboard_clear()
self.tk.clipboard_append(line2set)
elif os.name == 'posix':
outf = os.popen('xsel -i', 'w')
outf.write(line2set)
outf.close()
else:
raise RuntimeError('Unsupported os.name: %s' % os.name)
def append_clipboard(self, line2append):
self.lineedit.insert(line2append)
cur_pos = self.lineedit.cursorPosition()
self.lineedit.home(False)
self.lineedit.backspace()
self.lineedit.setCursorPosition(cur_pos)
ret_str = self.lineedit.displayText()
self.set_clipboard(ret_str)
def handleMessage(self, message):
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
self.showNormal()
self.show()
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_S:
self.back_but_callback()
elif e.key() == QtCore.Qt.Key_D:
self.reload_but_callback()
elif e.key() == QtCore.Qt.Key_F:
self.clear_but_callback()
else:
self.oldScene.keyCallback(e)
def main():
key = 'vstype_unique_key'
style = QtGui.QStyleFactory.create('Windows')
if not style:
return
QtGui.QApplication.setStyle(style)
app = SingleEntity(sys.argv, key)
if app.isRunning():
sys.exit(1)
# view/scene
view = app_view()
app.connect(app, QtCore.SIGNAL('messageAvailable'),
view.handleMessage)
view.setWindowIcon(QtGui.QIcon('resources/splash.png'))
view.setWindowTitle( 'VSType <<version>>')
view.show()
# bye
sys.exit(app.exec_())
if os.name == 'nt':
view.tk.destroy()
elif os.name != 'posix':
raise RuntimeError('Unsupported os.name: %s' % os.name)
if __name__ == '__main__':
main()