~kelemeng/software-center/bug953812

« back to all changes in this revision

Viewing changes to softwarecenter/ui/gtk3/widgets/cellrenderers.py

  • Committer: Anthony Lenton
  • Date: 2012-03-12 12:43:52 UTC
  • mto: This revision was merged to the branch mainline in revision 2844.
  • Revision ID: anthony.lenton@canonical.com-20120312124352-nib8p2kkvvnaqea0
Made the pep8 test pass on three more files in softwarecenter/ui/gtk3/widgets/

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
    # size of the install overlay icon
41
41
    OVERLAY_SIZE = 16
42
 
    
 
42
 
43
43
    # ratings
44
44
    MAX_STARS = 5
45
45
    STAR_SIZE = EM
46
46
 
47
47
    __gproperties__ = {
48
 
        'application' : (GObject.TYPE_PYOBJECT, 'document',
49
 
                         'a xapian document containing pkg information',
50
 
                         GObject.PARAM_READWRITE),
51
 
 
52
 
        'isactive'    : (bool, 'isactive', 'is cell active/selected', False,
53
 
                         GObject.PARAM_READWRITE),
54
 
                     }
55
 
 
 
48
        'application': (GObject.TYPE_PYOBJECT, 'document',
 
49
                       'a xapian document containing pkg information',
 
50
                       GObject.PARAM_READWRITE),
 
51
 
 
52
        'isactive': (bool, 'isactive', 'is cell active/selected', False,
 
53
                    GObject.PARAM_READWRITE),
 
54
                    }
56
55
 
57
56
    def __init__(self, icons, layout, show_ratings, overlay_icon_name):
58
57
        GObject.GObject.__init__(self)
59
 
        
 
58
 
60
59
        # the icon pixbuf to be displayed in the row
61
60
        self.icon = None
62
61
 
70
69
 
71
70
        # button packing
72
71
        self.button_spacing = 0
73
 
        self._buttons = {Gtk.PackType.START: [],
74
 
                         Gtk.PackType.END:   []}
 
72
        self._buttons = {
 
73
            Gtk.PackType.START: [],
 
74
            Gtk.PackType.END: []
 
75
        }
75
76
        self._all_buttons = {}
76
77
 
77
78
        # cache a layout
88
89
            # icon not present in theme, probably because running uninstalled
89
90
            self._installed = icons.load_icon('emblem-system',
90
91
                                              self.OVERLAY_SIZE, 0)
91
 
        return
92
92
 
93
93
    def _layout_get_pixel_width(self, layout):
94
94
        return layout.get_size()[0] / Pango.SCALE
109
109
            x = cell_area.x
110
110
        else:
111
111
            x = cell_area.x + cell_area.width - lw
112
 
        y = cell_area.y + (cell_area.height - lh)/2
 
112
        y = cell_area.y + (cell_area.height - lh) / 2
113
113
 
114
114
        Gtk.render_layout(context, cr, x, y, layout)
115
 
        return
116
115
 
117
 
    def _render_price(self, context, cr, app, layout, cell_area, xpad, ypad, is_rtl):
 
116
    def _render_price(self, context, cr, app, layout, cell_area, xpad, ypad,
 
117
        is_rtl):
118
118
        layout.set_markup("US$ %s" % self.model.get_price(app), -1)
119
119
 
120
120
        if is_rtl:
125
125
 
126
126
        Gtk.render_layout(context, cr,
127
127
                          x, ypad + cell_area.y, layout)
128
 
        return
129
128
 
130
129
    def _render_icon(self, cr, app, cell_area, xpad, ypad, is_rtl):
131
130
        # calc offsets so icon is nicely centered
132
131
        self.icon = self.model.get_icon(app)
133
132
        self.icon_x_offset = xpad + cell_area.x
134
133
        self.icon_y_offset = ypad + cell_area.y
135
 
        xo = (self.pixbuf_width - self.icon.get_width())/2
136
 
        
 
134
        xo = (self.pixbuf_width - self.icon.get_width()) / 2
 
135
 
137
136
        if not is_rtl:
138
137
            x = cell_area.x + xo + xpad
139
138
        else:
143
142
        # draw appicon pixbuf
144
143
        Gdk.cairo_set_source_pixbuf(cr, self.icon, x, y)
145
144
        cr.paint()
146
 
        
 
145
 
147
146
        # draw overlay if application is installed
148
147
        if self.model.is_installed(app):
149
148
            if not is_rtl:
153
152
            y += (self.pixbuf_width - self.OVERLAY_SIZE + self.OVERLAY_YO)
154
153
            Gdk.cairo_set_source_pixbuf(cr, self._installed, x, y)
155
154
            cr.paint()
156
 
        return
157
155
 
158
156
    def _render_summary(self, context, cr, app,
159
157
                        cell_area, layout, xpad, ypad,
165
163
        layout.set_width(-1)
166
164
        lw = self._layout_get_pixel_width(layout)
167
165
        max_layout_width = (cell_area.width - self.pixbuf_width -
168
 
                            3*xpad - star_width)
169
 
                            
170
 
        max_layout_width = cell_area.width - self.pixbuf_width - 3*xpad
171
 
        
 
166
                            3 * xpad - star_width)
 
167
 
 
168
        max_layout_width = cell_area.width - self.pixbuf_width - 3 * xpad
 
169
 
172
170
        stats = self.model.get_review_stats(app)
173
171
        if self.show_ratings and stats:
174
 
            max_layout_width -= star_width+6*xpad
175
 
            
176
 
        if self.props.isactive and self.model.get_transaction_progress(app) > 0:
 
172
            max_layout_width -= star_width + 6 * xpad
 
173
 
 
174
        if (self.props.isactive and
 
175
            self.model.get_transaction_progress(app) > 0):
177
176
            action_btn = self.get_button_by_name(CellButtonIDs.ACTION)
178
 
            max_layout_width -= (xpad + action_btn.width) 
 
177
            max_layout_width -= (xpad + action_btn.width)
179
178
 
180
179
        if lw >= max_layout_width:
181
 
            layout.set_width((max_layout_width)*Pango.SCALE)
 
180
            layout.set_width((max_layout_width) * Pango.SCALE)
182
181
            layout.set_ellipsize(Pango.EllipsizeMode.MIDDLE)
183
182
            lw = max_layout_width
184
183
 
187
186
        self.apptitle_height = apptitle_extents.height
188
187
 
189
188
        if not is_rtl:
190
 
            x = cell_area.x+2*xpad+self.pixbuf_width
 
189
            x = cell_area.x + 2 * xpad + self.pixbuf_width
191
190
        else:
192
 
            x = cell_area.x+cell_area.width-lw-self.pixbuf_width-2*xpad
 
191
            x = (cell_area.x + cell_area.width - lw - self.pixbuf_width -
 
192
                2 * xpad)
193
193
 
194
 
        y = cell_area.y+ypad
 
194
        y = cell_area.y + ypad
195
195
 
196
196
        Gtk.render_layout(context, cr, x, y, layout)
197
 
        return
198
197
 
199
198
    def _render_rating(self, context, cr, app,
200
199
                       cell_area, layout, xpad, ypad,
201
200
                       star_width, star_height, is_rtl):
202
201
 
203
202
        stats = self.model.get_review_stats(app)
204
 
        if not stats: return
 
203
        if not stats:
 
204
            return
205
205
 
206
206
        sr = self._stars
207
207
 
212
212
            x = (cell_area.x + cell_area.width
213
213
                 - 3 * xpad
214
214
                 - self.pixbuf_width
215
 
                 - self.apptitle_width 
 
215
                 - self.apptitle_width
216
216
                 - star_width)
217
217
 
218
 
        y = cell_area.y + ypad + (self.apptitle_height-self.STAR_SIZE)/2
 
218
        y = cell_area.y + ypad + (self.apptitle_height - self.STAR_SIZE) / 2
219
219
 
220
220
        sr.rating = stats.ratings_average
221
221
        sr.render_star(context, cr, x, y)
222
 
        
 
222
 
223
223
        # and nr-reviews in parenthesis to the right of the title
224
224
        nreviews = stats.ratings_total
225
225
        s = "(%i)" % nreviews
227
227
        layout.set_markup("<small>%s</small>" % s, -1)
228
228
 
229
229
        if not is_rtl:
230
 
            x += xpad+star_width
 
230
            x += xpad + star_width
231
231
        else:
232
 
            x -= xpad+self._layout_get_pixel_width(layout)
 
232
            x -= xpad + self._layout_get_pixel_width(layout)
233
233
 
234
234
        context.save()
235
235
        context.add_class("cellrenderer-avgrating-label")
236
236
        Gtk.render_layout(context, cr, x, y, layout)
237
237
        context.restore()
238
 
        return
239
238
 
240
239
    def _render_progress(self, context, cr, progress, cell_area, ypad, is_rtl):
241
240
        percent = progress * 0.01
242
 
        # per the spec, the progressbar should be the width of the action button
 
241
        # per the spec, the progressbar should be the width of the action
 
242
        # button
243
243
        action_btn = self.get_button_by_name(CellButtonIDs.ACTION)
244
244
 
245
245
        x, _, w, h = action_btn.allocation
252
252
        Gtk.render_background(context, cr, x, y, w, h)
253
253
        Gtk.render_frame(context, cr, x, y, w, h)
254
254
 
255
 
        context.restore ()
 
255
        context.restore()
256
256
 
257
257
        bar_size = w * percent
258
258
 
259
 
        context.save ()
260
 
        context.add_class ("progressbar")
 
259
        context.save()
 
260
        context.add_class("progressbar")
261
261
 
262
262
        if (bar_size > 0):
263
263
            if is_rtl:
264
264
                x += (w - bar_size)
265
265
            Gtk.render_activity(context, cr, x, y, bar_size, h)
266
266
 
267
 
        context.restore ()
268
 
        return
 
267
        context.restore()
269
268
 
270
 
    def _render_buttons(
271
 
            self, context, cr, cell_area, layout, xpad, ypad, is_rtl):
 
269
    def _render_buttons(self, context, cr, cell_area, layout, xpad, ypad,
 
270
        is_rtl):
272
271
 
273
272
        # layout buttons and paint
274
 
        y = cell_area.y+cell_area.height-ypad
 
273
        y = cell_area.y + cell_area.height - ypad
275
274
        spacing = self.button_spacing
276
275
 
277
276
        if not is_rtl:
286
285
            xb = cell_area.x + cell_area.width - 2 * xpad - self.pixbuf_width
287
286
 
288
287
        for btn in self._buttons[start]:
289
 
            btn.set_position(xs, y-btn.height)
 
288
            btn.set_position(xs, y - btn.height)
290
289
            btn.render(context, cr, layout)
291
290
            xs += btn.width + spacing
292
291
 
293
292
        for btn in self._buttons[end]:
294
293
            xb -= btn.width
295
 
            btn.set_position(xb, y-btn.height)
 
294
            btn.set_position(xb, y - btn.height)
296
295
            btn.render(context, cr, layout)
297
296
 
298
297
            xb -= spacing
299
 
        return
300
298
 
301
299
    def set_pixbuf_width(self, w):
302
300
        self.pixbuf_width = w
303
 
        return
304
301
 
305
302
    def set_button_spacing(self, spacing):
306
303
        self.button_spacing = spacing
307
 
        return
308
304
 
309
305
    def get_button_by_name(self, name):
310
306
        if name in self._all_buttons:
311
307
            return self._all_buttons[name]
312
 
        return None
313
308
 
314
309
    def get_buttons(self):
315
310
        btns = ()
320
315
    def button_pack(self, btn, pack_type=Gtk.PackType.START):
321
316
        self._buttons[pack_type].append(btn)
322
317
        self._all_buttons[btn.name] = btn
323
 
        return
324
318
 
325
319
    def button_pack_start(self, btn):
326
320
        self.button_pack(btn, Gtk.PackType.START)
327
 
        return
328
321
 
329
322
    def button_pack_end(self, btn):
330
323
        self.button_pack(btn, Gtk.PackType.END)
331
 
        return
332
324
 
333
325
    def do_set_property(self, pspec, value):
334
326
        setattr(self, pspec.name, value)
345
337
 
346
338
    def do_render(self, cr, widget, bg_area, cell_area, flags):
347
339
        app = self.props.application
348
 
        if not app: return
 
340
        if not app:
 
341
            return
349
342
 
350
343
        self.model = widget.appmodel
351
344
 
356
349
        is_rtl = widget.get_direction() == Gtk.TextDirection.RTL
357
350
        layout = self._layout
358
351
 
359
 
        # important! ensures correct text rendering, esp. when using hicolor theme
 
352
        # important! ensures correct text rendering, esp. when using hicolor
 
353
        # theme
360
354
        #~ if (flags & Gtk.CellRendererState.SELECTED) != 0:
361
 
            #~ # this follows the behaviour that gtk+ uses for states in treeviews
 
355
            #~ # this follows the behaviour that gtk+ uses for states in
 
356
            #~ # treeviews
362
357
            #~ if widget.has_focus():
363
358
                #~ state = Gtk.StateFlags.SELECTED
364
359
            #~ else:
388
383
                             xpad, ypad,
389
384
                             star_width,
390
385
                             is_rtl)
391
 
                             
 
386
 
392
387
        # only show ratings if we have one
393
388
        if self.show_ratings:
394
389
            self._render_rating(context, cr, app,
421
416
                             is_rtl)
422
417
 
423
418
        context.restore()
424
 
        return
425
419
 
426
420
 
427
421
class CellButtonRenderer(object):
441
435
        self.visible = True
442
436
 
443
437
        self.widget = widget
444
 
        return
445
438
 
446
439
    def _layout_reset(self, layout):
447
440
        layout.set_width(-1)
448
441
        layout.set_ellipsize(Pango.EllipsizeMode.NONE)
449
 
        return
450
 
 
451
 
    @property
452
 
    def x(self): return self.allocation[0]
453
 
 
454
 
    @property
455
 
    def y(self): return self.allocation[1]
456
 
 
457
 
    @property
458
 
    def width(self): return self.allocation[2]
459
 
 
460
 
    @property
461
 
    def height(self): return self.allocation[3]
 
442
 
 
443
    @property
 
444
    def x(self):
 
445
        return self.allocation[0]
 
446
 
 
447
    @property
 
448
    def y(self):
 
449
        return self.allocation[1]
 
450
 
 
451
    @property
 
452
    def width(self):
 
453
        return self.allocation[2]
 
454
 
 
455
    @property
 
456
    def height(self):
 
457
        return self.allocation[3]
462
458
 
463
459
    def configure_geometry(self, layout):
464
460
        self._layout_reset(layout)
465
 
        max_size = (0,0)
 
461
        max_size = (0, 0)
466
462
 
467
463
        for k, variant in self.markup_variants.items():
468
464
            safe_markup = GObject.markup_escape_text(utf8(variant))
474
470
        w /= Pango.SCALE
475
471
        h /= Pango.SCALE
476
472
 
477
 
        self.set_size(w+2*self.xpad, h+2*self.ypad)
478
 
        return
 
473
        self.set_size(w + 2 * self.xpad, h + 2 * self.ypad)
479
474
 
480
475
    def point_in(self, px, py):
481
476
        x, y, w, h = self.allocation
487
482
 
488
483
    def set_position(self, x, y):
489
484
        self.allocation[:2] = int(x), int(y)
490
 
        return
491
485
 
492
486
    def set_size(self, w, h):
493
487
        self.allocation[2:] = int(w), int(h)
494
 
        return
495
488
 
496
489
    def set_state(self, state):
497
490
        if not isinstance(state, Gtk.StateFlags):
498
 
            msg = "state should be of type Gtk.StateFlags, got %s" % type(state)
 
491
            msg = ("state should be of type Gtk.StateFlags, got %s" %
 
492
                type(state))
499
493
            raise TypeError(msg)
500
494
 
501
 
        elif state == self.state: return
 
495
        elif state == self.state:
 
496
            return
502
497
 
503
498
        self.state = state
504
499
        self.widget.queue_draw_area(*self.allocation)
505
 
        return
506
500
 
507
501
    def set_sensitive(self, is_sensitive):
508
502
        if is_sensitive:
510
504
        else:
511
505
            state = Gtk.StateFlags.INSENSITIVE
512
506
        self.set_state(state)
513
 
        return
514
507
 
515
508
    def show(self):
516
509
        self.visible = True
520
513
 
521
514
    def set_markup(self, markup):
522
515
        self.markup_variant = (markup,)
523
 
        return
524
516
 
525
517
    def set_markup_variants(self, markup_variants):
526
518
        if not isinstance(markup_variants, dict):
527
519
            msg = type(markup_variants)
528
520
            raise TypeError("Expects a dict object, got %s" % msg)
529
521
 
530
 
        elif not markup_variants: return
 
522
        elif not markup_variants:
 
523
            return
531
524
 
532
525
        self.markup_variants = markup_variants
533
526
        self.current_variant = markup_variants.keys()[0]
534
 
        return
535
527
 
536
528
    def set_variant(self, current_var):
537
529
        self.current_variant = current_var
538
 
        return
539
530
 
540
531
    def is_sensitive(self):
541
532
        return self.state == Gtk.StateFlags.INSENSITIVE
576
567
        context.restore()
577
568
 
578
569
        context.restore()
579
 
        return