From 9b7c2a2a4089af5c532a4829418b19ad509b7ae8 Mon Sep 17 00:00:00 2001
From: mhby1g21 <mhby1g21@soton.ac.uk>
Date: Tue, 29 Oct 2024 13:56:59 +0000
Subject: [PATCH] added clean output folder and clean all files buttons

---
 scripts/debug_tool/tabs/edge_net_tab.py | 199 +++++++++++++++++++++++-
 1 file changed, 197 insertions(+), 2 deletions(-)

diff --git a/scripts/debug_tool/tabs/edge_net_tab.py b/scripts/debug_tool/tabs/edge_net_tab.py
index fc2446e..6760896 100644
--- a/scripts/debug_tool/tabs/edge_net_tab.py
+++ b/scripts/debug_tool/tabs/edge_net_tab.py
@@ -228,6 +228,34 @@ class EdgeNetTab:
             state='disabled'
         )
         self.cancel_button.pack(pady=5)
+        
+        # Add separator before clean buttons
+        ttk.Separator(button_frame, orient='horizontal').pack(fill=tk.X, pady=5)
+        
+        # Create frame for clean buttons
+        clean_frame = ttk.Frame(button_frame)
+        clean_frame.pack(fill=tk.X, pady=5)
+        
+        # Add clean output button
+        ttk.Button(
+            clean_frame,
+            text="Clean Output Directory",
+            command=self.clean_output_directory,
+            style='CleanOutput.TButton'  # Custom style for warning-like button but less severe
+        ).pack(side=tk.LEFT, padx=5)
+        
+        # Add clean all files button
+        ttk.Button(
+            clean_frame,
+            text="Clean All Files",
+            command=self.clean_all_files,
+            style='CleanAll.TButton'  # Custom style for warning-like button
+        ).pack(side=tk.LEFT, padx=5)
+        
+        # Create custom styles for clean buttons
+        style = ttk.Style()
+        style.configure('CleanOutput.TButton', foreground='orange')  # Less severe warning color
+        style.configure('CleanAll.TButton', foreground='red')       # Severe warning color
 
     def run_process_with_output(self, cmd, description):
         """Run a process and capture output in real-time"""
@@ -733,7 +761,7 @@ class EdgeNetTab:
     def update_depth_preview(self):
         """Update the enhanced depth preview"""
         preview_size = (300, 150)  # Adjust size as needed
-        depth_path = os.path.join(self.edge_net_dir, "Input", "enhanced_depth_e.png")
+        depth_path = os.path.join(self.input_dir, "enhanced_depth_e.png")
         
         if os.path.exists(depth_path):
             try:
@@ -749,4 +777,171 @@ class EdgeNetTab:
         """Update the mesh preview"""
         # Note: This would require additional implementation to render mesh preview
         # Could potentially use a screenshot or placeholder image
-        self.update_status("Mesh preview not implemented")
\ No newline at end of file
+        self.update_status("Mesh preview not implemented")
+        
+    def clean_all_files(self):
+        """Clean all output and intermediary files"""
+        # Create detailed message of what will be cleaned
+        clean_message = "This will delete all files in:\n\n"
+        clean_message += "1. edgenet-360/Data/Input/\n"
+        clean_message += "2. edgenet-360/Output/\n"
+        clean_message += "3. Temporary files\n\n"
+        clean_message += "Are you sure you want to proceed?"
+        
+        # Show confirmation dialog
+        if not messagebox.askyesno("Confirm Clean", clean_message, icon='warning'):
+            self.update_status("Clean operation cancelled by user")
+            return
+            
+        try:
+            # List of directories to clean
+            directories = [
+                os.path.join(self.edge_net_dir, "Data", "Input"),
+                self.output_dir
+            ]
+            
+            # List of temp files to remove
+            temp_files = [
+                self.config_reader.file_paths['shiftedImage'],
+                self.config_reader.file_paths['monoDepthImage'],
+                os.path.join(self.edge_net_dir, "process_output.log")  # WSL output log if exists
+            ]
+            
+            files_removed = 0
+            
+            # Clean directories
+            for directory in directories:
+                if os.path.exists(directory):
+                    try:
+                        # List all files before deleting for reporting
+                        files = os.listdir(directory)
+                        self.update_status(f"\nCleaning directory: {directory}")
+                        for file in files:
+                            file_path = os.path.join(directory, file)
+                            try:
+                                if os.path.isfile(file_path):
+                                    os.remove(file_path)
+                                    self.update_status(f"Removed: {file}")
+                                    files_removed += 1
+                                elif os.path.isdir(file_path):
+                                    shutil.rmtree(file_path)
+                                    self.update_status(f"Removed directory: {file}")
+                                    files_removed += 1
+                            except Exception as e:
+                                self.update_status(f"Error removing {file}: {str(e)}")
+                    except Exception as e:
+                        self.update_status(f"Error cleaning directory {directory}: {str(e)}")
+                else:
+                    self.update_status(f"Directory does not exist: {directory}")
+            
+            # Clean temp files
+            for temp_file in temp_files:
+                if os.path.exists(temp_file):
+                    try:
+                        os.remove(temp_file)
+                        self.update_status(f"Removed temp file: {temp_file}")
+                        files_removed += 1
+                    except Exception as e:
+                        self.update_status(f"Error removing temp file {temp_file}: {str(e)}")
+            
+            # Final report
+            if files_removed > 0:
+                self.update_status(f"\nCleaning complete. Removed {files_removed} files/directories.")
+                messagebox.showinfo("Clean Complete", f"Successfully removed {files_removed} files/directories.")
+            else:
+                self.update_status("\nNo files found to clean.")
+                messagebox.showinfo("Clean Complete", "No files found to clean.")
+            
+            # Reset status indicators
+            self.edge_status.config(text="Not started")
+            self.split_status.config(text="Not started")
+            self.flip_status.config(text="Not started")
+            
+            # Clear previews
+            self.clear_previews()
+            
+        except Exception as e:
+            error_msg = f"Error during cleaning: {str(e)}"
+            self.update_status(error_msg)
+            messagebox.showerror("Error", error_msg)
+
+    def clean_output_directory(self):
+        """Clean only the output directory"""
+        # Create detailed message of what will be cleaned
+        clean_message = "This will delete all files in:\n\n"
+        clean_message += "edgenet-360/Output/\n\n"
+        clean_message += "Are you sure you want to proceed?"
+        
+        # Show confirmation dialog
+        if not messagebox.askyesno("Confirm Clean Output", clean_message, icon='warning'):
+            self.update_status("Clean output operation cancelled by user")
+            return
+            
+        try:
+            files_removed = 0
+            
+            if os.path.exists(self.output_dir):
+                try:
+                    # List all files before deleting for reporting
+                    files = os.listdir(self.output_dir)
+                    
+                    if not files:
+                        self.update_status("\nOutput directory is already empty.")
+                        messagebox.showinfo("Clean Output", "Output directory is already empty.")
+                        return
+                    
+                    self.update_status(f"\nCleaning output directory: {self.output_dir}")
+                    
+                    for file in files:
+                        file_path = os.path.join(self.output_dir, file)
+                        try:
+                            if os.path.isfile(file_path):
+                                os.remove(file_path)
+                                self.update_status(f"Removed: {file}")
+                                files_removed += 1
+                            elif os.path.isdir(file_path):
+                                shutil.rmtree(file_path)
+                                self.update_status(f"Removed directory: {file}")
+                                files_removed += 1
+                        except Exception as e:
+                            self.update_status(f"Error removing {file}: {str(e)}")
+                            
+                    # Final report
+                    self.update_status(f"\nOutput directory cleaning complete. Removed {files_removed} files/directories.")
+                    messagebox.showinfo("Clean Output Complete", 
+                                      f"Successfully removed {files_removed} files/directories from output directory.")
+                    
+                    # Reset relevant status indicators
+                    self.split_status.config(text="Not started")  # Reset status for operations that create output files
+                    self.flip_status.config(text="Not started")
+                    
+                    # Clear mesh preview if it exists
+                    self.mesh_preview.configure(image='')
+                    
+                except Exception as e:
+                    error_msg = f"Error cleaning output directory: {str(e)}"
+                    self.update_status(error_msg)
+                    messagebox.showerror("Error", error_msg)
+            else:
+                self.update_status("\nOutput directory does not exist.")
+                messagebox.showinfo("Clean Output", "Output directory does not exist.")
+            
+        except Exception as e:
+            error_msg = f"Error during output cleaning: {str(e)}"
+            self.update_status(error_msg)
+            messagebox.showerror("Error", error_msg)
+
+    def clear_previews(self):
+        """Clear all preview images"""
+        try:
+            # Clear depth preview
+            self.depth_preview.configure(image='')
+            
+            # Clear mesh preview
+            self.mesh_preview.configure(image='')
+            
+            # Force update
+            self.tab.update_idletasks()
+            
+        except Exception as e:
+            self.update_status(f"Error clearing previews: {str(e)}")
\ No newline at end of file
-- 
GitLab