def updatePath(self): axeSize = self.size yZero = self.yZero try: X = [] for item in self.fixedPoints: X.extend([item.B.x() + item.x(), item.C.x() + item.x()]) X = np.array(X) Y = np.array( [-(item.A.y() + item.y()) for item in self.fixedPoints]) + yZero T = displacementSpline(X, Y, self.xCoords, clippingInterval=[-self.scene().axeSize, 0], period=self.period) self.spline = [ QPointF(x, y + yZero) for x, y in zip(self.xCoords, -T) ] # scene coord. # build path polygon = QPolygonF(self.spline) qpp = QPainterPath() qpp.addPolygon(polygon) # stroke path stroker = QPainterPathStroker() stroker.setWidth(self.strokeWidth) mboundingPath = stroker.createStroke(qpp) self.setPath(mboundingPath) except ValueError: pass
def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: Optional[QWidget] = ...): painter.setPen(self.pen()) brush = self.brush() painter.setBrush(brush) path = QPainterPath() path.moveTo(self.__sourcePoint) path.cubicTo(self._edge1, self._edge2, self.__destPoint) if self._isDigraph: painter.setBrush(Qt.gray) painter.drawPolygon(self.drawArrow()) path.addPolygon(self.drawArrow()) painter.setBrush(Qt.NoBrush) if self.isSelected(): pen = painter.pen() pen.setColor(self.get_isSelectedPenColor()) else: pen = painter.pen() pen.setColor(self.get_noSelectedPenColor()) painter.setPen(pen) painter.drawPath(path) self.__path = path
def updatePath(self, calculate=True): """ Calculates (if calculate=true) and displays the spline. Called by activePoint and activeTangent mouse event @param calculate: @type calculate: bool """ # calculate the array of slopes d = [ item.controlPoint - item.contactPoint for item in self.fixedTangents ] d1 = -np.array(list(map(lambda a: a.y(), d))) d2 = np.array(list(map(lambda a: a.x(), d))) d = d1 / d2 # add boundary points if needed X = [item.x() for item in self.fixedPoints] Y = [item.y() for item in self.fixedPoints] X0, X1 = X[0], X[-1] Y0, Y1 = Y[0], Y[-1] t = (Y1 - Y0) / (X1 - X0) Y2 = Y0 - X0 * t Y3 = Y0 + (self.size - X0) * t d = d.tolist() if X[0] > 0.0: X.insert(0, 0.0) Y.insert(0, Y2) d.insert(0, t) if X[-1] < self.size: X.append(self.size) Y.append(Y3) d.append(t) d = np.array(d) try: if calculate: T = interpolationQuadSpline( np.array(X) / self.size, -np.array(Y) / self.size, d) * self.size self.spline = [ QPointF(x, y) for x, y in zip(np.arange(256) * (self.size / 255.0), -T) ] for P in self.spline: if P.x() < X0: P.setY(Y0) elif P.x() > X1: P.setY(Y1) polygon = QPolygonF(self.spline) qpp = QPainterPath() qpp.addPolygon(polygon) # stroke path stroker = QPainterPathStroker() stroker.setWidth(self.strokeWidth) mboundingPath = stroker.createStroke(qpp) self.setPath(mboundingPath) except Exception as e: print(str(e))
def paintEngine(self): myGradient = QLinearGradient() myPen = QPen() myPolygon = QPolygonF() myPath = QPainterPath() myPath.addPolygon(myPolygon) painter = QPainter() painter.setBrush(myGradient) painter.setPen(myPen) painter.drawPath(myPath)
def updatePath(self): """ Update and display the spline. Should be called after each control point or tangent modification : see activePoint and activeTangent mouse event handlers """ # add ending control points, if needed, # to get full range 0..self.size X = [item.x() for item in self.fixedPoints] Y = [item.y() for item in self.fixedPoints] X0, X1 = X[0], X[-1] Y0, Y1 = Y[0], Y[-1] Y2 = Y0 - X0 * (Y1 - Y0) / (X1 - X0) Y3 = Y0 + (self.size - X0) * (Y1 - Y0) / (X1 - X0) if X[0] > 0.0: X.insert(0, 0.0) Y.insert(0, Y2) if X[-1] < self.size: X.append(self.size) Y.append(Y3) # interpolate try: # interpolationCubSpline raises an exception if two points have identical x-coordinates self.spline = interpolationCubSpline( np.array(X), np.array(Y), clippingInterval=[-self.scene().axeSize, 0]) # set the curve constant outside ]X0..X1[ for P in self.spline: if P.x() < X0: P.setY(Y0) elif P.x() > X1: P.setY(Y1) # build path polygon = QPolygonF(self.spline) qpp = QPainterPath() qpp.addPolygon(polygon) # stroke path stroker = QPainterPathStroker() stroker.setWidth(self.strokeWidth) mboundingPath = stroker.createStroke(qpp) self.setPath(mboundingPath) except ValueError: pass
def shape(self) -> QPainterPath: """ Returns the shape of the Port as a QPainterPath. Ports are shaped like an inverted, elongated pentagon. :return: The shape of the Port as a QPainterPath. :rtype: QPainterPath """ # Create the polygon (pentagon-like shape) poly = QPolygon() poly << QPoint(PortGraphics.X_POS, PortGraphics.Y_POS) poly << QPoint(PortGraphics.X_POS + PortGraphics.WIDTH, PortGraphics.Y_POS) poly << QPoint(PortGraphics.X_POS + PortGraphics.WIDTH, PortGraphics.Y_POS + PortGraphics.SIDE_HEIGHT) poly << QPoint(0, PortGraphics.Y_POS + PortGraphics.TOTAL_HEIGHT) poly << QPoint(PortGraphics.X_POS, PortGraphics.Y_POS + PortGraphics.SIDE_HEIGHT) poly << QPoint(PortGraphics.X_POS, PortGraphics.Y_POS) path = QPainterPath() path.addPolygon(poly) return path
def drawChannelHistogram(painter, hist, bin_edges, color): # Draw histogram for a single channel. # param painter: QPainter # param hist: histogram to draw # smooth the histogram (first and last bins excepted) for a better visualization of clipping. # hist = np.concatenate(([hist[0]], SavitzkyGolayFilter.filter(hist[1:-1]), [hist[-1]])) # To emphasize significant values we clip the first bin to max height of the others M = max(hist[1: ]) # max(hist) # max(hist[1:-1]) imgH = size.height() # drawing trapezia instead of rectangles to quickly "smooth" the histogram # the rightmost trapezium is not drawn poly = QPolygonF() poly.append(QPointF(range[0], imgH)) # bottom left point for i, y in enumerate(hist): try: h = imgH * y / M if np.isnan(h): raise ValueError except (ValueError, ArithmeticError, FloatingPointError, ZeroDivisionError): # don't draw the histogram for this channel if M is too small return poly.append(QPointF((bin_edges[i] - range[0]) * scale, max(imgH - h, 0))) # clipping indicators if i == 0 or i == len(hist)-1: left = bin_edges[0 if i == 0 else -1] * scale left = left - (20 if i > 0 else 0) # shift the indicator at right percent = hist[i] * (bin_edges[i+1]-bin_edges[i]) if percent > clipping_threshold: # set the color of the indicator according to percent value nonlocal gPercent gPercent = min(gPercent, np.clip((0.05 - percent) / 0.03, 0, 1)) painter.fillRect(left, 0, 10, 10, QColor(255, 255*gPercent, 0)) # draw the filled polygon poly.append(QPointF(poly.constLast().x(), imgH)) # bottom right point path = QPainterPath() path.addPolygon(poly) path.closeSubpath() painter.setPen(Qt.NoPen) painter.fillPath(path, QBrush(color))
def load_json_world(self, file): if not os.path.isfile(file): print("Error reading world file, check config params:", file) return False with open(file, "r") as read_file: json_data = json.load(read_file) polygon_points = [] paths_count = 0 for item in json_data: if 'json_geometry' in item: geometry = item['json_geometry'] if geometry['type'] == 'Polygon': for coord in geometry['coordinates'][0]: if isinstance(coord, list) and ( (isinstance(coord, list) and len(coord) == 2) or (len(coord) == 3 and coord[3] == 0)): current_point = QPointF(coord[0], coord[1]) polygon_points.append(current_point) else: print("Unknown coord", geometry["coordinates"][0]) polygon = QPolygonF(polygon_points) path = QPainterPath() path.addPolygon(polygon) contour = QGraphicsPathItem(path) # r = lambda: random.randint(0, 255) # next_color = '#%02X%02X%02X' % (r(), r(), r()) contour.setPen(QPen(QColor("red"), 0.1)) contour.setBrush(QBrush(Qt.transparent)) # if paths_count == 4: print(item['json_featuretype']) self._scene.addItem(contour) paths_count += 1 self.update()
def _pointsToPath(self, points: List[QtCore.QPointF]) -> QPainterPath: """Converts list of `QtCore.QPointF` objects to a `QPainterPath`.""" path = QPainterPath() path.addPolygon(QPolygonF(points)) return path