def __init__(self, widget=None, parent=None): super(MBadge, self).__init__(parent) self._widget = widget self._overflow_count = 99 self._dot = None self._text = None self._count = None self._badge_button = QPushButton() self._badge_button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) if widget is not None: self._main_lay.addWidget(widget, 0, 0) self._main_lay.addWidget(self._badge_button, 0, 0, Qt.AlignTop | Qt.AlignRight) self.setLayout(self._main_lay)
def test_cursor_mixin(qtbot): @mixin.cursor_mixin class _TestClass(QPushButton): def __init__(self, parent=None): super(_TestClass, self).__init__(parent) geo = QApplication.desktop().screenGeometry() self.setGeometry(geo.width() / 4, geo.height() / 4, geo.width() / 2, geo.height() / 2) main_widget = QWidget() button_test = _TestClass() button_normal = QPushButton() test_lay = QVBoxLayout() test_lay.addWidget(button_test) test_lay.addWidget(button_normal) main_widget.setLayout(test_lay) qtbot.addWidget(main_widget) main_widget.show() button_test.setEnabled(False) assert QApplication.overrideCursor() is None # Not override cursor qtbot.mouseMove(button_test) # mouse enter def check_cursor(): assert QApplication.overrideCursor() is not None assert QApplication.overrideCursor().shape() == Qt.ForbiddenCursor qtbot.waitUntil(check_cursor) qtbot.mouseMove(button_normal) # mouse leave def check_cursor(): assert QApplication.overrideCursor() is None # Restore override cursor qtbot.waitUntil(check_cursor) button_test.setEnabled(True) qtbot.mouseMove(button_test) # mouse enter def check_cursor(): assert QApplication.overrideCursor() is not None assert QApplication.overrideCursor().shape() == Qt.PointingHandCursor qtbot.waitUntil(check_cursor) qtbot.mouseMove(button_normal) # mouse leave def check_cursor(): assert QApplication.overrideCursor() is None # Restore override cursor qtbot.waitUntil(check_cursor)
def test_hover_shadow_mixin(qtbot): @mixin.hover_shadow_mixin class _TestClass(QPushButton): def __init__(self, parent=None): super(_TestClass, self).__init__(parent) geo = QApplication.desktop().screenGeometry() self.setGeometry(geo.width() / 4, geo.height() / 4, geo.width() / 2, geo.height() / 2) main_widget = QWidget() button_test = _TestClass() button_normal = QPushButton() test_lay = QVBoxLayout() test_lay.addWidget(button_test) test_lay.addWidget(button_normal) main_widget.setLayout(test_lay) qtbot.addWidget(main_widget) assert button_test.graphicsEffect() is None main_widget.show() qtbot.mouseMove(button_test) # mouse in def check_effect(): graphics_effect = button_test.graphicsEffect() assert graphics_effect is not None assert graphics_effect.isEnabled() assert isinstance(graphics_effect, QGraphicsDropShadowEffect) qtbot.waitUntil(check_effect) qtbot.mouseMove(button_normal) # mouse out def check_effect(): assert button_test.graphicsEffect() is not None assert not button_test.graphicsEffect().isEnabled() qtbot.waitUntil(check_effect) qtbot.mouseMove(button_test) # mouse in def check_effect(): assert button_test.graphicsEffect() is not None assert button_test.graphicsEffect().isEnabled() qtbot.waitUntil(check_effect)
class MBadge(QWidget): """ Badge normally appears in proximity to notifications or user avatars with eye-catching appeal, typically displaying unread messages count. Show something at the wrapped widget top right. There is 3 type styles: dot: show a dot count: show a number at text: show a string Property: dayu_dot: bool dayu_text: basestring dayu_count: int dayu_overflow: int """ def __init__(self, widget=None, parent=None): super(MBadge, self).__init__(parent) self._widget = widget self._overflow_count = 99 self._dot = None self._text = None self._count = None self._badge_button = QPushButton() self._badge_button.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) if widget is not None: self._main_lay.addWidget(widget, 0, 0) self._main_lay.addWidget(self._badge_button, 0, 0, Qt.AlignTop | Qt.AlignRight) self.setLayout(self._main_lay) def get_dayu_overflow(self): """ Get current overflow number :return: int """ return self._overflow_count def set_dayu_overflow(self, num): """ Set the overflow number :param num: new max number :return: None """ self._overflow_count = num self._update_number() def get_dayu_dot(self): """ Get current style is dot or not and dot is show or not :return: bool """ return self._dot def set_dayu_dot(self, show): """ Set dot style and weather show the dot or not :param show: bool :return: None """ self._dot = show self._badge_button.setText('') self._badge_button.setVisible(show) self.style().polish(self) def get_dayu_count(self): """ Get actual count number :return: int """ return self._count def set_dayu_count(self, num): """ Set current style to show a number :param num: int :return: None """ self._count = num self._update_number() def _update_number(self): self._badge_button.setText(utils.overflow_format(self._count, self._overflow_count)) self._badge_button.setVisible(self._count > 0) self._dot = None self.style().polish(self) def get_dayu_text(self): """ Get current showed text :return: basestring """ return self._text def set_dayu_text(self, text): """ Set current style to show a text. :param text: basestring :return: None """ self._text = text self._badge_button.setText(self._text) self._badge_button.setVisible(bool(self._text)) self._dot = None self.style().polish(self) dayu_overflow = Property(int, get_dayu_overflow, set_dayu_overflow) dayu_dot = Property(bool, get_dayu_dot, set_dayu_dot) dayu_count = Property(int, get_dayu_count, set_dayu_count) dayu_text = Property(basestring, get_dayu_text, set_dayu_text) @classmethod def dot(cls, show=False, widget=None): """ Create a Badge with dot style. :param show: bool :param widget: the wrapped widget :return: instance badge """ inst = cls(widget=widget) inst.set_dayu_dot(show) return inst @classmethod def count(cls, count=0, widget=None): """ Create a Badge with number style. :param count: int :param widget: the wrapped widget :return: instance badge """ inst = cls(widget=widget) inst.set_dayu_count(count) return inst @classmethod def text(cls, text='', widget=None): """ Create a Badge with text style. :param text: basestring :param widget: the wrapped widget :return: instance badge """ inst = cls(widget=widget) inst.set_dayu_text(text) return inst