def __init__(self, text="New Tag", parent=None): super(MNewTag, self).__init__(parent) self.setAttribute(Qt.WA_StyledBackground) self._add_button = MToolButton().text_beside_icon().small().svg( "add_line.svg") self._add_button.setText(text) self._add_button.clicked.connect(self._slot_show_edit) self._line_edit = MLineEdit().small() self._line_edit.returnPressed.connect(self._slot_return_pressed) self._line_edit.setVisible(False) self._line_edit.installEventFilter(self) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(3, 3, 3, 3) self._main_lay.addWidget(self._add_button, 0, 0) self._main_lay.addWidget(self._line_edit, 0, 0) self.setLayout(self._main_lay) scale_x, _ = get_scale_factor() style = QssTemplate(""" MNewTag{ border: @border@unit dashed @border_color; } MNewTag MToolButton:hover{ border:none; } """) self.setStyleSheet( style.substitute( border_color=utils.fade_color(dayu_theme.secondary_text_color, "35%"), unit=dayu_theme.unit, border=1 * scale_x, ))
def __init__(self, text='New Tag', parent=None): super(MNewTag, self).__init__(parent) self.setAttribute(Qt.WA_StyledBackground) self._add_button = MToolButton().text_beside_icon().tiny().svg( 'add_line.svg') self._add_button.setText(text) self._add_button.clicked.connect(self._slot_show_edit) self._line_edit = MLineEdit().tiny() self._line_edit.returnPressed.connect(self._slot_return_pressed) self._line_edit.setVisible(False) self._line_edit.installEventFilter(self) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(3, 3, 3, 3) self._main_lay.addWidget(self._add_button, 0, 0) self._main_lay.addWidget(self._line_edit, 0, 0) self.setLayout(self._main_lay) style = QssTemplate(''' MNewTag{ border: 1px dashed @border_color; } MNewTag MToolButton:hover{ border:none; } ''') self.setStyleSheet(style.substitute(vars(dayu_theme)))
def __init__(self, text, parent=None): super(MCheckableTag, self).__init__(text, parent) scale_x, x = get_scale_factor() self.setCheckable(True) style = QssTemplate(""" MCheckableTag{ spacing: @spaceing@unit; padding-top: @padding@unit; padding-bottom: @padding@unit; padding-right: @spaceing@unit; border-radius: @border_radius_base@unit; font-size: @font_size_base@font_unit; } MCheckableTag::indicator{ width: 0; height: 0; background: transparent; } MCheckableTag:pressed{ background-color: @primary_7; } MCheckableTag:checked{ color: @text_color_inverse; background-color: @primary_color; } MCheckableTag:checked:hover{ background-color: @primary_5; } MCheckableTag:unchecked{ color: @secondary_text_color; background-color: transparent; } MCheckableTag:unchecked:hover{ color: @primary_5; } """) self.setStyleSheet( style.substitute( spaceing=8 * scale_x, padding=4 * scale_x, border_radius_base=dayu_theme.border_radius_base, unit=dayu_theme.unit, font_unit=dayu_theme.font_unit, font_size_base=dayu_theme.font_size_base, primary_5=dayu_theme.primary_5, primary_7=dayu_theme.primary_7, primary_color=dayu_theme.primary_color, secondary_text_color=dayu_theme.secondary_text_color, text_color_inverse=dayu_theme.text_color_inverse, ))
def __init__(self, text="", parent=None): super(MTag, self).__init__(text=text, parent=parent) self._is_pressed = False self._close_button = MToolButton().tiny().svg( "close_line.svg").icon_only() self._close_button.clicked.connect(self.sig_closed) self._close_button.clicked.connect(self.close) self._close_button.setVisible(False) self._main_lay = QHBoxLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.addStretch() self._main_lay.addWidget(self._close_button) self.setLayout(self._main_lay) self._clickable = False self._border = True self._border_style = QssTemplate(""" MTag{ font-size: @font_size_base@font_unit; padding: @padding_small@unit; color: @text_color; border-radius: @border_radius; border: 1px solid @border_color; background-color: @background_color; } MTag:hover{ color: @hover_color; } """) self._no_border_style = QssTemplate(""" MTag{ font-size: @font_size_base@font_unit; padding: @padding@unit; border-radius: @border_radius; color: @text_color; border: 0 solid @border_color; background-color: @background_color; } MTag:hover{ background-color:@hover_color; } """) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._color = None self.set_dayu_color(dayu_theme.secondary_text_color)
def __init__(self, text, parent=None): super(MCheckableTag, self).__init__(text, parent) self.setCheckable(True) style = QssTemplate(''' MCheckableTag{ spacing: 8px; padding-top: 4px; padding-bottom: 4px; padding-right: 8px; border-radius: @border_radius_base@unit; font-size: @font_size_small@unit; } MCheckableTag::indicator{ width: 0; height: 0; background: transparent; } MCheckableTag:pressed{ background-color: @primary_7; } MCheckableTag:checked{ color: @text_color_inverse; background-color: @primary_color; } MCheckableTag:checked:hover{ background-color: @primary_5; } MCheckableTag:unchecked{ color: @secondary_text_color; background-color: transparent; } MCheckableTag:unchecked:hover{ color: @primary_5; } ''') self.setStyleSheet(style.substitute(vars(dayu_theme)))
class MTag(QLabel): """ Tag for categorizing or markup. """ sig_closed = Signal() sig_clicked = Signal() def __init__(self, text="", parent=None): super(MTag, self).__init__(text=text, parent=parent) self._is_pressed = False self._close_button = MToolButton().tiny().svg( "close_line.svg").icon_only() self._close_button.clicked.connect(self.sig_closed) self._close_button.clicked.connect(self.close) self._close_button.setVisible(False) self._main_lay = QHBoxLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.addStretch() self._main_lay.addWidget(self._close_button) self.setLayout(self._main_lay) self._clickable = False self._border = True self._border_style = QssTemplate(""" MTag{ font-size: @font_size_base@font_unit; padding: @padding_small@unit; color: @text_color; border-radius: @border_radius; border: 1px solid @border_color; background-color: @background_color; } MTag:hover{ color: @hover_color; } """) self._no_border_style = QssTemplate(""" MTag{ font-size: @font_size_base@font_unit; padding: @padding@unit; border-radius: @border_radius; color: @text_color; border: 0 solid @border_color; background-color: @background_color; } MTag:hover{ background-color:@hover_color; } """) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._color = None self.set_dayu_color(dayu_theme.secondary_text_color) def minimumSizeHint(self, *args, **kwargs): """Override minimumSizeHint for expand width when the close button is visible.""" orig = super(MTag, self).minimumSizeHint(*args, **kwargs) orig.setWidth(orig.width() + ( dayu_theme.tiny if self._close_button.isVisible() else 0)) return orig def get_dayu_color(self): """Get tag's color""" return self._color def set_dayu_color(self, value): """Set Tag primary color.""" self._color = value self._update_style() def _update_style(self): scale_x, _ = get_scale_factor() if self._border: self.setStyleSheet( self._border_style.substitute( padding_small=3 * scale_x, font_size_base=dayu_theme.font_size_base, font_unit=dayu_theme.font_unit, unit=dayu_theme.unit, background_color=utils.fade_color(self._color, "15%"), border_radius=dayu_theme.border_radius_base, border_color=utils.fade_color(self._color, "35%"), hover_color=utils.generate_color(self._color, 5), text_color=self._color, )) else: self.setStyleSheet( self._no_border_style.substitute( padding=4 * scale_x, font_size_base=dayu_theme.font_size_base, font_unit=dayu_theme.font_unit, unit=dayu_theme.unit, background_color=utils.generate_color(self._color, 6), border_radius=dayu_theme.border_radius_base, border_color=utils.generate_color(self._color, 6), hover_color=utils.generate_color(self._color, 5), text_color=dayu_theme.text_color_inverse, )) dayu_color = Property(str, get_dayu_color, set_dayu_color) def mousePressEvent(self, event): """Override mousePressEvent to flag _is_pressed.""" if event.button() == Qt.LeftButton: self._is_pressed = True return super(MTag, self).mousePressEvent(event) def leaveEvent(self, event): """Override leaveEvent to reset _is_pressed flag.""" self._is_pressed = False return super(MTag, self).leaveEvent(event) def mouseReleaseEvent(self, event): """Override mouseReleaseEvent to emit sig_clicked signal.""" if event.button() == Qt.LeftButton and self._is_pressed: if self._clickable: self.sig_clicked.emit() self._is_pressed = False return super(MTag, self).mouseReleaseEvent(event) def closeable(self): """Set Tag can be closed and show the close icon button.""" self._close_button.setVisible(True) return self def clickable(self): """Set Tag can be clicked and change the cursor to pointing-hand shape when enter.""" self.setCursor(Qt.PointingHandCursor) self._clickable = True return self def no_border(self): """Set Tag style is border or fill.""" self._border = False self._update_style() return self def coloring(self, color): """Same as set_dayu_color. Support chain.""" self.set_dayu_color(color) return self
class MTag(QLabel): sig_closed = Signal() sig_clicked = Signal() def __init__(self, text='', parent=None): super(MTag, self).__init__(text=text, parent=parent) self._is_pressed = False self._close_button = MToolButton().tiny().svg( 'close_line.svg').icon_only() self._close_button.clicked.connect(self.sig_closed) self._close_button.clicked.connect(self.close) self._close_button.setVisible(False) self._main_lay = QHBoxLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.addStretch() self._main_lay.addWidget(self._close_button) self.setLayout(self._main_lay) self._border = True self._border_style = QssTemplate(''' MTag{ font-size: 12px; padding: 3px; color: @text_color; border-radius: @border_radius; border: 1px solid @border_color; background-color: @background_color; } MTag:hover{ color: @hover_color; } ''') self._no_border_style = QssTemplate(''' MTag{ font-size: 12px; padding: 4px; border-radius: @border_radius; color: @text_color; border: 0 solid @border_color; background-color: @background_color; } MTag:hover{ background-color:@hover_color; } ''') self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._color = None self.set_dayu_color(dayu_theme.secondary_text_color) def minimumSizeHint(self, *args, **kwargs): orig = super(MTag, self).minimumSizeHint(*args, **kwargs) orig.setWidth(orig.width() + ( dayu_theme.tiny if self._close_button.isVisible() else 0)) return orig def get_dayu_color(self): """Get tag's color""" return self._color def set_dayu_color(self, value): self._color = value self._update_style() def _update_style(self): if self._border: self.setStyleSheet( self._border_style.substitute( background_color=utils.fade_color(self._color, '15%'), border_radius=dayu_theme.border_radius_base, border_color=utils.fade_color(self._color, '35%'), hover_color=utils.generate_color(self._color, 5), text_color=self._color)) else: self.setStyleSheet( self._no_border_style.substitute( background_color=utils.generate_color(self._color, 6), border_radius=dayu_theme.border_radius_base, border_color=utils.generate_color(self._color, 6), hover_color=utils.generate_color(self._color, 5), text_color=dayu_theme.text_color_inverse)) dayu_color = Property(str, get_dayu_color, set_dayu_color) def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self._is_pressed = True return super(MTag, self).mousePressEvent(event) def leaveEvent(self, event): self._is_pressed = False return super(MTag, self).leaveEvent(event) def mouseReleaseEvent(self, event): if event.button() == Qt.LeftButton and self._is_pressed: self.sig_clicked.emit() self._is_pressed = False return super(MTag, self).mouseReleaseEvent(event) def closeable(self): self._close_button.setVisible(True) return self def clickable(self): self.setCursor(Qt.PointingHandCursor) return self def border(self, is_border): self._border = is_border self._update_style() return self