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. This method handles the painting with parameters from the stylesheet, configures the brush, pen and calls ```draw_icon``` so the specifics can be performed for each of the drawing classes. Parameters ---------- event : QPaintEvent """ opt = QStyleOption() opt.initFrom(self) painter = QPainter(self) painter.setClipping(True) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.setRenderHint(QPainter.Antialiasing) x = event.rect().x() y = event.rect().y() w = event.rect().width() h = event.rect().height() painter.translate(w / 2.0, h / 2.0) painter.rotate(self._rotation) painter.translate(-w / 2.0, -h / 2.0) painter.translate(self._pen_width / 2.0, self._pen_width / 2.0) painter.scale(w - self._pen_width, h - self._pen_width) painter.translate(x, y) painter.setBrush(self._brush) painter.setPen(self._pen) self.draw_icon(painter) QWidget.paintEvent(self, event)
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 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 borderPath(self, rect): """ Calculate the painter path for a styled or rounded border When the canvas has no styled background or rounded borders the painter path is empty. :param QRect rect: Bounding rectangle of the canvas :return: Painter path, that can be used for clipping """ if self.testAttribute(Qt.WA_StyledBackground): recorder = QwtStyleSheetRecorder(rect.size()) painter = QPainter(recorder) opt = QStyleOption() opt.initFrom(self) opt.rect = rect self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.end() if not recorder.background.path.isEmpty(): return recorder.background.path if len(recorder.border.rectList) > 0: return qwtCombinePathList(rect, recorder.border.pathlist) elif self.__data.borderRadius > 0.0: fw2 = self.frameWidth() * 0.5 r = QRectF(rect).adjusted(fw2, fw2, -fw2, -fw2) path = QPainterPath() path.addRoundedRect(r, self.__data.borderRadius, self.__data.borderRadius) return path return QPainterPath()
def paintEvent(self, event): painter = QPainter(self) painter.setClipRegion(event.region()) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) self.draw(painter)
def fillPixmap(self, widget, pixmap, offset=None): """ Fill a pixmap with the content of a widget In Qt >= 5.0 `QPixmap.fill()` is a nop, in Qt 4.x it is buggy for backgrounds with gradients. Thus `fillPixmap()` offers an alternative implementation. :param QWidget widget: Widget :param QPixmap pixmap: Pixmap to be filled :param QPoint offset: Offset .. seealso:: :py:meth:`QPixmap.fill()` """ if offset is None: offset = QPoint() rect = QRect(offset, pixmap.size()) painter = QPainter(pixmap) painter.translate(-offset) autoFillBrush = widget.palette().brush(widget.backgroundRole()) if not (widget.autoFillBackground() and autoFillBrush.isOpaque()): bg = widget.palette().brush(QPalette.Window) qwtFillRect(widget, painter, rect, bg) if widget.autoFillBackground(): qwtFillRect(widget, painter, rect, autoFillBrush) if widget.testAttribute(Qt.WA_StyledBackground): painter.setClipRegion(QRegion(rect)) opt = QStyleOption() opt.initFrom(widget) widget.style().drawPrimitive(QStyle.PE_Widget, opt, painter, widget)
def paintEvent(self, event): """ Needed to be able to use stylesheets on custom widgets that subclass QWidget. """ QWidget.paintEvent(self, event) opt = QStyleOption() opt.initFrom(self) p = QPainter(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, p, self)
def paintEvent(self, event): """ Needed to get Stylesheets working :-( :param event: paint event """ # _log.debug("Tab: paint event.") opt = QStyleOption() opt.initFrom(self) painter = QPainter(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
def test_label_alarms(qtbot, signals, alarm_severity, alarm_sensitive_content, alarm_sensitive_border): """ Test the widget's appearance changes according to changes in alarm severity. Expectations: 1. The widget receives the correct alarm severity 2. The appearance changes to check for are the alarm content (alarm color), and the widget's border appearance, e.g. solid, transparent, etc. 3. The alarm color and border appearance will change only if each corresponding Boolean flag is set to True NOTE: This test depends on the default stylesheet having different values for 'color' for different alarm states of PyDMLabel. Parameters ---------- qtbot : fixture pytest-qt window for widget testing signals : fixture The signals fixture, which provides access signals to be bound to the appropriate slots alarm_severity : int The severity of an alarm (NONE, MINOR, MAJOR, INVALID, or DISCONNECTED) alarm_sensitive_content : bool True if the widget will change color accordingly to the alarm's severity; False if not alarm_sensitive_border : bool True if the widget's border will change color and thickness accordingly to the alarm's severity; False if not """ QApplication.instance().make_main_window() main_window = QApplication.instance().main_window qtbot.addWidget(main_window) pydm_label = PyDMLabel(parent=main_window, init_channel="ca://FOOO") qtbot.addWidget(pydm_label) pydm_label.alarmSensitiveContent = alarm_sensitive_content pydm_label.alarmSensitiveBorder = alarm_sensitive_border signals.connection_state_signal.connect(pydm_label.connectionStateChanged) signals.connection_state_signal.emit(True) signals.new_severity_signal.connect(pydm_label.alarmSeverityChanged) initial_severity = pydm_label._alarm_state option = QStyleOption() option.initFrom(pydm_label) before_color = option.palette.text().color().name() signals.new_severity_signal.emit(alarm_severity) assert pydm_label._alarm_state == alarm_severity option = QStyleOption() option.initFrom(pydm_label) after_color = option.palette.text().color().name() if alarm_sensitive_content and (alarm_severity != initial_severity): assert after_color != before_color else: assert after_color == before_color
def paintEvent(self, event): """Handle appearence of the widget on state updates.""" self.style().unpolish(self) self.style().polish(self) option = QStyleOption() option.initFrom(self) h = option.rect.height() w = option.rect.width() if self.m_shape in (self.ShapeMap.Triangle, self.ShapeMap.Round): aspect = (4 / 3.0) if self.m_shape == self.ShapeMap.Triangle else 2.0 ah = w / aspect aw = w if ah > h: ah = h aw = h * aspect x = abs(aw - w) / 2.0 y = abs(ah - h) / 2.0 bounds = QRectF(x, y, aw, ah) else: size = min(w, h) x = abs(size - w) / 2.0 y = abs(size - h) / 2.0 bounds = QRectF(x, y, size, size) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) ind = self.m_state % len(self.m_stateColors) dark_r, dark_g, dark_b = self.getRGBfromQColor(self.m_stateColors[ind]) if not self.isEnabled(): dark_r, dark_g, dark_b = self.getRGBfromQColor(self.m_dsblColor) sel1_r, sel1_g, sel1_b = self.getRGBfromQColor(self.SelColor) sel2_r, sel2_g, sel2_b = self.getRGBfromQColor(self.SelColor) opc = '1.000' if not self.isSelected(): sel1_r, sel1_g, sel1_b = self.getRGBfromQColor(self.NotSelColor1) sel2_r, sel2_g, sel2_b = self.getRGBfromQColor(self.NotSelColor2) opc = '0.145' dark_str = "rgb(%d,%d,%d)" % (dark_r, dark_g, dark_b) light_str = "rgb(%d,%d,%d)" % self.adjust(dark_r, dark_g, dark_b) sel1_str = "rgb(%d,%d,%d)" % (sel1_r, sel1_g, sel1_b) sel2_str = "rgb(%d,%d,%d)" % (sel2_r, sel2_g, sel2_b) shape_bytes = bytes( self.shapesdict[self.m_shape] % (sel1_str, opc, sel2_str, dark_str, light_str), 'utf-8') self.renderer.load(QByteArray(shape_bytes)) self.renderer.render(painter, bounds)
def paintEvent(self, event): """Override Qt method. Parameters ---------- event : qtpy.QtCore.QEvent Event from the Qt context. """ option = QStyleOption() option.initFrom(self) p = QPainter(self) self.style().drawPrimitive(QStyle.PE_Widget, option, p, self)
def test_label_alarms(qtbot, signals, alarm_severity, alarm_sensitive_content, alarm_sensitive_border): """ Test the widget's appearance changes according to changes in alarm severity. Expectations: 1. The widget receives the correct alarm severity 2. The appearance changes to check for are the alarm content (alarm color), and the widget's border appearance, e.g. solid, transparent, etc. 3. The alarm color and border appearance will change only if each corresponding Boolean flag is set to True NOTE: This test depends on the default stylesheet having different values for 'color' for different alarm states of PyDMLabel. Parameters ---------- qtbot : fixture pytest-qt window for widget testing signals : fixture The signals fixture, which provides access signals to be bound to the appropriate slots alarm_severity : int The severity of an alarm (NONE, MINOR, MAJOR, INVALID, or DISCONNECTED) alarm_sensitive_content : bool True if the widget will change color accordingly to the alarm's severity; False if not alarm_sensitive_border : bool True if the widget's border will change color and thickness accordingly to the alarm's severity; False if not """ QApplication.instance().make_main_window() main_window = QApplication.instance().main_window qtbot.addWidget(main_window) pydm_label = PyDMLabel(parent=main_window, init_channel="ca://FOOO") qtbot.addWidget(pydm_label) pydm_label.alarmSensitiveContent = alarm_sensitive_content pydm_label.alarmSensitiveBorder = alarm_sensitive_border signals.connection_state_signal.connect(pydm_label.connectionStateChanged) signals.connection_state_signal.emit(True) signals.new_severity_signal.connect(pydm_label.alarmSeverityChanged) initial_severity = pydm_label._alarm_state option = QStyleOption() option.initFrom(pydm_label) before_color = option.palette.text().color().name() signals.new_severity_signal.emit(alarm_severity) assert pydm_label._alarm_state == alarm_severity option = QStyleOption() option.initFrom(pydm_label) after_color = option.palette.text().color().name() if alarm_sensitive_content and (alarm_severity != initial_severity): assert after_color != before_color else: assert after_color == before_color
def paintEvent(self, event): """ 重写paintEvent, 如此在继承widget等控件之后依然可以通过调用qss样式文件进行样式重载 Rewrite paintEvent, In this way, after inheriting widgets and other controls, you can still reload styles by calling the qss style file """ opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, QPainter(self), self) super().paintEvent(event)
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. At PyDMSymbol this method handles the alarm painting with parameters from the stylesheet and draws the proper image. 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) if self._current_key is None: self._painter.end() return image_to_draw = self._state_images.get(self._current_key, (None, None))[1] if image_to_draw is None: self._painter.end() return if isinstance(image_to_draw, QPixmap): w = float(image_to_draw.width()) h = float(image_to_draw.height()) if self._aspect_ratio_mode == Qt.IgnoreAspectRatio: scale = (event.rect().width() / w, event.rect().height() / h) elif self._aspect_ratio_mode == Qt.KeepAspectRatio: sf = min(event.rect().width() / w, event.rect().height() / h) scale = (sf, sf) elif self._aspect_ratio_mode == Qt.KeepAspectRatioByExpanding: sf = max(event.rect().width() / w, event.rect().height() / h) scale = (sf, sf) self._painter.scale(scale[0], scale[1]) self._painter.drawPixmap(event.rect().x(), event.rect().y(), image_to_draw) elif isinstance(image_to_draw, QSvgRenderer): draw_size = QSizeF(image_to_draw.defaultSize()) draw_size.scale(QSizeF(event.rect().size()), self._aspect_ratio_mode) image_to_draw.render( self._painter, QRectF(0.0, 0.0, draw_size.width(), draw_size.height())) self._painter.end()
def paintEvent(self, event): """ Override from QWidget, needed to get stylesheets working :param event: paint event """ # _log.debug("Tab: paint event.") opt = QStyleOption() opt.initFrom(self) painter = QPainter(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) if CONFIG.debug_layout: painter.setPen(QPen(Qt.red, 1.0)) painter.drawRect(self.rect().adjusted(0, 0, -1, -1)) painter.setPen(QPen(Qt.black, 1.0)) painter.drawText(QPointF(5, 12), str(self))
def paintEvent(self, evt): """ 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. This method handles the painting with parameters from the stylesheet. Parameters ---------- evt : QPaintEvent """ painter = QPainter(self) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.setRenderHint(QPainter.Antialiasing) super(PCDSSymbolBase, self).paintEvent(evt)
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. At PyDMSymbol this method handles the alarm painting with parameters from the stylesheet and draws the proper image. 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) if self._current_key is None: self._painter.end() return image_to_draw = self._state_images.get(self._current_key, (None, None))[1] if image_to_draw is None: self._painter.end() return if isinstance(image_to_draw, QPixmap): w = float(image_to_draw.width()) h = float(image_to_draw.height()) if self._aspect_ratio_mode == Qt.IgnoreAspectRatio: scale = (event.rect().width() / w, event.rect().height() / h) elif self._aspect_ratio_mode == Qt.KeepAspectRatio: sf = min(event.rect().width() / w, event.rect().height() / h) scale = (sf, sf) elif self._aspect_ratio_mode == Qt.KeepAspectRatioByExpanding: sf = max(event.rect().width() / w, event.rect().height() / h) scale = (sf, sf) self._painter.scale(scale[0], scale[1]) self._painter.drawPixmap(event.rect().x(), event.rect().y(), image_to_draw) elif isinstance(image_to_draw, QSvgRenderer): draw_size = QSizeF(image_to_draw.defaultSize()) draw_size.scale(QSizeF(event.rect().size()), self._aspect_ratio_mode) image_to_draw.render(self._painter, QRectF(0.0, 0.0, draw_size.width(), draw_size.height())) self._painter.end()
def paintEvent(self, _): """ 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. At PyDMDrawing this method handles the alarm painting with parameters from the stylesheet, configures the brush, pen and calls ```draw_item``` so the specifics can be performed for each of the drawing classes. Parameters ---------- event : QPaintEvent """ painter = QPainter(self) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.setRenderHint(QPainter.Antialiasing)
def drawBackground(self, painter, rect, widget): """ Fill rect with the background of a widget :param QPainter painter: Painter :param QRectF rect: Rectangle to be filled :param QWidget widget: Widget .. seealso:: :py:data:`QStyle.PE_Widget`, :py:meth:`QWidget.backgroundRole()` """ if widget.testAttribute(Qt.WA_StyledBackground): opt = QStyleOption() opt.initFrom(widget) opt.rect = QRectF(rect).toAlignedRect() widget.style().drawPrimitive(QStyle.PE_Widget, opt, painter, widget) else: brush = widget.palette().brush(widget.backgroundRole()) painter.fillRect(rect, brush)
def paintEvent(self, event): """Treat appearence changes based on connection state and value.""" self.style().unpolish(self) self.style().polish(self) if not self.isEnabled(): state = 'Disconnected' elif self._bit_val == self._on: state = 'On' elif self._bit_val == self._off: state = 'Off' else: state = 'Disconnected' if self.shape == 0: shape_dict = PyDMStateButton.squaredbuttonstatesdict elif self.shape == 1: shape_dict = PyDMStateButton.roundedbuttonstatesdict option = QStyleOption() option.initFrom(self) h = option.rect.height() w = option.rect.width() aspect = 2.0 ah = w/aspect aw = w if ah > h: ah = h aw = h*aspect x = abs(aw-w)/2.0 y = abs(ah-h)/2.0 bounds = QRectF(x, y, aw, ah) painter = QPainter(self) painter.setRenderHint(QPainter.Antialiasing, True) shape_str = shape_dict[state] buttonstate_bytearray = bytes(shape_str, 'utf-8') self.renderer.load(QByteArray(buttonstate_bytearray)) self.renderer.render(painter, bounds)
def updateStyleSheetInfo(self): """ Update the cached information about the current style sheet """ if not self.testAttribute(Qt.WA_StyledBackground): return recorder = QwtStyleSheetRecorder(self.size()) painter = QPainter(recorder) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self) painter.end() self.__data.styleSheet.hasBorder = len(recorder.border.rectList) > 0 self.__data.styleSheet.cornerRects = recorder.clipRects if recorder.background.path.isEmpty(): if self.__data.styleSheet.hasBorder: self.__data.styleSheet.borderPath = qwtCombinePathList( self.rect(), recorder.border.pathlist) else: self.__data.styleSheet.borderPath = recorder.background.path self.__data.styleSheet.background.brush = recorder.background.brush self.__data.styleSheet.background.origin = recorder.background.origin
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. At PyDMSymbolEditor this method handles the image preview. Parameters ---------- event : QPaintEvent """ if not self.preview: return size = QSize(140, 140) _painter = QPainter() _painter.begin(self) opt = QStyleOption() opt.initFrom(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, _painter, self) image_to_draw = self.preview_file if isinstance(image_to_draw, QPixmap): w = float(image_to_draw.width()) h = float(image_to_draw.height()) sf = min(size.width() / w, size.height() / h) scale = (sf, sf) _painter.scale(scale[0], scale[1]) _painter.drawPixmap(335 / sf, 120 / sf, image_to_draw) elif isinstance(image_to_draw, QSvgRenderer): draw_size = QSizeF(image_to_draw.defaultSize()) draw_size.scale(QSizeF(size), Qt.KeepAspectRatio) image_to_draw.render( _painter, QRectF(335, 120, draw_size.width(), draw_size.height())) _painter.end() self.preview = False
def buttonShift(w): option = QStyleOption() option.initFrom(w) ph = w.style().pixelMetric(QStyle.PM_ButtonShiftHorizontal, option, w) pv = w.style().pixelMetric(QStyle.PM_ButtonShiftVertical, option, w) return QSize(ph, pv)
def paintEvent(self, event): """Need to override paintEvent in order to apply CSS.""" opt = QStyleOption() opt.initFrom(self) p = QPainter(self) self.style().drawPrimitive(QStyle.PE_Widget, opt, p, self)
def qwtDrawStyledBackground(w, painter): opt = QStyleOption() opt.initFrom(w) w.style().drawPrimitive(QStyle.PE_Widget, opt, painter, w)