def updatePaintNode(self, old_node, _): if not old_node: self.node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), self.m_segment_count) geometry.setLineWidth(2) geometry.setDrawingMode(QSGGeometry.DrawLineStrip) self.node.setGeometry(geometry) self.node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor(255, 0, 0)) self.node.setMaterial(material) self.node.setFlag(QSGNode.OwnsMaterial) else: geometry = self.node.geometry() geometry.allocate(self.m_segment_count) w = self.width() h = self.height() vertices = geometry.vertexDataAsPoint2D() for i in range(self.m_segment_count): t = i / (self.m_segment_count - 1) invt = 1 - t pos = invt * invt * invt * self.m_p1 \ + 3 * invt * invt * t * self.m_p2 \ + 3 * invt * t * t * self.m_p3 \ + t * t * t * self.m_p4 vertices[i].set(pos.x() * w, pos.y() * h) self.node.markDirty(QSGNode.DirtyGeometry) return self.node
def updatePaintNode(self, old_node, node_data): if self._container is None: return self._root_node if self._showsig_trig: self._showsig_trig = False if self._root_node is not None: self._root_node.removeAllChildNodes() del self._root_node self._root_node = None if self._root_node is None: self._root_node = QSGNode() for i in range(self._container.num_signals): # создаем ноды для рисования графиков transform_node = QSGTransformNode() transform_matrix = transform_node.matrix() transform_matrix.translate(0, self._lineshift_y * (2 * i + 1)) transform_matrix.scale(self._xscales[i], -self._yscale) transform_node.setMatrix(transform_matrix) transform_node.markDirty(QSGNode.DirtyMatrix) geometry_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), self._visible_signal_sizes[i]) geometry.setLineWidth(1.0) geometry.setDrawingMode(QSGGeometry.GL_LINE_STRIP) geometry_node.setGeometry(geometry) geometry_node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor("#4CAF50")) geometry_node.setMaterial(material) geometry_node.setFlag(QSGNode.OwnsMaterial) transform_node.appendChildNode(geometry_node) self._root_node.appendChildNode(transform_node) eeg_signals = self._container.signals fd = self._container.fd for i in range(self._container.num_signals): curr_signal = eeg_signals[i] curr_timeaxe = self._time_axes[i] curr_transform_node = self._root_node.childAtIndex(i) curr_geometry_node = curr_transform_node.firstChild() curr_geometry = curr_geometry_node.geometry() curr_vertexes = curr_geometry.vertexDataAsPoint2D() curr_shift = int(self._time_shift * fd[i]) for j in range(self._visible_signal_sizes[i]): curr_vertexes[j].set(curr_timeaxe[j], curr_signal[j + curr_shift]) curr_geometry_node.markDirty(QSGNode.DirtyGeometry) return self._root_node
def updatePaintNode(self, paint_node, update_data): if paint_node is None: paint_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 6, 12) geometry.setDrawingMode(QSGGeometry.GL_TRIANGLES) geometry.vertexDataAsPoint2D()[0].set(self._corner_size, 0) geometry.vertexDataAsPoint2D()[1].set(0, self._corner_size) geometry.vertexDataAsPoint2D()[2].set(0, self.height()) geometry.vertexDataAsPoint2D()[3].set(self.width() - self._corner_size, self.height()) geometry.vertexDataAsPoint2D()[4].set(self.width(), self.height() - self._corner_size) geometry.vertexDataAsPoint2D()[5].set(self.width(), 0) geometry.indexDataAsUShort()[0] = 0 geometry.indexDataAsUShort()[1] = 1 geometry.indexDataAsUShort()[2] = 2 geometry.indexDataAsUShort()[3] = 0 geometry.indexDataAsUShort()[4] = 2 geometry.indexDataAsUShort()[5] = 3 geometry.indexDataAsUShort()[6] = 0 geometry.indexDataAsUShort()[7] = 3 geometry.indexDataAsUShort()[8] = 4 geometry.indexDataAsUShort()[9] = 0 geometry.indexDataAsUShort()[10] = 4 geometry.indexDataAsUShort()[11] = 5 paint_node.setGeometry(geometry) material = QSGFlatColorMaterial() material.setColor(self._color) paint_node.setMaterial(material) # For PyQt 5.4, I need to store these otherwise they will be garbage collected before rendering # and never show up, but otherwise never crash. self._paint_node = paint_node self._geometry = geometry self._material = material return paint_node
def updatePaintNode(self, oldNode, nodeData): if self._root_node is None: self._root_node = QSGGeometryNode() geometry = QSGGeometry( QSGGeometry.defaultAttributes_Point2D(), self._segmentCount ) geometry.setLineWidth(2) geometry.setDrawingMode(QSGGeometry.GL_LINE_STRIP) self._root_node.setGeometry(geometry) self._root_node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor(255, 0, 0)) self._root_node.setMaterial(material) self._root_node.setFlag(QSGNode.OwnsMaterial) else: geometry = self._root_node.geometry() geometry.allocate(self._segmentCount) w = self.width() h = self.height() vertices = geometry.vertexDataAsPoint2D() for i in range(self._segmentCount): t = i / float(self._segmentCount - 1) invt = 1 - t pos = ( invt * invt * invt * self._p1 + 3 * invt * invt * t * self._p2 + 3 * invt * t * t * self._p3 + t * t * t * self._p4 ) vertices[i].set(pos.x() * w, pos.y() * h) self._root_node.markDirty(QSGNode.DirtyGeometry) return self._root_node
def updatePaintNode(self, paint_node, update_data): if paint_node is None: paint_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 6, 12) geometry.setDrawingMode(QSGGeometry.GL_TRIANGLES) geometry.vertexDataAsPoint2D()[0].set(self._corner_size, 0) geometry.vertexDataAsPoint2D()[1].set(0, self._corner_size) geometry.vertexDataAsPoint2D()[2].set(0, self.height()) geometry.vertexDataAsPoint2D()[3].set(self.width() - self._corner_size, self.height()) geometry.vertexDataAsPoint2D()[4].set( self.width(), self.height() - self._corner_size) geometry.vertexDataAsPoint2D()[5].set(self.width(), 0) geometry.indexDataAsUShort()[0] = 0 geometry.indexDataAsUShort()[1] = 1 geometry.indexDataAsUShort()[2] = 2 geometry.indexDataAsUShort()[3] = 0 geometry.indexDataAsUShort()[4] = 2 geometry.indexDataAsUShort()[5] = 3 geometry.indexDataAsUShort()[6] = 0 geometry.indexDataAsUShort()[7] = 3 geometry.indexDataAsUShort()[8] = 4 geometry.indexDataAsUShort()[9] = 0 geometry.indexDataAsUShort()[10] = 4 geometry.indexDataAsUShort()[11] = 5 paint_node.setGeometry(geometry) material = QSGFlatColorMaterial() material.setColor(self._color) paint_node.setMaterial(material) # For PyQt 5.4, I need to store these otherwise they will be garbage collected before rendering # and never show up, but otherwise never crash. self._paint_node = paint_node self._geometry = geometry self._material = material return paint_node
def updatePaintNode(self, oldNode, nodeData): if self._root_node is None: self._root_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), self._segmentCount) geometry.setLineWidth(2) geometry.setDrawingMode(QSGGeometry.GL_LINE_STRIP) self._root_node.setGeometry(geometry) self._root_node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor(255, 0, 0)) self._root_node.setMaterial(material) self._root_node.setFlag(QSGNode.OwnsMaterial) else: geometry = self._root_node.geometry() geometry.allocate(self._segmentCount) w = self.width() h = self.height() vertices = geometry.vertexDataAsPoint2D() for i in range(self._segmentCount): t = i / float(self._segmentCount - 1) invt = 1 - t pos = invt * invt * invt * self._p1 \ + 3 * invt * invt * t * self._p2 \ + 3 * invt * t * t * self._p3 \ + t * t * t * self._p4 vertices[i].set(pos.x() * w, pos.y() * h) self._root_node.markDirty(QSGNode.DirtyGeometry) return self._root_node
def updatePaintNode(self, paint_node, update_data): self._node = QSGGeometryNode() self._geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 6, 12) self._geometry.setDrawingMode(0x0004) self._geometry.vertexDataAsPoint2D()[0].set(self._corner_size, 0) self._geometry.vertexDataAsPoint2D()[1].set(0, self._corner_size) self._geometry.vertexDataAsPoint2D()[2].set(0, self.height()) self._geometry.vertexDataAsPoint2D()[3].set(self.width() - self._corner_size, self.height()) self._geometry.vertexDataAsPoint2D()[4].set(self.width(), self.height() - self._corner_size) self._geometry.vertexDataAsPoint2D()[5].set(self.width(), 0) self._geometry.indexDataAsUShort()[0] = 0 self._geometry.indexDataAsUShort()[1] = 1 self._geometry.indexDataAsUShort()[2] = 2 self._geometry.indexDataAsUShort()[3] = 0 self._geometry.indexDataAsUShort()[4] = 2 self._geometry.indexDataAsUShort()[5] = 3 self._geometry.indexDataAsUShort()[6] = 0 self._geometry.indexDataAsUShort()[7] = 3 self._geometry.indexDataAsUShort()[8] = 4 self._geometry.indexDataAsUShort()[9] = 0 self._geometry.indexDataAsUShort()[10] = 4 self._geometry.indexDataAsUShort()[11] = 5 self._node.setGeometry(self._geometry) self._material = QSGFlatColorMaterial() self._material.setColor(self._color) self._node.setMaterial(self._material) return self._node
def updatePaintNode(self, paint_node, update_data): if paint_node is None: paint_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 7, 9) geometry.setDrawingMode(QSGGeometry.GL_TRIANGLES) vertex_data = geometry.vertexDataAsPoint2D() vertex_data[0].set(0, 0) vertex_data[1].set(0, self.height()) vertex_data[2].set(self.width(), self.height()) vertex_data[3].set(self.width(), 0) # no arrow by default vertex_data[4].set(0, 0) vertex_data[5].set(0, 0) vertex_data[6].set(0, 0) target_offset = self._target - QPoint(self.x(), self.y()) arrow_on_side = -1 # no arrow arrow_size = 0 arrow_offset = 0 if target_offset.x() >= 0 and target_offset.x() <= self.width(): arrow_size = min(self._arrow_size, self.width() / 2) arrow_offset = max( arrow_size, min(self.width() - arrow_size, target_offset.x())) if target_offset.y() < 0: # top vertex_data[4].set(arrow_offset - arrow_size, 0) vertex_data[5].set(arrow_offset, -arrow_size) vertex_data[6].set(arrow_offset + arrow_size, 0) arrow_on_side = 0 elif target_offset.y() > self.height(): # bottom vertex_data[4].set(arrow_offset - arrow_size, self.height()) vertex_data[5].set(arrow_offset, self.height() + arrow_size) vertex_data[6].set(arrow_offset + arrow_size, self.height()) arrow_on_side = 2 elif target_offset.y() >= 0 and target_offset.y() <= self.height(): arrow_size = min(self._arrow_size, self.height() / 2) arrow_offset = max( arrow_size, min(self.height() - arrow_size, target_offset.y())) if target_offset.x() < 0: # left vertex_data[4].set(0, arrow_offset - arrow_size) vertex_data[5].set(-arrow_size, arrow_offset) vertex_data[6].set(0, arrow_offset + arrow_size) arrow_on_side = 3 elif target_offset.x() > self.width(): # right vertex_data[4].set(self.width(), arrow_offset - arrow_size) vertex_data[5].set(self.width() + arrow_size, arrow_offset) vertex_data[6].set(self.width(), arrow_offset + arrow_size) arrow_on_side = 1 index_data = geometry.indexDataAsUShort() index_data[0] = 0 index_data[1] = 1 index_data[2] = 2 index_data[3] = 0 index_data[4] = 2 index_data[5] = 3 index_data[6] = 4 index_data[7] = 5 index_data[8] = 6 paint_node.setGeometry(geometry) material = QSGFlatColorMaterial() material.setColor(self._color) paint_node.setMaterial(material) if self._border_width > 0: if paint_node.childCount() == 0: paint_node.appendChildNode(QSGGeometryNode()) border_node = paint_node.firstChild() border_vertices = [] border_vertices.append((0, 0)) if arrow_on_side == 0: border_vertices.append((arrow_offset - arrow_size, 0)) border_vertices.append((arrow_offset, -arrow_size)) border_vertices.append((arrow_offset + arrow_size, 0)) border_vertices.append((self.width(), 0)) if arrow_on_side == 1: border_vertices.append( (self.width(), arrow_offset - arrow_size)) border_vertices.append( (self.width() + arrow_size, arrow_offset)) border_vertices.append( (self.width(), arrow_offset + arrow_size)) border_vertices.append((self.width(), self.height())) if arrow_on_side == 2: border_vertices.append( (arrow_offset + arrow_size, self.height())) border_vertices.append( (arrow_offset, self.height() + arrow_size)) border_vertices.append( (arrow_offset - arrow_size, self.height())) border_vertices.append((0, self.height())) if arrow_on_side == 3: border_vertices.append((0, arrow_offset + arrow_size)) border_vertices.append((-arrow_size, arrow_offset)) border_vertices.append((0, arrow_offset - arrow_size)) border_geometry = QSGGeometry( QSGGeometry.defaultAttributes_Point2D(), 2 * len(border_vertices), 0) border_geometry.setDrawingMode(QSGGeometry.GL_LINES) border_geometry.setLineWidth(self._border_width) border_vertex_data = border_geometry.vertexDataAsPoint2D() for index in range(len(border_vertices)): start_vertex = border_vertices[index] end_vertex = border_vertices[index + 1] if index < len( border_vertices) - 1 else border_vertices[0] border_vertex_data[2 * index].set(start_vertex[0], start_vertex[1]) border_vertex_data[2 * index + 1].set(end_vertex[0], end_vertex[1]) border_node.setGeometry(border_geometry) border_material = QSGFlatColorMaterial() border_material.setColor(self._border_color) border_node.setMaterial(border_material) else: border_node = None border_geometry = None border_material = None paint_node.removeAllChildNodes() # For PyQt 5.4, I need to store these otherwise they will be garbage collected before rendering # and never show up, but otherwise never crash. self._node = paint_node self._geometry = geometry self._material = material self._border_node = border_node self._border_geometry = border_geometry self._border_material = border_material return paint_node
class BezierCurve(QQuickItem): p1Changed = pyqtSignal(QPointF) @pyqtProperty(QPointF, notify=p1Changed) def p1(self): return self._p1 @p1.setter def p1(self, p): if self._p1 != p: self._p1 = QPointF(p) self.p1Changed.emit(p) self.update() p2Changed = pyqtSignal(QPointF) @pyqtProperty(QPointF, notify=p2Changed) def p2(self): return self._p2 @p2.setter def p2(self, p): if self._p2 != p: self._p2 = QPointF(p) self.p2Changed.emit(p) self.update() p3Changed = pyqtSignal(QPointF) @pyqtProperty(QPointF, notify=p3Changed) def p3(self): return self._p3 @p3.setter def p3(self, p): if self._p3 != p: self._p3 = QPointF(p) self.p3Changed.emit(p) self.update() p4Changed = pyqtSignal(QPointF) @pyqtProperty(QPointF, notify=p4Changed) def p4(self): return self._p4 @p4.setter def p4(self, p): if self._p4 != p: self._p4 = QPointF(p) self.p4Changed.emit(p) self.update() segmentCountChanged = pyqtSignal(int) @pyqtProperty(int, notify=segmentCountChanged) def segmentCount(self): return self._segmentCount @segmentCount.setter def segmentCount(self, count): if self._segmentCount != count: self._segmentCount = count self.segmentCountChanged.emit(count) self.update() def __init__(self, parent=None): super(BezierCurve, self).__init__(parent) self._p1 = QPointF(0, 0) self._p2 = QPointF(1, 0) self._p3 = QPointF(0, 1) self._p4 = QPointF(1, 1) self._segmentCount = 32 self._root_node = None self.setFlag(QQuickItem.ItemHasContents, True) def updatePaintNode(self, oldNode, nodeData): if self._root_node is None: self._root_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), self._segmentCount) geometry.setLineWidth(2) geometry.setDrawingMode(QSGGeometry.GL_LINE_STRIP) self._root_node.setGeometry(geometry) self._root_node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor(255, 0, 0)) self._root_node.setMaterial(material) self._root_node.setFlag(QSGNode.OwnsMaterial) else: geometry = self._root_node.geometry() geometry.allocate(self._segmentCount) w = self.width() h = self.height() vertices = geometry.vertexDataAsPoint2D() for i in range(self._segmentCount): t = i / float(self._segmentCount - 1) invt = 1 - t pos = invt * invt * invt * self._p1 \ + 3 * invt * invt * t * self._p2 \ + 3 * invt * t * t * self._p3 \ + t * t * t * self._p4 vertices[i].set(pos.x() * w, pos.y() * h) self._root_node.markDirty(QSGNode.DirtyGeometry) return self._root_node
def updatePaintNode(self, paint_node, update_data): if paint_node is None: paint_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 7, 9) geometry.setDrawingMode(QSGGeometry.GL_TRIANGLES) geometry.vertexDataAsPoint2D()[0].set(0, 0) geometry.vertexDataAsPoint2D()[1].set(0, self.height()) geometry.vertexDataAsPoint2D()[2].set(self.width(), self.height()) geometry.vertexDataAsPoint2D()[3].set(self.width(), 0) # no arrow by default geometry.vertexDataAsPoint2D()[4].set(0, 0) geometry.vertexDataAsPoint2D()[5].set(0, 0) geometry.vertexDataAsPoint2D()[6].set(0, 0) target_offset = self._target - QPoint(self.x(), self.y()) if target_offset.x() >= 0 and target_offset.x() <= self.width(): arrow_size = min(self._arrow_size, self.width()/2) arrow_offset = max(arrow_size, min(self.width() - arrow_size, target_offset.x())) if target_offset.y() < 0: # top geometry.vertexDataAsPoint2D()[4].set(arrow_offset - arrow_size, 0) geometry.vertexDataAsPoint2D()[5].set(arrow_offset, - arrow_size) geometry.vertexDataAsPoint2D()[6].set(arrow_offset + arrow_size, 0) elif target_offset.y() > self.height(): # bottom geometry.vertexDataAsPoint2D()[4].set(arrow_offset - arrow_size, self.height()) geometry.vertexDataAsPoint2D()[5].set(arrow_offset, self.height() +arrow_size) geometry.vertexDataAsPoint2D()[6].set(arrow_offset + arrow_size, self.height()) elif target_offset.y() >= 0 and target_offset.y() <= self.height(): arrow_size = min(self._arrow_size, self.height()/2) arrow_offset = max(arrow_size, min(self.height() - arrow_size, target_offset.y())) if target_offset.x() < 0: # left geometry.vertexDataAsPoint2D()[4].set(0, arrow_offset - arrow_size) geometry.vertexDataAsPoint2D()[5].set(-arrow_size, arrow_offset) geometry.vertexDataAsPoint2D()[6].set(0, arrow_offset + arrow_size) elif target_offset.x() > self.width(): # right geometry.vertexDataAsPoint2D()[4].set(self.width(), arrow_offset - arrow_size) geometry.vertexDataAsPoint2D()[5].set(self.width() + arrow_size, arrow_offset) geometry.vertexDataAsPoint2D()[6].set(self.width(), arrow_offset + arrow_size) geometry.indexDataAsUShort()[0] = 0 geometry.indexDataAsUShort()[1] = 1 geometry.indexDataAsUShort()[2] = 2 geometry.indexDataAsUShort()[3] = 0 geometry.indexDataAsUShort()[4] = 2 geometry.indexDataAsUShort()[5] = 3 geometry.indexDataAsUShort()[6] = 4 geometry.indexDataAsUShort()[7] = 5 geometry.indexDataAsUShort()[8] = 6 paint_node.setGeometry(geometry) material = QSGFlatColorMaterial() material.setColor(self._color) paint_node.setMaterial(material) # For PyQt 5.4, I need to store these otherwise they will be garbage collected before rendering # and never show up, but otherwise never crash. self._paint_node = paint_node self._geometry = geometry self._material = material return paint_node
class BezierCurve(QQuickItem): # Signals p1_changed = pyqtSignal('QPointF') p2_changed = pyqtSignal('QPointF') p3_changed = pyqtSignal('QPointF') p4_changed = pyqtSignal('QPointF') segment_count_changed = pyqtSignal(int) #Properties @pyqtProperty(QPointF, notify=p1_changed) def p1(self): return self.m_p1 @p1.setter def p1(self, p): if self.m_p1 != p: self.m_p1 = p self.p1_changed.emit(p) self.update() @pyqtProperty(QPointF, notify=p2_changed) def p2(self): return self.m_p2 @p2.setter def p2(self, p): if self.m_p2 != p: self.m_p2 = p self.p2_changed.emit(p) self.update() @pyqtProperty(QPointF, notify=p3_changed) def p3(self): return self.m_p3 @p3.setter def p3(self, p): if self.m_p3 != p: self.m_p3 = p self.p3_changed.emit(p) self.update() @pyqtProperty(QPointF, notify=p4_changed) def p4(self): return self.m_p4 @p4.setter def p4(self, p): if self.m_p4 != p: self.m_p4 = p self.p4_changed.emit(p) self.update() @pyqtProperty(int, notify=segment_count_changed) def segment_count(self): return self.m_segment_count @segment_count.setter def segment_count(self, s): if self.m_segment_count != s: self.m_segment_count = s self.segment_count_changed.emit(s) self.update() def __init__(self, parent=None): super().__init__(parent=parent) self.m_p1 = QPointF(0, 0) self.m_p2 = QPointF(1, 0) self.m_p3 = QPointF(0, 1) self.m_p4 = QPointF(1, 1) self.m_segment_count = 16 self.setFlag(QQuickItem.ItemHasContents, True) self.node = None def updatePaintNode(self, old_node, _): if not old_node: self.node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), self.m_segment_count) geometry.setLineWidth(2) geometry.setDrawingMode(QSGGeometry.DrawLineStrip) self.node.setGeometry(geometry) self.node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor(255, 0, 0)) self.node.setMaterial(material) self.node.setFlag(QSGNode.OwnsMaterial) else: geometry = self.node.geometry() geometry.allocate(self.m_segment_count) w = self.width() h = self.height() vertices = geometry.vertexDataAsPoint2D() for i in range(self.m_segment_count): t = i / (self.m_segment_count - 1) invt = 1 - t pos = invt * invt * invt * self.m_p1 \ + 3 * invt * invt * t * self.m_p2 \ + 3 * invt * t * t * self.m_p3 \ + t * t * t * self.m_p4 vertices[i].set(pos.x() * w, pos.y() * h) self.node.markDirty(QSGNode.DirtyGeometry) return self.node
def updatePaintNode(self, paint_node, update_data): if paint_node is None: paint_node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 7, 9) geometry.setDrawingMode(QSGGeometry.GL_TRIANGLES) geometry.vertexDataAsPoint2D()[0].set(0, 0) geometry.vertexDataAsPoint2D()[1].set(0, self.height()) geometry.vertexDataAsPoint2D()[2].set(self.width(), self.height()) geometry.vertexDataAsPoint2D()[3].set(self.width(), 0) # no arrow by default geometry.vertexDataAsPoint2D()[4].set(0, 0) geometry.vertexDataAsPoint2D()[5].set(0, 0) geometry.vertexDataAsPoint2D()[6].set(0, 0) target_offset = self._target - QPoint(self.x(), self.y()) if target_offset.x() >= 0 and target_offset.x() <= self.width(): arrow_size = min(self._arrow_size, self.width() / 2) arrow_offset = max( arrow_size, min(self.width() - arrow_size, target_offset.x())) if target_offset.y() < 0: # top geometry.vertexDataAsPoint2D()[4].set( arrow_offset - arrow_size, 0) geometry.vertexDataAsPoint2D()[5].set(arrow_offset, -arrow_size) geometry.vertexDataAsPoint2D()[6].set( arrow_offset + arrow_size, 0) elif target_offset.y() > self.height(): # bottom geometry.vertexDataAsPoint2D()[4].set( arrow_offset - arrow_size, self.height()) geometry.vertexDataAsPoint2D()[5].set( arrow_offset, self.height() + arrow_size) geometry.vertexDataAsPoint2D()[6].set( arrow_offset + arrow_size, self.height()) elif target_offset.y() >= 0 and target_offset.y() <= self.height(): arrow_size = min(self._arrow_size, self.height() / 2) arrow_offset = max( arrow_size, min(self.height() - arrow_size, target_offset.y())) if target_offset.x() < 0: # left geometry.vertexDataAsPoint2D()[4].set( 0, arrow_offset - arrow_size) geometry.vertexDataAsPoint2D()[5].set(-arrow_size, arrow_offset) geometry.vertexDataAsPoint2D()[6].set( 0, arrow_offset + arrow_size) elif target_offset.x() > self.width(): # right geometry.vertexDataAsPoint2D()[4].set( self.width(), arrow_offset - arrow_size) geometry.vertexDataAsPoint2D()[5].set( self.width() + arrow_size, arrow_offset) geometry.vertexDataAsPoint2D()[6].set( self.width(), arrow_offset + arrow_size) geometry.indexDataAsUShort()[0] = 0 geometry.indexDataAsUShort()[1] = 1 geometry.indexDataAsUShort()[2] = 2 geometry.indexDataAsUShort()[3] = 0 geometry.indexDataAsUShort()[4] = 2 geometry.indexDataAsUShort()[5] = 3 geometry.indexDataAsUShort()[6] = 4 geometry.indexDataAsUShort()[7] = 5 geometry.indexDataAsUShort()[8] = 6 paint_node.setGeometry(geometry) material = QSGFlatColorMaterial() material.setColor(self._color) paint_node.setMaterial(material) # For PyQt 5.4, I need to store these otherwise they will be garbage collected before rendering # and never show up, but otherwise never crash. self._paint_node = paint_node self._geometry = geometry self._material = material return paint_node
class AngledCornerRectangle(QQuickItem): def __init__(self, parent = None): super().__init__(parent) self.setFlag(QQuickItem.ItemHasContents) self._corner_size = 0 self._color = QColor(255, 255, 255, 255) self._geometry = None self._material = None self._node = None def getCornerSize(self): return self._corner_size def setCornerSize(self, size): if size != self._corner_size: self._corner_size = size self.update() self.cornerSizeChanged.emit() cornerSizeChanged = pyqtSignal() cornerSize = pyqtProperty(float, fget=getCornerSize, fset=setCornerSize, notify=cornerSizeChanged) def setColor(self, color): if color != self._color: self._color = color self.update() self.colorChanged.emit() colorChanged = pyqtSignal() @pyqtProperty(QColor, fset=setColor, notify=colorChanged) def color(self): return self._color def updatePaintNode(self, paint_node, update_data): self._node = QSGGeometryNode() self._geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), 6, 12) self._geometry.setDrawingMode(0x0004) self._geometry.vertexDataAsPoint2D()[0].set(self._corner_size, 0) self._geometry.vertexDataAsPoint2D()[1].set(0, self._corner_size) self._geometry.vertexDataAsPoint2D()[2].set(0, self.height()) self._geometry.vertexDataAsPoint2D()[3].set(self.width() - self._corner_size, self.height()) self._geometry.vertexDataAsPoint2D()[4].set(self.width(), self.height() - self._corner_size) self._geometry.vertexDataAsPoint2D()[5].set(self.width(), 0) self._geometry.indexDataAsUShort()[0] = 0 self._geometry.indexDataAsUShort()[1] = 1 self._geometry.indexDataAsUShort()[2] = 2 self._geometry.indexDataAsUShort()[3] = 0 self._geometry.indexDataAsUShort()[4] = 2 self._geometry.indexDataAsUShort()[5] = 3 self._geometry.indexDataAsUShort()[6] = 0 self._geometry.indexDataAsUShort()[7] = 3 self._geometry.indexDataAsUShort()[8] = 4 self._geometry.indexDataAsUShort()[9] = 0 self._geometry.indexDataAsUShort()[10] = 4 self._geometry.indexDataAsUShort()[11] = 5 self._node.setGeometry(self._geometry) self._material = QSGFlatColorMaterial() self._material.setColor(self._color) self._node.setMaterial(self._material) return self._node
def updatePaintNode(self, oldNode: QSGGeometryNode, _): if oldNode == None: node = QSGGeometryNode() geometry = QSGGeometry(QSGGeometry.defaultAttributes_Point2D(), self._segment_count) geometry.setLineWidth(3) geometry.setDrawingMode(QSGGeometry.DrawLineStrip) node.setGeometry(geometry) node.setFlag(QSGNode.OwnsGeometry) material = QSGFlatColorMaterial() material.setColor(QColor(45, 100, 120)) node.setMaterial(material) node.setFlag(QSGNode.OwnsMaterial) else: node = oldNode geometry = node.geometry() geometry.allocate(self._segment_count) itemSize = self.size() vertices = geometry.vertexDataAsPoint2D() x1 = self._p1.x() y1 = self._p1.y() vertices[0].set(x1, y1) x2 = self._p2.x() y2 = self._p2.y() vertices[1].set(x2, y2) print(vertices[1].x) node.markDirty(QSGNode.DirtyGeometry) return node