genet / view / genet_window.py

  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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python
################################################################################
# $Id$
################################################################################
# Copyright (c) 2008 Santiago Paya Miralta <santiagopm::wanadoo.es>
#
# This file is part of mine.
#
# Mine 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
################################################################################
"""
Interface.

Has one tab for every work:

* Admin tab: create levels and types.
* Create tab: create nodes into the network. Must declare type of node and
parent in which the node is embebed.
* Connect tab: connects nodes.
* Browse tab: shows network graphs (uses networkx, graphviz, ...).
"""

## TODO My emacs needs this
import sys
if '/home/santiago/workspace/genet' not in sys.path:
    sys.path.append('/home/santiago/workspace/genet')
##

import gtk
import gtk.glade
import pygtk

from control import genet_browser
from control import genet_creator
from control import genet_global

#from model import class_edge
#from model import class_node
#from model.db_connection import db

from view import genet_treemodels

class GenetWindow(gtk.Window):
    def __init__(self):
        """
        The window.

        """
        gladefile = "/home/santiago/workspace/genet/view/genet.glade"
        windowname = "genet"
        self.wTree = gtk.glade.XML(gladefile, windowname)

        # Fill tree views.
        ##
        # Browse tree
        tree = self.wTree.get_widget("treeview_browser")
        model = genet_treemodels.BrowseNodesTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Level", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("Attribute", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        ##
        # Connect nodes A tree
        tree = self.wTree.get_widget("treeview_conn_nodes_A")
        model = genet_treemodels.ConnectNodesTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Level", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("Attribute", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        ##
        # Connect nodes B tree
        tree = self.wTree.get_widget("treeview_conn_nodes_B")
        model = genet_treemodels.ConnectNodesTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Level", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("Attribute", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        ##
        # Connect nodes edge types tree
        tree = self.wTree.get_widget("treeview_conn_nodes_edge_types")
        model = genet_treemodels.ConnectNodesEdgeTypesTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Type", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        ##
        # Create node types tree
        tree = self.wTree.get_widget("treeview_create_node_types")
        model = genet_treemodels.CreateNodeTypesTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Level", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("Type", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        ##
        # Create node parents tree
        tree = self.wTree.get_widget("treeview_create_node_parents")
        model = genet_treemodels.CreateNodeParentsTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Level", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("Type", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        ##
        # Admin levels tree
        tree = self.wTree.get_widget("treeview_admin_levels")
        model = genet_treemodels.AdminTreeModel()
        tree.set_model(model)

        cell = gtk.CellRendererText()

        # Ids column should disapear in production version
        column00 = gtk.TreeViewColumn("Id", cell, text=0)
        column00.set_clickable(True)
        tree.append_column(column00)

        column01 = gtk.TreeViewColumn("Level", cell, text=1)
        column01.set_clickable(True)
        tree.append_column(column01)

        column02 = gtk.TreeViewColumn("Type", cell, text=2)
        column02.set_clickable(True)
        tree.append_column(column02)

        tree.set_expander_column(tree.get_column(1))

        # Declare actions dictionary.
        dic = {
            # Browse tab.
            "on_button_browser_draw_level_clicked" :
            self.button_browser_draw_level_clicked,
            "on_button_browser_draw_tree_clicked" :
            self.button_browser_draw_tree_clicked,

            # Connect nodes tab.
            "on_button_conn_nodes_clicked" :
                self.button_conn_nodes_clicked,
            "on_button_conn_nodes_refresh_clicked" :
                self.button_conn_nodes_refresh_clicked,

            # Create tab.
            "on_button_create_node_clicked" :
                self.button_create_node_clicked,

            # Admin tab.
            "on_button_create_level_clicked" :
            self.button_create_level_clicked,
            "on_button_drop_item_clicked" :
            self.button_drop_item_clicked,
            "on_button_create_node_type_clicked" :
                self.button_create_node_type_clicked,
            "on_button_create_edge_type_clicked" :
                self.button_create_edge_type_clicked,
            "on_button_admin_reset_clicked" :
                self.button_admin_reset_clicked,
            "on_genet_destroy" : (gtk.main_quit) }
        self.wTree.signal_autoconnect(dic)
        return

    ############################################################################
    # Browse tab.
    #
    def button_browser_draw_level_clicked(self, widget):
        print 'Level Clicked!'

    def button_browser_draw_tree_clicked(self, widget):
        print 'Tree Clicked!'

    ############################################################################
    # Connect nodes tab.
    #
    def button_conn_nodes_clicked(self, widget):
        """Connects nodes selected left and right with an edge type.

        The edge type is taken from the edge types tree.
        """
        # TODO all interface flow must be loaded by steps, selecting node A,
        # then edge type and then node B.
        
        # Get node A level
        root_values = self.get_tree_selected_root_values("treeview_conn_nodes_A")
        level_id = root_values[0]

        # Load edge types in this level
#        tree = self.wTree.get_widget("treeview_conn_nodes_edge_types")
#        tree.get_model().reload(int(level_id))
#        tree.expand_all()

        # Get type
        type_values = self.get_tree_selected_values("treeview_conn_nodes_edge_types")
        type_id = type_values[0]

        # Get parent id
        parent_id = 1

        # Get parent level
        parentlevel_id = 1

        # Get node A id
        nA_values = self.get_tree_selected_values("treeview_conn_nodes_A")
        nA_id = nA_values[0]

        # Get node B id
        nB_values = self.get_tree_selected_values("treeview_conn_nodes_B")
        nB_id = nB_values[0]

        genet_creator.connectNodes(level_id, nA_id, nB_id, parent_id, parentlevel_id, type_id, ('Created from code.',))

    def button_conn_nodes_refresh_clicked(self, widget):
        """ Refresh trees from data model.
        """
        self.tree_refresh("treeview_conn_nodes_A")
        self.tree_refresh("treeview_conn_nodes_edge_types")
        self.tree_refresh("treeview_conn_nodes_B")

    ############################################################################
    # Create tab.
    #
    def button_create_node_clicked(self, widget):
        # Get level
        root_values = self.get_tree_selected_root_values("treeview_create_node_types")
        level_id = root_values[0]

        # Get type
        type_values = self.get_tree_selected_values("treeview_create_node_types")
        type_id = type_values[0]

        # Get parent level
        parent_root_values = self.get_tree_selected_root_values("treeview_create_node_parents")
        parentlevel_id = parent_root_values[0]

        # Get parent
        parent_values = self.get_tree_selected_values("treeview_create_node_parents")
        parent_id = parent_values[0]

        # Get attributes
        att01 = self.wTree.get_widget("entry_create_node_att").get_text()
        attributes = att01,

        # Create node
        # TODO When there is no parent, parentlevel should be its own level
        # and parent id should be 0.
        genet_creator.createNode(level_id, parent_id,
                                 parentlevel_id, type_id, attributes)

        # Refresh tree
        self.tree_refresh("treeview_create_node_types")
        self.tree_refresh("treeview_create_node_parents")

    ############################################################################
    # Admin tab.
    #
    def button_create_level_clicked(self, widget):
        """Create a level in the network.
        """
        # Get description
        description = self.wTree.get_widget("entry_admin_level").get_text()

        # Create level
        genet_creator.createLevel(description)

        # Refresh tree
        self.tree_refresh("treeview_admin_levels")

    def button_create_node_type_clicked(self, widget):
        """Create a node type.

        Gets the level in which to create the node type from any selected
        item in the tree.
        """
        # Get level
        root_values = self.get_tree_selected_root_values("treeview_admin_levels")
        level_id = root_values[0]

        # Get description
        description = self.wTree.get_widget("entry_admin_type").get_text()

        # Create type
        genet_creator.createNodeType(level_id, description)

        # Refresh tree
        self.tree_refresh("treeview_admin_levels")

    def button_create_edge_type_clicked(self, widget):
        """Create an edge type.

        Gets the level in which to create the edge type from any selected
        item in the tree.
        """
        # Get level
        root_values = self.get_tree_selected_root_values("treeview_admin_levels")
        level_id = root_values[0]

        # Get description
        description = self.wTree.get_widget("entry_admin_type").get_text()

        # Create type
        genet_creator.createEdgeType(level_id, description)

        # Refresh tree
        self.tree_refresh("treeview_admin_levels")

    def button_drop_item_clicked(self, widget):
        """Purge selected item.

        Gets the item to delete from selection tree.
        """
        values = self.get_tree_selected_values("treeview_admin_levels")

        values_id = values[0]
        values_type = values[2]

        if values_type == "Edge":
            genet_creator.dropEdgeType(values_id)
        elif values_type == "Node":
            genet_creator.dropNodeType(values_id)
        elif values_type == "":
            genet_creator.dropLevel(values_id)
        else:
            # TODO raise exception. Never should be here.
            print "*** Error in code ***"

        # Refresh tree
        self.tree_refresh("treeview_admin_levels")

    def button_admin_reset_clicked(self, widget):
        genet_global.restartDatabase()

        # Refresh tree
        self.tree_refresh("treeview_admin_levels")

    ############################################################################
    # Auxiliary functions.
    #
    def main(self):
        gtk.main()

    def get_tree_selected_root_values(self, tree_widget):
        """Get all information from the root of the selected row in the tree.
        """
        tree = self.wTree.get_widget(tree_widget)
        tree_selection = tree.get_selection()
        tree_selection_model = tree_selection.get_selected()[0]
        tree_selection_iter = tree_selection.get_selected()[1]
        tree_n_columns = tree_selection_model.get_n_columns()

        # Get the root from selection. Could be in root now, so root iter is
        # the actually selected iter.
        root_iter = tree_selection_iter
        parent_iter = tree_selection_model.iter_parent(root_iter)
        while parent_iter is not None:
            root_iter = parent_iter
            parent_iter = tree_selection_model.iter_parent(parent_iter)

        values = ()
        for c in range(tree_n_columns):
            values += tree_selection_model.get(root_iter, c)

        return values

    def get_tree_selected_values(self, tree_widget):
        """Get all information from the selected row in the tree.
        """
        tree = self.wTree.get_widget(tree_widget)
        tree_sel = tree.get_selection()
        tree_sel_model, tree_sel_iter = tree_sel.get_selected()
        tree_n_columns = tree_sel_model.get_n_columns()

        values = ()
        for c in range(tree_n_columns):
            values += tree_sel_model.get(tree_sel_iter, c)

        return values

    def tree_refresh(self, tree_widget):
        """Refresh tree data without forget selection.
        """
        tree = self.wTree.get_widget(tree_widget)
        tree_sel = tree.get_selection()
        tree_sel_model, tree_sel_iter = tree_sel.get_selected()

        # Save selected item or path
        tree_sel_path = (0,)
        if tree_sel_iter is not None:
            tree_sel_path = tree_sel_model.get_path(tree_sel_iter)

        # Refresh data, reload data into model
        tree.get_model().reload()

        # Expand old selection
        tree.expand_to_path(tree_sel_path)
        #tree_sel.select_path(tree_sel_path)

if __name__ == '__main__':
    app = GenetWindow()
    app.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.