utils/grid.py
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 13 Mar 2008 12:56:49 -0700
changeset 2602 d9262bff6df2
parent 222 8954863e5047
permissions -rw-r--r--
add back support for introspected doxygen.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     1
#!/usr/bin/env python
124
c0d0f7bebb73 add magic emacs incantation for python
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 12
diff changeset
     2
## -*- Mode: python; py-indent-offset: 4; indent-tabs-mode: nil; coding: utf-8; -*-
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     3
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     4
import cairo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     5
import sys
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     6
import re
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     7
import gtk
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     8
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
     9
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    11
class DataRange:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    12
    def __init__(self, start = 0, end = 0, value = ''):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    13
        self.start = start
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    14
        self.end = end
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    15
        self.value = value
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    16
class EventString:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    17
    def __init__(self, at = 0, value = ''):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    18
        self.at = at
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    19
        self.value = value
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    20
class EventFloat:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    21
    def __init__(self, at = 0, value = 0.0):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    22
        self.at = at
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    23
        self.value = value
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    24
class EventInt:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    25
    def __init__(self, at = 0, value = 0.0):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    26
        self.at = at
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    27
        self.value = value
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    28
def ranges_cmp(a, b):
222
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    29
    diff = a.start - b.start
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    30
    if diff < 0:
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    31
        return -1
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    32
    elif diff > 0:
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    33
        return +1
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    34
    else:
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    35
        return 0
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    36
def events_cmp(a, b):
222
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    37
    diff = a.at - b.at
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    38
    if diff < 0:
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    39
        return -1
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    40
    elif diff > 0:
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    41
        return +1
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    42
    else:
8954863e5047 handle correctly nanosecond input
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 136
diff changeset
    43
        return 0
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    44
class TimelineDataRange:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    45
    def __init__(self, name = ''):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    46
        self.name = name
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    47
        self.ranges = []
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    48
        return
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    49
    def __search(self, key):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    50
        l = 0
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    51
        u = len(self.ranges)-1
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    52
        while l <= u:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    53
            i = int((l + u) / 2)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    54
            if key >= self.ranges[i].start and key <= self.ranges[i].end:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    55
                return i
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    56
            elif key < self.ranges[i].start:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    57
                u = i - 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    58
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    59
                # key > self.ranges[i].end
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    60
                l = i + 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    61
        return - 1
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    62
    def add_range(self, range):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    63
        self.ranges.append(range)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    64
    def get_all(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    65
        return self.ranges
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    66
    def get_ranges(self, start, end):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    67
        s = self.__search(start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    68
        e = self.__search(end)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    69
        if s == -1 and e == -1:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    70
            return []
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    71
        elif s == -1:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    72
            return self.ranges[0:e + 1]
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    73
        elif e == -1:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    74
            return self.ranges[s:len(self.ranges)]
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    75
        else:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    76
            return self.ranges[s:e + 1]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    77
    def get_ranges_bounds(self, start, end):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    78
        s = self.__search(start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    79
        e = self.__search(end)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    80
        if s == -1 and e == -1:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    81
            return(0, 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    82
        elif s == -1:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    83
            return(0, e + 1)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    84
        elif e == -1:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    85
            return(s, len(self.ranges))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    86
        else:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    87
            return(s, e + 1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    88
    def sort(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    89
        self.ranges.sort(ranges_cmp)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    90
    def get_bounds(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    91
        if len(self.ranges) > 0:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    92
            lo = self.ranges[0].start
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    93
            hi = self.ranges[len(self.ranges)-1].end
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    94
            return(lo, hi)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    95
        else:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    96
            return(0, 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    97
class TimelineEvent:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
    98
    def __init__(self, name = ''):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
    99
        self.name = name
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   100
        self.events = []
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   101
    def __search(self, key):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   102
        l = 0
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   103
        u = len(self.events)-1
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   104
        while l <= u:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   105
            i = int((l + u) / 2)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   106
            if key == self.events[i].at:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   107
                return i
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   108
            elif key < self.events[i].at:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   109
                u = i - 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   110
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   111
                # key > self.events[i].at
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   112
                l = i + 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   113
        return l
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   114
    def add_event(self, event):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   115
        self.events.append(event)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   116
    def get_events(self, start, end):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   117
        s = self.__search(start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   118
        e = self.__search(end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   119
        return self.events[s:e + 1]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   120
    def get_events_bounds(self, start, end):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   121
        s = self.__search(start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   122
        e = self.__search(end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   123
        return(s, e + 1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   124
    def sort(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   125
        self.events.sort(events_cmp)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   126
    def get_bounds(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   127
        if len(self.events) > 0:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   128
            lo = self.events[0].at
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   129
            hi = self.events[-1].at
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   130
            return(lo, hi)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   131
        else:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   132
            return(0, 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   133
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   134
class Timeline:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   135
    def __init__(self, name = ''):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   136
        self.ranges = []
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   137
        self.event_str = []
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   138
        self.event_int = []
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   139
        self.name = name
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   140
    def get_range(self, name):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   141
        for range in self.ranges:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   142
            if range.name == name:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   143
                return range
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   144
        timeline = TimelineDataRange(name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   145
        self.ranges.append(timeline)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   146
        return timeline
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   147
    def get_event_str(self, name):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   148
        for event_str in self.event_str:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   149
            if event_str.name == name:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   150
                return event_str
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   151
        timeline = TimelineEvent(name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   152
        self.event_str.append(timeline)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   153
        return timeline
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   154
    def get_event_int(self, name):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   155
        for event_int in self.event_int:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   156
            if event_int.name == name:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   157
                return event_int
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   158
        timeline = TimelineEvent(name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   159
        self.event_int.append(timeline)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   160
        return timeline
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   161
    def get_ranges(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   162
        return self.ranges
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   163
    def get_events_str(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   164
        return self.event_str
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   165
    def get_events_int(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   166
        return self.event_int
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   167
    def sort(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   168
        for range in self.ranges:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   169
            range.sort()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   170
        for event in self.event_int:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   171
            event.sort()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   172
        for event in self.event_str:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   173
            event.sort()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   174
    def get_bounds(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   175
        lo = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   176
        hi = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   177
        for range in self.ranges:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   178
            (range_lo, range_hi) = range.get_bounds()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   179
            if range_lo < lo:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   180
                lo = range_lo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   181
            if range_hi > hi:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   182
                hi = range_hi
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   183
        for event_str in self.event_str:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   184
            (ev_lo, ev_hi) = event_str.get_bounds()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   185
            if ev_lo < lo:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   186
                lo = ev_lo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   187
            if ev_hi > hi:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   188
                hi = ev_hi
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   189
        for event_int in self.event_int:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   190
            (ev_lo, ev_hi) = event_int.get_bounds()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   191
            if ev_lo < lo:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   192
                lo = ev_lo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   193
            if ev_hi > hi:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   194
                hi = ev_hi
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   195
        return(lo, hi)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   196
class Timelines:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   197
    def __init__(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   198
        self.timelines = []
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   199
    def get(self, name):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   200
        for timeline in self.timelines:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   201
            if timeline.name == name:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   202
                return timeline
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   203
        timeline = Timeline(name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   204
        self.timelines.append(timeline)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   205
        return timeline
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   206
    def get_all(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   207
        return self.timelines
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   208
    def sort(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   209
        for timeline in self.timelines:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   210
            timeline.sort()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   211
    def get_bounds(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   212
        lo = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   213
        hi = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   214
        for timeline in self.timelines:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   215
            (t_lo, t_hi) = timeline.get_bounds()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   216
            if t_lo < lo:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   217
                lo = t_lo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   218
            if t_hi > hi:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   219
                hi = t_hi
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   220
        return(lo, hi)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   221
    def get_all_range_values(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   222
        range_values = {}
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   223
        for timeline in self.timelines:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   224
            for ranges in timeline.get_ranges():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   225
                for ran in ranges.get_all():
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   226
                    range_values[ran.value] = 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   227
        return range_values.keys()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   228
class Color:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   229
    def __init__(self, r = 0.0, g = 0.0, b = 0.0):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   230
        self.r = r
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   231
        self.g = g
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   232
        self.b = b
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   233
    def set(self, r, g, b):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   234
        self.r = r
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   235
        self.g = g
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   236
        self.b = b
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   237
class Colors:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   238
    # XXX add more
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   239
    default_colors = [Color(1, 0, 0), Color(0, 1, 0), Color(0, 0, 1), Color(1, 1, 0), Color(1, 0, 1), Color(0, 1, 1)]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   240
    def __init__(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   241
        self.__colors = {}
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   242
    def add(self, name, color):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   243
        self.__colors[name] = color
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   244
    def lookup(self, name):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   245
        if not self.__colors.has_key(name):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   246
            self.add(name, self.default_colors.pop())
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   247
        return self.__colors.get(name)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   248
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   249
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   250
class TopLegendRenderer:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   251
    def __init__(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   252
        self.__padding = 10
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   253
    def set_padding(self, padding):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   254
        self.__padding = padding
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   255
    def set_legends(self, legends, colors):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   256
        self.__legends = legends
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   257
        self.__colors = colors
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   258
    def layout(self, width):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   259
        self.__width = width
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   260
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   261
        ctx = cairo.Context(surface)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   262
        line_height = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   263
        total_height = self.__padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   264
        line_used = self.__padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   265
        for legend in self.__legends:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   266
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   267
            item_width = self.__padding + self.__padding + t_width + self.__padding
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   268
            item_height = t_height + self.__padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   269
            if item_height > line_height:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   270
                line_height = item_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   271
            if line_used + item_width > self.__width:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   272
                line_used = self.__padding + item_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   273
                total_height += line_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   274
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   275
                line_used += item_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   276
            x = line_used - item_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   277
        total_height += line_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   278
        self.__height = total_height
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   279
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   280
    def get_height(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   281
        return self.__height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   282
    def draw(self, ctx):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   283
        i = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   284
        line_height = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   285
        total_height = self.__padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   286
        line_used = self.__padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   287
        for legend in self.__legends:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   288
            (t_width, t_height) = ctx.text_extents(legend)[2:4]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   289
            item_width = self.__padding + self.__padding + t_width + self.__padding
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   290
            item_height = t_height + self.__padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   291
            if item_height > line_height:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   292
                line_height = item_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   293
            if line_used + item_width > self.__width:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   294
                line_used = self.__padding + item_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   295
                total_height += line_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   296
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   297
                line_used += item_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   298
            x = line_used - item_width
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   299
            ctx.rectangle(x, total_height, self.__padding, self.__padding)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   300
            ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   301
            ctx.set_line_width(2)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   302
            ctx.stroke_preserve()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   303
            ctx.set_source_rgb(self.__colors[i].r, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   304
                               self.__colors[i].g, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   305
                               self.__colors[i].b)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   306
            ctx.fill()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   307
            ctx.move_to(x + self.__padding*2, total_height + t_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   308
            ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   309
            ctx.show_text(legend)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   310
            i += 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   311
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   312
        return
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   313
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   314
class TimelinesRenderer:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   315
    def __init__(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   316
        self.padding = 10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   317
        return
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   318
    def get_height(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   319
        return self.height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   320
    def set_timelines(self, timelines, colors):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   321
        self.timelines = timelines
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   322
        self.colors = colors
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   323
    def set_render_range(self, start, end):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   324
        self.start = start
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   325
        self.end = end
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   326
    def get_data_x_start(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   327
        return self.padding / 2 + self.left_width + self.padding + self.right_width + self.padding / 2
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   328
    def layout(self, width):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   329
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   330
        ctx = cairo.Context(surface)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   331
        max_text_height = ctx.text_extents("ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz0123456789")[3]
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   332
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   333
        left_width = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   334
        right_width = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   335
        left_n_lines = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   336
        range_n = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   337
        eventint_n = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   338
        eventstr_n = 0
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   339
        for timeline in self.timelines.get_all():
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   340
            left_n_lines += 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   341
            t_width = ctx.text_extents(timeline.name)[2]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   342
            left_width = max(left_width, t_width)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   343
            for rang in timeline.get_ranges():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   344
                t_width = ctx.text_extents(rang.name)[2]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   345
                right_width = max(right_width, t_width)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   346
                range_n += 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   347
            for events_int in timeline.get_events_int():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   348
                t_width = ctx.text_extents(events_int.name)[2]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   349
                right_width = max(right_width, t_width)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   350
                eventint_n += 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   351
            for events_str in timeline.get_events_str():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   352
                t_width = ctx.text_extents(events_str.name)[2]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   353
                right_width = max(right_width, t_width)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   354
                eventstr_n += 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   355
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   356
        left_height = left_n_lines * max_text_height + (left_n_lines - 1) * self.padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   357
        right_n_lines = range_n + eventint_n + eventstr_n
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   358
        right_height = (right_n_lines - 1) * self.padding + right_n_lines * max_text_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   359
        right_data_height = (eventint_n + eventstr_n) * (max_text_height + 5) + range_n * 10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   360
        right_data_height += (right_n_lines - 1) * self.padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   361
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   362
        height = max(left_height, right_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   363
        height = max(height, right_data_height)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   364
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   365
        self.left_width = left_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   366
        self.right_width = right_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   367
        self.max_text_height = max_text_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   368
        self.width = width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   369
        self.height = height + self.padding
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   370
    def draw_line(self, ctx, x, y, width, height):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   371
        ctx.move_to(x, y)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   372
        ctx.rel_line_to(width, height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   373
        ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   374
        ctx.set_operator(cairo.OPERATOR_SOURCE)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   375
        ctx.set_line_width(1.0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   376
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   377
        ctx.stroke()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   378
    def draw_events(self, ctx, events, x, y, width, height):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   379
        if (self.grey_background % 2) == 0:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   380
            ctx.rectangle(x, y - self.padding / 2, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   381
                          width, height + self.padding)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   382
            ctx.set_source_rgb(0.9, 0.9, 0.9)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   383
            ctx.fill()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   384
        last_x_drawn = int(x)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   385
        (lo, hi) = events.get_events_bounds(self.start, self.end)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   386
        for event in events.events[lo:hi]:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   387
            real_x = int(x + (event.at - self.start) * width / (self.end - self.start))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   388
            if real_x > last_x_drawn + 2:
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   389
                ctx.rectangle(real_x, y, 1, 1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   390
                ctx.set_source_rgb(1, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   391
                ctx.stroke()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   392
                ctx.move_to(real_x, y + self.max_text_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   393
                ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   394
                ctx.show_text(str(event.value))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   395
                last_x_drawn = real_x
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   396
        self.grey_background += 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   397
    def draw_ranges(self, ctx, ranges, x, y, width, height):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   398
        if (self.grey_background % 2) == 0:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   399
            ctx.rectangle(x, y - self.padding / 2, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   400
                          width, height + self.padding)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   401
            ctx.set_source_rgb(0.9, 0.9, 0.9)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   402
            ctx.fill()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   403
        last_x_drawn = int(x - 1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   404
        (lo, hi) = ranges.get_ranges_bounds(self.start, self.end)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   405
        for data_range in ranges.ranges[lo:hi]:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   406
            s = max(data_range.start, self.start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   407
            e = min(data_range.end, self.end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   408
            x_start = int(x + (s - self.start) * width / (self.end - self.start))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   409
            x_end = int(x + (e - self.start) * width / (self.end - self.start))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   410
            if x_end > last_x_drawn:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   411
                ctx.rectangle(x_start, y, x_end - x_start, 10)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   412
                ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   413
                ctx.stroke_preserve()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   414
                color = self.colors.lookup(data_range.value)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   415
                ctx.set_source_rgb(color.r, color.g, color.b)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   416
                ctx.fill()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   417
                last_x_drawn = x_end
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   418
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   419
        self.grey_background += 1
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   420
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   421
    def draw(self, ctx):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   422
        timeline_top = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   423
        top_y = self.padding / 2
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   424
        left_x_start = self.padding / 2
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   425
        left_x_end = left_x_start + self.left_width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   426
        right_x_start = left_x_end + self.padding
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   427
        right_x_end = right_x_start + self.right_width
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   428
        data_x_start = right_x_end + self.padding / 2
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   429
        data_x_end = self.width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   430
        data_width = data_x_end - data_x_start
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   431
        cur_y = top_y
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   432
        self.draw_line(ctx, 0, 0, self.width, 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   433
        self.grey_background = 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   434
        for timeline in self.timelines.get_all():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   435
            (y_bearing, t_width, t_height) = ctx.text_extents(timeline.name)[1:4]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   436
            ctx.move_to(left_x_start, cur_y + self.max_text_height - (t_height + y_bearing))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   437
            ctx.show_text(timeline.name);
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   438
            for events_int in timeline.get_events_int():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   439
                (y_bearing, t_width, t_height) = ctx.text_extents(events_int.name)[1:4]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   440
                ctx.move_to(right_x_start, cur_y + self.max_text_height - (t_height + y_bearing))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   441
                ctx.show_text(events_int.name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   442
                self.draw_events(ctx, events_int, data_x_start, cur_y, data_width, self.max_text_height + 5)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   443
                cur_y += self.max_text_height + 5 + self.padding
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   444
                self.draw_line(ctx, right_x_start - self.padding / 2, cur_y - self.padding / 2, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   445
                               self.right_width + self.padding, 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   446
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   447
            for events_str in timeline.get_events_str():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   448
                (y_bearing, t_width, t_height) = ctx.text_extents(events_str.name)[1:4]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   449
                ctx.move_to(right_x_start, cur_y + self.max_text_height - (t_height + y_bearing))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   450
                ctx.show_text(events_str.name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   451
                self.draw_events(ctx, events_str, data_x_start, cur_y, data_width, self.max_text_height + 5)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   452
                cur_y += self.max_text_height + 5 + self.padding
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   453
                self.draw_line(ctx, right_x_start - self.padding / 2, cur_y - self.padding / 2, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   454
                               self.right_width + self.padding, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   455
            for ranges in timeline.get_ranges():
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   456
                (y_bearing, t_width, t_height) = ctx.text_extents(ranges.name)[1:4]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   457
                ctx.move_to(right_x_start, cur_y + self.max_text_height - (t_height + y_bearing))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   458
                ctx.show_text(ranges.name)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   459
                self.draw_ranges(ctx, ranges, data_x_start, cur_y, data_width, 10)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   460
                cur_y += self.max_text_height + self.padding
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   461
                self.draw_line(ctx, right_x_start - self.padding / 2, cur_y - self.padding / 2, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   462
                               self.right_width + self.padding, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   463
            self.draw_line(ctx, 0, cur_y - self.padding / 2, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   464
                           self.width, 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   465
        bot_y = cur_y - self.padding / 2
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   466
        self.draw_line(ctx, left_x_end + self.padding / 2, 0, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   467
                       0, bot_y)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   468
        self.draw_line(ctx, right_x_end + self.padding / 2, 0, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   469
                       0, bot_y)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   470
        return
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   471
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   472
class ScaleRenderer:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   473
    def __init__(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   474
        self.__top = 0
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   475
        return
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   476
    def set_bounds(self, lo, hi):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   477
        self.__lo = lo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   478
        self.__hi = hi
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   479
    def get_position(self, x):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   480
        real_x = (x - self.__lo ) * self.__width / (self.__hi - self.__lo)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   481
        return real_x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   482
    def set_top(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   483
        self.__top = 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   484
    def set_bot(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   485
        self.__top = 0
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   486
    def layout(self, width):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   487
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   488
        ctx = cairo.Context(surface)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   489
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   490
        # calculate scale delta
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   491
        data_delta = self.__hi - self.__lo
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   492
        closest = 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   493
        while (closest*10) < data_delta:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   494
            closest *= 10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   495
        if (data_delta / closest) == 0:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   496
            delta = closest
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   497
        elif(data_delta / closest) == 1:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   498
            delta = closest / 10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   499
        else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   500
            delta = closest
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   501
        start = self.__lo - (self.__lo % delta) + delta
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   502
        end = self.__hi - (self.__hi % delta)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   503
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   504
        self.__delta = delta
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   505
        self.__width = width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   506
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   507
        # calculate text height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   508
        max_text_height = ctx.text_extents("ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz0123456789")[3]
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   509
        self.max_text_height = max_text_height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   510
        height = max_text_height + 10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   511
        self.__height = height
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   512
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   513
    def get_height(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   514
        return self.__height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   515
    def draw(self, ctx):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   516
        delta = self.__delta
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   517
        start = self.__lo - (self.__lo % delta) + delta
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   518
        end = self.__hi - (self.__hi % delta)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   519
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   520
        if self.__top == 1:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   521
            s = -1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   522
        else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   523
            s = 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   524
        # print scale points
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   525
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   526
        ctx.set_line_width(1.0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   527
        ticks = range(int(start), int(end + delta), int(delta))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   528
        for x in ticks:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   529
            real_x = (x - self.__lo ) * self.__width / (self.__hi - self.__lo)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   530
            ctx.move_to(real_x, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   531
            ctx.line_to(real_x, 5*s)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   532
            ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   533
            ctx.stroke()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   534
            (t_y_bearing, t_width, t_height) = ctx.text_extents(str(x))[1:4]
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   535
            if self.__top:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   536
                text_delta = t_height + t_y_bearing
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   537
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   538
                text_delta = -t_y_bearing
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   539
            ctx.move_to(real_x - t_width / 2, (5 + 5 + text_delta)*s)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   540
            ctx.show_text(str(x))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   541
        # draw subticks
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   542
        delta /= 10
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   543
        if delta > 0:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   544
            start = self.__lo - (self.__lo % delta) + delta
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   545
            end = self.__hi - (self.__hi % delta)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   546
            for x in range(int(start), int(end + delta), int(delta)):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   547
                real_x = (x - self.__lo ) * self.__width / (self.__hi - self.__lo)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   548
                ctx.move_to(real_x, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   549
                ctx.line_to(real_x, 3*s)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   550
                ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   551
                ctx.stroke()
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   552
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   553
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   554
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   555
class GraphicRenderer:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   556
    def __init__(self, start, end):
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   557
        self.__start = float(start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   558
        self.__end = float(end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   559
        self.__mid_scale = ScaleRenderer()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   560
        self.__mid_scale.set_top()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   561
        self.__bot_scale = ScaleRenderer()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   562
        self.__bot_scale.set_bounds(start, end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   563
        self.__bot_scale.set_bot()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   564
        self.__width = 1
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   565
        self.__height = 1
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   566
    def get_width(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   567
        return self.__width
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   568
    def get_height(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   569
        return self.__height
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   570
    # return x, y, width, height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   571
    def get_data_rectangle(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   572
        y_start = self.__top_legend.get_height()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   573
        x_start = self.__data.get_data_x_start()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   574
        return(x_start, y_start, self.__width - x_start, self.__data.get_height())
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   575
    def scale_data(self, x):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   576
        x_start = self.__data.get_data_x_start()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   577
        x_scaled = x / (self.__width - x_start) * (self.__r_end - self.__r_start)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   578
        return x_scaled
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   579
    # return x, y, width, height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   580
    def get_selection_rectangle(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   581
        y_start = self.__top_legend.get_height() + self.__data.get_height() + self.__mid_scale.get_height() + 20
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   582
        y_height = self.__bot_scale.get_height() + 20
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   583
        x_start = self.__bot_scale.get_position(self.__r_start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   584
        x_end = self.__bot_scale.get_position(self.__r_end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   585
        return(x_start, y_start, x_end - x_start, y_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   586
    def scale_selection(self, x):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   587
        x_scaled = x / self.__width * (self.__end - self.__start)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   588
        return x_scaled
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   589
    def set_range(self, start, end):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   590
        s = min(start, end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   591
        e = max(start, end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   592
        start = max(self.__start, s)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   593
        end = min(self.__end, e)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   594
        self.__r_start = start
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   595
        self.__r_end = end
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   596
        self.__data.set_render_range(start, end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   597
        self.__mid_scale.set_bounds(start, end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   598
        self.layout(self.__width, self.__height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   599
    def get_range(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   600
        return(self.__r_start, self.__r_end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   601
    def set_data(self, data):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   602
        self.__data = data
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   603
    def set_top_legend(self, top_legend):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   604
        self.__top_legend = top_legend
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   605
    def layout(self, width, height):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   606
        self.__width = width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   607
        self.__height = height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   608
        self.__top_legend.layout(width)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   609
        top_legend_height = self.__top_legend.get_height()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   610
        self.__data.layout(width)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   611
        self.__mid_scale.layout(width - self.__data.get_data_x_start())
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   612
        self.__bot_scale.layout(width)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   613
        return
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   614
    def __x_pixel(self, x, width):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   615
        new_x = (x - self.__start) * width / (self.__end - self.__start)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   616
        return new_x
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   617
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   618
    def draw(self, ctx):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   619
        # default background is white
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   620
        ctx.save()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   621
        ctx.set_source_rgb(1, 1, 1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   622
        ctx.set_operator(cairo.OPERATOR_SOURCE)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   623
        ctx.rectangle(0, 0, self.__width, self.__height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   624
        ctx.fill()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   625
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
   626
        # top legend
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   627
        ctx.save()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   628
        self.__top_legend.draw(ctx)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   629
        top_legend_height = self.__top_legend.get_height()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   630
        ctx.restore()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   631
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   632
        # separation line
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   633
        ctx.move_to(0, top_legend_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   634
        ctx.line_to(self.__width, top_legend_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   635
        ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   636
        ctx.set_line_width(2)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   637
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   638
        ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   639
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   640
        # data
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   641
        ctx.save()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   642
        ctx.translate(0, 
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   643
                       top_legend_height)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   644
        self.__data.draw(ctx)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   645
        ctx.restore()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   646
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   647
        # scale below data
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   648
        ctx.save()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   649
        ctx.translate(self.__data.get_data_x_start(), 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   650
                       top_legend_height + self.__data.get_height() + self.__mid_scale.get_height())
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   651
        self.__mid_scale.draw(ctx)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   652
        ctx.restore()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   653
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   654
        height_used = top_legend_height + self.__data.get_height() + self.__mid_scale.get_height()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   655
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   656
        # separation between scale and left pane
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   657
        ctx.move_to(self.__data.get_data_x_start(), height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   658
        ctx.rel_line_to(0, -self.__mid_scale.get_height())
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   659
        ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   660
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   661
        ctx.set_line_width(2)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   662
        ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   663
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   664
        # separation below scale
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   665
        ctx.move_to(0, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   666
        ctx.line_to(self.__width, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   667
        ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   668
        ctx.set_line_width(2)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   669
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   670
        ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   671
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   672
        select_start = self.__bot_scale.get_position(self.__r_start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   673
        select_end = self.__bot_scale.get_position(self.__r_end)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   674
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   675
        # left connection between top scale and bottom scale
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   676
        ctx.move_to(0, height_used);
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   677
        ctx.line_to(self.__data.get_data_x_start(), height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   678
        ctx.line_to(select_start, height_used + 20)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   679
        ctx.line_to(0, height_used + 20)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   680
        ctx.line_to(0, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   681
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   682
        ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   683
        ctx.stroke_preserve()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   684
        ctx.set_source_rgb(0.9, 0.9, 0.9)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   685
        ctx.fill()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   686
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   687
        # right connection between top scale and bottom scale
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   688
        ctx.move_to(self.__width, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   689
        ctx.line_to(self.__width, height_used + 20)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   690
        ctx.line_to(select_end, height_used + 20)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   691
        ctx.line_to(self.__width, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   692
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   693
        ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   694
        ctx.stroke_preserve()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   695
        ctx.set_source_rgb(0.9, 0.9, 0.9)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   696
        ctx.fill()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   697
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   698
        height_used += 20
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   699
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   700
        # unused area background
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   701
        unused_start = self.__bot_scale.get_position(self.__r_start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   702
        unused_end = self.__bot_scale.get_position(self.__r_end)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   703
        unused_height = self.__bot_scale.get_height() + 20
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   704
        ctx.rectangle(0, height_used, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   705
                       unused_start, 
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   706
                       unused_height)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   707
        ctx.rectangle(unused_end, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   708
                       height_used, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   709
                       self.__width - unused_end, 
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   710
                       unused_height)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   711
        ctx.set_source_rgb(0.9, 0.9, 0.9)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   712
        ctx.fill()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   713
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   714
        # border line around bottom scale
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   715
        ctx.move_to(unused_end, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   716
        ctx.line_to(self.__width, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   717
        ctx.line_to(self.__width, height_used + unused_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   718
        ctx.line_to(0, height_used + unused_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   719
        ctx.line_to(0, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   720
        ctx.line_to(unused_start, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   721
        ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   722
        ctx.set_line_width(2)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   723
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   724
        ctx.stroke()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   725
        ctx.move_to(unused_start, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   726
        ctx.line_to(unused_end, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   727
        ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   728
        ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   729
        ctx.set_source_rgb(0.9, 0.9, 0.9)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   730
        ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   731
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   732
        # unused area dot borders
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   733
        ctx.save()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   734
        ctx.move_to(max(unused_start, 2), height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   735
        ctx.rel_line_to(0, unused_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   736
        ctx.move_to(min(unused_end, self.__width - 2), height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   737
        ctx.rel_line_to(0, unused_height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   738
        ctx.set_dash([5], 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   739
        ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   740
        ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   741
        ctx.stroke()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   742
        ctx.restore()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   743
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   744
        # bottom scale
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   745
        ctx.save()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   746
        ctx.translate(0, height_used)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   747
        self.__bot_scale.draw(ctx)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   748
        ctx.restore()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   749
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   750
class GtkGraphicRenderer(gtk.DrawingArea):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   751
    def __init__(self, data):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   752
        super(GtkGraphicRenderer, self).__init__()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   753
        self.__data = data
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   754
        self.__moving_left = False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   755
        self.__moving_right = False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   756
        self.__moving_both = False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   757
        self.__moving_top = False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   758
        self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   759
        self.add_events(gtk.gdk.POINTER_MOTION_MASK)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   760
        self.add_events(gtk.gdk.BUTTON_PRESS_MASK)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   761
        self.add_events(gtk.gdk.BUTTON_RELEASE_MASK)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   762
        self.connect("expose_event", self.expose)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   763
        self.connect('size-allocate', self.size_allocate)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   764
        self.connect('motion-notify-event', self.motion_notify)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   765
        self.connect('button-press-event', self.button_press)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   766
        self.connect('button-release-event', self.button_release)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   767
    def set_smaller_zoom(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   768
        (start, end) = self.__data.get_range()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   769
        self.__data.set_range(start, start + (end - start)*2)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   770
        self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   771
        self.queue_draw()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   772
    def set_bigger_zoom(self):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   773
        (start, end) = self.__data.get_range()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   774
        self.__data.set_range(start, start + (end - start) / 2)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   775
        self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   776
        self.queue_draw()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   777
    def output_png(self, filename):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   778
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   779
                                     self.__data.get_width(), 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   780
                                     self.__data.get_height())
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   781
        ctx = cairo.Context(self.__buffer_surface)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   782
        self.__data.draw(ctx)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   783
        surface.write_to_png(filename)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   784
    def button_press(self, widget, event):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   785
        (x, y, width, height) = self.__data.get_selection_rectangle()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   786
        (d_x, d_y, d_width, d_height) = self.__data.get_data_rectangle()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   787
        if event.y > y and event.y < y + height:
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   788
            if abs(event.x - x) < 5:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   789
                self.__moving_left = True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   790
                return True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   791
            if abs(event.x - (x + width)) < 5:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   792
                self.__moving_right = True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   793
                return True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   794
            if event.x > x and event.x < x + width:
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   795
                self.__moving_both = True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   796
                self.__moving_both_start = event.x
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   797
                self.__moving_both_cur = event.x
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   798
                return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   799
        if event.y > d_y and event.y < (d_y + d_height):
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   800
            if event.x > d_x and event.x < (d_x + d_width):
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   801
                self.__moving_top = True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   802
                self.__moving_top_start = event.x
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   803
                self.__moving_top_cur = event.x
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   804
                return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   805
        return False
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   806
    def button_release(self, widget, event):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   807
        if self.__moving_left:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   808
            self.__moving_left = False
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   809
            left = self.__data.scale_selection(self.__moving_left_cur)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   810
            right = self.__data.get_range()[1]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   811
            self.__data.set_range(left, right)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   812
            self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   813
            self.queue_draw()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   814
            return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   815
        if self.__moving_right:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   816
            self.__moving_right = False
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   817
            right = self.__data.scale_selection(self.__moving_right_cur)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   818
            left = self.__data.get_range()[0]
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   819
            self.__data.set_range(left, right)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   820
            self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   821
            self.queue_draw()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   822
            return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   823
        if self.__moving_both:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   824
            self.__moving_both = False
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   825
            delta = self.__data.scale_selection(self.__moving_both_cur - self.__moving_both_start)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   826
            (left, right) = self.__data.get_range()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   827
            self.__data.set_range(left + delta, right + delta)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   828
            self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   829
            self.queue_draw()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   830
            return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   831
        if self.__moving_top:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   832
            self.__moving_top = False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   833
        return False
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   834
    def motion_notify(self, widget, event):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   835
        (x, y, width, height) = self.__data.get_selection_rectangle()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   836
        if self.__moving_left:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   837
            if event.x <= 0:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   838
                self.__moving_left_cur = 0
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   839
            elif event.x >= x + width:
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   840
                self.__moving_left_cur = x + width
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   841
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   842
                self.__moving_left_cur = event.x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   843
            self.queue_draw_area(0, int(y), int(self.__width), int(height))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   844
            return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   845
        if self.__moving_right:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   846
            if event.x >= self.__width:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   847
                self.__moving_right = self.__width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   848
            elif event.x < x:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   849
                self.__moving_right_cur = x
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   850
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   851
                self.__moving_right_cur = event.x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   852
            self.queue_draw_area(0, int(y), int(self.__width), int(height))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   853
            return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   854
        if self.__moving_both:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   855
            cur_e = self.__width - (x + width - self.__moving_both_start)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   856
            cur_s = (self.__moving_both_start - x)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   857
            if event.x < cur_s:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   858
                self.__moving_both_cur = cur_s
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   859
            elif event.x > cur_e:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   860
                self.__moving_both_cur = cur_e
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   861
            else:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   862
                self.__moving_both_cur = event.x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   863
            self.queue_draw_area(0, int(y), int(self.__width), int(height))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   864
            return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   865
        if self.__moving_top:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   866
            self.__moving_top_cur = event.x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   867
            delta = self.__data.scale_data(self.__moving_top_start - self.__moving_top_cur)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   868
            (left, right) = self.__data.get_range()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   869
            self.__data.set_range(left + delta, right + delta)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   870
            self.__force_full_redraw = True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   871
            self.__moving_top_start = event.x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   872
            self.queue_draw()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   873
            return True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   874
        (d_x, d_y, d_width, d_height) = self.__data.get_data_rectangle()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   875
        if event.y > y and event.y < y + height:
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   876
            if abs(event.x - x) < 5 or abs(event.x - (x + width)) < 5:
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   877
                widget.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.SB_H_DOUBLE_ARROW))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   878
                return True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   879
            if event.x > x and event.x < x + width:
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   880
                widget.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   881
                return True
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   882
        if event.y > d_y and event.y < (d_y + d_height):
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   883
            if event.x > d_x and event.x < (d_x + d_width):
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   884
                widget.window.set_cursor(gtk.gdk.Cursor(gtk.gdk.FLEUR))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   885
                return True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   886
        widget.window.set_cursor(None)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   887
        return False
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   888
    def size_allocate(self, widget, allocation):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   889
        self.__width = allocation.width
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   890
        self.__height = allocation.height
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   891
        self.__data.layout(allocation.width, allocation.height)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   892
        self.__force_full_redraw = True
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   893
        self.queue_draw()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   894
    def expose(self, widget, event):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   895
        if self.__force_full_redraw:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   896
            self.__buffer_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   897
                                                       self.__data.get_width(), 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   898
                                                       self.__data.get_height())
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   899
            ctx = cairo.Context(self.__buffer_surface)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   900
            self.__data.draw(ctx)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   901
            self.__force_full_redraw = False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   902
        ctx = widget.window.cairo_create()
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   903
        ctx.rectangle(event.area.x, event.area.y, 
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   904
                      event.area.width, event.area.height)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   905
        ctx.clip()
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   906
        ctx.set_source_surface(self.__buffer_surface)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   907
        ctx.paint()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   908
        (x, y, width, height) = self.__data.get_selection_rectangle()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   909
        if self.__moving_left:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   910
            ctx.move_to(max(self.__moving_left_cur, 2), y)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   911
            ctx.rel_line_to(0, height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   912
            ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   913
            ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   914
            ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   915
            ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   916
        if self.__moving_right:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   917
            ctx.move_to(min(self.__moving_right_cur, self.__width - 2), y)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   918
            ctx.rel_line_to(0, height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   919
            ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   920
            ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   921
            ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   922
            ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   923
        if self.__moving_both:
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   924
            delta_x = self.__moving_both_cur - self.__moving_both_start
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   925
            left_x = x + delta_x
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   926
            ctx.move_to(x + delta_x, y)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   927
            ctx.rel_line_to(0, height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   928
            ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   929
            ctx.move_to(x + width + delta_x, y)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   930
            ctx.rel_line_to(0, height)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   931
            ctx.close_path()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   932
            ctx.set_source_rgb(0, 0, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   933
            ctx.set_line_width(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   934
            ctx.stroke()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   935
        return False
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   936
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   937
class MainWindow:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   938
    def __init__(self):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   939
        return
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   940
    def run(self, graphic):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   941
        window = gtk.Window()
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   942
        self.__window = window
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   943
        window.set_default_size(200, 200)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   944
        vbox = gtk.VBox()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   945
        window.add(vbox)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   946
        render = GtkGraphicRenderer(graphic)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   947
        self.__render = render
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   948
        vbox.pack_end(render, True, True, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   949
        hbox = gtk.HBox()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   950
        vbox.pack_start(hbox, False, False, 0)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   951
        smaller_zoom = gtk.Button("Zoom Out")
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   952
        smaller_zoom.connect("clicked", self.__set_smaller_cb)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   953
        hbox.pack_start(smaller_zoom)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   954
        bigger_zoom = gtk.Button("Zoom In")
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   955
        bigger_zoom.connect("clicked", self.__set_bigger_cb)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   956
        hbox.pack_start(bigger_zoom)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   957
        output_png = gtk.Button("Output Png")
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   958
        output_png.connect("clicked", self.__output_png_cb)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   959
        hbox.pack_start(output_png)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   960
        window.connect('destroy', gtk.main_quit)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   961
        window.show_all()
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   962
        #gtk.bindings_activate(gtk.main_quit, 'q', 0)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   963
        gtk.main()
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   964
    def __set_smaller_cb(self, widget):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   965
        self.__render.set_smaller_zoom()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   966
    def __set_bigger_cb(self, widget):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   967
        self.__render.set_bigger_zoom()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   968
    def __output_png_cb(self, widget):
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   969
        dialog = gtk.FileChooserDialog("Output Png", self.__window, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   970
                                       gtk.FILE_CHOOSER_ACTION_SAVE, ("Save", 1))
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   971
        self.__dialog = dialog
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   972
        dialog.set_default_response(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   973
        dialog.connect("response", self.__dialog_response_cb)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   974
        dialog.show()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   975
        return
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   976
    def __dialog_response_cb(self, widget, response):
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   977
        if response == 1:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   978
            filename = self.__dialog.get_filename()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   979
            self.__render.output_png(filename)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   980
            widget.hide()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   981
        return
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   982
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   983
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   984
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   985
def read_data(filename):
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   986
    timelines = Timelines()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   987
    colors = Colors()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   988
    fh = open(filename)
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   989
    m1 = re.compile('range ([^ ]+) ([^ ]+) ([^ ]+) ([0-9]+) ([0-9]+)')
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   990
    m2 = re.compile('event-str ([^ ]+) ([^ ]+) ([^ ]+) ([0-9]+)')
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   991
    m3 = re.compile('event-int ([^ ]+) ([^ ]+) ([0-9]+) ([0-9]+)')
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   992
    m4 = re.compile('color ([^ ]+) #([a-fA-F0-9]{2,2})([a-fA-F0-9]{2,2})([a-fA-F0-9]{2,2})')
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   993
    for line in fh.readlines():
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   994
        m = m1.match(line)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
   995
        if m:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   996
            line_name = m.group(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   997
            timeline = timelines.get(m.group(1))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   998
            rang = timeline.get_range(m.group(2))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
   999
            data_range = DataRange()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1000
            data_range.value = m.group(3)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1001
            data_range.start = int(m.group(4))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1002
            data_range.end = int(m.group(5))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1003
            rang.add_range(data_range)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1004
            continue
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1005
        m = m2.match(line)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1006
        if m:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1007
            line_name = m.group(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1008
            timeline = timelines.get(m.group(1))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1009
            ev = timeline.get_event_str(m.group(2))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1010
            event = EventString()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1011
            event.value = m.group(3)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1012
            event.at = int(m.group(4))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1013
            ev.add_event(event)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1014
            continue
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1015
        m = m3.match(line)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1016
        if m:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1017
            line_name = m.group(1)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1018
            timeline = timelines.get(m.group(1))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1019
            ev = timeline.get_event_int(m.group(2))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1020
            event = EventInt()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1021
            event.value = int(m.group(3))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1022
            event.at = int(m.group(4))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1023
            ev.add_event(event)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1024
            continue
128
b48c310f4e24 fix python tab indent
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 124
diff changeset
  1025
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1026
        m = m4.match(line)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1027
        if m:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1028
            r = int(m.group(2), 16)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1029
            g = int(m.group(3), 16)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1030
            b = int(m.group(4), 16)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1031
            color = Color(r / 255, g / 255, b / 255)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1032
            colors.add(m.group(1), color)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1033
            continue
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1034
    timelines.sort()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1035
    return (colors, timelines)
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1036
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1037
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1038
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1039
def main():
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1040
    (colors, timelines) = read_data(sys.argv[1])
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1041
    (lower_bound, upper_bound) = timelines.get_bounds()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1042
    graphic = GraphicRenderer(lower_bound, upper_bound)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1043
    top_legend = TopLegendRenderer()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1044
    range_values = timelines.get_all_range_values()
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1045
    range_colors = []
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1046
    for range_value in range_values:
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1047
        range_colors.append(colors.lookup(range_value))
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1048
    top_legend.set_legends(range_values, 
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1049
                           range_colors)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1050
    graphic.set_top_legend(top_legend)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1051
    data = TimelinesRenderer()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1052
    data.set_timelines(timelines, colors)
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1053
    graphic.set_data(data)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1054
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1055
    # default range
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1056
    range_mid = (upper_bound - lower_bound) / 2
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1057
    range_width = (upper_bound - lower_bound) / 10
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1058
    range_lo = range_mid - range_width / 2
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1059
    range_hi = range_mid + range_width / 2
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1060
    graphic.set_range(range_lo, range_hi)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1061
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1062
    main_window = MainWindow()
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1063
    main_window.run(graphic)
12
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1064
917ba023c576 utils and samples
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
diff changeset
  1065
136
4faf1726e8ff make python code more Pep8 conformant (patch from Gustavo Carneiro)
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 128
diff changeset
  1066
main()