Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # So, I have a grid of "buttons", stored in a list, in a json. ( config["front"]["buttons"] = [{},{},{},..] )
- # It's not a matrix, but you can convert it with this code:
- def create_matrix(config):
- matrix = []
- for folder_name, folder_content in config["front"]["buttons"].items():
- row_count = 0
- for count, button in enumerate(folder_content, start=1):
- if row_count >= len(matrix):
- matrix.append([])
- matrix[row_count].append(button)
- calc_height = int(config['front']['width'])
- if count % calc_height == 0:
- row_count += 1
- break
- return len(matrix), len(matrix[0])
- # With this function you can also get the old height and width:
- oldheight, oldwidth = create_matrix(config)
- # It will be necessary to put it back to normal at the end of the script with this code:
- def unmatrix(matrix):
- new_folder_content = []
- for row in matrix:
- for button in row:
- new_folder_content.append(button)
- return new_folder_content
- # The matrix can contain voids ( {"VOID": "VOID"} ), and your objective is to create an `update_gridsize(config)` function to
- # modify the size of the grid. If the size increases just add a line, which I have already coded:
- if oldheight != int(config['front']['height']):
- if oldheight < int(config['front']['height']):
- difference = int(config['front']['height']) - oldheight
- for _ in range(difference):
- for folder_name, folder_content in config["front"]["buttons"].items():
- for _ in range(int(config['front']['width'])):
- folder_content.append({"VOID": "VOID"})
- if oldwidth != int(config['front']['width']):
- if oldwidth < int(config['front']['width']):
- difference = int(config['front']['width']) - oldwidth
- for folder_name, folder_content in config["front"]["buttons"].items():
- for diff_count, _ in enumerate(range(difference)):
- additional_count = 0
- for count, button in enumerate(folder_content, start=1):
- calc_width = oldwidth + diff_count
- if count % calc_width == 0:
- print(f'{count} | {additional_count}')
- folder_content.insert(count+additional_count, {"VOID": "VOID"})
- additional_count += 1
- # If the size decreases, all the buttons must absolutely remain, and must be moved if necessary, in order to be able to have the
- # reduced size of a line in the end (if this is what is required). If the requested height is really small, you have to do everything
- # to enter all the buttons in the respected size. If that's really impossible even removing all the void,
- # just print("can't resize the grid")
- # Also, grid size are stored in config['front']['height'] and config['front']['width']
- # Please let me know if you have a question or if I was unclear in my request
- # Here's my entiere code that I tried, but it got a lot of bugs (cause I'm bad at coding):
- def update_gridsize(config):
- # create matrix to calculate the height
- def create_matrix(config):
- matrix = []
- for folder_name, folder_content in config["front"]["buttons"].items():
- row_count = 0
- for count, button in enumerate(folder_content, start=1):
- if row_count >= len(matrix):
- matrix.append([])
- matrix[row_count].append(button)
- calc_height = int(config['front']['width'])
- if count % calc_height == 0:
- row_count += 1
- break
- return len(matrix), len(matrix[0])
- oldheight, oldwidth = create_matrix(config)
- if oldheight != int(config['front']['height']):
- if oldheight < int(config['front']['height']):
- difference = int(config['front']['height']) - oldheight
- for _ in range(difference):
- for folder_name, folder_content in config["front"]["buttons"].items():
- for _ in range(int(config['front']['width'])):
- folder_content.append({"VOID": "VOID"})
- else:
- print('height decreased')
- difference = oldheight - int(config['front']['height'])
- print(f'difference: {difference}')
- print(f'oldheight: {oldheight}')
- print(f"newheight: {config['front']['height']}")
- def remove_height():
- remove_success = True
- reversed_matrix = []
- if remove_success:
- for folder_name, folder_content in config["front"]["buttons"].items():
- before_config = config["front"]["buttons"].copy() # Copier la configuration actuelle
- def unmatrix(matrix):
- new_folder_content = []
- for row in matrix:
- for button in row:
- new_folder_content.append(button)
- return new_folder_content
- matrix = []
- row_count = 0
- for count, button in enumerate(folder_content, start=1):
- if row_count >= len(matrix):
- matrix.append([])
- matrix[row_count].append(button)
- calc_height = oldwidth + diff_count
- if count % calc_height == 0:
- row_count += 1
- print(matrix)
- new_height = len(matrix)
- success = False
- for row in reversed(matrix):
- if all(element == {"VOID": "VOID"} for element in row):
- matrix.remove(row)
- success = True
- break
- if success:
- folder_content = unmatrix(matrix)
- else:
- reversed_matrix = []
- for col in range(len(matrix[0])):
- reversed_matrix.append([])
- for row in range(len(matrix)):
- try:
- reversed_matrix[col].append(matrix[row][col])
- except:
- pass
- print(reversed_matrix)
- success = True
- for col in reversed_matrix:
- if success:
- try:
- empty_index = len(col) - col[::-1].index({"VOID": "VOID"}) - 1
- for i in range(empty_index, len(col) - 1):
- col[i] = col[i + 1]
- except ValueError:
- success = False
- print("Impossible de replacer le bouton.")
- if success:
- for col in reversed_matrix:
- col.pop()
- result_matrix = []
- for col in range(len(reversed_matrix[0])):
- result_matrix.append([])
- for row in range(len(reversed_matrix)):
- try:
- result_matrix[col].append(reversed_matrix[row][col])
- except:
- pass
- matrix = result_matrix
- del result_matrix
- folder_content = unmatrix(matrix)
- else:
- remove_success = False
- config["front"]["buttons"] = before_config # Restaurer la configuration précédente
- if remove_success:
- config["front"]["buttons"][folder_name] = folder_content
- else:
- config["front"]["buttons"][folder_name] = before_config[folder_name]
- del matrix
- try:
- del reversed_matrix
- except:
- pass
- for diff_count, _ in enumerate(range(difference)):
- remove_height()
- # after_height, after_width = create_matrix(config)
- # while after_height > int(config['front']['height']):
- # print(f'After height: {after_height}')
- # print(f"Before height: {config['front']['height']}")
- # remove_height()
- # after_height, after_width = create_matrix(config)
- # creates a infinite loop sometimes for some reason
- if oldwidth != int(config['front']['width']):
- if oldwidth < int(config['front']['width']):
- difference = int(config['front']['width']) - oldwidth
- for folder_name, folder_content in config["front"]["buttons"].items():
- for diff_count, _ in enumerate(range(difference)):
- additional_count = 0
- for count, button in enumerate(folder_content, start=1):
- calc_width = oldwidth + diff_count
- if count % calc_width == 0:
- print(f'{count} | {additional_count}')
- folder_content.insert(count+additional_count, {"VOID": "VOID"})
- additional_count += 1
- else:
- print('width decreased')
- # recreate matrix to calculate the height
- new_height, new_width = create_matrix(config)
- config["front"]["height"] = str(new_height)
- config["front"]["width"] = str(new_width)
- with open('config.json', 'w', encoding="utf-8")as json_file:
- json.dump(config, json_file, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement