def __init__(self, name, parent): super(SnapshotsView, self).__init__(parent) self._pixmap = None self._name = name self._scene = QtGui.QGraphicsScene(parent) self.setGeometry(QtCore.QRect(0, 0, parent.width(), parent.height())) self._scale_factor = 1.15 css_frame = """ /*background: #000; border-top: 2px solid #000; border-left: 2px solid #000; border-right: 2px solid #000; border-top-left-radius: 4px; border-top-right-radius: 4px;*/ """ self.setStyleSheet(css_frame) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self._item = QtGui.QGraphicsPixmapItem() self._scene.addItem(self._item) self._drawings = [] image_pen_px = QtGui.QPixmap(':ftrack/image/integration/pencil') image_pen_px.setMask(image_pen_px.mask()) self._image_pen_cursor = QtGui.QCursor(image_pen_px) self._pen_color = QtCore.Qt.red self._drawing_mode = False self._pencil_pressed = False self._position_mouse = None self.setBackgroundBrush( QtGui.QBrush(QtCore.Qt.black, QtCore.Qt.SolidPattern)) self.setScene(self._scene)
def paintEvent(self, event): painter = QtGui.QPainter(self) painter.setRenderHint(QtGui.QPainter.Antialiasing) # Design the graph width_graph = self.width() - (self._pad_left + self._pad_right) height_graph = self.height() - (self._pad_top + self._pad_bottom) rect = QtCore.QRect( self._pad_left, self._pad_top, width_graph, height_graph) painter.fillRect(self.rect(), self._color_back_2) painter.fillRect(rect, self._color_back_1) # Sort graphs to draw the highlighted one at the end unhighlight_graphs = [ p for p in self._points.keys() if p != self._highlight_graph] all_graphs_type = unhighlight_graphs + [self._highlight_graph] # Collect the axis values to draw it at the end... pts_legend_Y = dict() pts_legend_X = dict() versions = range(self._version_number) width_interval = 1 if len( versions) == 1 else 1 / float(len(versions) - 1) for i in versions: pts_legend_X[i] = self._pad_left + width_graph * i * width_interval # collect the highlighted point to get the tool-tips.. points_h = dict() # Get the highlighted point hover_point = None # Draw each graph... for type_graph in all_graphs_type: if self._points[type_graph] == None: continue points_tuples = [] highlighted = type_graph == self._highlight_graph for i in sorted(pts_legend_X.keys(), reverse=True): hover = False try: graph_point = self._points[type_graph][i] except IndexError: pass else: padding = self._pad_top + height_graph relative_position = height_graph * self.raise_level * 0.1 ypos = padding - relative_position * \ graph_point.normalized_value pt = (pts_legend_X[i], ypos) points_tuples.append(pt) pts_legend_Y[graph_point.value] = ypos if highlighted: points_h[pt] = graph_point if self._cursor != None and hover_point == None: inter_points = width_interval * width_graph * 0.5 if (self._cursor[0] > pts_legend_X[i] - inter_points and self._cursor[0] < pts_legend_X[i] + inter_points): hover_point = (pts_legend_X[i], ypos) hover = True # Draw vertical lines of the graph (and horizontal if # necessary) color = self._color_dot_lines_hover if hover else self._color_dot_lines painter.setPen( QtGui.QPen(QtGui.QColor(color), 1, QtCore.Qt.DotLine)) line_tuples = [(pts_legend_X[i], self._pad_top), (pts_legend_X[i], self.height() - self._pad_bottom)] line_polys = QtGui.QPolygonF( map(lambda p: QtCore.QPointF(*p), line_tuples)) painter.drawPolyline(line_polys) if hover: line_H = [(self._pad_left, hover_point[1]), (self._pad_left + width_graph, hover_point[1])] line_H_polys = QtGui.QPolygonF( map(lambda p: QtCore.QPointF(*p), line_H)) painter.drawPolyline(line_H_polys) # Draw the lines of the graph width_line = 3 if highlighted else 2 color = self._points_colors[ type_graph] if highlighted else self._points_colors[type_graph].darker(110) painter.setPen(QtGui.QPen(QtGui.QColor(color), width_line)) points_polys = QtGui.QPolygonF( map(lambda p: QtCore.QPointF(*p), points_tuples)) painter.drawPolyline(points_polys) # Draw points for point_tuple in points_tuples: painter.drawEllipse( QtCore.QRectF(point_tuple[0] - 2, point_tuple[1] - 2, 4, 4)) # Draw highlighted graph points... if hover_point != None and self._highlight_graph in self._points_colors.keys(): x_pos, y_pos = hover_point color = self._points_colors[self._highlight_graph].lighter(160) painter.setBrush(QtGui.QBrush(color)) painter.setPen(QtGui.QPen(QtGui.QColor(QtCore.Qt.black), 1)) painter.drawEllipse(QtCore.QRectF(x_pos - 5, y_pos - 5, 10, 10)) painter.setPen(QtGui.QPen(QtGui.QColor(self._color_axis_text), 2)) # Draw X Axis buf_X = 0 for version_nb in sorted(pts_legend_X.keys(), reverse=True): version_str = "v%d" % (version_nb + 1) version_position_X = pts_legend_X[ version_nb] - len(version_str) * 5 if buf_X == 0 or buf_X - version_position_X > 40: pt_legend_X = QtCore.QPointF(version_position_X, self._pad_top + height_graph + 15) painter.drawText(pt_legend_X, version_str) buf_X = version_position_X # Draw Y Axis buf_Y = 0 for value_Y in sorted(pts_legend_Y.keys(), reverse=True): if buf_Y == 0 or pts_legend_Y[value_Y] - buf_Y > 15: value_str = str(value_Y) pt_legend_Y = QtCore.QPointF(self._pad_left - len(value_str) * 15, pts_legend_Y[value_Y] + 10) bounding_rect = QtCore.QRect(0, pts_legend_Y[value_Y] - 4, self._pad_left - 15, pts_legend_Y[value_Y] + 10) painter.drawText( bounding_rect, QtCore.Qt.AlignRight, value_str) buf_Y = pts_legend_Y[value_Y] if hover_point != None: td = QtGui.QTextDocument() td.setHtml(points_h[hover_point].tooltip) width_tooltip = 180 td.setTextWidth(width_tooltip) height_tooltip = td.size().height() pos_topleft = QtCore.QPoint( self._cursor[0] + 5, self._cursor[1] + 5) if pos_topleft.x() + width_tooltip > self.width() - self._pad_right - 10: pos_topleft.setX( self.width() - self._pad_right - width_tooltip - 10) if pos_topleft.y() + height_tooltip > self.height() - self._pad_bottom - 10: pos_topleft.setY( self.height() - self._pad_bottom - height_tooltip - 10) painter.setOpacity(0.6) rect = QtCore.QRect(pos_topleft.x(), pos_topleft.y(), width_tooltip + 20, height_tooltip + 20) painter.fillRect(rect, self._color_tooltip) painter.setOpacity(1.0) painter.translate(pos_topleft.x() + 10, pos_topleft.y() + 10) ctx = QtGui.QAbstractTextDocumentLayout.PaintContext() ctx.clip = QtCore.QRect(0, 0, width_tooltip, height_tooltip) td.documentLayout().draw(painter, ctx) # Place the buttons pad_left = 5 pos_bottom = self.height() - 25 for button_name in ["tracked", "in_tree", "enabled", "total"]: if self._buttons[button_name] != None: width_btn = self._buttons[button_name].width() self._buttons[button_name].move( self.width() - width_btn - pad_left, pos_bottom) pad_left += width_btn + 5 if self._highlight_graph == button_name: btn_css = "background:%s; color:#FFF; border:none; border-radius:3px;" % self._points_colors[ button_name].name() else: btn_css = "background:#444; color:#FFF; border:none; border-radius:3px;" self._buttons[button_name].setStyleSheet(btn_css) # Raise the curve at the first display.... if self.raise_level < 10: self.raise_level += 1 time.sleep(0.000015) self.update()