104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from PySide6.QtWidgets import (
|
|
QDialog,
|
|
QHBoxLayout,
|
|
QInputDialog,
|
|
QListWidget,
|
|
QListWidgetItem,
|
|
QPushButton,
|
|
QVBoxLayout,
|
|
)
|
|
|
|
|
|
class BookmarkManagerDialog(QDialog):
|
|
def __init__(self, bookmarks: list[dict[str, str]], parent: object | None = None) -> None:
|
|
super().__init__(parent)
|
|
self.setWindowTitle("书签管理")
|
|
self.setMinimumSize(560, 360)
|
|
self._bookmarks = [dict(item) for item in bookmarks]
|
|
self.selected_bookmark: dict[str, str] | None = None
|
|
|
|
self.list_widget = QListWidget()
|
|
self._populate()
|
|
|
|
self.open_button = QPushButton("打开")
|
|
self.open_button.clicked.connect(self._open_selected)
|
|
|
|
self.rename_button = QPushButton("重命名")
|
|
self.rename_button.clicked.connect(self._rename_selected)
|
|
|
|
self.delete_button = QPushButton("删除")
|
|
self.delete_button.clicked.connect(self._delete_selected)
|
|
|
|
self.close_button = QPushButton("关闭")
|
|
self.close_button.clicked.connect(self.accept)
|
|
|
|
button_layout = QHBoxLayout()
|
|
button_layout.addWidget(self.open_button)
|
|
button_layout.addWidget(self.rename_button)
|
|
button_layout.addWidget(self.delete_button)
|
|
button_layout.addStretch(1)
|
|
button_layout.addWidget(self.close_button)
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.addWidget(self.list_widget)
|
|
layout.addLayout(button_layout)
|
|
|
|
def bookmarks(self) -> list[dict[str, str]]:
|
|
return [dict(item) for item in self._bookmarks]
|
|
|
|
def _populate(self) -> None:
|
|
self.list_widget.clear()
|
|
for index, bookmark in enumerate(self._bookmarks):
|
|
label = bookmark.get("label") or Path(bookmark.get("path", "")).name
|
|
path = bookmark.get("path", "")
|
|
fragment = bookmark.get("fragment", "")
|
|
suffix = f" #{fragment}" if fragment else ""
|
|
item = QListWidgetItem(f"{label}{suffix}\n{path}")
|
|
item.setData(256, index)
|
|
self.list_widget.addItem(item)
|
|
|
|
def _current_index(self) -> int | None:
|
|
item = self.list_widget.currentItem()
|
|
if item is None:
|
|
return None
|
|
index = item.data(256)
|
|
if isinstance(index, int) and 0 <= index < len(self._bookmarks):
|
|
return index
|
|
return None
|
|
|
|
def _open_selected(self) -> None:
|
|
index = self._current_index()
|
|
if index is None:
|
|
return
|
|
self.selected_bookmark = dict(self._bookmarks[index])
|
|
self.accept()
|
|
|
|
def _rename_selected(self) -> None:
|
|
index = self._current_index()
|
|
if index is None:
|
|
return
|
|
|
|
current = self._bookmarks[index].get("label", "")
|
|
text, ok = QInputDialog.getText(self, "重命名书签", "名称", text=current)
|
|
if not ok:
|
|
return
|
|
text = text.strip()
|
|
if text:
|
|
self._bookmarks[index]["label"] = text
|
|
self._populate()
|
|
self.list_widget.setCurrentRow(index)
|
|
|
|
def _delete_selected(self) -> None:
|
|
index = self._current_index()
|
|
if index is None:
|
|
return
|
|
del self._bookmarks[index]
|
|
self._populate()
|
|
if self._bookmarks:
|
|
self.list_widget.setCurrentRow(min(index, len(self._bookmarks) - 1))
|
|
|