/usr/share/pyshared/tryton/gui/window/view_form/view/graph_gtk/line.py is in tryton-client 3.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | #This file is part of Tryton. The COPYRIGHT file at the top level of
#this repository contains the full copyright notices and license terms.
#This code is inspired by the pycha project
#(http://www.lorenzogil.com/projects/pycha/)
from graph import Graph
from tryton.common import hex2rgb, float_time_to_text
import locale
import math
import cairo
import tryton.rpc as rpc
class Line(Graph):
def updateGraph(self):
yfield2attrs = {}
for yfield in self.yfields:
yfield2attrs[yfield.get('key', yfield['name'])] = yfield
self.points = []
i = 0
keys = self.datas.keys()
keys.sort()
for xfield in keys:
j = 0
for yfield in self.datas[xfield]:
xval = i
yval = self.datas[xfield][yfield]
x = (xval - self.minxval) * self.xscale
y = 1.0 - (yval - self.minyval) * self.yscale
if self.xrange == 0:
x = 1.0
if (not bool(int(yfield2attrs[yfield].get('empty', 1)))
and yval == 0):
continue
point = Point(x, y, xval, yval, xfield, yfield)
if (0.0 <= point.x <= 1.0) and (0.0 <= point.y <= 1.0):
self.points.append(point)
j += 1
i += 1
def drawGraph(self, cr, width, height):
key2fill = {}
key2interpolation = {}
for yfield in self.yfields:
key = yfield.get('key', yfield['name'])
key2fill[key] = bool(int(yfield.get('fill', 0)))
key2interpolation[key] = yfield.get('interpolation', 'linear')
def preparePath(key):
interpolation = key2interpolation[key]
points = (p for p in self.points if p.yname == key)
zero = 1.0 + self.minyval * self.yscale
cr.new_path()
cr.move_to(self.area.x, zero * self.area.h + self.area.y)
if interpolation == 'linear':
for point in points:
cr.line_to(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y)
else:
previous = Point(0, zero, None, None, None, None)
def breakage(previous, point):
if interpolation == 'constant-center':
return previous.x + ((point.x - previous.x) / 2.0)
elif interpolation == 'constant-left':
return point.x
elif interpolation == 'constant-right':
return previous.x
for point in points:
cr.line_to(
breakage(previous, point) * self.area.w + self.area.x,
previous.y * self.area.h + self.area.y)
cr.line_to(
breakage(previous, point) * self.area.w + self.area.x,
point.y * self.area.h + self.area.y)
cr.line_to(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y)
previous = point
cr.line_to(breakage(previous,
Point(1, zero, None, None, None, None))
* self.area.w + self.area.x,
previous.y * self.area.h + self.area.y)
cr.line_to(self.area.w + self.area.x,
zero * self.area.h + self.area.y)
cr.move_to(self.area.x, zero * self.area.h + self.area.y)
if key2fill[key]:
cr.close_path()
else:
cr.set_source_rgb(*self.colorScheme[key])
cr.stroke()
cr.save()
cr.set_line_width(2)
for key in self._getDatasKeys():
if key2fill[key]:
cr.save()
cr.set_source_rgba(0, 0, 0, 0.15)
cr.translate(2, -2)
preparePath(key)
cr.fill()
cr.restore()
r, g, b = self.colorScheme[key]
linear = cairo.LinearGradient(width / 2, 0, width / 2, height)
linear.add_color_stop_rgb(0,
3.5 * r / 5.0, 3.5 * g / 5.0, 3.5 * b / 5.0)
linear.add_color_stop_rgb(1, r, g, b)
cr.set_source(linear)
preparePath(key)
cr.fill()
else:
preparePath(key)
for point in self.points:
if key2fill[point.yname]:
continue
cr.set_source_rgb(*self.colorScheme[point.yname])
cr.move_to(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y)
cr.arc(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y,
3, 0, 2 * math.pi)
cr.fill()
cr.restore()
def drawLegend(self, cr, widht, height):
super(Line, self).drawLegend(cr, widht, height)
cr.save()
for point in self.points:
if point.highlight:
cr.set_line_width(2)
cr.set_source_rgb(*hex2rgb('#000000'))
cr.move_to(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y)
cr.arc(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y,
3, 0, 2 * math.pi)
cr.stroke()
cr.set_source_rgb(*self.colorScheme['__highlight'])
cr.arc(point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y,
3, 0, 2 * math.pi)
cr.fill()
cr.restore()
def motion(self, widget, event):
nearest = None
for point in self.points:
x = point.x * self.area.w + self.area.x
y = point.y * self.area.h + self.area.y
l = (event.x - x) ** 2 + (event.y - y) ** 2
if not nearest or l < nearest[1]:
nearest = (point, l)
dia = self.area.w ** 2 + self.area.h ** 2
keys2txt = {}
for yfield in self.yfields:
keys2txt[yfield.get('key', yfield['name'])] = yfield['string']
highlight = False
draw_points = []
yfields_float_time = dict(
(x.get('key', x['name']), x.get('float_time'))
for x in self.yfields if x.get('widget'))
for point in self.points:
if point == nearest[0] and nearest[1] < dia / 100:
if not point.highlight:
point.highlight = True
label = keys2txt[point.yname]
label += '\n'
if point.yname in yfields_float_time:
conv = None
if yfields_float_time[point.yname]:
conv = rpc.CONTEXT.get(
yfields_float_time[point.yname])
label += float_time_to_text(point.yval, conv)
else:
label += locale.format('%.2f', point.yval, True)
label += '\n'
label += str(self.labels[point.xname])
self.popup.set_text(label)
draw_points.append(point)
else:
if point.highlight:
point.highlight = False
draw_points.append(point)
if point.highlight:
self.popup.set_position(self,
point.x * self.area.w + self.area.x,
point.y * self.area.h + self.area.y)
highlight = True
if highlight:
self.popup.show()
else:
self.popup.hide()
if draw_points:
minx = self.area.w + self.area.x
miny = self.area.h + self.area.y
maxx = maxy = 0.0
for point in draw_points:
x = self.area.w * point.x + self.area.x
y = self.area.h * point.y + self.area.y
minx = min(x - 5, minx)
miny = min(y - 5, miny)
maxx = max(x + 5, maxx)
maxy = max(y + 5, maxy)
self.queue_draw_area(int(minx - 1), int(miny - 1),
int(maxx - minx + 1), int(maxy - miny + 1))
def updateXY(self):
super(Line, self).updateXY()
if self.xrange != 0:
self.xrange -= 1
if self.xrange == 0:
self.xscale = 1.0
else:
self.xscale = 1.0 / self.xrange
def drawAxis(self, cr, width, height):
super(Line, self).drawAxis(cr, width, height)
self.drawLine(cr, 1.0, 0)
def action(self):
super(Line, self).action()
for point in self.points:
if point.highlight:
ids = self.ids[point.xname]
self.action_keyword(ids)
def YLabels(self):
ylabels = super(Line, self).YLabels()
if len([x.get('key', x['name']) for x in self.yfields
if x.get('widget')]) == len(self.yfields):
conv = None
float_time = reduce(lambda x, y: x == y and x or False,
[x.get('float_time') for x in self.yfields])
if float_time:
conv = rpc.CONTEXT.get(float_time)
return [(x[0], float_time_to_text(locale.atof(x[1]), conv))
for x in ylabels]
return ylabels
class Point(object):
def __init__(self, x, y, xval, yval, xname, yname):
self.x, self.y = x, y
self.xval, self.yval = xval, yval
self.xname = xname
self.yname = yname
self.highlight = False
|