#!/usr/bin/env python # -*- coding: utf-8 -*- # IdeMain.py por: # Cristian García # Ignacio Rodriguez # Flavio Danesse # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import os import sys import json from gi.repository import Gtk from gi.repository import Gdk from Widgets import Menu #from Widgets import MainToolbar #from Widgets import TryToolbar from Widgets import My_FileChooser from Widgets import DialogoProyecto from BasePanel import BasePanel home = os.environ["HOME"] BatovideWorkSpace = os.path.join(home, 'BatovideWorkSpace') if not os.path.exists(BatovideWorkSpace): os.mkdir(BatovideWorkSpace) PATH = os.path.dirname(__file__) screen = Gdk.Screen.get_default() css_provider = Gtk.CssProvider() style_path = os.path.join(PATH, "Estilo.css") css_provider.load_from_path(style_path) context = Gtk.StyleContext() context.add_provider_for_screen( screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) class IdeMain(Gtk.Window): def __init__(self): Gtk.Window.__init__(self) #self.set_title("") #self.set_icon_from_file(".png") self.set_resizable(True) self.set_size_request(640, 480) self.set_border_width(5) self.set_position(Gtk.WindowPosition.CENTER) base_widget = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) menu = Menu() #toolbar = MainToolbar() self.base_panel = BasePanel() #trytoolbar = TryToolbar() self.proyecto = {} base_widget.pack_start(menu, False, False, 0) #base_widget.pack_start(toolbar, False, False, 0) base_widget.pack_start(self.base_panel, True, True, 0) #base_widget.pack_start(trytoolbar, False, False, 0) self.add(base_widget) self.show_all() self.maximize() menu.connect('accion', self.__ejecutar_accion) self.connect("destroy", self.__exit) def __ejecutar_accion(self, widget, accion): """ Ejecuta acciones según selección del usuario en el menú de la aplicación. """ if accion == "Nuevo proyecto": dialog = DialogoProyecto( parent_window = self, title = "Crear Proyecto Nuevo") response = dialog.run() if Gtk.ResponseType(response) == Gtk.ResponseType.ACCEPT: # FIXME: Agregar código para el caso en que ya exista un proyecto abierto. # Si el proyecto abierto tiene cambios no guardados, pedir confirmación # para guardarlos. # Cerrar el proyecto. # FIXME: Agregar codigo previendo que quizas el proyecto ya exista, # para evitar que el usuario lo sobre escriba sin darse cuenta. self.proyecto = dialog.get_proyecto() self.__guardar() dialog.destroy() elif accion == "Editar proyecto": if self.proyecto: dialog = DialogoProyecto( parent_window = self, title = "Editar Proyecto") dialog.set_proyecto(self.proyecto) response = dialog.run() if Gtk.ResponseType(response) == Gtk.ResponseType.ACCEPT: # FIXME: Agregar codigo previendo que el usuario # quizas cambie el nombre del proyecto. self.proyecto = dialog.get_proyecto() self.__guardar() dialog.destroy() elif accion == "Abrir proyecto": filechooser = My_FileChooser( parent_window = self, action_type = Gtk.FileChooserAction.OPEN, filter_type = "*.json", title = "Abrir proyecto", path = BatovideWorkSpace) filechooser.connect('load', self.__abrir_proyecto) elif accion == "Nuevo": self.__nuevo() elif accion == "Abrir": self.__abrir(direccion = None) def __abrir_proyecto(self, widget, archivo): """ Abrir archivo de un proyecto. """ # FIXME: Agregar código para el caso en que ya exista un proyecto abierto. # Si el proyecto abierto tiene cambios no guardados, pedir confirmación # para guardarlos. # Cerrar el proyecto. extension = os.path.splitext(os.path.split(archivo)[1])[1] if not extension == ".json": return else: import json import codecs pro = codecs.open(archivo, "r", "utf-8") proyecto = json.JSONDecoder("utf-8").decode(pro.read()) self.__cargar_proyecto(proyecto) def __cargar_proyecto(self, proyecto): """ Carga un proyecto. """ # FIXME: Esto debe además: # Actualizar los menús y toolbars correspondientes. self.proyecto = proyecto self.base_panel.load(self.proyecto) def __guardar(self): """ Guarda el proyecto actual. """ ### Todo Proyecto Requiere un Nombre. if not self.proyecto.get("nombre", False): return ### Seteo automático del path del proyecto. if self.proyecto.get("path", False): path = self.proyecto["path"] else: path = os.path.join(BatovideWorkSpace, self.proyecto["nombre"]) if not os.path.exists(path): os.mkdir(path) self.proyecto["path"] = path ### Seteo automático del main del proyecto. if not self.proyecto.get("main", False): self.proyecto["main"] = "Main.py" main_path = os.path.join(self.proyecto["path"], self.proyecto["main"]) if not os.path.exists(main_path): arch = open(main_path, "w") arch.write("#!/usr/bin/env python\n# -*- coding: utf-8 -*-") arch.close() # FIXME: Agregar Seteo automático de licencia del proyecto. ### Seteo automático de autores. autores_path = os.path.join(self.proyecto["path"], "AUTHORS") arch = open(autores_path, "w") for autor in self.proyecto["autores"]: arch.write("%s %s\n" % autor) arch.close() ### Guardar el Proyecto. proyecto = os.path.join(path, "proyecto.json") import simplejson archivo = open(proyecto, "w") archivo.write( simplejson.dumps( self.proyecto, indent=4, separators=(", ", ":"), sort_keys=True ) ) archivo.close() ### Cargar el Proyecto Creado self.__cargar_proyecto(self.proyecto) def __nuevo(self, widget=None): """ No hace nada. """ self.base_panel.abrir_archivo(None) def __abrir(self, widget=None, direccion=None): """ Abre un archivo. """ if direccion != None: self.base_panel.abrir_archivo(direccion) print direccion else: filechooser = My_FileChooser( parent_window = self, action_type = Gtk.FileChooserAction.OPEN, filter_type = "*.py", title = "Abrir archivo", path = BatovideWorkSpace) filechooser.connect('load', self.abrir) respuesta = filechooser.run() def abrir(self, widget, direccion): """ Abre archivo. """ self.__abrir(direccion = direccion) def __exit(self, widget=None): """ Sale de la aplicación. """ sys.exit(0) if __name__=="__main__": IdeMain() Gtk.main()