Changeset 8304:228813e2999c in orange
 Timestamp:
 06/11/11 11:05:16 (3 years ago)
 Branch:
 default
 Convert:
 6b80533392251164d78cc2210865d9d13e3a6b71
 Location:
 orange/OrangeWidgets
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

orange/OrangeWidgets/Graph/curve.py
r8300 r8304 3 3 from palette import * 4 4 from OWGraphQt import * 5 import OrangeGraph 5 6 6 7 from PyQt4.QtGui import QGraphicsItemGroup, QGraphicsEllipseItem, QGraphicsLineItem, QGraphicsPathItem … … 27 28 """ 28 29 29 class Curve(QGraphicsItemGroup): 30 def __init__(self, name, data, style, graph, parent=None): 31 QGraphicsItemGroup.__init__(self, parent) 32 self.name = name 33 self.data = data 34 self.style = style 35 self.graph = graph 36 self.continuous = False 37 self.path_item = None 38 self.point_items = [] 39 self.pen = self.style.pen() 40 self.brush = self.style.brush() 41 30 class Curve(OrangeGraph.Curve): 31 def __init__(self, parent=None): 32 OrangeGraph.Curve.__init__(self, parent) 33 42 34 def __setattr__(self, name, value): 43 unisetattr(self, name, value, QGraphicsItemGroup) 44 45 def update(self): 46 s = self.scene() 47 48 if s: 49 for i in self.point_items: 50 s.removeItem(i) 51 del self.point_items[:] 52 if self.path_item and s: 53 s. removeItem(self.path_item) 54 self.path_item = None 55 if not self.data: 56 return 57 if self.continuous: 58 self.path = QPainterPath() 59 (start_x, start_y) = self.graph.map_to_graph(self.data[0]) 60 self.path.moveTo(start_x, start_y) 61 for data_point in self.data: 62 (x, y) = self.graph.map_to_graph(data_point) 63 self.path.lineTo(x, y) 64 self.path_item = QGraphicsPathItem(self.path, self) 65 self.path_item.setPen(self.style.pen()) 66 self.path_item.show() 67 else: 68 a = self.graph.graph_area.translated(self.graph.graph_area.bottomLeft()) 69 for p in self.data: 70 (x, y) = self.graph.map_to_graph(p) 71 if a.contains(QPointF(x, y)): 72 i = self.symbol(x, y) 73 self.point_items.append(i) 74 75 def symbol(self, x, y, s=None, parent=None): 76 if not s: 77 s = self.style.point_size 78 if not parent: 79 parent = self 80 if self.style.point_shape is Ellipse: 81 i = QGraphicsEllipseItem(xs/2, ys/2, s, s, parent) 82 elif self.style.point_shape is Rect: 83 i = QGraphicsRectItem(xs/2, ys/2, s, s, parent) 84 else: 85 ## TODO: Implement all the other shapes 86 i = QGraphicsRectItem(xs/2, ys/2, 1.5*s, 0.8*s, parent) 87 i.setPen(QPen(Qt.NoPen)) 88 i.setBrush(self.brush) 89 return i 35 unisetattr(self, name, value, OrangeGraph.Curve) 
orange/OrangeWidgets/Graph/legend.py
r8294 r8304 26 26 length = 0 27 27 for curve in self.curves: 28 self.items.append(curve. symbol(10, y, parent=self))28 self.items.append(curve.pointItem(10, y, curve.pointSize(), self)) 29 29 text = QGraphicsTextItem(curve.name, self) 30 30 length = max(length, text.boundingRect().width()) 
orange/OrangeWidgets/OWGraphQt.py
r8301 r8304 1 2 1 """ 3 2 .. class:: QtGraph … … 117 116 self.map_to_graph = self.map_to_graph_cart 118 117 self.map_from_graph = self.map_from_graph_cart 118 self.map_transform = QTransform() 119 119 120 120 ## Performance optimization … … 135 135 self.update() 136 136 137 138 137 def __setattr__(self, name, value): 139 138 unisetattr(self, name, value, QGraphicsView) 140 139 141 140 def graph_area_rect(self): 142 """143 rect = self.childrenRect()144 if xBottom in self.axes:145 rect.setBottom(rect.bottom()  self.axis_margin)146 if yLeft in self.axes:147 rect.setLeft(rect.left() + self.axis_margin)148 return rect149 """150 141 return self.graph_area 151 142 152 def map_to_graph_cart(self, point, axes=None): 153 px, py = point 154 if not axes: 155 axes = [xBottom, yLeft] 156 min_x, max_x, t = self.axes[axes[0]].scale 157 min_y, max_y, t = self.axes[axes[1]].scale 158 rect = self.zoom_rect 159 rx = rect.left()  self.graph_area.left() + (px  min_x) * rect.width() / (max_x  min_x) 160 ry = rect.bottom()  self.graph_area.bottom() (py  min_y) * rect.height() / (max_y  min_y) 161 return (rx, ry) 162 163 def map_from_graph_cart(self, point, axes = None): 164 px, py = point 165 if not axes: 166 axes = [xBottom, yLeft] 167 min_x, max_x = self.data_range[axes[0]] 168 min_y, max_y = self.data_range[axes[1]] 169 rect = self.graph_area_rect() 170 rx = (px  rect.left()) / rect().width() * (max_x  min_x) 171 ry = (py  rect.bottom()) / rect.height() * (max_y  min_y) 172 return (rx, ry) 143 def map_to_graph(self, point, axes = None): 144 (x, y) = point 173 145 174 146 def saveToFile(self, extraButtons = []): … … 274 246 symbol = Ellipse, enableLegend = 0, xData = [], yData = [], showFilledSymbols = None, 275 247 lineWidth = 1, pen = None, autoScale = 0, antiAlias = None, penAlpha = 255, brushAlpha = 255): 276 data = [] 277 qDebug('Adding curve ' + name + ' with ' + str(len(xData)) + ' points' + (' to legend' if enableLegend else '')) 278 qDebug('Its shape is ' +str(symbol) ) 279 for i in range(len(xData)): 280 data.append( (xData[i], yData[i]) ) 281 282 line_style = palette.LineStyle(brushColor, lineWidth, style, symbol, size) 283 284 c = curve.Curve(name, data, line_style, self) 285 c.setPos(self.graph_area.bottomLeft()) 286 c.continuous = (style is not Qt.NoPen) 248 249 c = curve.Curve() 250 c.name = name 251 c.setAutoUpdate(False) 252 c.setContinuous(style is not Qt.NoPen) 253 c.setColor(brushColor) 254 c.setSymbol(symbol) 255 c.setPointSize(size) 256 c.setData(xData, yData) 257 c.setGraphTransform(self.map_transform * self.zoom_transform) 287 258 c.update() 259 288 260 self.canvas.addItem(c) 289 261 self.curves.append(c) … … 302 274 self.canvas.removeItem(c) 303 275 del self.curves[:] 304 305 def replot(self):276 277 def update_layout(self): 306 278 graph_rect = QRectF(self.childrenRect()) 307 279 m = self.graph_margin … … 360 332 361 333 self.graph_area = QRectF(graph_rect) 362 334 self.update_axes(axis_rects) 335 336 def update_zoom(self): 363 337 self.zoom_transform = self.transform_for_zoom(self._zoom_factor, self.zoom_point, self.graph_area) 364 338 self.zoom_rect = self.zoom_transform.mapRect(self.graph_area) 365 339 340 axes = self.axes.keys() 341 if not axes: 342 axes = [xBottom, yLeft] 343 min_x, max_x, t = self.axes[axes[0]].scale 344 min_y, max_y, t = self.axes[axes[1]].scale 345 data_rect = QRectF(min_x, max_y, max_xmin_y, min_ymax_y) 346 qDebug(repr(data_rect)) 347 self.map_transform = self.transform_from_rects(data_rect, self.zoom_rect) 348 349 for c in self.curves: 350 c.setGraphArea(self.graph_area) 351 c.setGraphTransform(self.map_transform * self.zoom_transform) 352 c.update() 353 self.setSceneRect(self.canvas.itemsBoundingRect()) 354 355 356 def update_axes(self, axis_rects): 366 357 for id, item in self.axes.iteritems(): 367 358 self.canvas.removeItem(item) … … 384 375 a.update() 385 376 a.show() 386 387 for c in self.curves:388 c.setPos(self.graph_area.bottomLeft())389 c.update()390 self. setSceneRect(self.canvas.itemsBoundingRect())391 377 378 379 def replot(self): 380 self.update_layout() 381 self.update_zoom() 382 392 383 def legend(self): 393 384 return self._legend … … 439 430 ts = QTransform().scale(r2.width()/r1.width(), r2.height()/r1.height()) 440 431 tr2 = QTransform().translate(r2.left(), r2.top()) 441 return tr 2 * ts * tr1432 return tr1 * ts * tr2 442 433 443 434 def transform_for_zoom(self, factor, point, rect): 444 435 if factor == 1: 445 436 return QTransform() 446 rect = rect.normalized() 447 s = (1.01.0/factor)/2.0 448 qDebug('factor=%d, s=%f' % (factor, s)) 437 449 438 t = QTransform() 450 t.translate( point.x(),point.y())439 t.translate(+point.x(), +point.y()) 451 440 t.scale(factor, factor) 452 441 t.translate(point.x(), point.y()) 442 qDebug(repr(point) + ' ===> ' + repr(t.map(point))) 453 443 return t 454 444 … … 472 462 qDebug(str(value)) 473 463 self._zoom_factor = value 474 self. replot()464 self.update_zoom()
Note: See TracChangeset
for help on using the changeset viewer.