Custom CellRendererToggle for Gtk Treeview



I am creating a viewer for PostgreSQL databases. A boolean column may have True, False, or None. A CellRendererToggle can not display/edit these three states without heavy manipulation (in fact, impossible as far as I can tell).

So I want to create a widget like PGAdmin III used. In normal state, it displayed True, False, or nothing. When editing, it had a togglebutton that showed active, inactive, or inconsistent. This leads me to subclass a Gtk.CellRenderer. However, my implementation overlaps the text because the tree row height is too small. Furthermore, editing doesn't seem to work. Any suggestions?


Here is the code:



#!/usr/bin/python3

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Gtk, GObject, PangoCairo
import sys

class CellRendererTog (Gtk.CellRenderer):
    __gtype_name__ = 'CellRendererToggle'
    __gproperties__ = {
        'active': ( str, # type
                    "active prop", # nick
                    "Property that holds None, False, or True", # blurb
                    'False',
                    GObject.PARAM_READWRITE # flags
                    ),
        'editable': ( bool, # type
                    "editable prop", # nick
                    "Property that holds False or True", # blurb
                    False,
                    GObject.PARAM_READWRITE # flags
                    )
    }
    def __init__(self):
        Gtk.CellRenderer.__init__(self)

    def set_editable (self, value):
        self.set_property('editable', value)

    def do_start_editing (self, event, treeview, path, background_area, cell_area, flags):
        if self.get_property ('editable') == False:
            print ('no edit')
            return
        print ('edit')
        return Gtk.CellRendererToggle()

    def do_render (self, cr, widget, background_area, cell_area, flags):
        layout = PangoCairo.create_layout(cr)
        active = self.get_property('active')
        if active == None:
            active = ''
        layout.set_text(active, len(active))
        PangoCairo.update_layout (cr, layout)
        cr.move_to (cell_area.x, cell_area.y)
        PangoCairo.show_layout (cr, layout)

    def do_set_property (self, pspec, value):
        setattr(self, pspec.name, value)

    def do_get_property (self, pspec):
        return getattr(self, pspec.name)

GObject.type_register(CellRendererTog)


class GUI:
    def __init__(self):

        window = Gtk.Window()
        store = Gtk.ListStore(str)
        store.append(['False'])
        store.append(['True'])
        store.append([None])
        treeview = Gtk.TreeView(model = store)
        window.add(treeview)
        toggleRenderer = CellRendererTog()
        toggleRenderer.set_editable(True)
        toggleColumn = Gtk.TreeViewColumn(None, toggleRenderer, active = 0)
        treeview.append_column(toggleColumn)
        window.show_all()
        window.connect('destroy', Gtk.main_quit)

def main():
    app = GUI()
    Gtk.main()

if __name__ == "__main__":
    sys.exit(main())




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]