优化更新
This commit is contained in:
+74
-17
@@ -17,6 +17,32 @@ import shortcut_target
|
||||
import ui.dialog_style as dialog_style
|
||||
|
||||
|
||||
def _copy_file_into_folder(src_path: str, folder_path: str) -> str:
|
||||
"""把文件复制到文件夹分组目录,避免同名覆盖(自动追加序号)。返回最终路径。"""
|
||||
import shutil
|
||||
dest_path = os.path.join(folder_path, os.path.basename(src_path))
|
||||
if os.path.exists(dest_path) and os.path.normpath(dest_path) != os.path.normpath(src_path):
|
||||
base, ext = os.path.splitext(os.path.basename(src_path))
|
||||
i = 1
|
||||
while os.path.exists(dest_path):
|
||||
dest_path = os.path.join(folder_path, f"{base} ({i}){ext}")
|
||||
i += 1
|
||||
try:
|
||||
if os.path.normpath(dest_path) != os.path.normpath(src_path):
|
||||
shutil.copy2(src_path, dest_path)
|
||||
return dest_path
|
||||
except Exception as e:
|
||||
print(f"复制失败: {src_path} -> {e}")
|
||||
return src_path # 复制失败就用原路径
|
||||
|
||||
|
||||
def _group_folder_path(group_id: int) -> str:
|
||||
"""分组对应的文件夹路径(非文件夹分组返回空串)。"""
|
||||
groups = database.get_groups()
|
||||
info = next((g for g in groups if g["id"] == group_id), {})
|
||||
return (info.get("folder_path", "") or "").strip()
|
||||
|
||||
|
||||
def item_count_for_group(group_id: int) -> int:
|
||||
"""分组内程序/文件总数(文件夹分组数磁盘文件,普通分组数数据库条目)。"""
|
||||
groups = database.get_groups()
|
||||
@@ -253,7 +279,53 @@ class FlowWidget(QWidget):
|
||||
self.update()
|
||||
mime = event.mimeData()
|
||||
if mime.hasFormat("application/x-item-id"):
|
||||
item_id = int(mime.data("application/x-item-id").data().decode())
|
||||
raw = bytes(mime.data("application/x-item-id")).decode()
|
||||
item_id: int | None
|
||||
if raw in ("", "none"):
|
||||
item_id = None
|
||||
else:
|
||||
try:
|
||||
item_id = int(raw)
|
||||
except ValueError:
|
||||
item_id = None
|
||||
|
||||
folder_path = _group_folder_path(self.group_id)
|
||||
is_folder_group = bool(folder_path) and os.path.isdir(folder_path)
|
||||
|
||||
if item_id is None:
|
||||
# 文件夹分组的条目(无数据库 id):按源文件处理
|
||||
if not mime.hasUrls():
|
||||
event.ignore()
|
||||
return
|
||||
if is_folder_group:
|
||||
for url in mime.urls():
|
||||
path = url.toLocalFile()
|
||||
if path and os.path.isfile(path):
|
||||
_copy_file_into_folder(path, folder_path)
|
||||
else:
|
||||
# 拖入普通分组:写入数据库
|
||||
for url in mime.urls():
|
||||
path = url.toLocalFile()
|
||||
if path:
|
||||
store = shortcut_target.path_for_storage(path)
|
||||
name = shortcut_target.item_name_from_sources(path, store)
|
||||
database.add_item(self.group_id, name, store)
|
||||
self.refresh()
|
||||
self.item_dropped.emit(0)
|
||||
event.acceptProposedAction()
|
||||
return
|
||||
|
||||
if is_folder_group:
|
||||
# 数据库条目拖入文件夹分组:复制源文件到目录,原分组条目保持不变
|
||||
if mime.hasUrls():
|
||||
for url in mime.urls():
|
||||
path = url.toLocalFile()
|
||||
if path and os.path.isfile(path):
|
||||
_copy_file_into_folder(path, folder_path)
|
||||
self.refresh()
|
||||
event.acceptProposedAction()
|
||||
return
|
||||
|
||||
pos = self._container.mapFrom(self, event.position().toPoint())
|
||||
insert_pos = self._container.index_at(pos)
|
||||
database.move_item(item_id, self.group_id, 999)
|
||||
@@ -344,22 +416,7 @@ class FlowWidget(QWidget):
|
||||
for path in clipboard_paths:
|
||||
if is_folder_group:
|
||||
# 真正复制文件到文件夹目录
|
||||
import shutil
|
||||
dest_path = os.path.join(folder_path, os.path.basename(path))
|
||||
# 避免覆盖同名文件,自动重命名
|
||||
if os.path.exists(dest_path) and dest_path != path:
|
||||
base, ext = os.path.splitext(os.path.basename(path))
|
||||
i = 1
|
||||
while os.path.exists(dest_path):
|
||||
dest_path = os.path.join(folder_path, f"{base} ({i}){ext}")
|
||||
i += 1
|
||||
try:
|
||||
if path != dest_path:
|
||||
shutil.copy2(path, dest_path)
|
||||
final_path = dest_path
|
||||
except Exception as e:
|
||||
print(f"复制失败: {path} -> {e}")
|
||||
final_path = path # 复制失败就用原路径
|
||||
final_path = _copy_file_into_folder(path, folder_path)
|
||||
name = os.path.splitext(os.path.basename(final_path))[0] or os.path.basename(final_path)
|
||||
else:
|
||||
final_path = shortcut_target.path_for_storage(path)
|
||||
|
||||
Reference in New Issue
Block a user