-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleoDataModel.py
2233 lines (2080 loc) · 77.9 KB
/
leoDataModel.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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#@+leo-ver=5-thin
#@+node:vitalije.20180510153405.1: * @file leoDataModel.py
import random
from collections import defaultdict, namedtuple
import re
import os
import xml.etree.ElementTree as ET
import pickle
import time
import leo.core.leoGlobals as g
assert g
class bunch:
# pylint: disable=no-member
def __init__(self, **kw):
self.__dict__.update(kw)
#@+others
#@+node:vitalije.20180510153753.1: ** nthChild
def nthChild(ltm, pos, n):
'''Returns index of nth child of a given position'''
i = ltm.positions.index(pos)
return nthChildI(ltm, i, n)
def nthChildI(ltm, i, n):
'''Returns index of nth child of a given parent index'''
gnx = ltm.nodes[i]
h, b, ps, chn, sz = ltm.attrs[gnx]
if len(chn) <= n: return -1
i += 1
while n:
n -= 1
cgnx = ltm.nodes[i]
i += ltm.attrs[cgnx][4]
return i
#@+node:vitalije.20180510153747.1: ** parPosIter
def parPosIter(ps, levs):
'''Helper iterator for parent positions. Given sequence of
positions and corresponding levels it generates sequence
of parent positions'''
rootpos = ps[0]
levPars = [rootpos for i in range(256)] # max depth 255 levels
it = zip(ps, levs)
next(it) # skip first root node which has no parent
yield rootpos
for p, l in it:
levPars[l] = p
yield levPars[l - 1]
#@+node:vitalije.20180510153733.2: ** nodes2treemodel
def nodes2treemodel(nodes):
'''
Creates LeoTreeModel from the sequence of tuples
(gnx, h, b, level, size, parentGnxes, childrenGnxes)
0, 1, 2, 3, 4, 5, 6
'''
ltm = LeoTreeModel()
ltm.positions = [random.random() for x in nodes]
ltm.levels = [x[3] for x in nodes]
ltm.parPos = list(parPosIter(ltm.positions, ltm.levels))
ltm.nodes = [x[0] for x in nodes]
gnx2pos = defaultdict(list)
for pos, x in zip(ltm.positions, nodes):
gnx2pos[x[0]].append(pos)
ltm.gnx2pos = gnx2pos
for gnx, h, b, lev, sz, ps, chn in nodes:
ltm.attrs[gnx] = [h, b, ps, chn, sz[0]]
# root node must not have parents
rgnx = nodes[0][0]
ltm.attrs[rgnx][2] = []
return ltm
#@+node:vitalije.20180510153733.1: ** vnode2treemodel & viter
def vnode2treemodel(vnode):
'''Utility convertor: converts VNode instance into
LeoTreeModel instance'''
def viter(v, lev0):
s = [1]
mnode = (v.gnx, v.h, v.b, lev0, s,
[x.gnx for x in v.parents],
[x.gnx for x in v.children])
yield mnode
for ch in v.children:
for x in viter(ch, lev0 + 1):
s[0] += 1
yield x
return nodes2treemodel(tuple(viter(vnode, 0)))
#@+node:vitalije.20180510153733.3: ** xml2treemodel
def xml2treemodel(xvroot, troot):
'''Returns LeoTreeModel instance from vnodes and tnodes elements of xml Leo file'''
parDict = defaultdict(list)
hDict = {}
bDict = dict((ch.attrib['tx'], ch.text or '') for ch in troot.getchildren())
xDict = {}
#@+others
#@+node:vitalije.20180510153945.1: *3* xml_viter
def xml_viter(xv, lev0, dumpingClone=False):
s = [1]
gnx = xv.attrib['t']
if not xv: ### len(xv) == 0:
# clone
for ch in xml_viter(xDict[gnx], lev0, True):
yield ch
return
chs = [ch.attrib['t'] for ch in xv if ch.tag == 'v']
if not dumpingClone:
xDict[gnx] = xv
hDict[gnx] = xv[0].text
for ch in chs:
parDict[ch].append(gnx)
mnode = [gnx, hDict[gnx], bDict.get(gnx, ''), lev0, s, parDict[gnx], chs]
yield mnode
for ch in xv.getchildren():
if ch.tag != 'v':continue
for x in xml_viter(ch, lev0 + 1, dumpingClone):
s[0] += 1
yield x
#@+node:vitalije.20180510154050.1: *3* riter
def riter():
# EKR: Read iterator
s = [1]
# s: list of sizes
chs = []
# chs: list of child gnx's
yield 'hidden-root-vnode-gnx', '<hidden root vnode>','', 0, s, [], chs
for xv in xvroot.getchildren():
gnx = xv.attrib['t']
chs.append(gnx)
parDict[gnx].append('hidden-root-vnode-gnx')
# parDict: dict of parents of given gnx.
for ch in xml_viter(xv, 1):
s[0] += 1
yield ch
#@-others
nodes = tuple(riter())
return nodes2treemodel(nodes)
#@+node:vitalije.20180510153733.4: ** loadLeo
def loadLeo(fname):
'''Loads given xml Leo file and returns LeoTreeModel instance'''
# g.trace('=====',fname)
with open(fname, 'rt') as inp:
s = inp.read()
xroot = ET.fromstring(s)
vnodesEl = xroot.find('vnodes')
tnodesEl = xroot.find('tnodes')
ltm = xml2treemodel(vnodesEl, tnodesEl)
return ltm
#@+node:vitalije.20180518104947.1: ** loadExternalFiles
def loadExternalFiles(ltm, loaddir):
mpaths = paths(ltm, loaddir)
for gnx, ps in mpaths.items():
h = ltm.attrs[gnx][0]
if h.startswith('@file '):
ltm2 = ltm_from_derived_file(ps[0])
ltm.replaceNode(ltm2)
elif h.startswith('@auto ') and h.endswith('.py'):
ltm2 = auto_py(gnx, ps[0])
ltm2.attrs[gnx][0] = h
ltm.replaceNode(ltm2)
ltm.invalidate_visual()
#@+node:vitalije.20180518155338.1: ** loadLeoFull
def loadLeoFull(fname):
'''Loads both given xml Leo file and external files.
Returns LeoTreeModel instance'''
ltm = loadLeo(fname)
loaddir = os.path.dirname(fname)
loaddir = os.path.normpath(loaddir)
loaddir = os.path.abspath(loaddir)
loadExternalFiles(ltm, loaddir)
return ltm
#@+node:vitalije.20180518100350.1: ** paths
atFileNames = [
"@auto-rst", "@auto","@asis",
"@edit",
"@file-asis", "@file-thin", "@file-nosent", "@file",
"@clean", "@nosent",
"@shadow",
"@thin"
]
atFilePat = re.compile(r'^(%s)\s+(.+)$'%('|'.join(atFileNames)))
def paths(ltm, loaddir):
'''Returns dict keys are gnx of each file node,
and values are lists of absolute paths corresponding
to the node.'''
stack = [loaddir for x in range(255)]
res = defaultdict(list)
pat = re.compile(r'^@path\s+(.+)$', re.M)
cdir = loaddir
npath = lambda x: os.path.normpath(os.path.abspath(x))
jpath = lambda x:npath(os.path.join(cdir, x))
for p, gnx, lev in zip(ltm.positions, ltm.nodes, ltm.levels):
if lev == 0: continue
cdir = stack[lev - 1]
h, b = ltm.attrs[gnx][:2]
m = pat.search(h) or pat.search(b)
if m:
cdir = jpath(m.group(1))
stack[lev] = cdir
m = atFilePat.match(h)
if m:
res[gnx].append(jpath(m.group(2)))
return res
#@+node:vitalije.20180510153738.1: ** class LeoTreeModel
class LeoTreeModel(object):
'''Model representing all of Leo outline data.
warning: work in progress - still doesn't contain all Leo data
TODO: add support for unknownAttributes
add support for status bits
add support for gui view values
- body cursor position
- selected text'''
def __init__(self):
self.pickled_vars = (
# for self.tobytes...
'positions', 'nodes', 'attrs',
'levels', 'gnx2pos', 'parPos',
'expanded', 'marked', 'selectedPosition',
)
self.positions = []
self.nodes = []
self.attrs = {}
self.levels = []
self.parPos = []
self.expanded = set()
self.marked = set()
self.selectedPosition = None
self.gnx2pos = defaultdict(list)
#
self._visible_positions_serial = 0
self._visible_positions_last = -1
self._visible_positions = tuple()
def ivars(self):
# Note: toBytes pickles exactly these ivars.
return (self.positions,
self.nodes,
self.attrs,
self.levels,
self.gnx2pos,
self.parPos,
self.expanded,
self.marked,
self.selectedPosition)
#@+others
#@+node:vitalije.20180510153738.2: *3* parents
def parents(self, gnx):
'''Returns list of gnxes of parents of node with given gnx'''
a = self.attrs.get(gnx)
return a[2] if a else []
#@+node:vitalije.20180510153738.3: *3* children
def children(self, gnx):
'''Returns list of gnxes of children of the node with given gnx'''
a = self.attrs.get(gnx)
return a[3] if a else []
#@+node:vitalije.20180516103839.1: *3* selectedIndex
@property
def selectedIndex(self):
try:
i = self.positions.index(self.selectedPosition)
except ValueError:
i = -1
return i
#@+node:vitalije.20180516160109.1: *3* insertLeaf (changed)
def insertLeaf(self, pos, gnx, h, b):
i = self.positions.index(pos)
return self.insertLeafI(i, gnx, h, b)
def insertLeafI(self, i, ignx, h, b):
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.positions
#
pp = parPos[i]
pi = positions.index(pp)
pgnx = nodes[pi]
lev = levels[pi] + 1
levels[i:i] = [lev]
positions[i:i] = [random.random()]
parPos[i:i] = [pp]
nodes[i:i] = [ignx]
gnx2pos[ignx].append(positions[i])
if ignx in attrs:
attrs[ignx][2].append(pgnx)
else:
attrs[ignx] = [h, b, [pgnx], [], 1]
def chiter(j):
while levels[j] == lev:
gnx = self.nodes[j]
yield gnx
j += attrs[gnx][4]
if j >= len(levels): break
attrs[pgnx][3] = list(chiter(pi + 1))
def updateSize(x):
attrs[x][4] += 1
for px in attrs[x][2]:
updateSize(px)
updateSize(pgnx)
return positions[i]
#@+node:vitalije.20180510153738.4: *3* insertTree
def insertTree(self, parent_gnx, index, t2):
'''Inserts subtree in this outline as a child of node whose
gnx is parent_gnx at given index'''
gnx = t2.nodes[0]
h, b = t2.attrs[gnx][:2]
for pi in self.gnx2pos[parent_gnx]:
i = nthChild(self, pi, index)
self.insertLeafI(i, gnx, h, b)
self.replaceNode(t2)
#@+node:vitalije.20180510194736.1: *3* replaceNode & updateParentSize
def replaceNode(self, t2):
'''Replaces node with given subtree. This outline must contain
node with the same gnx as root gnx of t2.'''
t1 = self
gnx = t2.nodes[0]
sz0 = t1.attrs[gnx][4]
# this function replaces one instance of given node
def insOne(i):
l0 = t1.levels[i]
npos = [random.random() for x in t2.nodes]
npos[0] = t1.positions[i]
ppos = t1.parPos[i]
t1.parPos[i:i+sz0] = list(parPosIter(npos, t2.levels))
t1.parPos[i] = ppos
t1.positions[i:i+sz0] = npos
t1.nodes[i:i+sz0] = t2.nodes
t1.levels[i:i+sz0] = [(l0 + x) for x in t2.levels]
for p, gnx in zip(npos[1:], t2.nodes[1:]):
t1.gnx2pos[gnx].append(p)
# difference in sizes between old node and new node
dsz = len(t2.positions) - sz0
# parents of this node must be preserved
t2.attrs[gnx][2] = t1.parents(gnx)
for pi in t1.gnx2pos[gnx]:
i = t1.positions.index(pi)
insOne(i)
# some of nodes in t2 may be clones of nodes in t1
# they will have some parents that are outside t2
# therefore it is necessary for these nodes in t2 to
# update their parents list by adding only those parents
# that are not part of t2.
t2gnx = set(t2.nodes)
for x in t2gnx:
if x not in t1.attrs:continue
if x is t2.nodes[0]:continue
ps = t1.attrs[x][2]
t2.attrs[x][2].extend([y for y in ps if y not in t2gnx])
# now we can safely update attrs dict of t1
t1.attrs.update(t2.attrs)
# one last task is to update size in all ancestors of replaced node
def updateParentSize(gnx):
for pgnx in t1.parents(gnx):
t1.attrs[pgnx][4] += dsz
updateParentSize(pgnx)
updateParentSize(gnx)
#@+node:vitalije.20180510153738.5: *3* deleteNode (changed)
def deleteNode(self, pos):
g.trace('*****')
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.positions
#
i = positions.index(pos)
sz = attrs[nodes[i]][4]
# this function removes deleted positions
# from list of positions for a single node
# when this list becomes empty, it removes
# also entry for this node in attrs dict
def removeOne(i):
gnx = nodes[i]
xs = gnx2pos[gnx]
xs.remove(positions[i])
if not xs:
gnx2pos.pop(gnx)
# reduce size of parent and returns its index
def updateParentSize(i):
pp = parPos[i]
j = positions.index(pp)
attrs[nodes[j]][4] -= sz
return j
# remove all nodes in subtree
for j in range(i, i + sz):
removeOne(j)
# now reduce sizes of all ancestors
j = i
while j > 0:
j = updateParentSize(j)
pp = parPos[i]
pi = positions.index(pp)
attrs[nodes[i]][2].remove(nodes[pi])
N = attrs[nodes[pi]][4] + pi
def chiter():
j = pi + 1
while j < N:
gnx = nodes[j]
yield gnx
j += attrs[gnx][4]
# finally delete all entries from data
del nodes[i:i+sz]
del positions[i:i+sz]
del parPos[i:i+sz]
del levels[i:i+sz]
attrs[nodes[pi]][3] = list(chiter())
#@+node:vitalije.20180515122209.1: *3* display_items
def display_items(self, skip=0, count=None):
'''
A generator yielding tuples for visible, non-skipped items:
(pos, gnx, h, levels[i], plusMinusIcon, iconVal, selInd == i)
'''
# g.trace('skip', skip, 'count', count)
Npos = len(self.positions)
if count is None:
count = Npos
( positions, nodes, attrs, levels, gnx2pos, parPos,
expanded, marked, selPos) = self.ivars()
#
selInd = self.selectedIndex
i = 1
while count > 0 and i < Npos:
gnx = nodes[i]
pos = positions[i]
h, b, parents, hasChildren, treeSize = attrs[gnx]
exp = (pos in expanded)
if skip > 0:
skip -= 1
else:
# There is one less line to be drawn.
count -= 1
# Compute the iconVal for the icon box.
iconVal = 1 if b else 0
iconVal += 2 if gnx in marked else 0
iconVal += 4 if len(parents) > 1 else 0
# Compute the +- icon if the node has children.
if hasChildren:
plusMinusIcon = 'minus' if exp else 'plus'
else:
plusMinusIcon = 'none'
# Yield a tuple describing the line to be drawn.
yield pos, gnx, h, levels[i], plusMinusIcon, iconVal, selInd == i
if hasChildren and exp:
i += 1
else:
i += treeSize
#@+node:vitalije.20180515155021.1: *3* select_next_node
def select_next_node(self, ev=None):
i = self.selectedIndex
if i < 0: i = 1
if self.selectedPosition in self.expanded:
i += 1
else:
i += self.attrs[self.nodes[i]][4]
if i < len(self.positions):
self.selectedPosition = self.positions[i]
return self.nodes[i]
#@+node:vitalije.20180515155026.1: *3* select_prev_node
def select_prev_node(self, ev=None):
i = self.selectedIndex
if i < 0: i = 1
j = j0 = 1
while j < i:
j0 = j
if self.positions[j] in self.expanded:
j += 1
else:
j += self.attrs[self.nodes[j]][4]
self.selectedPosition = self.positions[j0]
return self.nodes[j0]
#@+node:vitalije.20180516103325.1: *3* select_node_left
def select_node_left(self, ev=None):
'''If currently selected node is collapsed or has no
children selects parent node. If it is expanded and
has children collapses selected node'''
if self.selectedPosition in self.expanded:
self.expanded.remove(self.selectedPosition)
self.invalidate_visual()
return
i = self.selectedIndex
if i < 2:return
p = self.parPos[i]
if p == self.positions[0]:
# this is top level node
# let's find previous top level (level=1) node.
j = next(x for x in range(i - 1, 0, -1) if self.levels[x] == 1)
p = self.positions[j]
self.selectedPosition = p
return self.nodes[self.positions.index(p)]
#@+node:vitalije.20180516105003.1: *3* select_node_right
def select_node_right(self, ev=None):
'''If currently selected node is collapsed, expands it.
In any case selects next node.'''
i = self.selectedIndex
# This is a property: self.positions.index(self.selectedPosition)
if -1 < i < len(self.nodes) - 1:
hasChildren = self.levels[i] < self.levels[i + 1]
p = self.selectedPosition
if hasChildren and p not in self.expanded:
self.expanded.add(p)
self.invalidate_visual()
self.selectedPosition = self.positions[i + 1]
return self.nodes[i + 1]
#@+node:vitalije.20180518124047.1: *3* visible_positions
@property
def visible_positions(self):
if self._visible_positions_serial != self._visible_positions_last:
return self.refresh_visible_positions()
return self._visible_positions
def invalidate_visual(self):
self._visible_positions_serial += 1
def refresh_visible_positions(self):
self._visible_positions_last = self._visible_positions_serial
def refreshiter():
attrs = self.attrs
nodes = self.nodes
positions = self.positions
expanded = self.expanded
j = 1
N = len(positions)
while j < N:
p1 = positions[j]
yield p1
if p1 in expanded:
j += 1
else:
j += attrs[nodes[j]][4]
self._visible_positions = tuple(refreshiter())
return self._visible_positions
#@+node:vitalije.20180516150626.1: *3* subtree (changed)
def subtree(self, pos):
g.trace('*****')
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
expanded = self.expanded
levels = self.levels
marked = self.marked
nodes = self.nodes
parPos = self.parPos
positions = self.positions
#
i = positions.index(pos)
gnx = nodes[i]
sz = attrs[gnx][4]
t = LeoTreeModel()
t.positions = positions[i:i+sz]
lev0 = levels[i]
t.levels = [x - lev0 for x in levels[i:i+sz]]
t.nodes = nodes[i:i+sz]
t.parPos = parPos[i:i+sz]
t.parPos[0] = 0
for p, x in zip(t.positions, t.nodes):
t.gnx2pos[x].append(p)
if p in expanded:
t.expanded.add(p)
if p in marked:
t.marked.add(p)
knownGnx = set(t.nodes)
for x in t.nodes:
h, b, ps, chn, sz = attrs[x]
ps = [y for y in ps if y in knownGnx]
t.attrs[x] = [h, b, ps, chn[:], sz]
return t
#@+node:vitalije.20180516132431.1: *3* promote (changed)
def promote(self, pos):
'''Makes following siblings of pos, children of pos'''
g.trace('*****')
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
expanded = self.expanded
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.positions
#
# node at position pos
i = positions.index(pos)
gnx = nodes[i]
lev0 = levels[i]
# parent node
pp = parPos[i]
pi = positions.index(pp)
pgnx = nodes[pi]
psz = attrs[pgnx][4]
# index of node after tree
after = pi + psz
# remember originial size
oldsize = attrs[gnx][4]
A = i + oldsize
# check danger clones
if gnx in nodes[A:after]:
print('warning node can not be in its own subtree')
return
# adjust size of this node
attrs[gnx][4] = oldsize + after - A
#@+others
#@+node:vitalije.20180517160150.1: *4* 1. promote this part of outline
j = A # iterator of following siblings
while j < after:
cgnx = nodes[j]
parPos[j] = pos
h, b, ps, chn, sz = attrs[cgnx]
# let's replace pgnx with gnx in parents
ps[ps.index(pgnx)] = gnx
# append to children of this node
attrs[gnx][3].append(cgnx)
# remove from children of pgnx
attrs[pgnx][3].remove(cgnx)
# next sibling
j += sz
levels[A:after] = [x + 1 for x in levels[A:after]]
#@+node:vitalije.20180517160237.1: *4* 2. update clones of this node in outline
# now we have already made all following siblings
# children of this node (gnx at position pos)
# we need to insert the same nodes to
# outline after each clone in the outline
allpos = [x for x in gnx2pos[gnx] if x != pos]
if not allpos: return expanded.add(pos)
# prepare data for insertion
sibgnx = nodes[A:after]
siblev = [x-lev0 for x in levels[A:after]]
# for parPosIter we need level 0
levs = [0] + siblev
while allpos:
ipos = allpos.pop()
ii = positions.index(ipos)
# old index of after tree
jj = ii + oldsize
# insert in nodes
nodes[jj:jj] = sibgnx
# insert levels adjusted by level of this clone
lev1 = levels[ii]
levels[jj:jj] = [x + lev1 for x in siblev]
# we need new positions for inserted nodes
npos = [random.random() for x in siblev]
positions[jj:jj] = npos
# insert parPos
npos.insert(0, ipos)
ppos = list(parPosIter(npos, levs))
parPos[jj:jj] = ppos[1:]
# update gnx2pos for each new position
for p1,x in zip(npos[1:], sibgnx):
gnx2pos[x].append(p1)
#@+node:vitalije.20180517160615.1: *4* 3. update sizes in outline
def updateSize(x):
d = attrs[x]
d[4] += after - A
for x1 in d[2]:
updateSize(x1)
allparents = attrs[gnx][2][:]
allparents.remove(pgnx) # for one of them we have already updated size
for x in allparents:
updateSize(x)
#@-others
# finally to show indented nodes
expanded.add(pos)
#@+node:vitalije.20180516141710.1: *3* promote_children (changed)
def promote_children(self, pos):
'''Turns children to siblings of pos'''
g.trace('*****')
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.positions
# this node
i = positions.index(pos)
gnx = nodes[i]
# parent node
pp = parPos[i]
pi = positions.index(pp)
pgnx = nodes[pi]
pchn = attrs[pgnx][3]
h, b, mps, chn, sz0 = attrs[gnx]
# node after
B = i + sz0
#@+others
#@+node:vitalije.20180517171517.1: *4* 1. reduce levels
# reduce levels
levels[i+1:B] = [x - 1 for x in levels[i + 1:B]]
#@+node:vitalije.20180517171551.1: *4* 2. iterate over direct children
j = i + 1
while j < B:
cgnx = nodes[j]
h, b, ps, gchn, sz = attrs[cgnx]
# replace parent with grandparent
ps[ps.index(gnx)] = pgnx
# remove from children of this node
chn.remove(cgnx)
# append to grandparent's children
pchn.append(cgnx)
# adjust parPos
parPos[j] = pp
j += sz # next child
#@+node:vitalije.20180517171628.1: *4* 3. set size to 1
attrs[gnx][4] = 1
#@+node:vitalije.20180517171705.1: *4* 4. process standalone clones of this node
if len(mps) == 1:
# there is no standalone clones
return
def updateSize(x):
d = attrs[x]
d[4] = d[4] - sz0 + 1
for x1 in d[2]:
updateSize(x1)
for px in gnx2pos[gnx]:
i1 = positions.index(px)
if px != pos:
# this is another clone
for j in range(i1 + 1, i1 + sz0):
cgnx = nodes[j]
gnx2pos[cgnx].remove(positions[j])
pi1 = positions.index(parPos[i1])
updateSize(nodes[pi1])
del positions[i1+1:i1+sz0]
del levels[i1+1:i1+sz0]
del nodes[i1+1:i1+sz0]
del parPos[i1+1:i1+sz0]
#@-others
#@+node:vitalije.20180517172602.1: *3* indent_node (changed)
def indent_node(self, pos):
'''Moves right node at position pos'''
g.trace('*****')
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
expanded = self.expanded
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.positions
# this node
i = positions.index(pos)
if levels[i-1] == levels[i] - 1:
# if there is no previous siblings node
# can't be moved right
return
gnx = nodes[i]
lev0 = levels[i]
# parent node
pp = parPos[i]
pi = positions.index(pp)
pgnx = nodes[pi]
h, b, mps, chn, sz0 = attrs[gnx]
# node after
B = i + sz0
j = A = pi + 1
while j < i:
A = j
j += attrs[nodes[j]][4]
# in A is index of new parent
npgnx = nodes[A]
if npgnx == gnx:
print('Warning can not move node to its own subtree')
return
#@+others
#@+node:vitalije.20180517172756.1: *4* 1. increase levels
levels[i:B] = [x+1 for x in levels[i:B]]
#@+node:vitalije.20180517172946.1: *4* 2. link to new parent
mps[mps.index(pgnx)] = npgnx
parPos[i] = positions[A]
# remove from children of old parent
attrs[pgnx][3].remove(gnx)
# add to new parent's children
attrs[npgnx][3].append(gnx)
#@+node:vitalije.20180517173423.1: *4* 3. process clones of new parent
#@+node:vitalije.20180517181545.1: *5* 3.1 update sizes
oldSize = attrs[npgnx][4]
def updateSize(x):
d = attrs[x]
d[4] += sz0
for x1 in d[2]:
updateSize(x1)
attrs[npgnx][4] += sz0
xx = attrs[npgnx][2][:]
xx.remove(pgnx)
for x in xx:
updateSize(x)
#@+node:vitalije.20180517181729.1: *5* 3.2 prepare data for insertion
pxA = positions[A]
levs = [x-lev0 for x in levels[i:B]]
nnds = nodes[i:B]
#@+node:vitalije.20180517181737.1: *5* 3.3 insert data
for px in gnx2pos[npgnx]:
if px == pxA: continue
i1 = positions.index(px)
jj = i1 + oldSize
lev1 = levels[i1]
levels[jj:jj] = [x + lev1 for x in levs]
npos = [random.random() for x in levs]
positions[jj:jj] = npos
parPos[jj:jj] = list(parPosIter([px] + npos, [0] + levs))[1:]
nodes[jj:jj] = nnds
for p, x in zip(npos, nnds):
gnx2pos[x].append(p)
#@-others
# finally let's make node visible
expanded.add(positions[A])
#@+node:vitalije.20180517183334.1: *3* dedent_node (changed)
def dedent_node(self, pos):
'''Moves node left'''
g.trace('*****')
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
attrs = self.attrs
gnx2pos = self.gnx2pos
levels = self.levels
nodes = self.nodes
parPos = self.parPos
positions = self.positions
# this node
i = positions.index(pos)
if levels[i] == 1:
# can't move left
return
gnx = nodes[i]
# parent node
pp = parPos[i]
pi = positions.index(pp)
pgnx = nodes[pi]
psz = attrs[pgnx][4]
h, b, mps, chn, sz0 = attrs[gnx]
# grandparent node
gpp = parPos[pi]
gpi = positions.index(gpp)
gpgnx = nodes[gpi]
di0 = i - gpi
di1 = di0 + sz0
di2 = pi - gpi
di3 = di2 + psz
# replace parent with grandparent
mps[mps.index(pgnx)] = gpgnx
def chiter(a, b, skipIndex):
while a < b:
gnx = nodes[a]
if a != skipIndex:
yield gnx
a += attrs[gnx][4]
attrs[pgnx][3] = list(chiter(pi + 1, pi + psz, i))
attrs[pgnx][4] -= sz0
def movedata(j, ar):
ar[j+di0: j+di3] = ar[j+di1:j+di3] + ar[j+di0:j+di1]
for px in gnx2pos[pgnx]:
pxi = positions.index(px)
gxi = pxi - di2
if gxi >= 0 and nodes[gxi] == gpgnx:
# just move data if necessary
if di1 != di3:
movedata(gxi, positions)
movedata(gxi, parPos)
movedata(gxi, nodes)
movedata(gxi, levels)
levels[gxi+di3-sz0:gxi+di3] = [x - 1 for x in levels[gxi+di3-sz0:gxi+di3]]
parPos[gxi+di3-sz0] = positions[gxi]
else:
# delete moved node
j = pxi + di0 - di2
k = j + sz0
for xi in range(j, k):
gnx2pos[nodes[xi]].remove(positions[xi])
del positions[j:k]
del levels[j:k]
del nodes[j:k]
del parPos[j:k]
def updateSize(x):
attrs[x][4] -= sz0
for x1 in attrs[x][2]:
updateSize(x1)
xx = attrs[pgnx][2][:]
xx.remove(gpgnx)
for x in xx:
updateSize(x)
B = attrs[gpgnx][4] + gpi
attrs[gpgnx][3] = list(chiter(gpi+1, B, 0))
#@+node:vitalije.20180518062711.1: *3* prev_visible_index (changed)
def prev_visible_index(self, pos):
'''Assuming this node is visible, search for previous
visible node.'''
# ( positions, nodes, attrs, levels, gnx2pos, parPos,
# expanded, marked, selPos) = self.ivars()
# this node
i = self.positions.index(pos)
# parent node
pp = self.parPos[i]
pi = self.positions.index(pp)
j = pi + 1
A = pi
while j < i:
A = j
if self.positions[j] in self.expanded:
j += 1
else:
j += self.attrs[self.nodes[j]][4]
return A