Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/IdeMain.py
blob: 2d120e47e2abc2b0501284972a2b7d4f5dcae8d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
281
282
283
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#   IdeMain.py por:
#       Cristian García     <cristian99garcia@gmail.com>
#       Ignacio Rodriguez   <nachoel01@gmail.com>
#       Flavio Danesse      <fdanesse@gmail.com>

# 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()

    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.
        """

        pass

    def __abrir(self, widget=None):
        """
        No hace nada.
        """

        pass

    def __exit(self, widget=None):
        """
        Sale de la aplicación.
        """
        
        sys.exit(0)
        
if __name__=="__main__":
    
    IdeMain()
    Gtk.main()