Commit c2e084e0 authored by Francois Mares's avatar Francois Mares
Browse files

add GUI

done main architecture
done import JSON object
parent 0991640e
import demjson
from Model import Model
from tk.MainWindow import MainWindow
class Controller:
_model=None
def getModel(self): return self._model
_view=None
def getView(self): return self._view
"""
INITIALISATION
"""
def __init__(self):
self._model=Model()
self._view=MainWindow(self)
"""
IMPORT/EXPORT (JSON)
"""
def exportJSON(self, obj, file):
assert len(file)>5 and file[-4:]=="json"
with open(file, "w") as jsonFile:
jsonFile.write(demjson.encode(obj))
def importJSON(self, file):
assert len(file)>5 and file[-4:]=="json"
with open(file, "r") as jsonFile:
return demjson.decode(jsonFile.read())
\ No newline at end of file
import pandas as pd
from ModelCallable import ModelCallable
from GrapheDeComposition import GrapheDeComposition, GC
class Model:
"""
Contient tous les objets construits.
"""
_data=None
def __init__(self):
self._data=pd.DataFrame(columns=["class","object"])
self._data["class"]=self._data["class"].astype("string")
"""
CREATION D'OBJETS
"""
def addObject(self, obj:object):
newData={"class": type(obj).__name__,
"object": obj}
self._data=self._data.append(newData, ignore_index=True)
self.informObjectAdded(self._data.index[-1], obj)
def addObjects(self, objects:list):
newData=[]
for obj in objects:
newData.append({"class": type(obj).__name__,
"object": obj})
i=self._data.index[-1]
self._data=self._data.append(newData, ignore_index=True)
self.informObjectsAdded(list(range(i, self._data.index[-1]+1)))
def createGrapheDeComposition(self, objets:set = set(), nom:str = None):
gc=GC(objets=objets, nom=nom)
self.addObject([gc])
"""
GETTERS
"""
def getData(self):
return self._data
def getObject(self, uid):
return self._data.iloc(uid)
"""
INFORM
"""
_registeredModelCallable=[]
def registerModelCallable(self, mc:ModelCallable):
self._registeredModelCallable.append(mc)
def informDataModified(self):
for rdm in self._registeredModelCallable:
rdm.informDataModified()
def informObjectAdded(self, uid, obj:object):
for rdm in self._registeredModelCallable:
rdm.informObjectAdded(uid, obj)
def informObjectDeleted(self, uid):
for rdm in self._registeredModelCallable:
rdm.informObjectDeleted(uid)
def informObjectsAdded(self, uids:list):
for rdm in self._registeredModelCallable:
rdm.informObjectsAdded(uids)
def informObjectsDeleted(self, uids:list):
for rdm in self._registeredModelCallable:
rdm.informObjectsDeleted(uids)
class ModelCallable:
"""
A surcharger
Permet de s'enregistrer pour recevoir des notifications
d'une instance Model.
"""
def informDataModified(self):
pass
def informObjectAdded(self, uid, obj:object):
pass
def informObjectDeleted(self, uid):
pass
def informObjectsAdded(self, uids:list):
pass
def informObjectsDeleted(self, uids:list):
pass
"""
Paramètres de la GUI
"""
class Parameters():
SECTION_COLOR = "#649BB4"
SECTION_RELIEF="groove"
SECTION_FONT_SIZE = 10
SECTION_FONT_WEIGHT = 'bold'
SECTION_FONT_UNDERLINE= 0
parameters=Parameters()
from Controller import Controller
if __name__ == '__main__':
Controller()
"""
define constants and wrap OpenGL functions to Python
"""
from ctypes import c_bool, c_char_p, c_double, c_float, c_int, c_uint, c_ulong, c_void_p, POINTER
import sys
'''
wrap GDI constants and functions to Python
'''
if sys.platform.startswith('win32'):
from ctypes import WinDLL
_libGL = WinDLL('opengl32')
elif sys.platform.startswith('linux'):
from ctypes import cdll
# Shared library path hardcode for Xubuntu 15.10
_libGL = cdll.LoadLibrary('/usr/lib/i386-linux-gnu/mesa/libGL.so')
else:
raise NotImplementedError
# Data types for Linux Platform
XID = c_ulong
GLXDrawable = XID
# Data types for OpenGL
GLbitfield = c_uint
GLubyte = c_char_p
GLclampf = c_float
GLclampd = c_double
GLdouble = c_double
GLenum = c_uint
GLfloat = c_float
GLint = c_int
GL_BLEND = 0x0BE2
GL_COLOR_BUFFER_BIT = 0x00004000
GL_DEPTH_BUFFER_BIT = 0x00000100
GL_DEPTH_TEST = 0x0B71
GL_MODELVIEW = 0x1700
GL_ONE_MINUS_SRC_ALPHA = 0x0303
GL_PROJECTION = 0x1701
GL_QUADS = 0x0007
GL_RENDERER = 0x1F01
GL_SRC_ALPHA = 0x0302
GL_VENDOR = 0x1F00
GL_VERSION = 0x1F02
GL_TRUE = 1
# Constants for Linux Platform
# Hardcode number of array elements of int type
# used in this example
PGLint = GLint * 11
GLX_RGBA = 4
GLX_RED_SIZE = 8
GLX_GREEN_SIZE = 9
GLX_BLUE_SIZE = 10
GLX_DEPTH_SIZE = 12
GLX_DOUBLEBUFFER = 5
# OpenGL Function Definitions
glBegin = _libGL.glBegin
glBegin.restype = None
glBegin.argtypes = [GLenum]
glClear = _libGL.glClear
glClear.restype = None
glClear.argtypes = [GLbitfield]
glBlendFunc = _libGL.glBlendFunc
glBlendFunc.restype = None
glBlendFunc.argtypes = [GLenum, GLenum]
glClearColor = _libGL.glClearColor
glClearColor.restype = None
glClearColor.argtypes = [GLclampf, GLclampf, GLclampf, GLclampf]
glClearDepth = _libGL.glClearDepth
glClearDepth.restype = None
glClearDepth.argtypes = [GLclampd]
glColor3f = _libGL.glColor3f
glColor3f.restype = None
glColor3f.argtypes = [GLfloat, GLfloat, GLfloat]
glEnable = _libGL.glEnable
glEnable.restype = None
glEnable.argtypes = [GLenum]
glEnd = _libGL.glEnd
glEnd.restype = None
glEnd.argtypes = None
glFlush = _libGL.glFlush
glFlush.restype = None
glFlush.argtypes = None
glGetString = _libGL.glGetString
glGetString.restype = GLubyte
glGetString.argtypes = [GLenum]
glLoadIdentity = _libGL.glLoadIdentity
glLoadIdentity.restype = None
glLoadIdentity.argtypes = None
glMatrixMode = _libGL.glMatrixMode
glMatrixMode.restype = None
glMatrixMode.argtypes = None
glOrtho = _libGL.glOrtho
glOrtho.restype = None
glOrtho.argtypes = [GLdouble, GLdouble, GLdouble, GLdouble, GLdouble, GLdouble]
glRotatef = _libGL.glRotatef
glRotatef.restype = None
glRotatef.argtypes = [GLfloat, GLfloat, GLfloat, GLfloat]
glVertex3f = _libGL.glVertex3f
glVertex3f.restype = None
glVertex3f.argtypes = [GLfloat, GLfloat, GLfloat]
glViewport = _libGL.glViewport
glViewport.restype = None
glViewport.argtypes = [GLint, GLint, GLint, GLint]
if sys.platform.startswith('win32'):
wglCreateContext = _libGL.wglCreateContext
wglCreateContext.restype = c_void_p
wglCreateContext.argtypes = [c_void_p]
wglMakeCurrent = _libGL.wglMakeCurrent
wglMakeCurrent.restype = c_bool
wglMakeCurrent.argtypes = [c_void_p, c_void_p]
elif sys.platform.startswith('linux'):
glXChooseVisual = _libGL.glXChooseVisual
glXChooseVisual.argtypes = [c_void_p, c_int, POINTER(c_int)]
glXChooseVisual.restype = c_void_p
glXCreateContext = _libGL.glXCreateContext
glXCreateContext.argtypes = [c_void_p, c_void_p, c_void_p, c_bool]
glXCreateContext.restype = c_void_p
glXMakeCurrent = _libGL.glXMakeCurrent
glXMakeCurrent.argtypes = [c_void_p, GLXDrawable, c_void_p]
glXMakeCurrent.restype = c_bool
glXSwapBuffers = _libGL.glXSwapBuffers
glXSwapBuffers.argtypes = [c_void_p, GLXDrawable]
glXSwapBuffers.resttype = None
""" wrap GDI constants and functions to Python
"""
from ctypes import c_int, c_void_p, POINTER, Structure, WinDLL
from ctypes.wintypes import BOOL, BYTE, DWORD, HDC, WORD
_gdi32 = WinDLL('gdi32')
_user32 = WinDLL('user32')
class PixelFormatDescriptor(Structure):
_fields_ = [
('nSize', WORD),
('nVersion', WORD),
('dwFlags', DWORD),
('iPixelType', BYTE),
('cColorBits', BYTE),
('cRedBits', BYTE),
('cRedShift', BYTE),
('cGreenBits', BYTE),
('cGreenShift', BYTE),
('cBlueBits', BYTE),
('cBlueShift', BYTE),
('cAlphaBits', BYTE),
('cAlphaShift', BYTE),
('cAccumBits', BYTE),
('cAccumRedBits', BYTE),
('cAccumGreenBits', BYTE),
('cAccumBlueBits', BYTE),
('cAccumAlphaBits', BYTE),
('cDepthBits', BYTE),
('cStencilBits', BYTE),
('cAuxBuffers', BYTE),
('iLayerType', BYTE),
('bReserved', BYTE),
('dwLayerMask', DWORD),
('dwVisibleMask', DWORD),
('dwDamageMask', DWORD),
]
PFD_DRAW_TO_WINDOW = 4 # Variable c_int
PFD_SUPPORT_OPENGL = 32 # Variable c_int
PFD_DOUBLEBUFFER = 1 # Variable c_int
PFD_TYPE_RGBA = 0 # Variable c_int
get_dc = _user32.GetDC
get_dc.restype = HDC
get_dc.argtypes = [c_void_p]
choose_pixel_format = _gdi32.ChoosePixelFormat
choose_pixel_format.restype = c_int
choose_pixel_format.argtypes = [c_void_p, POINTER(PixelFormatDescriptor)]
set_pixel_format = _gdi32.SetPixelFormat
set_pixel_format.restype = BOOL
set_pixel_format.argtypes = [c_void_p, c_int, POINTER(PixelFormatDescriptor)]
get_pixel_format = _gdi32.GetPixelFormat
get_pixel_format.restype = c_int
get_pixel_format.argtypes = [c_void_p]
swap_buffers = _gdi32.SwapBuffers
swap_buffers.restype = BOOL
swap_buffers.argtypes = [HDC]
from ctypes import c_char_p, c_void_p, cdll
# Shared library path hardcode for Xubuntu 15.10
_x11lib = cdll.LoadLibrary('/usr/lib/i386-linux-gnu/libX11.so')
X11_None = 0
x_open_display = _x11lib.XOpenDisplay
x_open_display.argtypes = [c_char_p]
x_open_display.restype = c_void_p
["dezduef"]
\ No newline at end of file
2
\ No newline at end of file
"""
Paramètres de la GUI
"""
from tkinter import Label
import tkinter.font as tkFont
from Parameters import parameters
class Factory():
@staticmethod
def buildSectionLabel(parent, text):
return Label(parent, text=text,
bg=parameters.SECTION_COLOR,
relief=parameters.SECTION_RELIEF,
font=tkFont.Font(size=parameters.SECTION_FONT_SIZE,
weight=parameters.SECTION_FONT_WEIGHT,
underline=parameters.SECTION_FONT_UNDERLINE))
factory=Factory()
\ No newline at end of file
from os import getcwd
from tkinter import TOP, YES, BOTH, \
Tk, Frame, Label, Menu, \
filedialog
from tko.AppOgl import AppOgl
from tk.OutilsFrame import OutilsFrame
from tk.ObjetsFrame import ObjetsFrame
class MainWindow:
_controller:None
_root=None
_frame=None
_glFrame=None
_outFrame=None
_objFrame=None
_glApp=None
_menu=None
"""
INITIALISATION
"""
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
self._frame = Frame(self._root)
self._frame.pack(expand=YES, fill=BOTH)
self._frame.columnconfigure(0, weight=1, minsize=500)
self._frame.columnconfigure(1, weight=1, minsize=200)
self._frame.columnconfigure(2, weight=1, minsize=200)
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
self._outFrame = OutilsFrame(self._frame)
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()
fichierMenu=Menu(self._menu, tearoff=0)
fichierMenu.add_cascade(label="Importer", command=self.importer)
fichierMenu.add_cascade(label="Exporter", command=self.exporter)
fichierMenu.add_separator()
#fichierMenu.add_cascade(label="Quitter", command=self.test)
self._menu.add_cascade(label="Fichier", menu=fichierMenu)
self._menu.add_cascade(label="Paramètres")
self._root.config(menu=self._menu)
"""
CONTROLLER CALLS
"""
def importer(self):
filepath = filedialog.askopenfilename(title="Ouvrir une image",
filetypes=[('JSON file','.json')],
initialdir=getcwd())
self._controller.getModel().addObject(self._controller.importJSON(filepath))
def exporter(self):
filepath = filedialog.asksaveasfilename(initialdir = "/",title = "Select file",filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
print(filepath)
from tkinter import TOP, BOTTOM, LEFT, RIGHT, X, \
LabelFrame, Frame, Label, Text
from tkinter.ttk import Combobox
NEW_OBJECTS_LIST=["GC - Garphe de composition",
"CI - Catégorie indexante",
"It - Interraction",
"CP - Champ perceptif"]
class NewObjectFrame(LabelFrame):
"""
Frame objects
"""
_NOList=None
_NOFrames=None
_frame=None
def __init__(self, parent, *args, **kwargs):
LabelFrame .__init__(self, parent,
bd=2, text="Nouvel Objet",
*args, **kwargs)
self._initFrame()
def _initFrame(self):
self._NOList=Combobox(self,
values=NEW_OBJECTS_LIST,
state='readonly')
self._NOList.pack(side = TOP, fill=X)
self._NOList.bind("<<ComboboxSelected>>", self.selectNOType)
self._initNOFrames()