def paint(self, painter, option, index): """ Overrides the selection highlight color. https://www.qtcentre.org/threads/41299-How-to-Change-QTreeView-highlight-color Note: this can actually do alot more than that with the QPalette... which is something I should learn how to use apparently... """ from qtpy.QtGui import QPalette item = index.internalPointer() new_option = QStyleOptionViewItem(option) brush = QBrush() if item.isEnabled(): color = QColor(*iColor["rgba_text"]) else: color = QColor(*iColor["rgba_text_disabled"]) # TODO highlight selection color??? # why did I move this here? brush.setColor(color) # brush2 = QBrush(QColor(0, 255, 0, 128)) new_option.palette.setBrush(QPalette.Normal, QPalette.HighlightedText, brush) # new_option.palette.setBrush(QPalette.Normal, QPalette.Highlight, brush2) QStyledItemDelegate.paint(self, painter, new_option, index) # if option.state == QStyle.State_Selected: # brush2 = QBrush(QColor(0, 255, 255, 128)) return
def paint(self, painter, option, index): QItemDelegate.paint(self, painter, option, index) column = index.column() row = index.row() rect = option.rect # Draw borders pen = QPen() pen.setWidth(1) pen.setColor(QColor('#cdcdcd')) painter.setPen(pen) painter.drawLine(rect.topLeft(), rect.topRight()) if (row == self.current_hover_row() or row == self.current_row() and (self.has_focus_or_context())): brush = QBrush(Qt.SolidPattern) brush.setColor(QColor(255, 255, 255, 100)) painter.fillRect(rect, brush) if row == self.current_row() and column in [const.COL_START]: pen = QPen() pen.setWidth(10) pen.setColor(QColor('#7cbb4c')) painter.setPen(pen) dyt = QPoint(0, 5) dyb = QPoint(0, 4) painter.drawLine(rect.bottomLeft()-dyb, rect.topLeft()+dyt)
class PyDMBitIndicator(QWidget): """ A QWidget which draws a colored circle or rectangle Parameters ---------- parent : QWidget The parent widget for the Label """ def __init__(self, parent=None, circle=False): super(PyDMBitIndicator, self).__init__(parent) self.setAutoFillBackground(True) self.circle = circle self._painter = QPainter() self._brush = QBrush(Qt.SolidPattern) self._pen = QPen(Qt.SolidLine) def paintEvent(self, event): """ Paint events are sent to widgets that need to update themselves, for instance when part of a widget is exposed because a covering widget was moved. Parameters ---------- event : QPaintEvent """ self._painter.begin(self) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, self._painter, self) self._painter.setRenderHint(QPainter.Antialiasing) self._painter.setBrush(self._brush) self._painter.setPen(self._pen) if self.circle: rect = self.rect() w = rect.width() h = rect.height() r = min(w, h) / 2.0 - 2.0 * max(self._pen.widthF(), 1.0) self._painter.drawEllipse(QPoint(w / 2.0, h / 2.0), r, r) else: self._painter.drawRect(self.rect()) self._painter.end() def setColor(self, color): """ Property for the color to be used when drawing Parameters ---------- QColor """ self._brush.setColor(color) self.update() def minimumSizeHint(self): fm = QFontMetrics(self.font()) return QSize(fm.height(), fm.height())
class PyDMBitIndicator(QWidget): """ A QWidget which draws a colored circle or rectangle Parameters ---------- parent : QWidget The parent widget for the Label """ def __init__(self, parent=None, circle=False): super(PyDMBitIndicator, self).__init__(parent) self.setAutoFillBackground(True) self.circle = circle self._painter = QPainter() self._brush = QBrush(Qt.SolidPattern) self._pen = QPen(Qt.SolidLine) def paintEvent(self, event): """ Paint events are sent to widgets that need to update themselves, for instance when part of a widget is exposed because a covering widget was moved. Parameters ---------- event : QPaintEvent """ self._painter.begin(self) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, self._painter, self) self._painter.setRenderHint(QPainter.Antialiasing) self._painter.setBrush(self._brush) self._painter.setPen(self._pen) if self.circle: rect = event.rect() w = rect.width() h = rect.height() r = min(w, h) / 2.0 - 2.0 * max(self._pen.widthF(), 1.0) self._painter.drawEllipse(QPoint(w / 2.0, h / 2.0), r, r) else: self._painter.drawRect(event.rect()) self._painter.end() def setColor(self, color): """ Property for the color to be used when drawing Parameters ---------- QColor """ self._brush.setColor(color) self.update() def minimumSizeHint(self): fm = QFontMetrics(self.font()) return QSize(fm.height(), fm.height())
def paint(self, painter, styles, widget=None): pen1 = QPen(Qt.SolidLine) pen1.setColor(QColor(128, 128, 128)) painter.setPen(pen1) brush1 = QBrush(Qt.SolidPattern) brush1.setColor(self.color) painter.setBrush(brush1) painter.setRenderHint(QPainter.Antialiasing) # 反锯齿 painter.drawRoundedRect(self.boundingRect(), 10, 10)
def paintBoxes(self, painter): rect = QRect( self.start.x(), self.start.y(), self.end.x() - self.start.x(), self.end.y() - self.start.y(), ) painter.setPen(QColor.fromRgb(0, 255, 0)) painter.drawRect(rect) # Now draw the lines for the boxes in the rectangle. x1 = self.start.x() y1 = self.start.y() x2 = self.end.x() y2 = self.end.y() inc_x = (x2 - x1) / self.xDivs inc_y = (y2 - y1) / self.yDivs lines = time.perf_counter() for i in range(1, self.xDivs): painter.drawLine(x1 + i * inc_x, y1, x1 + i * inc_x, y2) for i in range(1, self.yDivs): painter.drawLine(x1, y1 + i * inc_y, x2, y1 + i * inc_y) mid = time.perf_counter() # Now draw the color overlay thing if requested rects = time.perf_counter() if self.color: brushColor = QColor(0, 255, 0, 20) brush = QBrush(brushColor) painter.setBrush(brush) painter.setPen(QColor.fromRgb(0, 255, 0)) for i in range(0, self.xDivs): for j in range(0, self.yDivs): alpha = i / self.yDivs * 255 if True: # j % 2 == 0: brushColor.setAlpha(alpha / 2) brushColor.setGreen(255) else: brushColor.setAlpha(255 / 2) brushColor.setGreen(alpha) brush.setColor(brushColor) painter.setBrush(brush) rect = QRect(x1 + i * inc_x, y1 + j * inc_y, inc_x, inc_y) painter.drawRect(rect) rects2 = time.perf_counter()
def paintEvent(self, event): """Override Qt method.""" painter = QPainter(self) painter.setOpacity(self.current_opacity) max_width = ( SASS_VARIABLES.WIDGET_CONTENT_TOTAL_WIDTH - 2 * SASS_VARIABLES.WIDGET_CONTENT_PADDING - 2 * SASS_VARIABLES.WIDGET_CONTENT_MARGIN ) # Hover top br = self.label_icon.rect().bottomRight() tl = self.label_icon.rect().topLeft() + QPoint(1, 1) y = br.y() + self.label_text.height() - 2 # br_new = QPoint(br.x() + 2, y) - QPoint(1, 1) br_new = QPoint(max_width - 1, y) - QPoint(1, 1) rect_hover = QRect(tl, br_new) # 2 is the border pen = QPen(Qt.NoPen) brush = QBrush(Qt.SolidPattern) brush.setColor(QColor('#fff')) painter.setBrush(brush) painter.setPen(pen) painter.drawRect(rect_hover) font = self.font() font.setPointSize(10) painter.setFont(font) pen = QPen() pen.setColor(QColor('black')) painter.setPen(pen) td = self.text_document td.setPageSize(QSizeF(rect_hover.size())) td.setHtml(self.summary) td.drawContents(painter) self.raise_()
class MarkerFactory(QObject): """ Parameters ------------ shape: Shape See config.Shape color: str, QColor Color of the marker. Marker must be filled and outlined with the same color. size: int Width and height of the output QGraphicsItem filled: bool Whether to fill the item. """ _shape_to_class = EnumDict([(Shape.CIRCLE, EllipseMarker), (Shape.DIAMOND, DiamondMarker)]) def __init__(self, shape=Shape.CIRCLE, color=Qt.red, size=3, filled=True): super().__init__() self._brush = QBrush() self._pen = QPen() self.set_marker_shape(shape) self.set_marker_size(size) self.set_marker_color(color) self.set_marker_fill(filled) def get_marker_color(self) -> QColor: return self._pen.color() def set_marker_color(self, color: Union[QColor, int]): # XXX: what's the difference between settning alpha to 0 and # passing Qt.NoBrush to the constructor? # filled = deepcopy(self._brush.style()) self._pen.setColor(color) self._brush.setColor(color) # self._brush.setStyle(filled) def get_marker_fill(self) -> bool: return bool(self._brush.style()) def set_marker_fill(self, f: bool): if f: style = Qt.SolidPattern else: style = Qt.NoBrush self._brush.setStyle(style) def get_marker_shape(self) -> Shape: return self._shape def set_marker_shape(self, shape: Shape): self._shape = shape def get_marker_size(self): return self._size def set_marker_size(self, size: int): self._size = size def __call__(self, x: float, y: float, **kwargs: Any) -> Marker: """ Create a Marker with the properties specified by this Factory. Parameters ------------ x : float y : float X, Y coordinates of the marker. key : int Index that this marker will have in the parent VGraphicsGroup. """ marker = TunableMarker(self._shape, self._size, key=None, **kwargs) marker.set_marker_color(self.get_marker_color()) marker.set_marker_fill(self.get_marker_fill()) # marker = self._marker_class(self._size, **kwargs) # marker.setBrush(self._brush) # marker.setPen(self._pen) marker.setPos(x, y) return marker
class TunableMarker(QGraphicsItem): def __init__(self, shape: Shape, size: int, key: Optional[int] = None, **kwargs): super().__init__() self._shape = shape self._size = size self.key = key self.info = kwargs self._pen = QPen() self._pen.setWidthF(0.25) self._brush = QBrush() def _make_rect(self, length): # make a rectangle of width and height equal to 'length' and centered # about (0, 0) return QRectF(-length / 2, -length / 2, length, length) def boundingRect(self): return self._make_rect(self._size + 4) def _paint_ellipse(self, painter): rect = self._make_rect(self._size) painter.drawEllipse(rect) def _paint_diamond(self, painter): size = self._size X = [0, size / 2, 0, -size / 2, 0] Y = [size / 2, 0, -size / 2, 0, size / 2] points = [QPointF(x, y) for x, y in zip(X, Y)] polygon = QPolygonF(points) painter.drawPolygon(polygon, len(points)) def paint(self, painter: QPainter, option: QStyleOptionGraphicsItem, widget: QWidget): painter.setPen(self._pen) painter.setBrush(self._brush) shape_to_paint_method = { Shape.CIRCLE: self._paint_ellipse, Shape.DIAMOND: self._paint_diamond } drawing_method = shape_to_paint_method[self._shape] drawing_method(painter) def get_marker_color(self): """ Marker color. """ return self._pen.color() def set_marker_color(self, color: Union[QColor, int]): color = QColor(color) self._pen.setColor(color) self._brush.setColor(color) self.update() def get_marker_fill(self) -> bool: """ Whether this marker is filled in or merely an outline. """ return bool(self._brush.style()) def set_marker_fill(self, f: bool): if f: f = Qt.SolidPattern else: f = Qt.NoBrush self._brush.setStyle(f) self.update() def get_marker_shape(self): return self._shape def set_marker_shape(self, shape: Shape): self._shape = shape self.update() def get_marker_size(self): return self._size def set_marker_size(self, sz: int): self._size = sz self.update()
def paint(self, painter, option, index): if index.row() > 3: brush = QBrush() brush.setColor('red') option.backgroundBrush = brush super().paint(painter, option, index)