Package iceprod :: Package client :: Module GtkConnections
[hide private]
[frames] | no frames]

Source Code for Module iceprod.client.GtkConnections

  1  #!/bin/env python 
  2  #   copyright  (c) 2005 
  3  #   the icecube collaboration 
  4  #   $Id: $ 
  5  # 
  6  #   @version $Revision: $ 
  7  #   @date $Date: $ 
  8  #   @author Juan Carlos Diaz Velez <juancarlos@icecube.wisc.edu> 
  9  #       @brief icetray connections frame for GtkIcetraConfig application 
 10  ######################################################################### 
 11  import pygtk 
 12  pygtk.require('2.0') 
 13  import gtk 
 14  from iceprod.core.dataclasses import * 
 15  from GtkEditConnection import GtkEditConnection 
 16  from GtkIPModule import * 
 17  import logging 
 18   
 19  logger = logging.getLogger('GtkConnections') 
 20   
21 -class GtkConnections(GtkIPModule):
22 23 TARGETS = [ ('MY_TREE_MODEL_ROW', gtk.TARGET_SAME_WIDGET, 0), ] 24
25 - def on_selection_changed(self,selection):
26 try: 27 model, iter = selection.get_selected() 28 src = model.get_value(iter, 0) 29 outbox = model.get_value(iter, 1) 30 dest = model.get_value(iter, 2) 31 inbox = model.get_value(iter, 3) 32 logger.debug("selected connection: %s@%s => %s@%s" % (outbox,src,inbox,dest)) 33 34 self.tips.disable() 35 src_class = self.iconfig.GetModule(src).GetClass() 36 dest_class = self.iconfig.GetModule(dest).GetClass() 37 self.tips.set_tip(self.tv, "%s <> %s" % (src_class,dest_class)) 38 self.tips.enable() 39 except: 40 None
41 42
43 - def delete_event(self, widget, event, data=None):
44 gtk.main_quit() 45 return False
46
47 - def delete_connection(self, b):
48 sel = self.tv.get_selection() 49 model, iter = sel.get_selected() 50 path = model.get_path(iter) 51 connection_number = model.get_value(iter, 4) 52 self.iconfig.RemoveConnection(int(connection_number)) 53 self.showconnections()
54
55 - def add_connection(self, b):
56 row = [] 57 con = Connection() 58 con.From('[ select module ]','OutBox') 59 con.To('[ select module ]') 60 outbox = con.GetOutbox() 61 inbox = con.GetInbox() 62 self.iconfig.AddConnection(con) 63 self.showconnections() 64 GtkEditConnection(self,self.iconfig,con)
65 66
67 - def __init__(self,iconfig):
68 self.iconfig = iconfig 69 gtk.VBox.__init__(self) 70 71 self.tips = gtk.Tooltips() 72 self.tips.enable() 73 74 # create a liststore with three int columns 75 self.liststore = gtk.ListStore(str,str,str,str,int) 76 77 self.sw = gtk.ScrolledWindow() 78 79 # Set sort column 80 self.tv = gtk.TreeView(self.liststore) 81 self.pack_start(self.sw) 82 self.b0 = gtk.Button('Add Connection') 83 self.b1 = gtk.Button('Delete Connection') 84 self.b0.connect('clicked', self.add_connection) 85 self.b1.connect('clicked', self.delete_connection) 86 87 self.hbbox = gtk.HButtonBox() 88 self.hbbox.pack_start(self.b0, False, False, 1) 89 self.hbbox.pack_start(self.b1, False, False, 1) 90 91 self.pack_start(self.hbbox, False) 92 self.sw.add(self.tv) 93 94 self.tv.column = [None]*6 95 self.tv.column[0] = gtk.TreeViewColumn('From') 96 self.tv.column[1] = gtk.TreeViewColumn('outbox') 97 self.tv.column[2] = gtk.TreeViewColumn('To') 98 self.tv.column[3] = gtk.TreeViewColumn('inbox') 99 100 self.tv.connect('row-activated', self.edit_connection) 101 102 self.tv.cell = [None]*6 103 104 self.selection = self.tv.get_selection() 105 self.selection.connect('changed', self.on_selection_changed) 106 # ------- 107 # Allow enable drag and drop of rows including row move 108 self.tv.enable_model_drag_source( gtk.gdk.BUTTON1_MASK, 109 self.TARGETS, 110 gtk.gdk.ACTION_DEFAULT| 111 gtk.gdk.ACTION_MOVE) 112 self.tv.enable_model_drag_dest(self.TARGETS, 113 gtk.gdk.ACTION_DEFAULT) 114 115 self.tv.connect("drag_data_get", self.drag_data_get_data) 116 self.tv.connect("drag_data_received", self.drag_data_received_data) 117 # ------- 118 119 120 for i in range(4): 121 self.tv.cell[i] = gtk.CellRendererText() 122 self.tv.cell[i].set_property( 'editable', False) 123 self.tv.cell[i].connect( 'edited', self.cell_edit,self.liststore,i) 124 self.tv.append_column(self.tv.column[i]) 125 self.tv.column[i].set_sort_column_id(i) 126 self.tv.column[i].pack_start(self.tv.cell[i], True) 127 self.tv.column[i].set_attributes(self.tv.cell[i], text=i) 128 129 self.showconnections() 130 self.show_all()
131
132 - def edit_connection(self, tv, path, viewcol):
133 sel = self.tv.get_selection() 134 sel.select_path(path) 135 model, iter = sel.get_selected() 136 sourcename = model.get_value(iter, 0) 137 outbox = model.get_value(iter, 1) 138 destname = model.get_value(iter, 2) 139 inbox = model.get_value(iter, 3) 140 row = model.get_value(iter, 4) 141 logger.debug("clicked on: %d %s %s %s %s" % (row,sourcename,outbox,destname,inbox)) 142 connection = self.iconfig.GetConnections()[row] 143 GtkEditConnection(self,self.iconfig,connection)
144
145 - def reload(self,steering):
146 self.iconfig = steering.GetTrays()[0] 147 self.showconnections()
148
149 - def showconnections(self):
150 self.liststore.clear() 151 connections = self.iconfig.GetConnections() 152 153 for c in range(len(connections)): 154 inbox = connections[c].GetInbox() 155 outbox = connections[c].GetOutbox() 156 157 row = [] 158 row.append(outbox.GetModule()) 159 row.append(outbox.GetBoxName()) 160 row.append(inbox.GetModule()) 161 row.append(inbox.GetBoxName()) 162 row.append(c) 163 self.liststore.append(row)
164 165
166 - def cell_edit( self, cell, path, new_text,model,col ):
167 """ 168 Canges the value of the connection 169 """ 170 logger.debug("Change '%s' to '%s'" % (model[path][col], new_text)) 171 row = model[path] 172 oldname = row[col] 173 newname = new_text 174 if not self.iconfig.GetModule(newname): 175 module_matches = [] 176 for mod in self.iconfig.GetModules(): 177 if mod.GetName().startswith(newname): 178 module_matches.append(mod.GetName()) 179 if len(module_matches) == 1: 180 newname = module_matches[0] 181 else: 182 newname = oldname 183 184 185 connection_number = int(row[4]) 186 con = self.iconfig.GetConnections()[connection_number] 187 if (col < 2): 188 box = con.GetOutbox() 189 else: 190 box = con.GetInbox() 191 if not (col % 2): 192 box.SetModule(newname) 193 else: 194 box.SetBoxName(newname) 195 row[col] = u'%s' % newname 196 self.showconnections()
197
198 - def drag_data_get_data(self, treeview, context, selection, target_id, etime):
199 treeselection = treeview.get_selection() 200 model, iter = treeselection.get_selected() 201 data = str(model.get_value(iter, 4)) 202 selection.set(selection.target, 8, data)
203
204 - def drag_data_received_data(self, treeview, context, x, y, selection, 205 info, etime):
206 model = treeview.get_model() 207 data = selection.data 208 drop_info = treeview.get_dest_row_at_pos(x, y) 209 if drop_info: 210 path, position = drop_info 211 iter = model.get_iter(path) 212 213 row = [] 214 old_row = int(data) 215 new_row = int(model.get_value(iter, 4)) 216 conn = self.iconfig.RemoveConnection(old_row) 217 self.iconfig.InsertConnection(new_row,conn) 218 row.append(conn.GetOutbox().GetModule()) 219 row.append(conn.GetOutbox().GetBoxName()) 220 row.append(conn.GetInbox().GetModule()) 221 row.append(conn.GetInbox().GetBoxName()) 222 223 if (position == gtk.TREE_VIEW_DROP_BEFORE 224 or position == gtk.TREE_VIEW_DROP_INTO_OR_BEFORE): 225 row.append( new_row ) 226 model.insert_before(iter, row) 227 else: 228 row.append( new_row + 1) 229 model.insert_after(iter, row) 230 else: 231 model.append([data]) 232 if context.action == gtk.gdk.ACTION_MOVE: 233 context.finish(True, True, etime) 234 return
235