def show_items(self, cursor, items: list[str], prefix_length: int = 0): """Show the widget with 'items' at the position specified by 'cursor'.""" point = self._get_top_left_position(cursor) self.clear() path_items = [] for item in items: # Check if the item could refer to a file or dir. The replacing # of '"' is needed for items on Windows path = os.path.abspath(item.replace('"', "")) if os.path.isfile(path) or os.path.isdir(path): path_items.append(item.replace('"', "")) else: list_item = widgets.ListWidgetItem() list_item.setData(QtCore.Qt.ItemDataRole.UserRole, item) # type: ignore # Need to split to only show last element of a dot completion list_item.setText(item.split(".")[-1]) self.addItem(list_item) common_prefix = os.path.dirname(os.path.commonprefix(path_items)) for path_item in path_items: list_item = widgets.ListWidgetItem() list_item.setData(QtCore.Qt.ItemDataRole.UserRole, path_item) # type: ignore text = path_item.split( common_prefix)[-1] if common_prefix else path_item list_item.setText(text) self.addItem(list_item) height = self.sizeHint().height() screen_rect = self.get_screen().availableGeometry() if screen_rect.size().height() + screen_rect.y() - point.y( ) - height < 0: point = self._text_edit.mapToGlobal( self._text_edit.cursorRect().topRight()) point.setY(int(point.y() - height)) scrollbar_width = self.verticalScrollBar().sizeHint().width() w = self.sizeHintForColumn(0) + scrollbar_width + 2 * self.frameWidth() self.setGeometry(point.x(), point.y(), w, height) # Move cursor to start of the prefix to replace it # when a item is selected cursor.movePosition(QtGui.QTextCursor.MoveOperation.Left, n=prefix_length) self._start_position = cursor.position() self.setCurrentRow(0) self.raise_() self.show()
def test_listwidgetitem(): item = widgets.ListWidgetItem() with open("data.pkl", "wb") as jar: pickle.dump(item, jar) with open("data.pkl", "rb") as jar: item = pickle.load(jar) repr(item)
def add(self, label: str, data=NoData, icon: types.IconType = None): if data is NoData: data = label item = widgets.ListWidgetItem(label) item.set_icon(icon) item.setData(constants.USER_ROLE, data) # type: ignore self.addItem(item)
def add(self, label: str, data=NoData, icon=None): if data is NoData: data = label item = widgets.ListWidgetItem(label) item.set_icon(icon) item.setData(QtCore.Qt.UserRole, data) self.addItem(item)
def test_listwidgetitem(): item = widgets.ListWidgetItem() with open("data.pkl", "wb") as jar: pickle.dump(item, jar) with open("data.pkl", "rb") as jar: item = pickle.load(jar) repr(item) item.set_icon("mdi.timer") item.set_checkstate("unchecked") with pytest.raises(ValueError): item.set_checkstate("test") assert item.get_checkstate() == "unchecked"
def test_listwidget(): widget = widgets.ListWidget() widget.add("test", icon="mdi.timer") widget.add("test", icon="mdi.timer") with open("data.pkl", "wb") as jar: pickle.dump(widget, jar) with open("data.pkl", "rb") as jar: widget = pickle.load(jar) assert len(widget) == 2 for item in widget: pass item = widgets.ListWidgetItem() widget += item repr(widget)
def add_item( self, name: str = "", icon: types.IconType = None, data: dict | None = None, foreground: QtGui.QBrush | None = None, background: QtGui.QBrush | None = None, font: QtGui.QFont | None = None, selected: bool = None, status_tip: str | None = None, tool_tip: str | None = None, whats_this: str | None = None, # text_alignment: Optional[str] = None, checkstate: constants.StateStr | None = None, flags: QtCore.Qt.ItemFlags | None = None, size_hint: types.SizeType | None = None, is_user_type: bool = False, ) -> widgets.ListWidgetItem: typ = 1 if is_user_type else 0 item = widgets.ListWidgetItem(name, self, typ) if icon is not None: icon = iconprovider.get_icon(icon) item.setIcon(icon) if data is not None: for k, v in data.items(): item.setData(k, v) if foreground is not None: item.setForeground(foreground) if background is not None: item.setBackground(background) if font is not None: item.setFont(font) if flags is not None: item.setFlags(flags) if selected: item.setSelected(selected) if status_tip: item.setStatusTip(status_tip) if tool_tip: item.setToolTip(tool_tip) if whats_this: item.setWhatsThis(whats_this) if size_hint is not None: item.set_size_hint(size_hint) if checkstate is not None: item.set_checkstate(checkstate) self.addItem(item) return item
def test_listwidget(): widget = widgets.ListWidget() widget.add("test", icon="mdi.timer") widget.add("test", icon="mdi.timer") with open("data.pkl", "wb") as jar: pickle.dump(widget, jar) with open("data.pkl", "rb") as jar: widget = pickle.load(jar) assert len(widget) == 2 item = widget[0] assert item is not None widget.scroll_to_item(item, mode="ensure_visible") with pytest.raises(ValueError): widget.scroll_to_item(item, mode="test") widget.on_index_change() widget.add_items(["a", "b"]) widget.set_value("b") for item in widget: pass item = widgets.ListWidgetItem() widget += item repr(widget)