Untitled

 avatar
unknown
plain_text
4 months ago
7.1 kB
2
Indexable
    def get_current_config(self):
        """Get current bin configuration"""
        config = []
        for bin_id in self.bin_frames.keys():
            config.append({
                'id': bin_id,
                'name': self.name_entries[bin_id].get().strip(),
                'color': self.color_buttons[bin_id].cget('fg_color'),
                'color_dark': self.color_dark_buttons[bin_id].cget('fg_color'),
                'is_default': False
            })
        return config
    
    def save_custom_config(self, name):
        """Save current configuration to a JSON file"""
        config_dir = 'custom_configs'
        if not os.path.exists(config_dir):
            os.makedirs(config_dir)
        
        config = {
            'name': name,
            'bins': self.get_current_config(),
            'created_at': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }
        
        filename = f"{name.lower().replace(' ', '_')}.json"
        filepath = os.path.join(config_dir, filename)
        
        try:
            with open(filepath, 'w', encoding='utf-8') as f:
                json.dump(config, f, indent=2, ensure_ascii=False)
            return True
        except Exception as e:
            print(f"Error saving configuration: {e}")
            return False
    
    def load_custom_config(self, filename):
        """Load a custom configuration from a JSON file"""
        try:
            with open(filename, 'r', encoding='utf-8') as f:
                config = json.load(f)
                
            # Update each bin with loaded configuration
            for bin_config in config['bins']:
                bin_id = bin_config['id']
                if bin_id in self.name_entries:
                    # Update name
                    self.name_entries[bin_id].delete(0, 'end')
                    self.name_entries[bin_id].insert(0, bin_config['name'])
                    
                    # Update colors
                    self.color_buttons[bin_id].configure(
                        fg_color=bin_config['color'],
                        hover_color=bin_config['color']
                    )
                    self.color_dark_buttons[bin_id].configure(
                        fg_color=bin_config['color_dark'],
                        hover_color=bin_config['color_dark']
                    )
            return True
        except Exception as e:
            print(f"Error loading configuration: {e}")
            return False
    
    def get_saved_configs(self):
        """Get list of saved configurations"""
        config_dir = 'custom_configs'
        if not os.path.exists(config_dir):
            return []
            
        configs = []
        for filename in os.listdir(config_dir):
            if filename.endswith('.json'):
                try:
                    with open(os.path.join(config_dir, filename), 'r', encoding='utf-8') as f:
                        config = json.load(f)
                        configs.append({
                            'filename': os.path.join(config_dir, filename),
                            'name': config['name'],
                            'created_at': config.get('created_at', 'Unknown')
                        })
                except Exception as e:
                    print(f"Error reading configuration file {filename}: {e}")
        
        return sorted(configs, key=lambda x: x['created_at'], reverse=True)
    
    def show_save_dialog(self):
        """Show dialog to save current configuration"""
        def save_callback(name):
            if self.save_custom_config(name):
                print(f"Configuration '{name}' saved successfully")
            else:
                print("Error saving configuration")
                
        SaveConfigDialog(self, save_callback, self.dark_mode)
    
    def show_load_dialog(self):
        """Show dialog to load a saved configuration"""
        configs = self.get_saved_configs()
        if not configs:
            print("No saved configurations found")
            return
            
        def load_callback(filename):
            if self.load_custom_config(filename):
                print("Configuration loaded successfully")
            else:
                print("Error loading configuration")
                
        LoadConfigDialog(self, configs, load_callback, self.dark_mode)
    
    def setup_config_management(self):
        """Set up the configuration management buttons"""
        # Create buttons container with two rows
        self.config_buttons = ctk.CTkFrame(
            self.container,
            fg_color="transparent"
        )
        self.config_buttons.pack(pady=(0, 20))
    
        # Top row for save/load configuration presets
        self.top_row = ctk.CTkFrame(
            self.config_buttons,
            fg_color="transparent"
        )
        self.top_row.pack(pady=(0, 10))
    
        # Load configuration button
        self.load_button = ctk.CTkButton(
            self.top_row,
            text="Load Configuration",
            command=self.show_load_dialog,
            font=("Dongle", 20),
            fg_color=self.button_color,
            hover_color=self.button_hover,
            text_color=self.text_color,
            height=40,
            width=150
        )
        self.load_button.pack(side='left', padx=10)
    
        # Save configuration button
        self.save_button = ctk.CTkButton(
            self.top_row,
            text="Save Configuration",
            command=self.show_save_dialog,
            font=("Dongle", 20),
            fg_color=self.button_color,
            hover_color=self.button_hover,
            text_color=self.text_color,
            height=40,
            width=150
        )
        self.save_button.pack(side='left', padx=10)
    
        # Bottom row for action buttons
        self.bottom_row = ctk.CTkFrame(
            self.config_buttons,
            fg_color="transparent"
        )
        self.bottom_row.pack()
    
        # Save changes button
        self.save_changes_button = ctk.CTkButton(
            self.bottom_row,
            text="Save Changes",
            command=self.save_changes,
            font=("Dongle", 20),
            fg_color='#34c759',  # Green color
            hover_color='#30d158',
            text_color='white',
            height=40,
            width=150
        )
        self.save_changes_button.pack(side='left', padx=10)
    
        # Restore defaults button
        self.restore_button = ctk.CTkButton(
            self.bottom_row,
            text="Restore All Defaults",
            command=self.restore_defaults,
            font=("Dongle", 20),
            fg_color='#ff3b30',  # Red color
            hover_color='#ff453a',
            text_color='white',
            height=40,
            width=150
        )
        self.restore_button.pack(side='left', padx=10)
Editor is loading...
Leave a Comment