summaryrefslogtreecommitdiff
path: root/cesar/common/tools/traceviewer/trace_view.py
blob: ffe530271e5455189c17e2a590543ee0b2f7f84d (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
251
252
253
254
255
256
257
258
import pygtk
pygtk.require('2.0')
import gtk
import gobject

from trace import *

class TraceView (gtk.DrawingArea):

    trace_margin = 7
    trace_height = 10

    def __init__ (self):
        gtk.DrawingArea.__init__ (self)
        self.connect ('expose-event', self.expose)
        self.connect_after ('realize', self.realize_after)
        self.connect ('button-press-event', self.button_press)
        self.add_events (gtk.gdk.BUTTON_PRESS_MASK)
        self.traces = [ ]
        self.visible_traces = 0
        self.zoom = 1
        self.base = 0
        self.end = 0
        self.cursor = None
        self.cursor_user = [ None, None ]
        self.cursor_aux = None
        self.highlighted = None

    def add (self, trace):
        self.traces.append (trace)
        trace.visible = True
        self.visible_traces += 1
        self.__traces_update ()

    def remove (self, trace):
        self.traces.remove (trace)
        del trace.visible
        self.visible_traces -= 1
        self.__traces_update ()

    def show (self, trace):
        if not trace.visible:
            trace.visible = True
            self.visible_traces += 1
            self.__traces_update ()

    def hide (self, trace):
        if trace.visible:
            trace.visible = False
            self.visible_traces -= 1
            self.__traces_update ()

    def __traces_update (self):
        min = None
        max = None
        # Find trace range.
        for t in self.traces:
            if t.visible:
                rmin = t.trace[0].date
                rmax = t.trace[-1].date
                if rmin != rmax or rmin != 0:
                    if min is None or rmin < min:
                        min = rmin
                    if max is None or rmax > max:
                        max = rmax
        # Adapt display range.
        if min is None:
            min, max = 0, 0
        self.base = min
        self.end = max + 1
        extend = long ((self.end - self.base) * self.zoom)
        if extend > 100000:
            self.zoom = float (100000) / (self.end - self.base)
        self.__resize ()

    def __resize (self):
        self.set_size_request (
                long ((self.end - self.base) * self.zoom),
                self.visible_traces * (self.trace_margin + self.trace_height)
                + self.trace_margin)

    def realize_after (self, widget):
        self.color_a = { }
        self.attr_gc = { }
        self.modify_bg (gtk.STATE_NORMAL, self.color ('black'))
        self.trace_gc = self.attr ('white')
        self.highlight_gc = self.attr ('grey40')
        self.cursor_gc = self.attr ('red')
        self.cursor_user_gc = (self.attr ('green'), self.attr ('green,dash'))
        self.cursor_aux_gc = self.attr ('blue,dash')

    def color (self, color):
        if color not in self.color_a:
            self.color_a[color] = (self.window.get_colormap ()
                    .alloc_color (color))
        return self.color_a[color]

    def attr (self, attr):
        if attr not in self.attr_gc:
            s = attr.split (',')
            if len (s) == 1:
                fg, bg = s[0], None
            else:
                fg, bg = s
            fg = self.color (fg)
            if bg is None:
                gc = self.window.new_gc (foreground = fg)
            elif bg == 'dash':
                gc = self.window.new_gc (foreground = fg,
                        line_style = gtk.gdk.LINE_ON_OFF_DASH)
            else:
                bg = self.color (bg)
                gc = self.window.new_gc (foreground = fg, background = bg,
                        line_style = gtk.gdk.LINE_DOUBLE_DASH)
            self.attr_gc[attr] = gc
        return self.attr_gc[attr]

    def expose (self, widget, event):
        self.draw (event.area.x, event.area.y, event.area.width,
                event.area.height)
        return True

    def button_press (self, widget, event):
        date = self.coord_x_to_date (event.x)
        self.set_cursor (date)
        return True

    def set_cursor (self, date, aux = None):
        self.cursor = date
        self.cursor_aux = aux
        self.queue_draw ()
        self.emit ('cursor-tck', date)

    def set_cursor_user (self, n, date):
        self.cursor_user[n] = date
        self.queue_draw ()

    def highlight (self, trace = None):
        self.highlighted = trace
        self.queue_draw ()

    def draw (self, x, y, w, h):
        # Cursors user.
        for cursor, gc in zip (self.cursor_user, self.cursor_user_gc):
            if cursor is not None:
                cx = long ((cursor - self.base) * self.zoom)
                if cx >= x and cx < x + w:
                    self.window.draw_line (gc, cx, y, cx, y + h - 1)
        # Cursor.
        if self.cursor is not None:
            cx = long ((self.cursor - self.base) * self.zoom)
            if cx >= x and cx < x + w:
                self.window.draw_line (self.cursor_gc, cx, y, cx, y + h - 1)
        # Cursor aux.
        if self.cursor_aux is not None:
            for c in self.cursor_aux:
                cx = long ((c - self.base) * self.zoom)
                if cx >= x and cx < x + w:
                    self.window.draw_line (self.cursor_aux_gc, cx, y, cx,
                            y + h - 1)
        # For each trace.
        line = 0
        for t in self.traces:
            if t.visible:
                lyt = ((self.trace_margin + self.trace_height) * (line + 1)
                        - self.trace_height)
                lyb = (self.trace_margin + self.trace_height) * (line + 1)
                if not (y + h <= lyt or y > lyb):
                    self.draw_trace (t, x, w, lyb, lyt)
                line += 1

    def draw_trace (self, trace, x, w, lyb, lyt):
        xend = min (long ((self.end - self.base) * self.zoom), x + w) - 1
        if trace is self.highlighted:
            self.window.draw_rectangle (self.highlight_gc, True, x, lyt,
                    xend - x, lyb - lyt)
        self.window.draw_line (self.trace_gc, x, lyb, xend, lyb)
        # Draw a tick for each date corresponding to a trace event (avoid
        # duplicate drawing).
        miin = long (x // self.zoom) + self.base
        max = long (((x + w) + self.zoom) // self.zoom) + self.base
        last = None
        for i in xrange (*trace.range (miin, max)):
            d = long ((trace.trace[i].date - self.base) * self.zoom)
            # Draw if not drawn yet.
            if d != last:
                # Get optional view attribute and text.
                if hasattr (trace.trace[i], 'view_attr'):
                    gc = self.attr (trace.trace[i].view_attr)
                else:
                    gc = self.trace_gc
                if hasattr (trace.trace[i], 'view_text'):
                    text = trace.trace[i].view_text
                    text = self.create_pango_layout (text)
                else:
                    text = None
                # Draw tick.
                self.window.draw_line (gc, d, lyt, d, lyb)
                # Draw text if there is room.
                if text is not None:
                    if len (trace.trace) > i + 1:
                        dnext = long ((trace.trace[i + 1].date - self.base)
                                * self.zoom)
                    else:
                        dnext = None
                    if dnext is None or d + text.get_pixel_size ()[0] < dnext:
                        self.window.draw_layout (gc, d + 1, lyt - 1, text)
                last = d

    def zoom_fit (self, px):
        self.zoom = float (px) / (self.end - self.base)
        self.__resize ()

    def zoom_set (self, zoom):
        self.zoom = zoom
        self.__resize ()

    def date_to_coord_x (self, date):
        return long ((date - self.base) * self.zoom)

    def coord_x_to_date (self, date_x):
        return min (long (date_x // self.zoom) + self.base, self.end)

gobject.signal_new ('cursor-tck', TraceView, gobject.SIGNAL_RUN_LAST,
        gobject.TYPE_NONE, (gobject.TYPE_INT64, ))

class TraceTest:

    def __init__ (self):
        window = gtk.Window (gtk.WINDOW_TOPLEVEL)
        window.set_title ("TraceView test")
        window.connect ("destroy", lambda w: gtk.main_quit ())
        trace_view = TraceView ()
        trace_view.connect ('cursor-tck', self.cursor_tck)
        trace_view.zoom = 10
        trace = Trace ('test')
        tf = [
                '[.] first undated\r\n',
                '[.] second undated\r\n',
                '[0x00000011] first dated\r\n',
                '[0x00000022] second dated\r\n',
                '[.] third undated\r\n',
                '[0x00000033] third dated\r\n',
                '[0x00000033] third dated bis\r\n',
                ]
        trace.from_dump (tf)
        trace_view.add (trace)
        sw = gtk.ScrolledWindow ()
        sw.add_with_viewport (trace_view)
        window.add (sw)
        window.show_all ()

    def cursor_tck (self, trace_view, date):
        print "Cursor:", date

if __name__ == '__main__':
    tt = TraceTest ()
    gtk.main ()