def _init_ui(self): basic_card_lay = MFlowLayout() basic_card_lay.setSpacing(20) for setting in [{ 'title': '', }, { 'title': 'Card Title', 'size': dayu_theme.small }, { 'title': 'Card Title', 'image': MPixmap('app-houdini.png') }, { 'title': 'Card Title', 'extra': 'More', 'image': MPixmap('app-houdini.png') }, { 'title': 'Card Title', 'extra': 'More', }]: card_0 = MCard(**setting) content_widget_0 = QWidget() content_lay_0 = QVBoxLayout() content_lay_0.setContentsMargins(15, 15, 15, 15) content_widget_0.setLayout(content_lay_0) for i in range(4): content_lay_0.addWidget(MLabel('Card Content {}'.format(i + 1))) card_0.set_widget(content_widget_0) basic_card_lay.addWidget(card_0) meta_card_lay = MFlowLayout() meta_card_lay.setSpacing(20) for setting in [ { 'title': u'Houdini', 'description': u'Side Effects Software的旗舰级产品,是创建高级视觉效果的有效工具', 'avatar': MPixmap('user_line.svg'), 'cover': MPixmap('app-houdini.png') }, { 'title': u'Autodesk Maya', 'description': u'3D 数字动画和视觉效果的世界领先软件应用程序', 'cover': MPixmap('app-maya.png') }, ]: meta_card = MMeta() meta_card.setup_data(setting) meta_card_lay.addWidget(meta_card) task_card_lay = QVBoxLayout() # task_card_lay.setSpacing(10) for setting in [ { 'title': u'Task A', 'description': u'demo pl_0010 Animation \n2019/04/01 - 2019/04/09', 'avatar': MPixmap('success_line.svg', dayu_theme.success_color), }, { 'title': u'Task B', 'description': u'#2 closed by xiao hua.', 'avatar': MPixmap('error_line.svg', dayu_theme.error_color) }, { 'title': u'Task C', 'description': u'#3 closed by xiao hua.', 'avatar': MPixmap('warning_line.svg', dayu_theme.warning_color) } ] * 5: meta_card = MMeta(extra=True) meta_card.setup_data(setting) task_card_lay.addWidget(meta_card) left_lay = QVBoxLayout() left_lay.addWidget(MDivider('Basic')) left_lay.addLayout(basic_card_lay) left_lay.addWidget(MDivider('Meta E-Commerce Example')) left_lay.addLayout(meta_card_lay) left_lay.addStretch() left_widget = QWidget() left_widget.setLayout(left_lay) right_lay = QVBoxLayout() right_lay.addWidget(MDivider('Meta Task Item Example')) scroll = QScrollArea() scroll.setWidgetResizable(True) task_widget = QWidget() task_widget.setLayout(task_card_lay) scroll.setWidget(task_widget) right_lay.addWidget(scroll) right_widget = QWidget() right_widget.setLayout(right_lay) splitter = QSplitter() splitter.addWidget(left_widget) splitter.addWidget(right_widget) splitter.setStretchFactor(0, 80) splitter.setStretchFactor(1, 20) main_lay = QVBoxLayout() main_lay.addWidget(splitter) self.setLayout(main_lay)
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()