class MNewTag(QWidget): sig_add_tag = Signal(str) def __init__(self, text='New Tag', parent=None): super(MNewTag, self).__init__(parent) self.setAttribute(Qt.WA_StyledBackground) self._add_button = MToolButton().tiny().svg( 'add_line.svg').text_beside_icon() 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._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) def set_completer(self, completer): self._line_edit.setCompleter(completer) def _slot_show_edit(self): self._line_edit.setVisible(True) self._add_button.setVisible(False) self._line_edit.setFocus(Qt.MouseFocusReason) def _slot_return_pressed(self): self._line_edit.setVisible(False) self._add_button.setVisible(True) if self._line_edit.text(): self.sig_add_tag.emit(self._line_edit.text()) self._line_edit.clear() def focusOutEvent(self, *args, **kwargs): self._line_edit.setVisible(False) self._add_button.setVisible(True) return super(MNewTag, self).focusOutEvent(*args, **kwargs)
class MMessage(QWidget): """ Display global messages as feedback in response to user operations. """ InfoType = 'info' SuccessType = 'success' WarningType = 'warning' ErrorType = 'error' LoadingType = 'loading' default_config = {'duration': 2, 'top': 24} sig_closed = Signal() def __init__(self, text, duration=None, dayu_type=None, closable=False, parent=None): super(MMessage, self).__init__(parent) self.setObjectName('message') self.setWindowFlags(Qt.FramelessWindowHint | Qt.Dialog | Qt.WA_TranslucentBackground | Qt.WA_DeleteOnClose) self.setAttribute(Qt.WA_StyledBackground) if dayu_type == MMessage.LoadingType: _icon_label = MLoading.tiny() else: _icon_label = MAvatar.tiny() current_type = dayu_type or MMessage.InfoType _icon_label.set_dayu_image( MPixmap('{}_fill.svg'.format(current_type), vars(dayu_theme).get(current_type + '_color'))) self._content_label = MLabel(parent=self) # self._content_label.set_elide_mode(Qt.ElideMiddle) self._content_label.setText(text) self._close_button = MToolButton( parent=self).icon_only().svg('close_line.svg').tiny() self._close_button.clicked.connect(self.close) self._close_button.setVisible(closable or False) self._main_lay = QHBoxLayout() self._main_lay.addWidget(_icon_label) self._main_lay.addWidget(self._content_label) self._main_lay.addStretch() self._main_lay.addWidget(self._close_button) self.setLayout(self._main_lay) _close_timer = QTimer(self) _close_timer.setSingleShot(True) _close_timer.timeout.connect(self.close) _close_timer.timeout.connect(self.sig_closed) _close_timer.setInterval( (duration or self.default_config.get('duration')) * 1000) _ani_timer = QTimer(self) _ani_timer.timeout.connect(self._fade_out) _ani_timer.setInterval( (duration or self.default_config.get('duration')) * 1000 - 300) _close_timer.start() _ani_timer.start() self._pos_ani = QPropertyAnimation(self) self._pos_ani.setTargetObject(self) self._pos_ani.setEasingCurve(QEasingCurve.OutCubic) self._pos_ani.setDuration(300) self._pos_ani.setPropertyName(b'pos') self._opacity_ani = QPropertyAnimation() self._opacity_ani.setTargetObject(self) self._opacity_ani.setDuration(300) self._opacity_ani.setEasingCurve(QEasingCurve.OutCubic) self._opacity_ani.setPropertyName(b'windowOpacity') self._opacity_ani.setStartValue(0.0) self._opacity_ani.setEndValue(1.0) self._set_proper_position(parent) self._fade_int() def _fade_out(self): self._pos_ani.setDirection(QAbstractAnimation.Backward) self._pos_ani.start() self._opacity_ani.setDirection(QAbstractAnimation.Backward) self._opacity_ani.start() def _fade_int(self): self._pos_ani.start() self._opacity_ani.start() def _set_proper_position(self, parent): parent_geo = parent.geometry() pos = parent_geo.topLeft( ) if parent.parent() is None else parent.mapToGlobal( parent_geo.topLeft()) offset = 0 for child in parent.children(): if isinstance(child, MMessage) and child.isVisible(): offset = max(offset, child.y()) base = pos.y() + MMessage.default_config.get('top') target_x = pos.x() + parent_geo.width() / 2 - 100 target_y = (offset + 50) if offset else base self._pos_ani.setStartValue(QPoint(target_x, target_y - 40)) self._pos_ani.setEndValue(QPoint(target_x, target_y)) @classmethod def info(cls, text, parent, duration=None, closable=None): """Show a normal message""" inst = cls(text, dayu_type=MMessage.InfoType, duration=duration, closable=closable, parent=parent) inst.show() return inst @classmethod def success(cls, text, parent, duration=None, closable=None): """Show a success message""" inst = cls(text, dayu_type=MMessage.SuccessType, duration=duration, closable=closable, parent=parent) inst.show() return inst @classmethod def warning(cls, text, parent, duration=None, closable=None): """Show a warning message""" inst = cls(text, dayu_type=MMessage.WarningType, duration=duration, closable=closable, parent=parent) inst.show() return inst @classmethod def error(cls, text, parent, duration=None, closable=None): """Show an error message""" inst = cls(text, dayu_type=MMessage.ErrorType, duration=duration, closable=closable, parent=parent) inst.show() return inst @classmethod def loading(cls, text, parent): """Show a message with loading animation""" inst = cls(text, dayu_type=MMessage.LoadingType, parent=parent) inst.show() return inst @classmethod def config(cls, duration=None, top=None): """ Config the global MMessage duration and top setting. :param duration: int (unit is second) :param top: int (unit is px) :return: None """ if duration is not None: cls.default_config['duration'] = duration if top is not None: cls.default_config['top'] = top
class MAlert(QWidget): """ Alert component for feedback. Property: dayu_type: The feedback type with different color container. dayu_text: The feedback string showed in container. """ InfoType = 'info' SuccessType = 'success' WarningType = 'warning' ErrorType = 'error' def __init__(self, text='', parent=None, flags=0): super(MAlert, self).__init__(parent, flags) self.setAttribute(Qt.WA_StyledBackground) self._icon_label = MAvatar() self._icon_label.set_dayu_size(dayu_theme.tiny) self._content_label = MLabel().secondary() self._close_button = MToolButton().svg( 'close_line.svg').tiny().icon_only() self._close_button.clicked.connect( functools.partial(self.setVisible, False)) self._main_lay = QHBoxLayout() self._main_lay.setContentsMargins(8, 8, 8, 8) self._main_lay.addWidget(self._icon_label) self._main_lay.addWidget(self._content_label) self._main_lay.addStretch() self._main_lay.addWidget(self._close_button) self.setLayout(self._main_lay) self.set_show_icon(True) self.set_closeable(False) self._dayu_type = None self._dayu_text = None self.set_dayu_type(MAlert.InfoType) self.set_dayu_text(text) def set_closeable(self, closeable): """Display the close icon button or not.""" self._close_button.setVisible(closeable) def set_show_icon(self, show_icon): """Display the information type icon or not.""" self._icon_label.setVisible(show_icon) def _set_dayu_text(self): self._content_label.setText(self._dayu_text) self.setVisible(bool(self._dayu_text)) def set_dayu_text(self, value): """Set the feedback content.""" if isinstance(value, basestring): self._dayu_text = value else: raise TypeError("Input argument 'value' should be string type, " "but get {}".format(type(value))) self._set_dayu_text() def _set_dayu_type(self): self._icon_label.set_dayu_image( MPixmap('{}_fill.svg'.format(self._dayu_type), vars(dayu_theme).get(self._dayu_type + '_color'))) self.style().polish(self) def set_dayu_type(self, value): """Set feedback type.""" if value in [ MAlert.InfoType, MAlert.SuccessType, MAlert.WarningType, MAlert.ErrorType ]: self._dayu_type = value else: raise ValueError("Input argument 'value' should be one of " "info/success/warning/error string.") self._set_dayu_type() def get_dayu_type(self): """ Get MAlert feedback type. :return: str """ return self._dayu_type def get_dayu_text(self): """ Get MAlert feedback message. :return: basestring """ return self._dayu_text dayu_text = Property(unicode, get_dayu_text, set_dayu_text) dayu_type = Property(str, get_dayu_type, set_dayu_type) def info(self): """Set MAlert to InfoType""" self.set_dayu_type(MAlert.InfoType) return self def success(self): """Set MAlert to SuccessType""" self.set_dayu_type(MAlert.SuccessType) return self def warning(self): """Set MAlert to WarningType""" self.set_dayu_type(MAlert.WarningType) return self def error(self): """Set MAlert to ErrorType""" self.set_dayu_type(MAlert.ErrorType) return self def closable(self): """Set MAlert closebale is True""" self.set_closeable(True) return self
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 MSectionItem(QWidget): sig_context_menu = Signal(object) def __init__(self, title='', expand=False, widget=None, closeable=False, parent=None): super(MSectionItem, self).__init__(parent) self._central_widget = None self.setAttribute(Qt.WA_StyledBackground) self.title_label = MLabel(parent=self) self.expand_icon = MLabel(parent=self) self.expand_icon.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._close_button = MToolButton().icon_only().tiny().svg( 'close_line.svg') self._close_button.clicked.connect(self.close) header_lay = QHBoxLayout() header_lay.addWidget(self.expand_icon) header_lay.addWidget(self.title_label) header_lay.addStretch() header_lay.addWidget(self._close_button) self.header_widget = QWidget(parent=self) self.header_widget.setAttribute(Qt.WA_StyledBackground) self.header_widget.setObjectName('title') self.header_widget.setLayout(header_lay) self.header_widget.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self.header_widget.setCursor(Qt.PointingHandCursor) self.title_label.setCursor(Qt.PointingHandCursor) self.header_widget.installEventFilter(self) self.title_label.installEventFilter(self) self.content_widget = QWidget(parent=self) self.content_layout = QHBoxLayout() self.content_widget.setLayout(self.content_layout) self.main_lay = QVBoxLayout() self.main_lay.setContentsMargins(0, 0, 0, 0) self.main_lay.setSpacing(0) self.main_lay.addWidget(self.header_widget) self.main_lay.addWidget(self.content_widget) self.setLayout(self.main_lay) self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Minimum) self.setMouseTracking(True) self.set_title(title) self.set_closeable(closeable) if widget: self.set_content(widget) self.set_expand(expand) def set_content(self, widget): if self._central_widget: self.content_layout.removeWidget(self._central_widget) self._central_widget.close() self.content_layout.addWidget(widget) self._central_widget = widget def get_content(self): return self._central_widget def set_closeable(self, value): self.setProperty('closeable', value) def _set_closeable(self, value): self.content_widget.setVisible(value) self._close_button.setVisible(value) def set_expand(self, value): self.setProperty('expand', value) def _set_expand(self, value): self.content_widget.setVisible(value) self.expand_icon.setPixmap( MPixmap('down_line.svg' if value else 'right_line.svg'). scaledToHeight(12)) def set_title(self, value): self.setProperty('title', value) def _set_title(self, value): self.title_label.setText(value) def eventFilter(self, widget, event): if widget in [self.header_widget, self.title_label]: if event.type() == QEvent.MouseButtonRelease: self.set_expand(not self.property('expand')) return super(QWidget, self).eventFilter(widget, event)
class MDrawer(QWidget): """ A panel which slides in from the edge of the screen. """ LeftPos = 'left' RightPos = 'right' TopPos = 'top' BottomPos = 'bottom' sig_closed = Signal() def __init__(self, title, position='right', closable=True, parent=None): super(MDrawer, self).__init__(parent) self.setObjectName('message') # self.setWindowFlags(Qt.Popup ) # self.setWindowFlags( # Qt.FramelessWindowHint | Qt.Popup | Qt.WA_TranslucentBackground) self.setAttribute(Qt.WA_StyledBackground) self._title_label = MLabel(parent=self).h4() # self._title_label.set_elide_mode(Qt.ElideRight) self._title_label.setText(title) self._close_button = MToolButton( parent=self).icon_only().svg('close_line.svg').small() self._close_button.clicked.connect(self.close) self._close_button.setVisible(closable or False) _title_lay = QHBoxLayout() _title_lay.addWidget(self._title_label) _title_lay.addStretch() _title_lay.addWidget(self._close_button) self._button_lay = QHBoxLayout() self._button_lay.addStretch() self._scroll_area = QScrollArea() self._main_lay = QVBoxLayout() self._main_lay.addLayout(_title_lay) self._main_lay.addWidget(MDivider()) self._main_lay.addWidget(self._scroll_area) self._main_lay.addWidget(MDivider()) self._main_lay.addLayout(self._button_lay) self.setLayout(self._main_lay) self._position = position self._close_timer = QTimer(self) self._close_timer.setSingleShot(True) self._close_timer.timeout.connect(self.close) self._close_timer.timeout.connect(self.sig_closed) self._close_timer.setInterval(300) self._is_first_close = True self._pos_ani = QPropertyAnimation(self) self._pos_ani.setTargetObject(self) self._pos_ani.setEasingCurve(QEasingCurve.OutCubic) self._pos_ani.setDuration(300) self._pos_ani.setPropertyName('pos') self._opacity_ani = QPropertyAnimation() self._opacity_ani.setTargetObject(self) self._opacity_ani.setDuration(300) self._opacity_ani.setEasingCurve(QEasingCurve.OutCubic) self._opacity_ani.setPropertyName('windowOpacity') self._opacity_ani.setStartValue(0.0) self._opacity_ani.setEndValue(1.0) # self._shadow_effect = QGraphicsDropShadowEffect(self) # color = dayu_theme.red # self._shadow_effect.setColor(color) # self._shadow_effect.setOffset(0, 0) # self._shadow_effect.setBlurRadius(5) # self._shadow_effect.setEnabled(False) # self.setGraphicsEffect(self._shadow_effect) self.app = QApplication.instance() self.app.installEventFilter(self) self.protect_time = time.time() def retrieveChildren(self, parent, receiver): if parent is receiver: return True if not hasattr(parent, "children"): return for child in parent.children(): ret = self.retrieveChildren(child, receiver) if ret: return ret def eventFilter(self, receiver, event): # Note QEvent.Type.MouseButtonPress 为 2 if event.type() == 2: if self.retrieveChildren(self, receiver): self.protect_time = time.time() # NOTE 如果点击多次触发,通过时间进行保护 if (time.time() - self.protect_time) > .1: self.close() elif event.type() == QEvent.Type.Resize and receiver is self.window(): self.close() return False def set_widget(self, widget): self._scroll_area.setWidget(widget) def add_button(self, button): self._button_lay.addWidget(button) def _fade_out(self): self._pos_ani.setDirection(QAbstractAnimation.Backward) self._pos_ani.start() self._opacity_ani.setDirection(QAbstractAnimation.Backward) self._opacity_ani.start() def _fade_int(self): self._pos_ani.start() self._opacity_ani.start() def _set_proper_position(self): parent = self.parent() parent_geo = parent.geometry() if self._position == MDrawer.LeftPos: pos = parent_geo.topLeft( ) if parent.parent() is None else parent.mapToGlobal( parent_geo.topLeft()) pos -= self.window().geometry().topLeft() target_x = pos.x() target_y = pos.y() self.setFixedHeight(parent_geo.height()) self._pos_ani.setStartValue( QPoint(target_x - self.width(), target_y)) self._pos_ani.setEndValue(QPoint(target_x, target_y)) if self._position == MDrawer.RightPos: pos = parent_geo.topRight( ) if parent.parent() is None else parent.mapToGlobal( parent_geo.topRight()) pos -= self.window().geometry().topLeft() self.setFixedHeight(parent_geo.height()) target_x = pos.x() - self.width() target_y = pos.y() self._pos_ani.setStartValue( QPoint(target_x + self.width(), target_y)) self._pos_ani.setEndValue(QPoint(target_x, target_y)) if self._position == MDrawer.TopPos: pos = parent_geo.topLeft( ) if parent.parent() is None else parent.mapToGlobal( parent_geo.topLeft()) pos -= self.window().geometry().topLeft() self.setFixedWidth(parent_geo.width()) target_x = pos.x() target_y = pos.y() self._pos_ani.setStartValue( QPoint(target_x, target_y - self.height())) self._pos_ani.setEndValue(QPoint(target_x, target_y)) if self._position == MDrawer.BottomPos: pos = parent_geo.bottomLeft( ) if parent.parent() is None else parent.mapToGlobal( parent_geo.bottomLeft()) pos -= self.window().geometry().topLeft() self.setFixedWidth(parent_geo.width()) target_x = pos.x() target_y = pos.y() - self.height() self._pos_ani.setStartValue( QPoint(target_x, target_y + self.height())) self._pos_ani.setEndValue(QPoint(target_x, target_y)) def set_dayu_position(self, value): """ Set the placement of the MDrawer. top/right/bottom/left, default is right :param value: str :return: None """ self._position = value if value in [MDrawer.BottomPos, MDrawer.TopPos]: self.setFixedHeight(200) else: self.setFixedWidth(200) def get_dayu_position(self): """ Get the placement of the MDrawer :return: str """ return self._position dayu_position = Property(str, get_dayu_position, set_dayu_position) def left(self): """Set drawer's placement to left""" self.set_dayu_position(MDrawer.LeftPos) return self def right(self): """Set drawer's placement to right""" self.set_dayu_position(MDrawer.RightPos) return self def top(self): """Set drawer's placement to top""" self.set_dayu_position(MDrawer.TopPos) return self def bottom(self): """Set drawer's placement to bottom""" self.set_dayu_position(MDrawer.BottomPos) return self def show(self): self._set_proper_position() self._fade_int() return super(MDrawer, self).show() def closeEvent(self, event): self.app.removeEventFilter(self) if self._is_first_close: self._is_first_close = False self._close_timer.start() self._fade_out() event.ignore() else: event.accept()
class MMeta(QWidget): def __init__(self, cover=None, avatar=None, title=None, description=None, extra=False, parent=None): super(MMeta, self).__init__(parent) self.setAttribute(Qt.WA_StyledBackground) self._cover_label = QLabel() self._avatar = MAvatar() self._title_label = MLabel().h4() self._description_label = MLabel().secondary() self._description_label.setWordWrap(True) self._description_label.set_elide_mode(Qt.ElideRight) self._title_layout = QHBoxLayout() self._title_layout.addWidget(self._title_label) self._title_layout.addStretch() self._extra_button = MToolButton( parent=self).icon_only().svg('more.svg') self._title_layout.addWidget(self._extra_button) self._extra_button.setVisible(extra) content_lay = QFormLayout() content_lay.setContentsMargins(5, 5, 5, 5) content_lay.addRow(self._avatar, self._title_layout) content_lay.addRow(self._description_label) self._button_layout = QHBoxLayout() main_lay = QVBoxLayout() main_lay.setSpacing(0) main_lay.setContentsMargins(1, 1, 1, 1) main_lay.addWidget(self._cover_label) main_lay.addLayout(content_lay) main_lay.addLayout(self._button_layout) main_lay.addStretch() self.setLayout(main_lay) self._cover_label.setFixedSize(QSize(200, 200)) # self.setFixedWidth(200) def get_more_button(self): return self._extra_button def setup_data(self, data_dict): if data_dict.get('title'): self._title_label.setText(data_dict.get('title')) self._title_label.setVisible(True) else: self._title_label.setVisible(False) if data_dict.get('description'): self._description_label.setText(data_dict.get('description')) self._description_label.setVisible(True) else: self._description_label.setVisible(False) if data_dict.get('avatar'): self._avatar.set_dayu_image(data_dict.get('avatar')) self._avatar.setVisible(True) else: self._avatar.setVisible(False) if data_dict.get('cover'): fixed_height = self._cover_label.width() self._cover_label.setPixmap( data_dict.get('cover').scaledToWidth(fixed_height, Qt.SmoothTransformation)) self._cover_label.setVisible(True) else: self._cover_label.setVisible(False)
class MNewTag(QWidget): """New Tag input component.""" sig_add_tag = Signal(str) 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 set_completer(self, completer): """Set the input completer""" self._line_edit.setCompleter(completer) def _slot_show_edit(self): self._line_edit.setVisible(True) self._add_button.setVisible(False) self._line_edit.setFocus(Qt.MouseFocusReason) def _slot_return_pressed(self): self._line_edit.setVisible(False) self._add_button.setVisible(True) if self._line_edit.text(): self.sig_add_tag.emit(self._line_edit.text()) self._line_edit.clear() self.update() def focusOutEvent(self, *args, **kwargs): """Override focusOutEvent to change the edit mode to button mode.""" self._line_edit.setVisible(False) self._add_button.setVisible(True) return super(MNewTag, self).focusOutEvent(*args, **kwargs) def eventFilter(self, widget, event): if widget is self._line_edit: if event.type() == QEvent.Type.KeyPress and event.key( ) == Qt.Key_Escape: self._line_edit.setVisible(False) self._add_button.setVisible(True) return super(MNewTag, self).eventFilter(widget, event)
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