Commit 6f1c2d03 authored by Francois Mares's avatar Francois Mares
Browse files

upgrade import JSON

done export JSON
parent c2e084e0
...@@ -21,13 +21,12 @@ class Controller: ...@@ -21,13 +21,12 @@ class Controller:
""" """
IMPORT/EXPORT (JSON) IMPORT/EXPORT (JSON)
""" """
def exportJSON(self, obj, file): def exportJSON(self, file:str, obj:object):
assert len(file)>5 and file[-4:]=="json" assert len(file)>5 and file[-4:]=="json"
with open(file, "w") as jsonFile: with open(file, "w") as jsonFile:
jsonFile.write(demjson.encode(obj)) jsonFile.write(demjson.encode(obj))
def importJSON(self, file): def importJSON(self, file:str):
assert len(file)>5 and file[-4:]=="json" assert len(file)>5 and file[-4:]=="json"
with open(file, "r") as jsonFile: with open(file, "r") as jsonFile:
......
["dezduef"]
\ No newline at end of file
...@@ -45,7 +45,11 @@ class Model: ...@@ -45,7 +45,11 @@ class Model:
return self._data return self._data
def getObject(self, uid): def getObject(self, uid):
return self._data.iloc(uid) """
@return DataFrame
"""
return self._data.iloc[uid]
""" """
INFORM INFORM
""" """
......
...@@ -10,5 +10,12 @@ class Parameters(): ...@@ -10,5 +10,12 @@ class Parameters():
SECTION_FONT_WEIGHT = 'bold' SECTION_FONT_WEIGHT = 'bold'
SECTION_FONT_UNDERLINE= 0 SECTION_FONT_UNDERLINE= 0
FRAME_BD=2
INFORM_BG='#DCFFFF'
INFORM_FG='black'
ExportAsDialog_BG='white'
parameters=Parameters() parameters=Parameters()
from tkinter import TOP, BOTTOM, LEFT, RIGHT, BOTH, \
Frame, Label, StringVar
from Parameters import parameters
class InformFrame(Frame):
_label=None
_text=None
def __init__(self, parent, *args, **kwargs):
Frame .__init__(self, parent,
*args, **kwargs)
self._initFrame()
def _initFrame(self):
self._text=StringVar()
self._label=Label(self,
bg=parameters.INFORM_BG,
fg=parameters.INFORM_FG,
anchor='nw',
textvariable=self._text)
self._label.pack(expand=True, fill=BOTH)
"""
SETTERS
"""
def setText(self, text:str):
self._text.set(text)
def inform(self, text:str):
self._text.set(self._text.get()+text+"\n")
from os import getcwd from os import getcwd
from tkinter import TOP, YES, BOTH, \ from tkinter import TOP, YES, BOTH, \
Tk, Frame, Label, Menu, \ Tk, Frame, Label, Menu, Toplevel, Entry, Button, \
filedialog filedialog, messagebox
from Parameters import parameters
from tko.AppOgl import AppOgl from tko.AppOgl import AppOgl
from tk.OutilsFrame import OutilsFrame from tk.OutilsFrame import OutilsFrame
from tk.ObjetsFrame import ObjetsFrame from tk.ObjetsFrame import ObjetsFrame
...@@ -73,33 +74,102 @@ class MainWindow: ...@@ -73,33 +74,102 @@ class MainWindow:
def _initMenu(self): def _initMenu(self):
self._menu=Menu() self._menu=Menu()
# >Menu
fichierMenu=Menu(self._menu, tearoff=0) fichierMenu=Menu(self._menu, tearoff=0)
fichierMenu.add_cascade(label="Importer", command=self.importer) # >> Importer
fichierMenu.add_cascade(label="Exporter", command=self.exporter) importerMenu=Menu(fichierMenu, tearoff=0)
importerMenu.add_cascade(label="JSON", command=self.importerJSON)
fichierMenu.add_cascade(label="Importer", menu=importerMenu)
# >> Exporter
exporterMenu=Menu(fichierMenu, tearoff=0)
exporterMenu.add_cascade(label="JSON", command=self.exporterJSON)
exporterMenu.add_cascade(label="Graphviz", command=self.exporterGraphviz)
fichierMenu.add_cascade(label="Exporter", menu=exporterMenu)
# ---------
fichierMenu.add_separator() fichierMenu.add_separator()
#fichierMenu.add_cascade(label="Quitter", command=self.test) # ) Quitter
fichierMenu.add_cascade(label="Quitter", command=self._root.destroy)
self._menu.add_cascade(label="Fichier", menu=fichierMenu) self._menu.add_cascade(label="Fichier", menu=fichierMenu)
self._menu.add_cascade(label="Paramètres")
self._root.config(menu=self._menu) self._root.config(menu=self._menu)
""" """
CONTROLLER CALLS CONTROLLER CALLS
""" """
def importer(self): def importerJSON(self):
filepath = filedialog.askopenfilename(title="Ouvrir une image", filepath = filedialog.askopenfilename(title="Importer un objet",
filetypes=[('JSON file','.json')], filetypes=[('JSON file','.json')],
initialdir=getcwd()) initialdir=getcwd()+"/JSON")
self._controller.getModel().addObject(self._controller.importJSON(filepath)) self._controller.getModel().addObject(self._controller.importJSON(filepath))
def exporter(self): def exporterJSON(self):
filepath = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*"))) uids=self._objFrame.getSelectedObjects()
print(filepath) data=self._controller.getModel().getObject(uids)
if data.shape[0]==0:
messagebox.showinfo(title="JSON Export", message="Aucun objet séléctionné.")
return
elif data.shape[0]==1:
row=data.loc[0]
filepath = filedialog.asksaveasfilename(title="Exporter un objet",
initialfile=str(row["class"])+str(data.index[0]),
filetypes=[('JSON file','.json')],
initialdir=getcwd()+"/JSON")+".json"
self._controller.exportJSON(filepath, row["object"])
else:
ExportAsDialog(data=data)
def exporterGraphviz(self):
#transformer_graphviz()
print("Graphviz")
class ExportAsDialog(Toplevel):
_frame=None
_dir=None
_data=None
def __init__(self, data, master=None, **kwargs):
super().__init__(master, **kwargs)
self.title("Paramètres d'export'")
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)
self._initDirectoryFrame(self._frame)
self._initObjectsFrame(self._frame)
def _initDirectoryFrame(self, parent):
frame=Frame(parent)
frame.grid(row=0, column=0, sticky="new")
frame.columnconfigure(1, weight=1)
Label(frame, text="Directory: ").grid(row=0, column=0, sticky="w")
self._dir=Entry(frame)
self._dir.insert(0, getcwd()+"/JSON/")
self._dir.grid(row=0, column=1, sticky="ew")
Button(frame, text="Browse...", command=self.setDirectory).grid(row=0, column=2, sticky="e")
def _initObjectsFrame(self, parent):
frame=Frame(parent)
frame.grid(row=1, column=0, sticky="nsew")
def setDirectory(self):
dir=filedialog.askdirectory(initialdir=getcwd()+"/JSON",
title = "Select directory")
self._dir.delete(0,"end")
self._dir.insert(0, dir+"/")
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
""" """
from pandas import DataFrame from pandas import DataFrame
from ModelCallable import ModelCallable from ModelCallable import ModelCallable
from tk.InformFrame import InformFrame
from tkinter import TOP, X, NO, W, Y, YES, BOTH, RIGHT, VERTICAL, \ from tkinter import TOP, X, NO, W, Y, YES, BOTH, RIGHT, VERTICAL, \
Frame, Label Frame, Label
...@@ -15,6 +16,7 @@ class ObjetsFrame(Frame, ModelCallable): ...@@ -15,6 +16,7 @@ class ObjetsFrame(Frame, ModelCallable):
_tree=None _tree=None
_scrollbarY=None _scrollbarY=None
_informFrame=None
_folders={} _folders={}
...@@ -26,12 +28,14 @@ class ObjetsFrame(Frame, ModelCallable): ...@@ -26,12 +28,14 @@ class ObjetsFrame(Frame, ModelCallable):
self.columnconfigure(0, weight=1, minsize=100) self.columnconfigure(0, weight=1, minsize=100)
self.columnconfigure(1, weight=1, minsize=16)#default value of scrollbar width self.columnconfigure(1, weight=1, minsize=16)#default value of scrollbar width
self.rowconfigure(0, weight=1, minsize=100) self.rowconfigure(0, weight=4, minsize=100)
self.rowconfigure(1, weight=1, minsize=10)
self._initFrame() self._initFrame()
def _initFrame(self): def _initFrame(self):
self._tree=Treeview(self, selectmode='browse') # TREE
self._tree=Treeview(self, selectmode='extended')
self._tree.grid(row=0, column=0, sticky="nesw") self._tree.grid(row=0, column=0, sticky="nesw")
style = Style() style = Style()
style.theme_use("default") style.theme_use("default")
...@@ -53,6 +57,11 @@ class ObjetsFrame(Frame, ModelCallable): ...@@ -53,6 +57,11 @@ class ObjetsFrame(Frame, ModelCallable):
self._tree.heading("description", text="Description",anchor=W) self._tree.heading("description", text="Description",anchor=W)
self._tree.bind("<<TreeviewSelect>>", self.objectSelection) self._tree.bind("<<TreeviewSelect>>", self.objectSelection)
self._tree.bind("<Delete>", self.objectSelection)
# INFORM
self._informFrame=InformFrame(self)
self._informFrame.grid(row=1, column=0, sticky="nesw")
""" """
GETTERS GETTERS
""" """
...@@ -65,6 +74,14 @@ class ObjetsFrame(Frame, ModelCallable): ...@@ -65,6 +74,14 @@ class ObjetsFrame(Frame, ModelCallable):
def getFolder(self, class_name:str): def getFolder(self, class_name:str):
return self.getIfExist(self._folders, class_name) return self.getIfExist(self._folders, class_name)
def getSelectedObjects(self):
curItems = self._tree.selection()
sel=[]
for i in curItems:
if self._tree.parent(i):
sel.append(i)
return sel
""" """
INFORM FROM MODEL INFORM FROM MODEL
""" """
...@@ -94,8 +111,8 @@ class ObjetsFrame(Frame, ModelCallable): ...@@ -94,8 +111,8 @@ class ObjetsFrame(Frame, ModelCallable):
f=self._tree.insert(parent="", index="end", text=folder) f=self._tree.insert(parent="", index="end", text=folder)
self._folders[folder]=f self._folders[folder]=f
objects=(df['object'][df['class']==folder]).sort_index(ascending=True) objects=(df['object'][df['class']==folder]).sort_index(ascending=True)
for index, obj in objects.items(): for uid, obj in objects.items():
self._tree.insert(parent=f, index="end", text=index, values=(folder, obj)) self._tree.insert(parent=f, iid=uid, index="end", text=uid, values=(folder, obj))
def addObject(self, uid, obj:object): def addObject(self, uid, obj:object):
class_name=type(obj).__name__ class_name=type(obj).__name__
...@@ -103,13 +120,27 @@ class ObjetsFrame(Frame, ModelCallable): ...@@ -103,13 +120,27 @@ class ObjetsFrame(Frame, ModelCallable):
if not f: if not f:
f=self._tree.insert(parent="", index="end", text=class_name) f=self._tree.insert(parent="", index="end", text=class_name)
self._folders[class_name]=f self._folders[class_name]=f
self._tree.insert(parent=f, index="end", text=uid, values=(class_name, obj)) self._tree.insert(parent=f, iid=uid, index="end", text=uid, values=(class_name, obj))
def deleteObject(self, uid):
self._tree.delete(self._tree.item(uid))
""" """
BIND FUNCTIONS BIND FUNCTIONS
""" """
def objectSelection(self, event): def objectSelection(self, event):
curItems = self._tree.selection()
text=""
for i in curItems:
if self._tree.parent(i):
item=self._tree.item(i)
text+= (str(item['text'])+" : <"+
str(item['values'][0])+"> "+
str(item['values'][1])+"\n"
)
self._informFrame.setText(text)
def objectDelete(self, event):
print(self._tree.focus()) print(self._tree.focus())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment