def setter(w, val): order_map = {x: i for i, x in enumerate(val)} items = list(w.defaults) limit = len(items) items.sort(key=lambda x: order_map.get(x, limit)) w.clear() for x in items: i = QListWidgetItem(w) i.setText(x) i.setFlags(i.flags() | Qt.ItemFlag.ItemIsDragEnabled)
def setup_ui(self): self.vl = vl = QVBoxLayout(self) self.la = la = QLabel( _('Pick multiple columns to sort by. Drag and drop to re-arrange. Higher columns are more important.' ' Ascending or descending order can be toggled by clicking the column name at the bottom' ' of this dialog, after having selected it.')) la.setWordWrap(True) vl.addWidget(la) self.order_label = la = QLabel('\xa0') la.setTextFormat(Qt.TextFormat.RichText) la.setWordWrap(True) la.linkActivated.connect(self.link_activated) self.column_list = cl = QListWidget(self) vl.addWidget(cl) vl.addWidget(la) vl.addWidget(self.bb) for name in self.all_names: i = QListWidgetItem(cl) i.setText(name) i.setData(Qt.ItemDataRole.UserRole, self.name_map[name]) cl.addItem(i) i.setCheckState(Qt.CheckState.Unchecked) if self.name_map[name] in self.hidden_fields: i.setHidden(True) cl.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove) cl.currentRowChanged.connect(self.current_changed) cl.itemDoubleClicked.connect(self.item_double_clicked) cl.setCurrentRow(0) cl.itemChanged.connect(self.update_order_label) cl.model().rowsMoved.connect(self.update_order_label) self.clear_button = b = self.bb.addButton( _('&Clear'), QDialogButtonBox.ButtonRole.ActionRole) b.setToolTip(_('Clear all selected columns')) b.setAutoDefault(False) b.clicked.connect(self.clear) self.save_button = b = self.bb.addButton( _('&Save'), QDialogButtonBox.ButtonRole.ActionRole) b.setToolTip(_('Save this sort order for easy re-use')) b.clicked.connect(self.save) b.setAutoDefault(False) self.load_button = b = self.bb.addButton( _('&Load'), QDialogButtonBox.ButtonRole.ActionRole) b.setToolTip(_('Load previously saved settings')) b.setAutoDefault(False) self.load_menu = QMenu(b) b.setMenu(self.load_menu) self.load_menu.aboutToShow.connect(self.populate_load_menu)
def setText(self, txt): self.current_value = txt QListWidgetItem.setText(txt)