Normand Briere
2018-07-07 09ddd38fd4a8a7100c834a5e976f4796fae53541
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
package timeflow.vis.timeline;
 
import timeflow.vis.VisualAct;
import timeflow.data.time.*;
import timeflow.util.*;
 
import java.util.*;
 
public class TimelineTrack implements Comparable
{
 
        String label;
        List<VisualAct> visualActs = new ArrayList<VisualAct>();
        int y0, y1;
        DoubleBag<Long> histogram;
 
        TimelineTrack(String label)
        {
                this.label = label;
        }
 
        void add(VisualAct v)
        {
                visualActs.add(v);
        }
 
        int size()
        {
                return visualActs.size();
        }
 
        // assumes a>b>0
        int gcd(int a, int b)
        {
                int mod = a % b;
                if (mod == 0)
                {
                        return b;
                }
                return gcd(b, mod);
        }
 
        int nearAndRelPrime(int target, int modulus)
        {
                if (target < 2)
                {
                        return 1;
                }
                while (gcd(modulus, target) > 1)
                {
                        target--;
                }
                return target;
        }
 
        // top and height are in proportion of total height of frame.
        void layout(double top, double height, TimelineVisuals visuals)
        {
                int n = visualActs.size();
                if (n == 0)
                {
                        return;
                }
                
                int labelHeight = 0; // 80;
                int fh = visuals.getBounds().height - labelHeight;
                int fy = visuals.getBounds().y;
                int cellH = visuals.getModel().getDisplay().getInt("timeline.item.height.min");
 
                int fontHeight = 12;
                
                y0 = fy + (int) (fh * top);
                y1 = fy + (int) (fh * (top + height));
                //int mid = (y0 + y1) / 2;
                int iy0 = y0; // Math.min(y0 + 5, mid);
                int iy1 = y1; // Math.max(y1 - 15, mid);
 
                int numCells = Math.max(1, (iy1 - iy0) / cellH);
 
                VisualAct[] rights = new VisualAct[numCells];
 
                int step = nearAndRelPrime((int) (.61803399 * numCells), numCells);
                int i = 0;
                VisualAct last = null;
                for (VisualAct v : visualActs)
                {
                        if (!v.isVisible() || !v.getTrack().equals(this))
                        {
                                continue;
                        }
                        v.setSpaceToRight(1000);
 
                        double num = visuals.getTimeScale().toNum(v.getStart().getTime());
                        int x = (int) num;
 
                        int cell = numCells < 2 ? 0 : (i % numCells);
                        int y = iy0 + cell * cellH; // (iy1 - iy0 < 12 ? 0 : cell * cellH);
                        v.setX(x);
                        v.setY(y + fontHeight);
 
                        if (v.getEnd() != null)
                        {
                                v.setEndX((int) visuals.getTimeScale().toNum(v.getEnd().getTime()));
                        }
 
                        if (rights[cell] != null)
                        {
                                int space = x - rights[cell].getX();
                                rights[cell].setSpaceToRight(space);
                        }
                        rights[cell] = v;
                        if ((last != null && v.getStart().getTime() == last.getStart().getTime())
                                || visuals.getLayoutStyle() == TimelineVisuals.Layout.TIGHT)
                        {
                                i++;
                        } else
                        {
                                i += step;
                        }
                        last = v;
                }
        }
 
        @Override
        public int compareTo(Object o)
        {
                return ((TimelineTrack) o).size() - size();
        }
}