From 8d877eb71b1af99eca2100dd63238664efa1bdf8 Mon Sep 17 00:00:00 2001 From: Maxim Stewart Date: Sat, 6 Jul 2019 19:08:25 -0500 Subject: [PATCH] File action menu edits --- src/pytop-0.0.1/Pytop/resources/PyTop.glade | 171 ++++++++++++++---- .../Pytop/signal_classes/GridSignals.py | 80 ++++---- src/pytop-0.0.1/Pytop/utils/FileHandler.py | 107 ++++++----- src/pytop-0.0.1/Pytop/widgets/Grid.py | 56 +++--- 4 files changed, 279 insertions(+), 135 deletions(-) diff --git a/src/pytop-0.0.1/Pytop/resources/PyTop.glade b/src/pytop-0.0.1/Pytop/resources/PyTop.glade index 5436b5c..785772d 100644 --- a/src/pytop-0.0.1/Pytop/resources/PyTop.glade +++ b/src/pytop-0.0.1/Pytop/resources/PyTop.glade @@ -82,6 +82,11 @@ + + True + False + gtk-new + True False @@ -91,24 +96,28 @@ False False True + center + splashscreen False + center + 500 True False vertical - + 500 26 True True gtk-edit - + False @@ -117,7 +126,7 @@ - + True False @@ -154,6 +163,126 @@ 1 + + + gtk-delete + True + True + True + Delete... + True + True + + + + False + True + 4 + + + + + Trash + True + True + True + Move to Trash... + trashImage + True + + + + False + True + end + 3 + + + + + False + True + 1 + + + + + True + False + vertical + + + True + False + + + True + False + 15 + Folder + + + + + + True + True + 0 + + + + + True + False + 15 + File + + + + + + True + True + 1 + + + + + False + True + 0 + + + + + True + True + File/Folder + True + + + False + True + 1 + + + + + Create + True + True + True + Create File/Folder... + createImage + True + + + + False + True + 2 + + gtk-paste @@ -168,48 +297,14 @@ False True - 2 - - - - - Trash - True - True - True - trashImage - True - - - - False - True - end 3 - - - gtk-delete - True - True - True - True - True - - - - False - True - end - 4 - - False True - 1 + 2 diff --git a/src/pytop-0.0.1/Pytop/signal_classes/GridSignals.py b/src/pytop-0.0.1/Pytop/signal_classes/GridSignals.py index 1117f4f..ca1d22f 100644 --- a/src/pytop-0.0.1/Pytop/signal_classes/GridSignals.py +++ b/src/pytop-0.0.1/Pytop/signal_classes/GridSignals.py @@ -23,11 +23,11 @@ class GridSignals: self.selectedFiles = [] self.grid = None self.currentPath = "" + self.pasteType = 1 # copy == 1 and cut == 2 # Add filter to allow only folders to be selected selectedDirDialog.add_filter(filefilter) selectedDirDialog.set_filename(self.desktopPath) - self.setIconViewDir(selectedDirDialog) @@ -38,50 +38,64 @@ class GridSignals: # File control events - def createFile(arg): - pass + def create(self, wdget): + self.currentPath = self.grid.returnCurrentPath() + fileName = self.builder.get_object("filenameInput").get_text().strip() + type = self.builder.get_object("createSwitch").get_state() - def updateFile(self, widget, data): + if fileName != "": + fileName = self.currentPath + "/" + fileName + status = self.filehandler.create(fileName, type) + if status == 0: + self.grid.setIconViewDir(self.currentPath) + + def copy(self, widget): + self.pasteType = 1 + self.copyCutArry = self.grid.returnSelectedFiles() + + def cut(self, widget): + self.pasteType = 2 + self.copyCutArry = self.grid.returnSelectedFiles() + + def paste(self, widget): + print(len(self.copyCutArry)) + self.currentPath = self.grid.returnCurrentPath() + status = self.filehandler.paste(self.copyCutArry, self.currentPath, self.pasteType) + if status == 0: + self.grid.setIconViewDir(self.currentPath) + if self.pasteType == 2: # cut == 2 + self.copyCutArry = [] + + def delete(self, widget): + self.getGridInfo() + status = self.filehandler.delete(self.selectedFiles) + + if status == 0: + self.selectedFiles = [] + self.grid.setIconViewDir(self.currentPath) + + def trash(self, widget): + self.getGridInfo() + status = self.filehandler.trash(self.selectedFiles) + + if status == 0: + self.selectedFiles = [] + self.grid.setIconViewDir(self.currentPath) + + def rename(self, widget, data): if data.keyval == 65293: # Enter key press self.getGridInfo() file = widget.get_text(); if len(self.selectedFiles) == 1: newName = self.currentPath + "/" + file - status = self.filehandler.updateFile(self.selectedFiles[0], newName) print("Old Name: " + self.selectedFiles[0]) - print("New Name: " + newName) + print("New Name: " + newName.strip()) + status = self.filehandler.rename(self.selectedFiles[0], newName.strip()) if status == 0: self.selectedFiles = [newName] self.grid.setIconViewDir(self.currentPath) - - def trashFiles(self, widget): - self.getGridInfo() - status = self.filehandler.moveToTrash(self.selectedFiles) - - if status == 0: - self.selectedFiles = [] - self.grid.setIconViewDir(self.currentPath) - - - def deleteFiles(self, widget): - self.getGridInfo() - status = self.filehandler.deleteFiles(self.selectedFiles) - - if status == 0: - self.selectedFiles = [] - self.grid.setIconViewDir(self.currentPath) - - def copy(self): - self.copyCutArry = self.grid.returnSelectedFiles() - - def cut(self): - self.copyCutArry = self.grid.returnSelectedFiles() - - def paste(self): - self.currentPath = self.grid.returnCurrentPath() - def getGridInfo(self): self.selectedFiles = self.grid.returnSelectedFiles() self.currentPath = self.grid.returnCurrentPath() diff --git a/src/pytop-0.0.1/Pytop/utils/FileHandler.py b/src/pytop-0.0.1/Pytop/utils/FileHandler.py index 85096ea..69577f0 100644 --- a/src/pytop-0.0.1/Pytop/utils/FileHandler.py +++ b/src/pytop-0.0.1/Pytop/utils/FileHandler.py @@ -55,33 +55,58 @@ class FileHandler: subprocess.Popen(['xdg-open', file]) - def createFile(self, newFileName): - pass - - def updateFile(self, oldFileName, newFileName): + def create(self, name, type): try: - print("Renaming...") - print(oldFileName + " --> " + newFileName) - os.rename(oldFileName, newFileName) - return 0 + if type == True: # Create File + open(name, 'w') + else: # Create Folder + os.mkdir(name) except Exception as e: - print("An error occured renaming the file:") + return 1 + + return 0 + + def paste(self, files, toPath, pasteType): + try: + for file in files: + parts = file.split("/") + toBePath = toPath + parts[len(parts) - 1] + finalForm = file + self.dedupPathIter(toBePath) + + print(toBePath) + print(finalForm) + except Exception as e: + return 1 + + # if finalForm != file: + # os.rename(file, finalForm) + # + # if pasteType == 1: # copy paste == 1 + # shutil.move(finalForm, toPath) + # if pasteType == 2: # cut paste == 2 + # shutil.copy2(finalForm, toPath) + + def delete(self, toDeleteFiles): + try: + print("Deleting...") + for file in toDeleteFiles: + print(file) + if os.path.exists(file): + if os.path.isfile(file): + os.remove(file) + elif os.path.isdir(file): + shutil.rmtree(file) + else: + print("The folder/file does not exist") + return 1 + except Exception as e: + print("An error occured deleting the file:") print(e) return 1 - def dedupPathIter(self, toBeTrashPath): - i = 0 - duplicateFix = "" + return 0 - if os.path.exists(toBeTrashPath): - while os.path.exists(toBeTrashPath + duplicateFix) == True: - i+=1 - duplicateFix = "-" + str(i) - - return duplicateFix - - - def moveToTrash(self, toTrashFiles): + def trash(self, toTrashFiles): try: print("Moving to Trash...") for file in toTrashFiles: @@ -105,34 +130,30 @@ class FileHandler: return 0 - def deleteFiles(self, toDeleteFiles): + def rename(self, oldFileName, newFileName): try: - print("Deleting...") - for file in toDeleteFiles: - print(file) - if os.path.exists(file): - if os.path.isfile(file): - os.remove(file) - elif os.path.isdir(file): - shutil.rmtree(file) - else: - print("An error occured deleting the file:") - return 1 - else: - print("The folder/file does not exist") - return 1 + if os.path.exists(oldFileName): + print("Renaming...") + print(oldFileName + " --> " + newFileName) + os.rename(oldFileName, newFileName) + else: + print("The folder/file does not exist") + return 1 except Exception as e: - print("An error occured deleting the file:") + print("An error occured renaming the file:") print(e) return 1 return 0 - def copyFile(self): - pass - def cutFile(self): - pass + def dedupPathIter(self, toBeTrashPath): + duplicateFix = "" + i = 0 - def pasteFile(self): - pass + if os.path.exists(toBeTrashPath): + while os.path.exists(toBeTrashPath + duplicateFix) == True: + i+=1 + duplicateFix = "-" + str(i) + + return duplicateFix diff --git a/src/pytop-0.0.1/Pytop/widgets/Grid.py b/src/pytop-0.0.1/Pytop/widgets/Grid.py index bdfc736..a7a57a3 100644 --- a/src/pytop-0.0.1/Pytop/widgets/Grid.py +++ b/src/pytop-0.0.1/Pytop/widgets/Grid.py @@ -39,14 +39,14 @@ class Grid: self.threadLock = False # Gtk checks for thread lock self.helperThread = None # Helper thread object self.toWorkPool = [] # Thread fills pool and gtk empties it - self.SelectedFiles = [] + self.selectedFiles = [] self.currentPath = "" self.desktop.set_model(self.store) self.desktop.set_pixbuf_column(0) self.desktop.set_text_column(1) self.desktop.connect("item-activated", self.iconDblLeftClick) - self.desktop.connect("button_press_event", self.iconRightClick, (self.desktop,)) + self.desktop.connect("button_release_event", self.iconSingleClick, (self.desktop,)) def setIconViewDir(self, path): @@ -151,39 +151,53 @@ class Grid: except Exception as e: print(e) - def iconRightClick(self, widget, eve, rclicked_icon): + def iconSingleClick(self, widget, eve, rclicked_icon): try: - if eve.type == gdk.EventType.BUTTON_PRESS and eve.button == 3: - input = self.builder.get_object("iconRenameInput") - controls = self.builder.get_object("iconControlsWindow") + if eve.type == gdk.EventType.BUTTON_RELEASE and eve.button == 1: + self.selectedFiles.clear() items = widget.get_selected_items() model = widget.get_model() - self.SelectedFiles.clear() + dir = self.currentPath - if len(items) == 1: - fileName = model[items[0]][1] - dir = self.currentPath - file = dir + "/" + fileName + for item in items: + fileName = model[item][1] - self.SelectedFiles.append(file) # Used for return to caller - input.set_text(fileName) - controls.show_all() - elif len(items) > 1: - dir = self.currentPath - for item in items: - fileName = model[item][1] - file = dir + "/" + fileName - self.SelectedFiles.append(file) # Used for return to caller + if fileName != "." and fileName != "..": + file = dir + "/" + fileName + self.selectedFiles.append(file) # Used for return to caller + elif eve.type == gdk.EventType.BUTTON_RELEASE and eve.button == 3: + input = self.builder.get_object("filenameInput") + controls = self.builder.get_object("iconControlsWindow") + iconsButtonBox = self.builder.get_object("iconsButtonBox") + menuButtonBox = self.builder.get_object("menuButtonBox") + + + if len(self.selectedFiles) == 1: + parts = self.selectedFiles[0].split("/") + input.set_text(parts[len(parts) - 1]) + input.show() + iconsButtonBox.show() + menuButtonBox.hide() + controls.show() + elif len(self.selectedFiles) > 1: input.set_text("") input.hide() + menuButtonBox.hide() + iconsButtonBox.show() + controls.show() + else: + input.set_text("") + input.show() + menuButtonBox.show() + iconsButtonBox.hide() controls.show() except Exception as e: print(e) def returnSelectedFiles(self): - return self.SelectedFiles + return self.selectedFiles def returnCurrentPath(self): return self.currentPath