MainWindow.py 8.15 KB
Newer Older
Francois Mares's avatar
add GUI  
Francois Mares committed
1
from os import getcwd
Francois Mares's avatar
Francois Mares committed
2
from tkinter import TOP, YES, BOTH, X, \
Francois Mares's avatar
Francois Mares committed
3
4
                    Tk, Frame, Label, Menu, Toplevel, Entry, Button, \
                    filedialog, messagebox
Francois Mares's avatar
add GUI  
Francois Mares committed
5

Francois Mares's avatar
Francois Mares committed
6
from Parameters import parameters
Francois Mares's avatar
add GUI  
Francois Mares committed
7
8
from tk.OutilsFrame import OutilsFrame
from tk.ObjetsFrame import ObjetsFrame
Francois Mares's avatar
Francois Mares committed
9
from plot.PlotFrame import PlotFrame
Francois Mares's avatar
add GUI  
Francois Mares committed
10
11
12
13
14
15


class MainWindow:
    _controller:None

    _root=None
Francois Mares's avatar
Francois Mares committed
16
    def getRoot(self): return self._root
Francois Mares's avatar
add GUI  
Francois Mares committed
17
18
    _frame=None

Francois Mares's avatar
Francois Mares committed
19
    _plotFrame=None
Francois Mares's avatar
add GUI  
Francois Mares committed
20
21
    _outFrame=None
    _objFrame=None
Francois Mares's avatar
Francois Mares committed
22
    def getPlotFrame(self): return self._plotFrame
Francois Mares's avatar
add GUI  
Francois Mares committed
23
24
25
26
27

    _glApp=None
    _menu=None

    """
Francois Mares's avatar
Francois Mares committed
28
        INITIALISATIONS
Francois Mares's avatar
add GUI  
Francois Mares committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
    """
    def __init__(self, controller, *args, **kwargs):
        assert controller
        self._controller=controller

        self._initRoot()
        self._initFrames()
        self._initMenu()

        self._root.mainloop()

    def _initRoot(self):
        self._root= Tk()
        w=self._root.winfo_screenwidth()
        h=self._root.winfo_screenheight()
        self._root.geometry("%dx%d%+d%+d" % (w/2,h-80,0,0))
        self._root.title('Théorie des catégories ')
        self._root.iconbitmap("ressources\graphe.ico")
        self._root.config(bg = "#787878")

    def _initFrames(self):
Francois Mares's avatar
Francois Mares committed
50
        # Frame
Francois Mares's avatar
add GUI  
Francois Mares committed
51
52
        self._frame = Frame(self._root)
        self._frame.pack(expand=YES, fill=BOTH)
Francois Mares's avatar
Francois Mares committed
53
        self._frame.columnconfigure(0, weight=3, minsize=500)
Francois Mares's avatar
add GUI  
Francois Mares committed
54
        self._frame.columnconfigure(1, weight=1, minsize=200)
Francois Mares's avatar
Francois Mares committed
55
        self._frame.columnconfigure(2, weight=2, minsize=200)
Francois Mares's avatar
add GUI  
Francois Mares committed
56
57
        self._frame.rowconfigure(0, weight=1, minsize=300)

Francois Mares's avatar
Francois Mares committed
58
59
60
        # Plot frame
        self._plotFrame=PlotFrame(self._frame, self._controller)
        self._plotFrame.grid(row=0, column=0, sticky="nesw")
Francois Mares's avatar
add GUI  
Francois Mares committed
61
62

        # Outils frame
Francois Mares's avatar
Francois Mares committed
63
        self._outFrame = OutilsFrame(self._frame, self._controller)
Francois Mares's avatar
add GUI  
Francois Mares committed
64
65
66
        self._outFrame.grid(row=0, column=1, sticky="nesw")

        # Objets frame
Francois Mares's avatar
Francois Mares committed
67
        self._objFrame = ObjetsFrame(self._frame, self._controller, self)
Francois Mares's avatar
add GUI  
Francois Mares committed
68
69
70
71
72
73
        self._objFrame.grid(row=0, column=2, sticky="nesw")


    def _initMenu(self):
        self._menu=Menu()

Francois Mares's avatar
Francois Mares committed
74
        # >Menu
Francois Mares's avatar
add GUI  
Francois Mares committed
75
        fichierMenu=Menu(self._menu, tearoff=0)
Francois Mares's avatar
Francois Mares committed
76
77
        # >> Importer
        importerMenu=Menu(fichierMenu, tearoff=0)
Francois Mares's avatar
Francois Mares committed
78
        importerMenu.add_cascade(label="BYTES", command=self.importBYTES)
Francois Mares's avatar
Francois Mares committed
79
80
81
        fichierMenu.add_cascade(label="Importer", menu=importerMenu)
        # >> Exporter
        exporterMenu=Menu(fichierMenu, tearoff=0)
Francois Mares's avatar
Francois Mares committed
82
        exporterMenu.add_cascade(label="BYTES", command=self.exportBYTES)
Francois Mares's avatar
Francois Mares committed
83
84
85
        exporterMenu.add_cascade(label="Graphviz", command=self.exporterGraphviz)
        fichierMenu.add_cascade(label="Exporter", menu=exporterMenu)
        # ---------
Francois Mares's avatar
add GUI  
Francois Mares committed
86
        fichierMenu.add_separator()
Francois Mares's avatar
Francois Mares committed
87
88
        # ) Quitter
        fichierMenu.add_cascade(label="Quitter", command=self._root.destroy)
Francois Mares's avatar
add GUI  
Francois Mares committed
89
90
91
92
93
        self._menu.add_cascade(label="Fichier", menu=fichierMenu)

        self._root.config(menu=self._menu)

    """
Francois Mares's avatar
Francois Mares committed
94
        IMPORTS/EXPORTS
Francois Mares's avatar
add GUI  
Francois Mares committed
95
    """
Francois Mares's avatar
Francois Mares committed
96
    def importBYTES(self):
Francois Mares's avatar
Francois Mares committed
97
        filepaths = filedialog.askopenfilenames(title="Importer un objet",
Francois Mares's avatar
Francois Mares committed
98
99
                                                filetypes=[('BYTES file','.obj')],
                                                initialdir=getcwd()+"/BYTES")
Francois Mares's avatar
Francois Mares committed
100
        for filepath in filepaths:
Francois Mares's avatar
Francois Mares committed
101
            self._controller.getModel().addObject(self._controller.importBYTES(filepath))
Francois Mares's avatar
add GUI  
Francois Mares committed
102

Francois Mares's avatar
Francois Mares committed
103
    def exportBYTES(self):
Francois Mares's avatar
Francois Mares committed
104
105
106
107
        uids=self._objFrame.getSelectedObjects()
        data=self._controller.getModel().getObject(uids)

        if data.shape[0]==0:
Francois Mares's avatar
Francois Mares committed
108
            messagebox.showinfo(title="BYTES Export", message="Aucun objet séléctionné.")
Francois Mares's avatar
Francois Mares committed
109
110
            return
        elif data.shape[0]==1:
Francois Mares's avatar
Francois Mares committed
111
            row=data.loc[int(uids[0])]
Francois Mares's avatar
Francois Mares committed
112
113
            filepath = filedialog.asksaveasfilename(title="Exporter un objet",
                                                    initialfile=str(row["class"])+str(data.index[0]),
Francois Mares's avatar
Francois Mares committed
114
115
116
117
                                                    filetypes=[('BYTES file','.obj')],
                                                    initialdir=getcwd()+"/BYTES")+".obj"
            if filepath:
                self._controller.exportBYTES(filepath, row["object"])
Francois Mares's avatar
Francois Mares committed
118
        else:
Francois Mares's avatar
Francois Mares committed
119
120
121
122
123
            ExportAsDialog(self, data=data)

    def multipleExportJSON(self, filepaths:list, data):
        i=0
        for index, row in data.iterrows():
Francois Mares's avatar
Francois Mares committed
124
            self._controller.exportBYTES(filepaths[i], row["object"])
Francois Mares's avatar
Francois Mares committed
125
            i+=1
Francois Mares's avatar
Francois Mares committed
126
127

    def exporterGraphviz(self):
Francois Mares's avatar
Francois Mares committed
128
        uids=self._objFrame.getSelectedObjects()
Francois Mares's avatar
Francois Mares committed
129
        self._controller.getModel().exporterGraphviz(uids)
Francois Mares's avatar
Francois Mares committed
130

Francois Mares's avatar
Francois Mares committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    def exportObjectsGraphviz(self, uids):
        print(f"ddz{uids}")
        self._controller.getModel().exporterGraphviz(uids)


















Francois Mares's avatar
Francois Mares committed
152

Francois Mares's avatar
Francois Mares committed
153
154
155
"""
    EXPORTS DIALOG CLASS
"""
Francois Mares's avatar
Francois Mares committed
156
class ExportAsDialog(Toplevel):
Francois Mares's avatar
Francois Mares committed
157
158
    _registered=None

Francois Mares's avatar
Francois Mares committed
159
160
161
162
    _frame=None
    _dir=None
    _data=None

Francois Mares's avatar
Francois Mares committed
163
164
165
    _selectFileNameFrames=None

    def __init__(self, register, data, master=None, **kwargs):
Francois Mares's avatar
Francois Mares committed
166
        super().__init__(master, **kwargs)
Francois Mares's avatar
Francois Mares committed
167
168
        self._registered=register
        self.title("Paramètres d'export")
Francois Mares's avatar
Francois Mares committed
169
170
171
172
173
174
175
176
177
178
179
180
        self.config(bg=parameters.ExportAsDialog_BG)
        self.grab_set()

        self._data=data
        self._initFrame()

    def _initFrame(self):
        self._frame=Frame(self)
        self._frame.pack(expand=True, fill=BOTH)
        self._frame.columnconfigure(0, weight=1, minsize=200)
        self._frame.rowconfigure(1, weight=1, minsize=20)

Francois Mares's avatar
Francois Mares committed
181
182
183
        self._initDirectoryFrame()
        self._initObjectsFrame()
        self._initDialogFrame()
Francois Mares's avatar
Francois Mares committed
184

Francois Mares's avatar
Francois Mares committed
185
186
    def _initDirectoryFrame(self):
        frame=Frame(self._frame)
Francois Mares's avatar
Francois Mares committed
187
188
189
190
        frame.grid(row=0, column=0, sticky="new")

        frame.columnconfigure(1, weight=1)

Francois Mares's avatar
Francois Mares committed
191
        Label(frame, text="Dossier: ").grid(row=0, column=0, sticky="nsew")
Francois Mares's avatar
add GUI  
Francois Mares committed
192

Francois Mares's avatar
Francois Mares committed
193
194
195
        self._dir=Entry(frame)
        self._dir.insert(0, getcwd()+"/JSON/")
        self._dir.grid(row=0, column=1, sticky="ew")
Francois Mares's avatar
add GUI  
Francois Mares committed
196

Francois Mares's avatar
Francois Mares committed
197
        Button(frame, text="Browse...", command=self.setDirectory).grid(row=0, column=2, sticky="nsew")
Francois Mares's avatar
add GUI  
Francois Mares committed
198

Francois Mares's avatar
Francois Mares committed
199
200
    def _initObjectsFrame(self):
        frame=Frame(self._frame)
Francois Mares's avatar
Francois Mares committed
201
        frame.grid(row=1, column=0, sticky="nsew")
Francois Mares's avatar
add GUI  
Francois Mares committed
202

Francois Mares's avatar
Francois Mares committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
        self._selectFileNameFrames=[]
        self._data.apply(lambda row: self._selectFileNameFrames.append(SelectFileNameFrame(frame, row)),axis=1)
        for f in self._selectFileNameFrames:
            f.pack(expand = YES, fill=X)

    def _initDialogFrame(self):
        frame=Frame(self._frame)
        frame.grid(row=2, column=0, sticky="sew")
        frame.columnconfigure(0, weight=1)

        b=Button(frame, text="Exporter",
                        highlightcolor=parameters.HIGHLIGHTCOLOR_BUTTON,
                        command=self.validate)
        b.grid(row=0, column=1, sticky="e")
        b.focus_set()
        b.bind('<Return>', self.validate)

        Button(frame, text="Annuler", command=self.destroy).grid(row=0, column=2, sticky="e")

    """
        BIND FUNCTIONS
    """
Francois Mares's avatar
Francois Mares committed
225
226
227
228
229
    def setDirectory(self):
        dir=filedialog.askdirectory(initialdir=getcwd()+"/JSON",
                                    title = "Select directory")
        self._dir.delete(0,"end")
        self._dir.insert(0, dir+"/")
Francois Mares's avatar
add GUI  
Francois Mares committed
230

Francois Mares's avatar
Francois Mares committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    def validate(self, event=None):
        basePath=self._dir.get()
        filePaths=[]
        for frame in self._selectFileNameFrames:
            filePaths.append(basePath+frame.getFileName())
        self._registered.multipleExportJSON(filePaths, self._data)
        self.destroy()



class SelectFileNameFrame(Frame):
    _row=None
    _nameEntry=None
    def __init__(self, parent, row, *args, **kwargs):
        Frame.__init__(self, parent, *args, **kwargs)
        self._row=row
        self.columnconfigure(0, weight=1, minsize=10)
        self.columnconfigure(2, weight=1, minsize=10)
        self.rowconfigure(1, weight=1, minsize=20)
        self._initFrame()
Francois Mares's avatar
add GUI  
Francois Mares committed
251

Francois Mares's avatar
Francois Mares committed
252
253
    def _initFrame(self):
        Label(self, text=str(self._row.name)+" <"+str(self._row["class"])+"> "+str(self._row["object"])).grid(row=0, column=0, sticky="nsew")
Francois Mares's avatar
add GUI  
Francois Mares committed
254

Francois Mares's avatar
Francois Mares committed
255
256
257
258
259
260
261
262
263
264
        self._nameEntry=Entry(self)
        self._nameEntry.insert(0, str(self._row["class"])+str(self._row.name))
        self._nameEntry.grid(row=0, column=1, sticky="ew")

        Label(self, text=".json").grid(row=0, column=2, sticky="nsew")
    """
        GETTERS
    """
    def getFileName(self):
        return self._nameEntry.get()+".json"
Francois Mares's avatar
add GUI  
Francois Mares committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290