|
| 1 | +from PIL import Image |
| 2 | +from PyQt5.QtWidgets import QApplication, QFileDialog |
| 3 | +from filters import * |
| 4 | +from movements import * |
| 5 | + |
1 | 6 | class MenuManager: |
2 | | - def __init__(self, tree): |
3 | | - self.tree = tree |
| 7 | + def __init__(self): |
| 8 | + self.img = None |
| 9 | + self.function_dict = None |
| 10 | + self.tree = None |
| 11 | + self.loadImg() |
4 | 12 |
|
5 | | - def displayMenu(self, tree=None): |
6 | | - dict = self.tree if tree == None else tree |
| 13 | + def displayMenu(self, depth=0, tree=None, menuName="Menu"): |
| 14 | + dict = self.tree if tree is None else tree |
| 15 | + print("\n"+"==="*(3*depth)+"========"+menuName+"========"+"==="*(3*depth)) |
7 | 16 | for index in dict: |
8 | 17 | branch = dict[index] |
9 | | - print(f"{branch['name']} ({list(dict.keys())[list(dict.values()).index(branch)]})") |
10 | | - listener = input("Your choice : ") |
| 18 | + print("\t" * depth + f"-{branch['name']} ({list(dict.keys())[list(dict.values()).index(branch)]})") |
| 19 | + listener = input("\nYour choice: ") |
11 | 20 | if listener in dict.keys(): |
12 | | - if dict[listener]["type"] == "function" : |
| 21 | + if dict[listener]["type"] == "function": |
| 22 | + dict[listener]["function"](self.img) |
| 23 | + self.displayMenu() |
| 24 | + elif dict[listener]["type"] == "speFunction": |
13 | 25 | dict[listener]["function"]() |
14 | 26 | self.displayMenu() |
15 | | - elif dict[listener]["type"] == "folder" : |
16 | | - self.displayMenu(dict[listener]["children"]) |
17 | | - |
18 | | - else : |
19 | | - quit = input("Do you want to quit ? (Y/n) : ") |
20 | | - quits = ["y", "yes", "ouais", "q", "quit", "quitter", "exit", "bye", "oui", "si"] |
| 27 | + elif dict[listener]["type"] == "folder": |
| 28 | + self.displayMenu(depth + 1, dict[listener]["children"], dict[listener]["name"]) |
| 29 | + else: |
| 30 | + quit = input("Do you want to quit? (Y/n): ") |
| 31 | + quits = ["y", "yes", "ouais", "q", "quit", "quitter", "exit", "bye", "o", "oui", "si", "return"] |
21 | 32 | if quit.lower() not in quits: |
22 | 33 | self.displayMenu() |
| 34 | + |
| 35 | + def loadImg(self): |
| 36 | + _ = QApplication([]) |
| 37 | + filePath, _ = QFileDialog.getOpenFileName() |
| 38 | + extension = filePath[-4:] |
| 39 | + if extension == ".png": |
| 40 | + self.img = Image.open(filePath) |
| 41 | + self.loadTree() |
| 42 | + self.displayMenu() |
| 43 | + elif filePath != "": |
| 44 | + self.tree = {"1": {"type": "speFunction", "function": self.loadImg, "name": "Load image (png)"}} |
| 45 | + self.displayMenu() |
| 46 | + |
| 47 | + def loadTree(self): |
| 48 | + self.tree = { |
| 49 | + "1": {"type": "folder", "name": "Filters", "children": { |
| 50 | + "1": {"type": "function", "function": filterNegative, "name": "Negative filter"}, |
| 51 | + "2": {"type": "function", "function": filterBlackAndWhite, "name": "Black and White filter"}, |
| 52 | + "3": {"type": "function", "function": filterWithHexadecimal, "name": "Custom filter"}, |
| 53 | + "4": {"type": "speFunction", "function": self.displayMenu, "name": "Retour au menu principal"}, |
| 54 | + }}, |
| 55 | + "2": {"type": "folder", "name": "Movements", "children": { |
| 56 | + "1": {"type": "function", "function": verticalMirror, "name": "Vertical mirror"}, |
| 57 | + "2": {"type": "function", "function": horizontalMirror, "name": "Horizontal mirror"}, |
| 58 | + "3": {"type": "function", "function": rotate, "name": "Rotate"}, |
| 59 | + "4": {"type": "speFunction", "function": self.displayMenu, "name": "Retour au menu principal"}, |
| 60 | + }}, |
| 61 | + "3": {"type": "speFunction", "function": self.loadImg, "name": "Load another image (png)"} |
| 62 | + } |
0 commit comments