def _init_ui(self): standalone_lay = QHBoxLayout() standalone_lay.addWidget(MBadge.count(0)) standalone_lay.addWidget(MBadge.count(20)) standalone_lay.addWidget(MBadge.count(100)) standalone_lay.addWidget(MBadge.dot(True)) standalone_lay.addWidget(MBadge.text('new')) standalone_lay.addStretch() button = MToolButton().svg('trash_line.svg') avatar = MAvatar.large(MPixmap('avatar.png')) button_alert = MToolButton().svg('alert_fill.svg').large() badge_1 = MBadge.dot(True, widget=button) badge_2 = MBadge.dot(True, widget=avatar) badge_3 = MBadge.dot(True, widget=button_alert) button.clicked.connect(lambda: badge_1.set_dayu_dot(False)) spin_box = MSpinBox() spin_box.setRange(0, 9999) spin_box.valueChanged.connect(badge_3.set_dayu_count) spin_box.setValue(1) self.register_field('button1_selected', u'北京') menu1 = MMenu() menu1.set_data([u'北京', u'上海', u'广州', u'深圳']) select1 = MComboBox() select1.set_menu(menu1) self.bind('button1_selected', select1, 'value', signal='sig_value_changed') badge_hot = MBadge.text('hot', widget=MLabel(u'你的理想城市 ')) sub_lay1 = QHBoxLayout() sub_lay1.addWidget(badge_1) sub_lay1.addWidget(badge_2) sub_lay1.addWidget(badge_3) sub_lay1.addStretch() sub_lay2 = QHBoxLayout() sub_lay2.addWidget(badge_hot) sub_lay2.addWidget(select1) sub_lay2.addStretch() main_lay = QVBoxLayout() main_lay.addWidget(MDivider('use standalone')) main_lay.addLayout(standalone_lay) main_lay.addWidget(MDivider('different type')) main_lay.addLayout(sub_lay1) main_lay.addWidget(spin_box) main_lay.addWidget(MDivider('different type')) main_lay.addLayout(sub_lay2) main_lay.addStretch() self.setLayout(main_lay)
def createEditor(self, parent, option, index): self.parent_widget = parent self.editor = MMenu(exclusive=self.exclusive, parent=parent) self.editor.setWindowFlags(Qt.FramelessWindowHint | Qt.Window) model = utils.real_model(index) real_index = utils.real_index(index) data_obj = real_index.internalPointer() attr = '{}_list'.format( model.header_list[real_index.column()].get('key')) self.editor.set_data(utils.get_obj_value(data_obj, attr, [])) self.editor.sig_value_changed.connect(self._slot_finish_edit) return self.editor
def _slot_context_menu(self, point): context_menu = MMenu(parent=self) action_select_all = context_menu.addAction('Select All') action_select_none = context_menu.addAction('Select None') action_select_invert = context_menu.addAction('Select Invert') action_select_all.triggered.connect(functools.partial(self._slot_set_select, True)) action_select_none.triggered.connect(functools.partial(self._slot_set_select, False)) action_select_invert.triggered.connect(functools.partial(self._slot_set_select, None)) context_menu.exec_(QCursor.pos() + QPoint(10, 10))
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)
def _init_ui(self): self.register_field('button1_selected', u'北京') self.register_field('button1_selected_text', lambda: self.field('button1_selected')) button1 = MPushButton(text='Normal Menu') menu1 = MMenu(parent=self) menu1.set_data([u'北京', u'上海', u'广州', u'深圳']) button1.setMenu(menu1) button1.clicked.connect(button1.showMenu) label1 = MLabel() self.bind('button1_selected', menu1, 'value', signal='sig_value_changed') self.bind('button1_selected_text', label1, 'text') self.register_field('button2_selected', [u'北京']) self.register_field('button2_selected_text', lambda: ', '.join(self.field('button2_selected'))) button2 = MPushButton(text='Multi Select Menu') menu2 = MMenu(exclusive=False, parent=self) menu2.set_data([u'北京', u'上海', u'广州', u'深圳']) button2.setMenu(menu2) button2.clicked.connect(button2.showMenu) label2 = MLabel() self.bind('button2_selected', menu2, 'value', signal='sig_value_changed') self.bind('button2_selected_text', label2, 'text') self.register_field('button3_selected', '') self.register_field('button3_selected_text', lambda: self.field('button3_selected')) button3 = MPushButton(text=u'回调函数获取选项') menu3 = MMenu(parent=self) menu3.set_load_callback(lambda: [u'北京', u'上海', u'广州', u'深圳']) button3.setMenu(menu3) button3.clicked.connect(button2.showMenu) label3 = MLabel() self.bind('button3_selected', menu3, 'value', signal='sig_value_changed') self.bind('button3_selected_text', label3, 'text') self.register_field('button4_selected', '') self.register_field('button4_selected_text', lambda: ' / '.join(self.field('button4_selected'))) button4 = MPushButton(text=u'级联选择') menu4 = MMenu(cascader=True, parent=self) menu4.set_data([u'北京/故宫', u'北京/天坛', u'北京/王府井', u'江苏/南京/夫子庙', u'江苏/苏州/拙政园', u'江苏/苏州/狮子林']) button4.setMenu(menu4) button4.clicked.connect(button4.showMenu) label4 = MLabel() self.bind('button4_selected', menu4, 'value', signal='sig_value_changed') self.bind('button4_selected_text', label4, 'text') a = [{'children': [{'value': u'\u6545\u5bab', 'label': u'\u6545\u5bab'}, {'value': u'\u5929\u575b', 'label': u'\u5929\u575b'}, {'value': u'\u738b\u5e9c\u4e95', 'label': u'\u738b\u5e9c\u4e95'}], 'value': u'\u5317\u4eac', 'label': u'\u5317\u4eac'}, {'children': [{'children': [{'value': u'\u592b\u5b50\u5e99', 'label': u'\u592b\u5b50\u5e99'}], 'value': u'\u5357\u4eac', 'label': u'\u5357\u4eac'}, {'children': [{'value': u'\u62d9\u653f\u56ed', 'label': u'\u62d9\u653f\u56ed'}, {'value': u'\u72ee\u5b50\u6797', 'label': u'\u72ee\u5b50\u6797'}], 'value': u'\u82cf\u5dde', 'label': u'\u82cf\u5dde'}], 'value': u'\u6c5f\u82cf', 'label': u'\u6c5f\u82cf'}] sub_lay1 = QHBoxLayout() sub_lay1.addWidget(button1) sub_lay1.addWidget(label1) sub_lay2 = QHBoxLayout() sub_lay2.addWidget(button2) sub_lay2.addWidget(label2) sub_lay3 = QHBoxLayout() sub_lay3.addWidget(button3) sub_lay3.addWidget(label3) sub_lay4 = QHBoxLayout() sub_lay4.addWidget(button4) sub_lay4.addWidget(label4) main_lay = QVBoxLayout() main_lay.addWidget(MDivider(u'Select')) main_lay.addLayout(sub_lay1) main_lay.addLayout(sub_lay2) main_lay.addLayout(sub_lay3) main_lay.addWidget(MDivider(u'级联选择')) main_lay.addLayout(sub_lay4) main_lay.addStretch() self.setLayout(main_lay)
def _slot_context_menu(self, point): context_menu = MMenu(parent=self) logical_column = self.logicalIndexAt(point) model = utils.real_model(self.model()) if logical_column >= 0 and model.header_list[logical_column].get( 'checkable', False): action_select_all = context_menu.addAction(self.tr('Select All')) action_select_none = context_menu.addAction(self.tr('Select None')) action_select_invert = context_menu.addAction( self.tr('Select Invert')) self.connect( action_select_all, SIGNAL('triggered()'), functools.partial(self._slot_set_select, logical_column, Qt.Checked)) self.connect( action_select_none, SIGNAL('triggered()'), functools.partial(self._slot_set_select, logical_column, Qt.Unchecked)) self.connect( action_select_invert, SIGNAL('triggered()'), functools.partial(self._slot_set_select, logical_column, None)) context_menu.addSeparator() fit_action = context_menu.addAction(self.tr('Fit Size')) fit_action.triggered.connect( functools.partial(self._slot_set_resize_mode, True)) context_menu.addSeparator() for column in range(self.count()): action = context_menu.addAction( model.headerData(column, Qt.Horizontal, Qt.DisplayRole)) action.setCheckable(True) action.setChecked(not self.isSectionHidden(column)) action.toggled.connect( functools.partial(self._slot_set_section_visible, column)) context_menu.exec_(QCursor.pos() + QPoint(10, 10))
class MOptionDelegate(QStyledItemDelegate): def __init__(self, parent=None): super(MOptionDelegate, self).__init__(parent) self.editor = None self.showed = False self.exclusive = True self.parent_widget = None self.arrow_space = 20 self.arrow_height = 6 def set_exclusive(self, flag): self.exclusive = flag def createEditor(self, parent, option, index): self.parent_widget = parent self.editor = MMenu(exclusive=self.exclusive, parent=parent) self.editor.setWindowFlags(Qt.FramelessWindowHint | Qt.Window) model = utils.real_model(index) real_index = utils.real_index(index) data_obj = real_index.internalPointer() attr = '{}_list'.format( model.header_list[real_index.column()].get('key')) self.editor.set_data(utils.get_obj_value(data_obj, attr, [])) self.editor.sig_value_changed.connect(self._slot_finish_edit) return self.editor def setEditorData(self, editor, index): editor.set_value(index.data(Qt.EditRole)) def setModelData(self, editor, model, index): model.setData(index, editor.property('value')) def updateEditorGeometry(self, editor, option, index): editor.move( self.parent_widget.mapToGlobal( QPoint(option.rect.x(), option.rect.y() + option.rect.height()))) def paint(self, painter, option, index): painter.save() icon_color = dayu_theme.icon_color if option.state & QStyle.State_MouseOver: painter.fillRect(option.rect, dayu_theme.primary_5) icon_color = '#fff' if option.state & QStyle.State_Selected: painter.fillRect(option.rect, dayu_theme.primary_6) icon_color = '#fff' painter.setRenderHint(QPainter.Antialiasing) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(Qt.white)) pix = MPixmap('down_fill.svg', icon_color) h = option.rect.height() pix = pix.scaledToWidth(h * 0.5, Qt.SmoothTransformation) painter.drawPixmap(option.rect.x() + option.rect.width() - h, option.rect.y() + h / 4, pix) painter.restore() super(MOptionDelegate, self).paint(painter, option, index) @Slot(object) def _slot_finish_edit(self, obj): self.commitData.emit(self.editor) def sizeHint(self, option, index): orig = super(MOptionDelegate, self).sizeHint(option, index) return QSize(orig.width() + self.arrow_space, orig.height())
def _init_ui(self): size_lay = QHBoxLayout() line_edit_l = MLineEdit().large() line_edit_l.setPlaceholderText('large size') line_edit_m = MLineEdit().medium() line_edit_m.setPlaceholderText('default size') line_edit_s = MLineEdit().small() line_edit_s.setPlaceholderText('small size') size_lay.addWidget(line_edit_l) size_lay.addWidget(line_edit_m) size_lay.addWidget(line_edit_s) line_edit_tool_button = MLineEdit(text='MToolButton') line_edit_tool_button.set_prefix_widget(MToolButton().svg('user_line.svg').icon_only()) line_edit_label = MLineEdit(text='MLabel') tool_button = MLabel(text='User').mark().secondary() tool_button.setAlignment(Qt.AlignCenter) tool_button.setFixedWidth(80) line_edit_label.set_prefix_widget(tool_button) line_edit_push_button = MLineEdit(text='MPushButton') push_button = MPushButton(text='Go').primary() push_button.setFixedWidth(40) line_edit_push_button.set_suffix_widget(push_button) search_engine_line_edit = MLineEdit().search_engine().large() search_engine_line_edit.returnPressed.connect(self.slot_search) line_edit_options = MLineEdit() combobox = MComboBox() option_menu = MMenu() option_menu.set_separator('|') option_menu.set_data([r'http://', r'https://']) combobox.set_menu(option_menu) combobox.set_value('http://') combobox.setFixedWidth(90) line_edit_options.set_prefix_widget(combobox) main_lay = QVBoxLayout() main_lay.addWidget(MDivider('different size')) main_lay.addLayout(size_lay) main_lay.addWidget(MDivider('custom prefix and suffix widget')) main_lay.addWidget(line_edit_tool_button) main_lay.addWidget(line_edit_label) main_lay.addWidget(line_edit_push_button) main_lay.addWidget(MDivider('preset')) main_lay.addWidget(MLabel('error')) main_lay.addWidget(MLineEdit(text='waring: file d:/ddd/ccc.jpg not exists.').error()) main_lay.addWidget(MLabel('search')) main_lay.addWidget(MLineEdit().search().small()) main_lay.addWidget(MLabel('search_engine')) main_lay.addWidget(search_engine_line_edit) main_lay.addWidget(MLabel('file')) main_lay.addWidget(MLineEdit().file().small()) main_lay.addWidget(MLabel('folder')) main_lay.addWidget(MLineEdit().folder().small()) main_lay.addWidget(MLabel('MLineEdit.options()')) main_lay.addWidget(line_edit_options) main_lay.addStretch() self.setLayout(main_lay)
def _init_ui(self): self.register_field('button1_selected', u'北京') menu1 = MMenu() menu1.set_data([u'北京', u'上海', u'广州', u'深圳']) size_list = [('Large', dayu_theme.large), ('Medium', dayu_theme.medium), ('Small', dayu_theme.small)] size_lay = QHBoxLayout() for label, size in size_list: combo_box = MComboBox() combo_box.set_dayu_size(size) combo_box.set_menu(menu1) size_lay.addWidget(combo_box) self.bind('button1_selected', combo_box, 'value', signal='sig_value_changed') self.register_field('button2_selected', [u'北京']) menu2 = MMenu(exclusive=False) menu2.set_data([u'北京', u'上海', u'广州', u'深圳']) select2 = MComboBox() select2.set_menu(menu2) self.bind('button2_selected', select2, 'value', signal='sig_value_changed') def dynamic_get_city(): data = [u'北京', u'上海', u'广州', u'深圳', u'郑州', u'石家庄'] start = random.randint(0, len(data)) end = random.randint(start, len(data)) return data[start:end] self.register_field('button3_selected', '') menu3 = MMenu() menu3.set_load_callback(dynamic_get_city) select3 = MComboBox() select3.set_menu(menu3) self.bind('button3_selected', select3, 'value', signal='sig_value_changed') a = [{ 'children': [{ 'value': u'\u6545\u5bab', 'label': u'\u6545\u5bab' }, { 'value': u'\u5929\u575b', 'label': u'\u5929\u575b' }, { 'value': u'\u738b\u5e9c\u4e95', 'label': u'\u738b\u5e9c\u4e95' }], 'value': u'\u5317\u4eac', 'label': u'\u5317\u4eac' }, { 'children': [{ 'children': [{ 'value': u'\u592b\u5b50\u5e99', 'label': u'\u592b\u5b50\u5e99' }], 'value': u'\u5357\u4eac', 'label': u'\u5357\u4eac' }, { 'children': [{ 'value': u'\u62d9\u653f\u56ed', 'label': u'\u62d9\u653f\u56ed' }, { 'value': u'\u72ee\u5b50\u6797', 'label': u'\u72ee\u5b50\u6797' }], 'value': u'\u82cf\u5dde', 'label': u'\u82cf\u5dde' }], 'value': u'\u6c5f\u82cf', 'label': u'\u6c5f\u82cf' }] self.register_field('button4_selected', '') menu4 = MMenu(cascader=True) menu4.set_data(a) select4 = MComboBox() select4.set_menu(menu4) select4.set_formatter(lambda x: ' / '.join(x)) self.bind('button4_selected', select4, 'value', signal='sig_value_changed') self.register_field('button5_selected', '') menu5 = MMenu(exclusive=False) menu5.set_data([u'北京', u'上海', u'广州', u'深圳']) select5 = MComboBox() select5.set_menu(menu5) select5.set_formatter(lambda x: ' & '.join(x)) self.bind('button5_selected', select5, 'value', signal='sig_value_changed') sub_lay1 = QHBoxLayout() sub_lay1.addWidget(MLabel(u'普通单选各种尺寸')) sub_lay1.addLayout(size_lay) sub_lay1.addStretch() sub_lay2 = QHBoxLayout() sub_lay2.addWidget(MLabel(u'多选')) sub_lay2.addWidget(select2) sub_lay2.addStretch() sub_lay3 = QHBoxLayout() sub_lay3.addWidget(MLabel(u'动态生成选项')) sub_lay3.addWidget(select3) sub_lay3.addStretch() sub_lay4 = QHBoxLayout() sub_lay4.addWidget(MLabel(u'级联选择')) sub_lay4.addWidget(select4) sub_lay4.addStretch() sub_lay5 = QHBoxLayout() sub_lay5.addWidget(MLabel(u'自定义显示')) sub_lay5.addWidget(select5) sub_lay5.addStretch() main_lay = QVBoxLayout() main_lay.addWidget(MDivider(u'Select')) main_lay.addLayout(sub_lay1) main_lay.addLayout(sub_lay2) main_lay.addLayout(sub_lay3) main_lay.addWidget(MDivider(u'自定义格式')) main_lay.addLayout(sub_lay4) main_lay.addLayout(sub_lay5) main_lay.addStretch() self.setLayout(main_lay)