pgconsole/dataview.py
changeset 76 3a41b351b122
parent 10 f3a1b9792cc9
equal deleted inserted replaced
75:39f777341db4 76:3a41b351b122
     1 import gtk, gobject, pango
     1 from gi.repository import GObject
       
     2 from gi.repository import Gtk
       
     3 from gi.repository import Pango
       
     4 
     2 from cgi import escape
     5 from cgi import escape
     3 
     6 
     4 
     7 
     5 class DataView(gtk.ScrolledWindow):
     8 class DataView(Gtk.ScrolledWindow):
     6     def __init__(self):
     9     def __init__(self):
     7         super(DataView, self).__init__()
    10         super(DataView, self).__init__()
     8 
    11 
     9         sw = self
    12         sw = self
    10         sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
    13         sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
    11         sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
    14         sw.set_shadow_type(Gtk.ShadowType.ETCHED_IN)
    12         self.treeview = gtk.TreeView(gtk.ListStore(str))
    15         self.treeview = Gtk.TreeView(Gtk.ListStore(str))
    13         self.treeview.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
    16         self.treeview.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
    14         self.treeview.set_property("rubber-banding", True)
    17         self.treeview.set_property("rubber-banding", True)
    15         sw.add(self.treeview)
    18         sw.add(self.treeview)
    16 
    19 
    17 
    20 
    18     def load_data(self, names, rows):
    21     def load_data(self, names, rows):
    19         x = [str] * (len(names) + 1)
    22         x = [str] * (len(names) + 1)
    20         liststore = gtk.ListStore(*x)
    23         liststore = Gtk.ListStore(*x)
    21 
    24 
    22         for c in self.treeview.get_columns():
    25         for c in self.treeview.get_columns():
    23             self.treeview.remove_column(c)
    26             self.treeview.remove_column(c)
    24 
    27 
    25         tvcolumn = gtk.TreeViewColumn()
    28         tvcolumn = Gtk.TreeViewColumn()
    26         cell = DataViewCellRenderer()
    29         cell = DataViewCellRenderer()
    27         cell.set_property('head', True)
    30         cell.set_property('head', True)
    28         tvcolumn.pack_start(cell, True)
    31         tvcolumn.pack_start(cell, True)
    29         tvcolumn.set_property('resizable', True)
    32         tvcolumn.set_property('resizable', True)
    30         #tvcolumn.set_property('sizing', gtk.TREE_VIEW_COLUMN_FIXED)
    33         #tvcolumn.set_property('sizing', Gtk.TREE_VIEW_COLUMN_FIXED)
    31         tvcolumn.set_attributes(cell, text=0)
    34         tvcolumn.set_attributes(cell, text=0)
    32         tvcolumn.set_min_width(2)
    35         tvcolumn.set_min_width(2)
    33         self.treeview.append_column(tvcolumn)
    36         self.treeview.append_column(tvcolumn)
    34 
    37 
    35         i = 0
    38         i = 0
    38             typename = c[1]
    41             typename = c[1]
    39             title = '<b>%s</b>' % escape(c[0])
    42             title = '<b>%s</b>' % escape(c[0])
    40             if typename:
    43             if typename:
    41                 title += '\n<span size="small">%s</span>' % typename
    44                 title += '\n<span size="small">%s</span>' % typename
    42 
    45 
    43             tvcolumn = gtk.TreeViewColumn()
    46             tvcolumn = Gtk.TreeViewColumn()
    44             cell = DataViewCellRenderer()
    47             cell = DataViewCellRenderer()
    45 
    48 
    46             lab = gtk.Label()
    49             lab = Gtk.Label()
    47             lab.set_use_underline(False)
    50             lab.set_use_underline(False)
    48             lab.set_markup(title)
    51             lab.set_markup(title)
    49             lab.show()
    52             lab.show()
    50             tvcolumn.set_widget(lab)
    53             tvcolumn.set_widget(lab)
    51             tvcolumn.set_property('resizable', True)
    54             tvcolumn.set_property('resizable', True)
    61             i += 1
    64             i += 1
    62             liststore.append([i]+list(row))
    65             liststore.append([i]+list(row))
    63 
    66 
    64 
    67 
    65 
    68 
    66 class DataViewCellRenderer(gtk.GenericCellRenderer):
    69 class DataViewCellRenderer(Gtk.CellRenderer):
    67     __gtype_name__ = 'DataViewCellRenderer'
    70     __gtype_name__ = 'DataViewCellRenderer'
    68     __gproperties__ = {
    71     __gproperties__ = {
    69         'text': (gobject.TYPE_STRING, None, None, '', gobject.PARAM_READWRITE),
    72         'text': (GObject.TYPE_STRING, None, None, '', GObject.PARAM_READWRITE),
    70         'head': (gobject.TYPE_BOOLEAN, None, None, False, gobject.PARAM_READWRITE)}
    73         'head': (GObject.TYPE_BOOLEAN, None, None, False, GObject.PARAM_READWRITE)}
    71 
    74 
    72 
    75 
    73     def __init__(self):
    76     def __init__(self):
    74         gtk.GenericCellRenderer.__init__(self)
    77         Gtk.CellRenderer.__init__(self)
    75         self._props = {'text' : '', 'head' : False}
    78         self._props = {'text' : '', 'head' : False}
    76 
    79 
    77 
    80 
    78     def do_set_property(self, pspec, value):
    81     def do_set_property(self, pspec, value):
    79         if not pspec.name in self._props:
    82         if not pspec.name in self._props:
    80             raise AttributeError, 'Unknown property: %s' % pspec.name
    83             raise AttributeError('Unknown property: %s' % pspec.name)
    81         self._props[pspec.name] = value
    84         self._props[pspec.name] = value
    82 
    85 
    83 
    86 
    84     def do_get_property(self, pspec):
    87     def do_get_property(self, pspec):
    85         return self._props[pspec.name]
    88         return self._props[pspec.name]
    86 
    89 
    87 
    90 
    88     def on_get_size(self, widget, cell_area):
    91     def on_get_size(self, widget, cell_area):
    89         if cell_area == None:
    92         if cell_area == None:
    90             pangoctx = widget.get_pango_context()
    93             pangoctx = widget.get_pango_context()
    91             layout = pango.Layout(pangoctx)
    94             layout = Pango.Layout(pangoctx)
    92             layout.set_width(-1)
    95             layout.set_width(-1)
    93             layout.set_text(self.get_property('text') or 'NULL')
    96             layout.set_text(self.get_property('text') or 'NULL')
    94             w,h = layout.get_pixel_size()
    97             w,h = layout.get_pixel_size()
    95             return (0, 0, w+5, 20)
    98             return (0, 0, w+5, 20)
    96         x = cell_area.x
    99         x = cell_area.x
   118         ctx.move_to(x+w-0.5, y-0.5)
   121         ctx.move_to(x+w-0.5, y-0.5)
   119         ctx.line_to(x+w-0.5, y+h-0.5)
   122         ctx.line_to(x+w-0.5, y+h-0.5)
   120         ctx.stroke()
   123         ctx.stroke()
   121 
   124 
   122         pangoctx = widget.get_pango_context()
   125         pangoctx = widget.get_pango_context()
   123         layout = pango.Layout(pangoctx)
   126         layout = Pango.Layout(pangoctx)
   124         text = self.get_property('text')
   127         text = self.get_property('text')
   125         head = self.get_property('head')
   128         head = self.get_property('head')
   126 
   129 
   127         if head:
   130         if head:
   128             layout.set_markup('<b>%s</b>' % text)
   131             layout.set_markup('<b>%s</b>' % text)
   130             if text is None:
   133             if text is None:
   131                 layout.set_markup('<span foreground="gray">NULL</span>')
   134                 layout.set_markup('<span foreground="gray">NULL</span>')
   132             else:
   135             else:
   133                 layout.set_text(text)
   136                 layout.set_text(text)
   134 
   137 
   135         widget.style.paint_layout(window, gtk.STATE_NORMAL, True,
   138         widget.style.paint_layout(window, Gtk.STATE_NORMAL, True,
   136                                 cell_area, widget, '',
   139                                 cell_area, widget, '',
   137                                 cell_area.x, cell_area.y,
   140                                 cell_area.x, cell_area.y,
   138                                 layout)
   141                                 layout)
   139 
   142