def __init__(self, item, timeline_range, rect, *args, **kwargs): rect.setHeight(TRANSITION_HEIGHT) super(TransitionItem, self).__init__( item, timeline_range, rect, *args, **kwargs ) self.setBrush( QtGui.QBrush(QtGui.QColor(237, 228, 148, 255)) ) self.setY(TRACK_HEIGHT - TRANSITION_HEIGHT) self.setZValue(2) # add extra bit of shading shading_poly_f = QtGui.QPolygonF() shading_poly_f.append(QtCore.QPointF(0, 0)) shading_poly_f.append(QtCore.QPointF(rect.width(), 0)) shading_poly_f.append(QtCore.QPointF(0, rect.height())) shading_poly = QtWidgets.QGraphicsPolygonItem( shading_poly_f, parent=self) shading_poly.setBrush(QtGui.QBrush(QtGui.QColor(0, 0, 0, 30))) try: shading_poly.setPen(QtCore.Qt.NoPen) except TypeError: shading_poly.setPen(QtCore.Qt.transparent)
def plot_all(self, Res): # original way of plotting all self.clear_scene() # check for empty result if not Res: return # unpack results dictionary x = Res.x y = Res.y wallAngles = Res.wallAngles # Styles bold_pencil = QtGui.QPen(QtCore.Qt.DashLine) bold_pencil.setColor(QtCore.Qt.black) bold_pencil.setWidth(10) # colour list for plotting pens colour_list = [ QtCore.Qt.darkGray, QtCore.Qt.blue, QtCore.Qt.red, QtCore.Qt.green, QtCore.Qt.darkRed, QtCore.Qt.darkMagenta, QtCore.Qt.darkBlue, QtCore.Qt.darkCyan, QtCore.Qt.darkGreen, QtCore.Qt. darkYellow, # for some reason the checkbox label goes black with this color QtCore.Qt.gray, QtCore.Qt.lightGray, # too light QtCore.Qt.cyan ] # plot geometry centre line rect = QtGui.QPolygonF() for i in range(len(x)): rect.append(QtCore.QPointF(x[i], -y[i])) self.scene.addPolygon(rect, pen=bold_pencil) # calculate largest dimension of cross-section section_dim = max(max(y) - min(y), max(x) - min(x)) for j in range(Res.plot_count ): # looping over the different result distributions # select styles pencil = QtGui.QPen(colour_list[j]) # create pen with next colour fill = QtGui.QBrush( colour_list[j]) # create brush with same colour pencil.setWidth(10) check_box = self.check_boxes[j] # check_box = getattr(self, 'checkBox_plot' + str(j+1)) # update checkbox visibility if not check_box.isVisible(): check_box.setVisible(True) check_box.setText(Res.plot_names[j]) check_box.setStyleSheet("color: " + colour_list[j].name.decode()) # plot if checked if check_box.isChecked(): scale = Res.plot_scale[j] * section_dim / max( 1e-12, max(abs(Res.plot_data[j]))) rect = QtGui.QPolygonF() # outline polygon for i in range(len(Res.x)): PX = Res.x[i] + scale * Res.plot_data[j][i] * math.sin( -wallAngles[i]) PY = Res.y[i] + scale * Res.plot_data[j][i] * math.cos( -wallAngles[i]) if Res.x[i] == Res.x[i - 1] and Res.y[i] == Res.y[ i - 1]: # new wall element started rect.append(QtCore.QPointF( Res.x[i], -Res.y[i])) # add plot point at geometric corner # prepare/plot shading if i > 0: # plot shading rect2.append( QtCore.QPointF(Res.x[i], -Res.y[i]) ) # add plot point at geometric corner poly_item = QtWidgets.QGraphicsPolygonItem(rect2) poly_item.setBrush(fill) poly_item.setOpacity(0.2) self.scene.addItem(poly_item) # self.scene.addPolygon(rect2, brush=fill) rect2 = QtGui.QPolygonF() # shading polygon rect2.append(QtCore.QPointF( Res.x[i], -Res.y[i])) # add plot point at geometric corner rect2.append(QtCore.QPointF(PX, -PY)) else: # add point for shading polygon rect2.append(QtCore.QPointF(PX, -PY)) rect.append(QtCore.QPointF(PX, -PY)) # prepare line for mouse clicks line = QtCore.QLineF( PX, -PY, Res.x[i], -Res.y[i]) # x pos. right, y pos. down # line_item = QtWidgets.QGraphicsLineItem(line) line_item = myLine(line) line_item.setPen(pencil) line_item.set_data_str('{}: {:.2f} {}'.format( Res.plot_names[j], Res.plot_data[j][i], Res.plot_units[j])) # string for click ev. line_item.setAcceptHoverEvents(True) self.scene.addItem(line_item) # plot shading rect2.append(QtCore.QPointF( Res.x[i], -Res.y[i])) # add plot point at geometric corner poly_item = QtWidgets.QGraphicsPolygonItem(rect2) poly_item.setBrush(fill) poly_item.setOpacity(0.2) self.scene.addItem(poly_item) # plot result outline self.scene.addPolygon(rect, pen=pencil) # Hide the remaining unneeded check boxes for j in range(Res.plot_count, 10): # check_box = getattr(self, 'checkBox_plot' + str(j + 1)) check_box = self.check_boxes[j] check_box.setVisible(False) # Fit plottet items in view self.fitInView(self.scene.sceneRect(), QtCore.Qt.KeepAspectRatio) # ui->graphicsView->fitInView(_scene->itemsBoundingRect(),Qt::KeepAspectRatio); # consider changing to itemsBoundingRect when sceneRect not updating!! self.scene.mousePressEvent = self.scene_mousePressEvent # overrule QGraphicsSceneEvent