1. J.A. Roberts Tunney
  2. asterator

Source

asterator / Asterator / zaptree.py


import gobject
import pygtk
pygtk.require('2.0')
import gtk

(
    CHANNEL_COLUMN,
    STATE_COLUMN,
    SIGNALLING_COLUMN,
    CONTEXT_COLUMN,
    DND_COLUMN,
    ALARM_COLUMN
) = range(6)

class zap_tree(gtk.TreeView):
    def gen_model(self):
        model = gtk.TreeStore(
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING,
            gobject.TYPE_STRING)
        
        return model
    
    def __init__(self):
        model = self.gen_model()
        
        gtk.TreeView.__init__(self, model)
        self.set_rules_hint(True)
        self.connect('realize', lambda tv: tv.expand_all())
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Channel", renderer, text=CHANNEL_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("State", renderer, text=STATE_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Signalling", renderer, text=SIGNALLING_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Context", renderer, text=CONTEXT_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("DND", renderer, text=DND_COLUMN)
        column.set_resizable(True)
        self.append_column(column)
        
        renderer = gtk.CellRendererText()
        renderer.set_property("xalign", 0.0)
        column = gtk.TreeViewColumn("Alarm", renderer, text=ALARM_COLUMN)
        column.set_resizable(True)
        self.append_column(column)

    def clear(self):
        self.set_model(self.gen_model())

    def myset(self, model, zap):
        if zap.dnd: dnd = 'Yes'
        else:       dnd = 'No'
        
        model.set(zap.iter,
                  CHANNEL_COLUMN, zap.channelno,
                  STATE_COLUMN, zap.state,
                  SIGNALLING_COLUMN, zap.signalling,
                  CONTEXT_COLUMN, zap.context,
                  DND_COLUMN, dnd,
                  ALARM_COLUMN, zap.alarm)

    def reconcile(self, listener):
        """A quickly written refresh method

        Don't call this from within a listener callback or you'll get
        a deadlock lolz because this locks the listener object.

        ToDo: Make this better
        
        """
        self.clear()
        listener.lock()
        ar = listener.zaps.keys()
        ar.sort()
        for zapkey in ar:
            self.add_zap(listener.zaps[zapkey])
        listener.unlock()

    def add_zap(self, zap):
        model = self.get_model()
        iter = model.append(None)
        zap.iter = iter
        self.myset(model, zap)

    def del_zap(self, zap):
        model = self.get_model()
        model.remove(zap.iter)

    def upd_zap(self, zap):
        model = self.get_model()
        self.myset(model, zap)