mpenn/scripts/folder_mangement/OpenFolder.py

111 lines
4.3 KiB
Python

import customtkinter as Ctk
import os
from icons.icons import Icons
from ..SaveData import SaveData
import scripts.get_sys_info as system_code
FONT = "Berlin Sans FB"
class OpenFolder(Ctk.CTkScrollableFrame):
def __init__(self,master,output_path, callback, **kwargs, ):
super().__init__(master, **kwargs)
self.my_font = Ctk.CTkFont(family=FONT, size=16)
self.warning_font = Ctk.CTkFont(family=FONT, size=20)
# the variables needed to get from the upper class
system_code.load_json_file()
self.icons = Icons(path=system_code.data_path)
self.data_saver = SaveData()
self.button_image = Ctk.CTkImage(self.icons.get_image("Folder"), size=(63, 63))
self.output_path = output_path
self.callback = callback # Store the callback function
self.folders = self.get_all_folders()
self.button_size = (50, 50) # Size width and height
self.padding = 10 # Padding around each button
if len(self.folders) == 0:
self.folder_has_no_subs()
else:
self.make_buttons()
self.bind("<Configure>", self.on_configure, '+')
def enable_keybinding(self):
# this function has to be in all Functions but is only needed in
# some Classes
pass
def disable_keybinding(self):
# this function has to be in all Functions but is only needed in
# some Classes
pass
def on_configure(self, event=None):
self.rearrange_buttons()
def get_button_widths(self):
"""
Estimates button widths based on button text size.
"""
average_char_width = 8
width = max(len(button._text) * average_char_width for button in self.buttons)
return max(width, self.button_size[0])
def get_max_folders_per_row(self):
"""
Calculate the maximum number of folders that can fit in a row based on the container's width.
"""
container_width = self.winfo_width()
button_width = self.get_button_widths()
# Calculate the space required for one button including padding
total_button_width = button_width + 2 * self.padding
# Divide the available space in the container by the space required for one button
max_folders_per_row = max(1, container_width // total_button_width)
return max_folders_per_row
def get_all_folders(self):
folders = {f.name: f.path for f in os.scandir(self.output_path) if f.is_dir()}
return dict(sorted(folders.items()))
def rearrange_buttons(self):
"""
Rearrange buttons based on the current window size and calculated folders per row.
If the window size changes, it recalculates the arrangement.
"""
max_folders_per_row = self.get_max_folders_per_row()
for index, button in enumerate(self.buttons):
row = index // max_folders_per_row
col = index % max_folders_per_row
button.grid(row=row, column=col, padx=self.padding, pady=self.padding)
def make_buttons(self):
"""
Generates buttons for each folder provided.
"""
self.buttons = []
for key, value in self.folders.items():
folder_btn = Ctk.CTkButton(
self, text=key,
width=self.button_size[0], height=self.button_size[1],
image=self.button_image, # Corrected: Use self.button_image for the image parameter
compound="top", # Position text below the image
command=lambda val=value: self.new_folder_chosen(val),
font=self.my_font
)
folder_btn.grid_remove()
self.buttons.append(folder_btn)
self.rearrange_buttons() # Rearrange buttons after creation
def new_folder_chosen(self, folder_path):
"""
Callback for when a new folder is chosen.
"""
self.data_saver.search_in_folder(folder_path)
self.callback(folder_path) # Call the callback passing the current value
def folder_has_no_subs(self):
self.place_forget()
self.label = Ctk.CTkLabel(self, text="No Folders were found", width=100, font=self.warning_font)
self.label.grid(row = 1, column = 1, padx= 50, pady = 50)