~gtg-user/gtg/ui_mods_rousseau

« back to all changes in this revision

Viewing changes to GTG/gtk/browser/CellRendererTags.py

  • Committer: Bertrand Rousseau
  • Date: 2010-07-30 21:57:43 UTC
  • Revision ID: bertrand.rousseau@gmail.com-20100730215743-fcuokbavapyysfox
Change display of tags in the main task view

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
            "Tag list", "A list of tags", gobject.PARAM_READWRITE),
35
35
        'tag': (gobject.TYPE_PYOBJECT, "Tag",\
36
36
             "Tag", gobject.PARAM_READWRITE),
 
37
        'show_text': (gobject.TYPE_PYOBJECT, 'Show text', \
 
38
                          'Show tag name', gobject.PARAM_READWRITE),
37
39
    }
38
40
 
39
41
    # Private methods
78
80
    def __init__(self): #pylint: disable-msg=W0231
79
81
        self.__gobject_init__()
80
82
        self.tag_list = None
81
 
        self.tag      = None
82
 
        self.xpad     = 1
83
 
        self.ypad     = 1
84
 
        self.PADDING  = 1
 
83
        self.show_text = False
 
84
        self.tag = None
 
85
        self.xpad = 1
 
86
        self.ypad = 1
 
87
        self.PADDING = 1
 
88
        self.HEIGHT = 16
 
89
        self.MINWIDTH = 16
 
90
        self.FONT_SIZE = 8.0
85
91
 
86
92
    def do_set_property(self, pspec, value):
87
93
        if pspec.name == "tag-list":
88
94
            self.tag_list = value
 
95
        elif pspec.name == 'show-text':
 
96
            self.show_text = True
89
97
        else:
90
98
            setattr(self, pspec.name, value)
91
99
 
92
100
    def do_get_property(self, pspec):
93
101
        if pspec.name == "tag-list":
94
102
            return self.tag_list
 
103
        elif pspec.name == "show-text":
 
104
            return self.show_text
95
105
        else:
96
106
            return getattr(self, pspec.name)
97
107
 
114
124
        gdkcontext = gtk.gdk.CairoContext(cr)
115
125
        gdkcontext.set_antialias(cairo.ANTIALIAS_NONE)
116
126
 
117
 
        # Coordinates of the origin point
118
 
        x_align = self.get_property("xalign")
119
 
        y_align = self.get_property("yalign")
120
 
        orig_x  = cell_area.x + int((cell_area.width  - 16*vw_tags -\
121
 
            self.PADDING*2*(vw_tags-1)) * x_align)
122
 
        orig_y  = cell_area.y + int((cell_area.height - 16) * y_align)
 
127
        rect = gtk.gdk.Rectangle()
 
128
        rect.x, rect.y, rect.width, rect.height = \
 
129
         self.on_get_size(widget, cell_area)
 
130
 
 
131
        rect.x += cell_area.x + self.get_property("xpad")
 
132
        rect.y += cell_area.y + self.get_property("ypad")
123
133
 
124
134
        # We draw the icons & squares
 
135
 
125
136
        for my_tag in tags:
126
137
 
127
138
            my_tag_icon  = my_tag.get_attribute("icon")
128
139
            my_tag_color = my_tag.get_attribute("color")
129
 
 
130
 
            rect_x  = orig_x + self.PADDING*2*count + 16*count
131
 
            rect_y  = orig_y
 
140
            my_tag_name  = my_tag.get_attribute("name")[1:]
132
141
 
133
142
            if my_tag_icon:
134
 
 
135
 
#                pixbuf     = gtk.gdk.pixbuf_new_from_file(my_tag_icon)
136
143
                pixbuf = gtk.icon_theme_get_default().load_icon(\
137
 
                    my_tag_icon, 16, 0)
138
 
                gdkcontext.set_source_pixbuf(pixbuf, rect_x, rect_y)
 
144
                    my_tag_icon, self.HEIGHT, 0)
 
145
                gdkcontext.set_source_pixbuf(pixbuf, rect.x, rect.y)
139
146
                gdkcontext.paint()
140
 
                count = count + 1
141
 
 
 
147
                rect.x = rect.x + 2 * self.PADDING + self.MINWIDTH
142
148
            elif my_tag_color:
143
 
 
144
 
                # Draw rounded rectangle
145
 
                my_color = gtk.gdk.color_parse(my_tag_color)
146
 
                gdkcontext.set_source_color(my_color)
147
 
                self.__roundedrec(gdkcontext, rect_x, rect_y, 16, 16, 8)
148
 
                gdkcontext.fill()
149
 
                count = count + 1
150
 
 
151
 
                # Outer line
152
 
                gdkcontext.set_source_rgba(0, 0, 0, 0.20)
153
 
                gdkcontext.set_line_width(1.0)
154
 
                self.__roundedrec(gdkcontext, rect_x, rect_y, 16, 16, 8)
155
 
                gdkcontext.stroke()
 
149
                if self.show_text:
 
150
                    box_padding = 3
 
151
 
 
152
                    # text
 
153
                    gdkcontext.set_font_size(self.FONT_SIZE)
 
154
                    xb, yb_ref, fw, fh_ref = cr.text_extents("peoplemoney")[:4]
 
155
                    x_bearing, y_bearing, f_width, f_height = cr.text_extents(my_tag_name)[:4]
 
156
 
 
157
                    # decide size
 
158
                    height = self.HEIGHT
 
159
                    width = max(f_width, self.MINWIDTH) + 2 * box_padding
 
160
 
 
161
                    # Draw rounded rectangle
 
162
                    my_color = gtk.gdk.color_parse(my_tag_color)
 
163
#                    gdkcontext.set_source_rgba(my_color.red/65535.0, \
 
164
#                                               my_color.green/65535.0, \
 
165
#                                               my_color.blue/65535.0, 0.75)
 
166
                    gdkcontext.set_source_color(my_color)
 
167
                    self.__roundedrec(gdkcontext, rect.x, rect.y, width, height, 6)
 
168
                    gdkcontext.fill()
 
169
 
 
170
#                    # Outer line
 
171
#                    gdkcontext.set_source_rgba(0, 0, 0, 0.20)
 
172
#                    gdkcontext.set_line_width(1.0)
 
173
#                    self.__roundedrec(gdkcontext, rect.x, rect.y, width, height, 8)
 
174
#                    gdkcontext.stroke()
 
175
 
 
176
                    # draw text
 
177
                    b = (my_color.red*299.0 + my_color.green*587 + \
 
178
                         my_color.blue*114) / 65535.0 / 1000.0
 
179
                    if b > 0.5:
 
180
                        c = 0.0
 
181
                    else:
 
182
                        c = 1.0
 
183
                    gdkcontext.set_source_rgba(c, c, c, 1.0)
 
184
                    gdkcontext.move_to(rect.x + width/2 - f_width/2,\
 
185
                                       rect.y + (height - fh_ref)/2 - yb_ref)
 
186
                    gdkcontext.show_text(my_tag_name)
 
187
 
 
188
                    rect.x = rect.x + 2 * self.PADDING + width
 
189
 
 
190
                else:
 
191
                    # decide size
 
192
                    height = self.HEIGHT
 
193
                    width = self.MINWIDTH
 
194
 
 
195
                    # Draw rounded rectangle
 
196
                    my_color = gtk.gdk.color_parse(my_tag_color)
 
197
                    gdkcontext.set_source_color(my_color)
 
198
                    self.__roundedrec(gdkcontext, rect.x, rect.y, width, height, 8)
 
199
                    gdkcontext.fill()
 
200
 
 
201
                    # Outer line
 
202
                    gdkcontext.set_source_rgba(0, 0, 0, 0.20)
 
203
                    gdkcontext.set_line_width(1.0)
 
204
                    self.__roundedrec(gdkcontext, rect.x, rect.y, width, height, 8)
 
205
                    gdkcontext.stroke()
 
206
 
 
207
                    rect.x = rect.x + 2 * self.PADDING + width
156
208
 
157
209
        if self.tag and my_tag: #pylint: disable-msg=W0631
158
210
 
163
215
 
164
216
                # Draw rounded rectangle
165
217
                gdkcontext.set_source_rgba(0.95, 0.95, 0.95, 1)
166
 
                self.__roundedrec(gdkcontext, rect_x, rect_y, 16, 16, 8)
 
218
                self.__roundedrec(gdkcontext, rect.x, rect.y, 16, 16, 8)
167
219
                gdkcontext.fill()
168
220
 
169
221
                # Outer line
170
222
                gdkcontext.set_source_rgba(0, 0, 0, 0.20)
171
223
                gdkcontext.set_line_width(1.0)
172
 
                self.__roundedrec(gdkcontext, rect_x, rect_y, 16, 16, 8)
 
224
                self.__roundedrec(gdkcontext, rect.x, rect.y, 16, 16, 8)
173
225
                gdkcontext.stroke()
174
226
 
175
227
    def on_get_size(self, widget, cell_area=None): #pylint: disable-msg=W0613
176
228
 
 
229
        retval = (0, 0, 0, 0)
 
230
 
 
231
        xpad = self.get_property("xpad")
 
232
        ypad = self.get_property("ypad")
 
233
 
177
234
        count = self.__count_viewable_tags()
178
235
 
179
236
        if count != 0:
180
 
            return (self.xpad, self.ypad, self.xpad*2 + 16*count +\
181
 
                 2*count*self.PADDING, 16 + 2*self.ypad)
182
 
        else:
183
 
            return (self.xpad, self.ypad, self.xpad*2, self.ypad*2)
 
237
            if self.show_text:
 
238
            # it's a tag "rectangle"
 
239
                acc_width = 0
 
240
                window = widget.get_window()
 
241
                if window is not None:
 
242
                    cr = window.cairo_create()
 
243
                else:
 
244
                    # window is not realized yet, but we must create a cairo
 
245
                    # context to compute text extents
 
246
                    surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, 1, 1)
 
247
                    cr = cairo.Context (surface)
 
248
                gdkcontext = gtk.gdk.CairoContext(cr)
 
249
                gdkcontext.set_font_size(self.FONT_SIZE)
 
250
                # Select source
 
251
                if self.tag_list != None:
 
252
                    tags = self.tag_list
 
253
                elif self.tag != None:
 
254
                    tags = [self.tag]
 
255
                # accumulate text widths
 
256
                for my_tag in tags:
 
257
                    my_tag_color = my_tag.get_attribute("color")
 
258
                    if my_tag_color:
 
259
                        my_tag_name  = my_tag.get_attribute("name")[1:]
 
260
                        x_bearing, y_bearing, f_width, f_height = cr.text_extents(my_tag_name)[:4]
 
261
                        # decide size
 
262
                        width = max(int(f_width), self.MINWIDTH) + 2 * self.PADDING + 2 * 3
 
263
                        acc_width = acc_width + width
 
264
            else:
 
265
            # it's a square on an icon
 
266
                acc_width = (16 + 2 * self.PADDING) * count
 
267
           # compute width, height and offsets
 
268
            width = acc_width + 2 * xpad
 
269
            height = self.HEIGHT + 2 * ypad
 
270
            if cell_area:   
 
271
                xoffset = max(self.get_property("xalign") * (cell_area.width - width), 0)
 
272
                yoffset = max(self.get_property("yalign") * (cell_area.height - height), 0)
 
273
            else:
 
274
                xoffset = 0
 
275
                yoffset = 0
 
276
            retval = (xoffset, yoffset, width, height)
 
277
        return retval
184
278
 
185
279
gobject.type_register(CellRendererTags)
186
280