def __init__(self, text='', orientation=Qt.Horizontal, alignment=Qt.AlignCenter, parent=None): super(MDivider, self).__init__(parent) self._orient = orientation self._text_label = MLabel().secondary() self._left_frame = QFrame() self._right_frame = QFrame() self._main_lay = QHBoxLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.setSpacing(0) self._main_lay.addWidget(self._left_frame) self._main_lay.addWidget(self._text_label) self._main_lay.addWidget(self._right_frame) self.setLayout(self._main_lay) if orientation == Qt.Horizontal: self._left_frame.setFrameShape(QFrame.HLine) self._left_frame.setFrameShadow(QFrame.Sunken) self._right_frame.setFrameShape(QFrame.HLine) self._right_frame.setFrameShadow(QFrame.Sunken) else: self._text_label.setVisible(False) self._right_frame.setVisible(False) self._left_frame.setFrameShape(QFrame.VLine) self._left_frame.setFrameShadow(QFrame.Plain) self.setFixedWidth(2) self._main_lay.setStretchFactor(self._left_frame, self._alignment_map.get(alignment, 50)) self._main_lay.setStretchFactor(self._right_frame, 100 - self._alignment_map.get(alignment, 50)) self._text = None self.set_dayu_text(text)
def __init__(self, loading=True, parent=None): super(MLoadingWrapper, self).__init__(parent) self._mask_widget = QFrame() self._mask_widget.setObjectName('mask') self._mask_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._loading_widget = MLoading() self._loading_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.addWidget(self._mask_widget, 0, 0) self._main_lay.addWidget(self._loading_widget, 0, 0, Qt.AlignCenter) self.setLayout(self._main_lay) self._loading = None self.set_dayu_loading(loading)
class MLoadingWrapper(QWidget): """ A wrapper widget to show the loading widget or hide. Property: dayu_loading: bool. current loading state. """ def __init__(self, widget, loading=True, parent=None): super(MLoadingWrapper, self).__init__(parent) self._widget = widget self._mask_widget = QFrame() self._mask_widget.setObjectName('mask') self._mask_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._loading_widget = MLoading() self._loading_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self._main_lay = QGridLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.addWidget(widget, 0, 0) self._main_lay.addWidget(self._mask_widget, 0, 0) self._main_lay.addWidget(self._loading_widget, 0, 0, Qt.AlignCenter) self.setLayout(self._main_lay) self._loading = None self.set_dayu_loading(loading) def _set_loading(self): self._loading_widget.setVisible(self._loading) self._mask_widget.setVisible(self._loading) def set_dayu_loading(self, loading): """ Set current state to loading or not :param loading: bool :return: None """ self._loading = loading self._set_loading() def get_dayu_loading(self): """ Get current loading widget is loading or not. :return: bool """ return self._loading dayu_loading = Property(bool, get_dayu_loading, set_dayu_loading)
class MDivider(QWidget): ''' A divider line separates different content. Property: dayu_text: six.string_types ''' _alignment_map = { Qt.AlignCenter: 50, Qt.AlignLeft: 20, Qt.AlignRight: 80, } def __init__(self, text='', orientation=Qt.Horizontal, alignment=Qt.AlignCenter, parent=None): super(MDivider, self).__init__(parent) self._orient = orientation self._text_label = MLabel().secondary() self._left_frame = QFrame() self._right_frame = QFrame() self._main_lay = QHBoxLayout() self._main_lay.setContentsMargins(0, 0, 0, 0) self._main_lay.setSpacing(0) self._main_lay.addWidget(self._left_frame) self._main_lay.addWidget(self._text_label) self._main_lay.addWidget(self._right_frame) self.setLayout(self._main_lay) if orientation == Qt.Horizontal: self._left_frame.setFrameShape(QFrame.HLine) self._left_frame.setFrameShadow(QFrame.Sunken) self._right_frame.setFrameShape(QFrame.HLine) self._right_frame.setFrameShadow(QFrame.Sunken) else: self._text_label.setVisible(False) self._right_frame.setVisible(False) self._left_frame.setFrameShape(QFrame.VLine) self._left_frame.setFrameShadow(QFrame.Plain) self.setFixedWidth(2) self._main_lay.setStretchFactor(self._left_frame, self._alignment_map.get(alignment, 50)) self._main_lay.setStretchFactor(self._right_frame, 100 - self._alignment_map.get(alignment, 50)) self._text = None self.set_dayu_text(text) def set_dayu_text(self, value): """ Set the divider's text. When text is empty, hide the text_label and right_frame to ensure the divider not has a gap. :param value: six.string_types :return: None """ self._text = value self._text_label.setText(value) if self._orient == Qt.Horizontal: self._text_label.setVisible(bool(value)) self._right_frame.setVisible(bool(value)) def get_dayu_text(self): """ Get current text :return: six.string_types """ return self._text dayu_text = Property(six.string_types[0], get_dayu_text, set_dayu_text) @classmethod def left(cls, text=''): """Create a horizontal divider with text at left.""" return cls(text, alignment=Qt.AlignLeft) @classmethod def right(cls, text=''): """Create a horizontal divider with text at right.""" return cls(text, alignment=Qt.AlignRight) @classmethod def center(cls, text=''): """Create a horizontal divider with text at center.""" return cls(text, alignment=Qt.AlignCenter) @classmethod def vertical(cls): """Create a vertical divider""" return cls(orientation=Qt.Vertical)