MainWindow.py 8.37 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
9
10
11
12
13
14
15
from tko.AppOgl import AppOgl
from tk.OutilsFrame import OutilsFrame
from tk.ObjetsFrame import ObjetsFrame


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
19
20
21
22
23
24
25
26
    _frame=None

    _glFrame=None
    _outFrame=None
    _objFrame=None

    _glApp=None
    _menu=None

    """
Francois Mares's avatar
Francois Mares committed
27
        INITIALISATIONS
Francois Mares's avatar
add GUI  
Francois Mares committed
28
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):
        # main frame
Francois Mares's avatar
Francois Mares committed
50
51
52
53
54
        #test
        #b=Button(self._root, text="test", command=lambda:self._controller.test(self._root))
        #b.pack(fill=X)

        # Frame
Francois Mares's avatar
add GUI  
Francois Mares committed
55
56
        self._frame = Frame(self._root)
        self._frame.pack(expand=YES, fill=BOTH)
Francois Mares's avatar
Francois Mares committed
57
        self._frame.columnconfigure(0, weight=3, minsize=500)
Francois Mares's avatar
add GUI  
Francois Mares committed
58
        self._frame.columnconfigure(1, weight=1, minsize=200)
Francois Mares's avatar
Francois Mares committed
59
        self._frame.columnconfigure(2, weight=2, minsize=200)
Francois Mares's avatar
add GUI  
Francois Mares committed
60
61
62
63
64
65
66
67
        self._frame.rowconfigure(0, weight=1, minsize=300)

        # OGL frame
        self._glFrame=Frame(self._frame)
        self._glFrame.grid(row=0, column=0, sticky="nesw")
        self.initGLApp()

        # Outils frame
Francois Mares's avatar
Francois Mares committed
68
        self._outFrame = OutilsFrame(self._frame, self._controller)
Francois Mares's avatar
add GUI  
Francois Mares committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
        self._outFrame.grid(row=0, column=1, sticky="nesw")

        # Objets frame
        self._objFrame = ObjetsFrame(self._frame, self._controller)
        self._objFrame.grid(row=0, column=2, sticky="nesw")


    def initGLApp(self):
        self._glApp=AppOgl(self._glFrame, width=320, height=200)
        self._glApp.pack(expand = YES, fill=BOTH)

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

Francois Mares's avatar
Francois Mares committed
83
        # >Menu
Francois Mares's avatar
add GUI  
Francois Mares committed
84
        fichierMenu=Menu(self._menu, tearoff=0)
Francois Mares's avatar
Francois Mares committed
85
86
        # >> Importer
        importerMenu=Menu(fichierMenu, tearoff=0)
Francois Mares's avatar
Francois Mares committed
87
        importerMenu.add_cascade(label="BYTES", command=self.importBYTES)
Francois Mares's avatar
Francois Mares committed
88
89
90
        fichierMenu.add_cascade(label="Importer", menu=importerMenu)
        # >> Exporter
        exporterMenu=Menu(fichierMenu, tearoff=0)
Francois Mares's avatar
Francois Mares committed
91
        exporterMenu.add_cascade(label="BYTES", command=self.exportBYTES)
Francois Mares's avatar
Francois Mares committed
92
93
94
        exporterMenu.add_cascade(label="Graphviz", command=self.exporterGraphviz)
        fichierMenu.add_cascade(label="Exporter", menu=exporterMenu)
        # ---------
Francois Mares's avatar
add GUI  
Francois Mares committed
95
        fichierMenu.add_separator()
Francois Mares's avatar
Francois Mares committed
96
97
        # ) Quitter
        fichierMenu.add_cascade(label="Quitter", command=self._root.destroy)
Francois Mares's avatar
add GUI  
Francois Mares committed
98
99
100
101
102
        self._menu.add_cascade(label="Fichier", menu=fichierMenu)

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

    """
Francois Mares's avatar
Francois Mares committed
103
        IMPORTS/EXPORTS
Francois Mares's avatar
add GUI  
Francois Mares committed
104
    """
Francois Mares's avatar
Francois Mares committed
105
    def importBYTES(self):
Francois Mares's avatar
Francois Mares committed
106
        filepaths = filedialog.askopenfilenames(title="Importer un objet",
Francois Mares's avatar
Francois Mares committed
107
108
                                                filetypes=[('BYTES file','.obj')],
                                                initialdir=getcwd()+"/BYTES")
Francois Mares's avatar
Francois Mares committed
109
        for filepath in filepaths:
Francois Mares's avatar
Francois Mares committed
110
            self._controller.getModel().addObject(self._controller.importBYTES(filepath))
Francois Mares's avatar
add GUI  
Francois Mares committed
111

Francois Mares's avatar
Francois Mares committed
112
    def exportBYTES(self):
Francois Mares's avatar
Francois Mares committed
113
114
115
116
        uids=self._objFrame.getSelectedObjects()
        data=self._controller.getModel().getObject(uids)

        if data.shape[0]==0:
Francois Mares's avatar
Francois Mares committed
117
            messagebox.showinfo(title="BYTES Export", message="Aucun objet séléctionné.")
Francois Mares's avatar
Francois Mares committed
118
119
            return
        elif data.shape[0]==1:
Francois Mares's avatar
Francois Mares committed
120
            row=data.loc[int(uids[0])]
Francois Mares's avatar
Francois Mares committed
121
122
            filepath = filedialog.asksaveasfilename(title="Exporter un objet",
                                                    initialfile=str(row["class"])+str(data.index[0]),
Francois Mares's avatar
Francois Mares committed
123
124
125
126
                                                    filetypes=[('BYTES file','.obj')],
                                                    initialdir=getcwd()+"/BYTES")+".obj"
            if filepath:
                self._controller.exportBYTES(filepath, row["object"])
Francois Mares's avatar
Francois Mares committed
127
        else:
Francois Mares's avatar
Francois Mares committed
128
129
130
131
132
            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
133
            self._controller.exportBYTES(filepaths[i], row["object"])
Francois Mares's avatar
Francois Mares committed
134
            i+=1
Francois Mares's avatar
Francois Mares committed
135
136

    def exporterGraphviz(self):
Francois Mares's avatar
Francois Mares committed
137
138
139
140
141
142
143
        uids=self._objFrame.getSelectedObjects()
        data=self._controller.getModel().getObject(uids)

        for index, row in data.iterrows():
            obj=row["object"]
            if callable(getattr(obj,"transformer_graphviz",None)):
                obj.transformer_graphviz()
Francois Mares's avatar
Francois Mares committed
144
145
146


class ExportAsDialog(Toplevel):
Francois Mares's avatar
Francois Mares committed
147
148
    _registered=None

Francois Mares's avatar
Francois Mares committed
149
150
151
152
    _frame=None
    _dir=None
    _data=None

Francois Mares's avatar
Francois Mares committed
153
154
155
    _selectFileNameFrames=None

    def __init__(self, register, data, master=None, **kwargs):
Francois Mares's avatar
Francois Mares committed
156
        super().__init__(master, **kwargs)
Francois Mares's avatar
Francois Mares committed
157
158
        self._registered=register
        self.title("Paramètres d'export")
Francois Mares's avatar
Francois Mares committed
159
160
161
162
163
164
165
166
167
168
169
170
        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
171
172
173
        self._initDirectoryFrame()
        self._initObjectsFrame()
        self._initDialogFrame()
Francois Mares's avatar
Francois Mares committed
174

Francois Mares's avatar
Francois Mares committed
175
176
    def _initDirectoryFrame(self):
        frame=Frame(self._frame)
Francois Mares's avatar
Francois Mares committed
177
178
179
180
        frame.grid(row=0, column=0, sticky="new")

        frame.columnconfigure(1, weight=1)

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

Francois Mares's avatar
Francois Mares committed
183
184
185
        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
186

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

Francois Mares's avatar
Francois Mares committed
189
190
    def _initObjectsFrame(self):
        frame=Frame(self._frame)
Francois Mares's avatar
Francois Mares committed
191
        frame.grid(row=1, column=0, sticky="nsew")
Francois Mares's avatar
add GUI  
Francois Mares committed
192

Francois Mares's avatar
Francois Mares committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
        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
215
216
217
218
219
    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
220

Francois Mares's avatar
Francois Mares committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
    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
241

Francois Mares's avatar
Francois Mares committed
242
243
    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
244

Francois Mares's avatar
Francois Mares committed
245
246
247
248
249
250
251
252
253
254
        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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280