From 6e2139f07a0a42bea1420eb60d8c259eb7c59e39 Mon Sep 17 00:00:00 2001 From: liugang0814 Date: Tue, 22 Sep 2020 23:55:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=A4=8D=E5=88=B6,=E7=B2=98?= =?UTF-8?q?=E8=B4=B4=E5=8A=9F=E8=83=BD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pmgwidgets/platform/__init__.py | 2 +- pmgwidgets/platform/fileutils.py | 22 +++++++++++++ pmgwidgets/treeviews/filetreeview.py | 47 ++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/pmgwidgets/platform/__init__.py b/pmgwidgets/platform/__init__.py index b2556b80..7dbd1587 100644 --- a/pmgwidgets/platform/__init__.py +++ b/pmgwidgets/platform/__init__.py @@ -1,3 +1,3 @@ -from .fileutils import move_to_trash,rename_file +from .fileutils import move_to_trash, rename_file, copy_paste from .commandutil import run_command_in_terminal from .translation import add_translation_file \ No newline at end of file diff --git a/pmgwidgets/platform/fileutils.py b/pmgwidgets/platform/fileutils.py index 81c5a6cf..285ce7a1 100644 --- a/pmgwidgets/platform/fileutils.py +++ b/pmgwidgets/platform/fileutils.py @@ -34,5 +34,27 @@ def rename_file(prev_absolute_path: str, new_absolute_path: str) -> bool: return False +def copy_paste(source_path: str, target_path: str): + """ + + :param source_path: 源文件或文件夹 + :param target_path: 目标文件或文件夹 + :return: + """ + import shutil, os + if os.path.isfile(source_path): + copy_func = shutil.copyfile + else: + copy_func = shutil.copytree + + try: + copy_func(source_path, target_path) + except: + import traceback + traceback.print_exc() + return False + return True + + if __name__ == '__main__': move_to_trash('C:/Users/12957/Desktop/1.jpg') diff --git a/pmgwidgets/treeviews/filetreeview.py b/pmgwidgets/treeviews/filetreeview.py index 372c5c14..d3b52731 100644 --- a/pmgwidgets/treeviews/filetreeview.py +++ b/pmgwidgets/treeviews/filetreeview.py @@ -89,6 +89,9 @@ class PMGFilesTreeview(QTreeView): self.renameAction.triggered.connect(self.on_rename) self.deleteAction.triggered.connect(self.on_delete) + self.copyAction.triggered.connect(self.on_copy) + self.pasteAction.triggered.connect(self.on_paste) + self.new_file_action.triggered.connect(self.on_new_file) self.new_folder_action.triggered.connect(self.on_new_folder) @@ -111,6 +114,10 @@ class PMGFilesTreeview(QTreeView): self.new_folder_action = self.new_file_or_folder_menu.addAction(self.tr('Folder')) self.new_file_or_folder_menu.addSeparator() + self.copyAction = self.contextMenu.addAction(self.tr("Copy")) + self.pasteAction = self.contextMenu.addAction(self.tr("Paste")) + self.pasteAction.setEnabled(False) + self.renameAction = self.contextMenu.addAction(self.tr('Rename')) self.deleteAction = self.contextMenu.addAction(self.tr('Delete')) @@ -226,6 +233,46 @@ class PMGFilesTreeview(QTreeView): else: self.delete_file_signal.emit(path) + def on_copy(self): + """ + copy file or dir , save path in pasteAction data. + :return: + """ + path = self.get_current_file_path() + self.pasteAction.setEnabled(True) + self.pasteAction.setData(path) + + def on_paste(self): + """ + Paste file or dir in pasteAction data + :return: + """ + from pmgwidgets.platform import copy_paste + target_dir_name = os.path.dirname(self.get_current_file_path()) + source_path = self.pasteAction.data() + # File + if os.path.isfile(source_path): + source_file_name = os.path.basename(source_path) + # if exist ,rename to copy_xxx + if os.path.isfile(os.path.join(target_dir_name, source_file_name)): + target_file_name = "copy_{0}".format(source_file_name) + else: + target_dir_name = source_file_name + target_path = os.path.join(target_dir_name, target_file_name) + # Directory + else: + last_dir_name = os.path.split(source_path)[-1] + # if exist , rename dir copy_xxxx + if os.path.isdir(os.path.join(target_dir_name,last_dir_name)): + target_name = "copy_{0}".format(last_dir_name) + else: + target_name = last_dir_name + target_path = os.path.join(target_dir_name,target_name) + + copy_succ = copy_paste(source_path,target_path) + if not copy_succ: + QMessageBox.critical(self, self.tr('Error'), + self.tr('Copy File or Directory Error.')) class Stack(object): -- Gitee