class MBreadcrumb(QWidget): """ MBreadcrumb A breadcrumb displays the current location within a hierarchy. It allows going back to states higher up in the hierarchy. """ def __init__(self, separator='/', parent=None): super(MBreadcrumb, self).__init__(parent) self._separator = separator self._main_layout = QHBoxLayout() self._main_layout.setContentsMargins(0, 0, 0, 0) self._main_layout.setSpacing(0) self._main_layout.addStretch() self.setLayout(self._main_layout) self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Minimum) self._button_group = QButtonGroup() self._label_list = [] def set_item_list(self, data_list): """Set the whole breadcrumb items. It will clear the old widgets.""" for button in self._button_group.buttons(): self._button_group.removeButton(button) self._main_layout.removeWidget(button) button.setVisible(False) for sep in self._label_list: self._main_layout.removeWidget(sep) sep.setVisible(False) for index, data_dict in enumerate(data_list): self.add_item(data_dict, index) def add_item(self, data_dict, index=None): """Add a item""" button = MToolButton() button.setText(data_dict.get('text')) if data_dict.get('svg'): button.svg(data_dict.get('svg')) if data_dict.get('tooltip'): button.setProperty('toolTip', data_dict.get('tooltip')) if data_dict.get('clicked'): button.clicked.connect(data_dict.get('clicked')) if data_dict.get('text'): if data_dict.get('svg') or data_dict.get('icon'): button.text_beside_icon() else: button.text_only() else: button.icon_only() if self._button_group.buttons(): separator = MLabel(self._separator).secondary() self._label_list.append(separator) self._main_layout.insertWidget(self._main_layout.count() - 1, separator) self._main_layout.insertWidget(self._main_layout.count() - 1, button) if index is None: self._button_group.addButton(button) else: self._button_group.addButton(button, index)
def __init__(self, parent=None): super(MPage, self).__init__(parent) self.register_field('page_size_selected', 25) self.register_field('page_size_list', [{ 'label': '25 - Fastest', 'value': 25 }, { 'label': '50 - Fast', 'value': 50 }, { 'label': '75 - Medium', 'value': 75 }, { 'label': '100 - Slow', 'value': 100 }]) self.register_field('total', 0) self.register_field('current_page', 0) self.register_field( 'total_page', lambda: utils.get_total_page( self.field('total'), self.field('page_size_selected'))) self.register_field('total_page_text', lambda: str(self.field('total_page'))) self.register_field( 'display_text', lambda: utils.get_page_display_string( self.field('current_page'), self.field('page_size_selected'), self.field('total'))) self.register_field('can_pre', lambda: self.field('current_page') > 1) self.register_field( 'can_next', lambda: self.field('current_page') < self.field('total_page')) page_setting_menu = MMenu(parent=self) self._display_label = MLabel() self._display_label.setAlignment(Qt.AlignCenter) self._change_page_size_button = MComboBox().small() self._change_page_size_button.setFixedWidth(110) self._change_page_size_button.set_menu(page_setting_menu) self._change_page_size_button.set_formatter( lambda x: u'{} per page'.format(x)) self._change_page_size_button.sig_value_changed.connect( self._emit_page_changed) self._pre_button = MToolButton().icon_only().svg( 'left_fill.svg').small() self._pre_button.clicked.connect( functools.partial(self._slot_change_current_page, -1)) self._next_button = MToolButton().small().icon_only().svg( 'right_fill.svg') self._next_button.clicked.connect( functools.partial(self._slot_change_current_page, 1)) self._current_page_spin_box = MSpinBox() self._current_page_spin_box.setMinimum(1) self._current_page_spin_box.set_dayu_size(dayu_theme.small) self._current_page_spin_box.valueChanged.connect( self._emit_page_changed) self._total_page_label = MLabel() self.bind('page_size_list', page_setting_menu, 'data') self.bind('page_size_selected', page_setting_menu, 'value', signal='sig_value_changed') self.bind('page_size_selected', self._change_page_size_button, 'value', signal='sig_value_changed') self.bind('current_page', self._current_page_spin_box, 'value', signal='valueChanged') self.bind('total_page', self._current_page_spin_box, 'maximum') self.bind('total_page_text', self._total_page_label, 'dayu_text') self.bind('display_text', self._display_label, 'dayu_text') self.bind('can_pre', self._pre_button, 'enabled') self.bind('can_next', self._next_button, 'enabled') main_lay = QHBoxLayout() main_lay.setContentsMargins(0, 0, 0, 0) main_lay.setSpacing(2) main_lay.addStretch() main_lay.addWidget(self._display_label) main_lay.addStretch() main_lay.addWidget(MLabel('|').secondary()) main_lay.addWidget(self._change_page_size_button) main_lay.addWidget(MLabel('|').secondary()) main_lay.addWidget(self._pre_button) main_lay.addWidget(MLabel('Page')) main_lay.addWidget(self._current_page_spin_box) main_lay.addWidget(MLabel('/')) main_lay.addWidget(self._total_page_label) main_lay.addWidget(self._next_button) self.setLayout(main_lay)
class MDivider(QWidget): ''' A divider line separates different content. Property: dayu_text: basestring ''' _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: basestring :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: basestring """ return self._text dayu_text = Property(basestring, 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)