示例#1
0
    def __init__(self, data, columns):
        """Create a new instance of a Legend. 
        
        Arguments:
            data --  a dictionary in which each item represent a legend item.
                     the keys are the legend strings, and value must be 
                     Gtk.gdk.Colors
            column -- the number of column to use to format the table 
        """
        def ceil(a, b):
            """Return the number of row required to pack a values in b columns"""
            q, r = divmod(a, b)
            if r: return q + 1
            else: return q

        goocanvas.Canvas.__init__(self)
        self.set_size_request(100, 500)
        self.table = goocanvas.Table(parent=self.get_root_item(),
                                     homogeneous_columns=True,
                                     homogeneous_rows=True)
        entries = data.keys()
        entries.sort()
        sorted_data = [(entry, data[entry]) for entry in entries]
        i = 0
        for (legend_string, legend_color) in sorted_data:
            col = i % columns
            row = i // columns
            item = self._legend_entry(legend_string, legend_color)
            self.table.set_child_properties(item,
                                            row=row,
                                            column=col,
                                            x_align=0,
                                            x_expand=True,
                                            x_fill=True,
                                            x_shrink=True,
                                            y_expand=True,
                                            y_fill=True,
                                            y_shrink=True,
                                            right_padding=20)
            i += 1
        b = self.table.get_bounds()
        self.set_size_request(int(b.x2), int(b.y2))
示例#2
0
 def _legend_entry(self, string, color):
     """Return a new goocanvas Item that represents a legend item."""
     rgb = [int(c * 65535) for c in color[:3]]
     color = Gtk.gdk.Color(*rgb)
     item = goocanvas.Table(parent=self.table)
     rect = goocanvas.Rect(parent=item,
                           x=0,
                           y=0,
                           width=25,
                           height=25,
                           line_width=1,
                           stroke_color='black',
                           fill_color=color.to_string())
     if string == 'failure':
         rect = goocanvas.Rect(parent=item,
                               x=0,
                               y=8,
                               width=25,
                               height=8,
                               line_width=1,
                               stroke_color='black',
                               fill_color='white')
     item.set_child_properties(rect,
                               column=0,
                               left_padding=10,
                               top_padding=10,
                               bottom_padding=10,
                               right_padding=10)
     text = goocanvas.Text(parent=item,
                           text=string,
                           x=0,
                           y=0,
                           anchor=Gtk.ANCHOR_W,
                           font='arial')
     item.set_child_properties(text, column=1)
     return item