~mmcg069/software-center/star-tweaks

« back to all changes in this revision

Viewing changes to softwarecenter/view/widgets/reviews.py

  • Committer: Matthew McGowan
  • Date: 2011-04-01 21:51:04 UTC
  • Revision ID: matthew.joseph.mcgowan@gmail.com-20110401215104-mx0wxq47214hb6uy
star tweaks

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
    STYLE_BIG         = 1
62
62
    STYLE_INTERACTIVE = 2
63
63
 
 
64
 
64
65
    def __init__(self):
65
66
        self.shape = ShapeStar(5, 0.575)
66
67
        self.theme = get_mkit_theme()
73
74
 
74
75
        self.bg_color = color_floats('#989898')     # gray
75
76
        self.fg_color = color_floats('#FFa000')     # yellow
76
 
 
77
 
        #~ self.star_theme = self._build_theme(self.fg_color)
78
77
        return
79
78
 
80
79
    def _paint_star(self, cr, widget, state, x, y, w, h, alpha=1.0):
85
84
 
86
85
        if self.paint_style == self.STYLE_INTERACTIVE:
87
86
            self._paint_star_interactive(cr, widget, state, x, y, w, h, alpha)
88
 
        else:
 
87
 
 
88
        elif self.paint_style == self.STYLE_BIG:
 
89
            self._paint_star_big(cr, widget, state, x, y, w, h, alpha)
 
90
 
 
91
        elif self.paint_style == self.STYLE_FLAT:
89
92
            self._paint_star_flat(cr, widget, state, x, y, w, h, alpha)
90
93
 
 
94
        else:
 
95
            raise AttributeError, 'paint_style value not recognised!'
 
96
 
91
97
        cr.restore()
92
98
        return
93
99
 
130
136
        self.shape.layout(cr, x, y, w, h)
131
137
        sel_color = color_floats(widget.style.base[gtk.STATE_SELECTED])
132
138
 
133
 
        cr.set_source_rgba(*sel_color+(0.75,))
 
139
        cr.set_source_rgba(*sel_color+(0.65,))
134
140
        cr.set_line_width(4)
135
141
        cr.stroke()
136
142
        cr.restore()
154
160
            cr.stroke()
155
161
        return
156
162
 
157
 
    def _paint_star_interactive(self, cr, widget, state, x, y, w, h, alpha):
158
 
        # XXX: a bit of a mess, but i'm in a rush ...
 
163
    def _paint_star_interactive(self, cr, widget, state, *args):
 
164
        if state == gtk.STATE_ACTIVE:
 
165
            self._paint_star_interactive_in(cr, widget, state, *args)
 
166
        else:
 
167
            self._paint_star_interactive_out(cr, widget, state, *args)
 
168
        return
159
169
 
160
 
        theme = self.theme
 
170
    def _paint_star_interactive_out(self, cr, widget, state, x, y, w, h, alpha):
161
171
        shape = self.shape
162
172
 
163
 
        cr.save()
164
 
        cr.translate(x, y)
 
173
        # define the color palate
 
174
        if self.fill == self.FILL_FULL:
 
175
            grad0 = white = color_floats(widget.style.white)
 
176
            grad1 = dark = color_floats('#B54D00') # brownish
 
177
 
 
178
            if state == gtk.STATE_PRELIGHT:
 
179
                fill = alpha_composite(self.fg_color+(0.75,), (1,1,1))
 
180
 
 
181
            else:
 
182
                fill = self.fg_color
 
183
 
 
184
        else:
 
185
            theme = self.theme
 
186
 
 
187
            grad0, grad1 = theme.gradients[state]
 
188
            grad0 = grad0.floats()
 
189
            grad1 = grad1.floats()
 
190
 
 
191
            white = theme.light_line[state].floats()
 
192
            dark = theme.dark_line[state].floats()
 
193
            fill = grad0
 
194
 
165
195
        cr.set_line_join(cairo.LINE_CAP_ROUND)
166
196
 
167
 
        # bevel
168
197
        if not self.border:
169
 
            shape.layout(cr, 0, 1, w, h)
170
 
            cr.set_source_rgba(*color_floats(widget.style.white)+(0.8,))
 
198
            # paint bevel
 
199
            shape.layout(cr, x, y+1, w, h)
 
200
            cr.set_line_width(2)
 
201
            light = self.theme.light_line[state].floats()
 
202
            cr.set_source_rgba(*light + (0.9,))
171
203
            cr.stroke()
172
204
 
173
 
        shape.layout(cr, 0, 0, w, h)
174
 
        lin = cairo.LinearGradient(0, 0, 0, h)
175
 
 
176
 
        # bg linear vertical gradient
177
 
        if self.fill == self.FILL_FULL:
178
 
 
179
 
            if state == gtk.STATE_PRELIGHT:
180
 
                color1 = alpha_composite(self.fg_color+(0.40,), (1,1,1))
181
 
                color2 = alpha_composite(color1+(0.7,), (1,0,0))
182
 
 
183
 
            elif state == gtk.STATE_ACTIVE:
184
 
                color1 = color2 = alpha_composite(self.fg_color+(0.75,), (1,0,0))
185
 
 
186
 
            else:   # gtk state normal
187
 
                color2 = alpha_composite(self.fg_color+(0.75,), (1,0,0))
188
 
                color1 = alpha_composite(self.fg_color+(0.50,), (1,1,1))
189
 
 
190
 
        else:
191
 
            color1, color2 = theme.gradients[state]
192
 
            color1 = color1.floats()
193
 
            color2 = color2.floats()
194
 
 
195
 
        lin.add_color_stop_rgb(0.0, *color1)
196
 
        lin.add_color_stop_rgb(1.0, *color2)
 
205
        shape.layout(cr, x+1, y+1, w-2, h-2)
 
206
 
 
207
        cr.set_source_rgb(*dark)
 
208
        cr.set_line_width(2)
 
209
        cr.stroke_preserve()
 
210
        cr.stroke()
 
211
 
 
212
        shape.layout(cr, x+1, y+1, w-2, h-2)
 
213
        cr.set_source_rgb(*fill)
 
214
        cr.fill_preserve()
 
215
 
 
216
        lin = cairo.LinearGradient(x, y, x, y+h)
 
217
        lin.add_color_stop_rgba(0.0, *white + (0.5,))
 
218
        lin.add_color_stop_rgba(1.0, *dark + (0.3,))
197
219
 
198
220
        cr.set_source(lin)
199
221
        cr.fill()
200
222
 
201
 
        # highlight
202
 
        shape.layout(cr, 2, 2, w-4, h-4)
203
 
        if state != gtk.STATE_ACTIVE:
204
 
 
205
 
            inline = theme.light_line[state].floats()
206
 
            lin = cairo.LinearGradient(0, 0, 0, h)
207
 
            lin.add_color_stop_rgba(0.0, *inline+(0.7,))
208
 
            lin.add_color_stop_rgba(1.0, *inline+(0.1,))
209
 
            cr.set_source(lin)
210
 
 
211
 
            if self.border == self.BORDER_ON:
212
 
                outline = theme.theme.base[gtk.STATE_SELECTED].floats()
213
 
            else:
214
 
                outline = theme.dark_line[state].floats()
215
 
 
216
 
        else:
217
 
 
218
 
            if self.fill != self.FILL_FULL:
219
 
                inline = outline = theme.dark_line[state].floats()
220
 
 
221
 
            else:
222
 
                # state active, i.e. button is pressed in
223
 
                brown = color_floats('#B54D00')
224
 
                outline = alpha_composite(self.fg_color+(0.1,), brown)
225
 
                inline = alpha_composite(self.fg_color+(0.05,), brown)
226
 
 
227
 
        # inline 1
228
 
        cr.set_line_width(4)
229
 
        cr.set_source_rgba(*inline+(0.2,))
230
 
        cr.stroke_preserve()
231
 
 
232
 
        # inline 2
233
 
        cr.set_line_width(2)
234
 
        cr.set_source_rgba(*inline+(0.3,))
235
 
        cr.stroke()
236
 
 
237
223
        cr.set_line_width(1)
238
224
 
239
 
        # outline
240
 
        shape.layout(cr, 0.5, 0.5, w-1, h-1)
241
 
        cr.set_source_rgb(*outline)
242
 
        cr.stroke()
243
 
 
244
 
        cr.restore()
 
225
        shape.layout(cr, x+1.5, y+1.5, w-3, h-3)
 
226
        lin = cairo.LinearGradient(x, y, x, y+h)
 
227
        lin.add_color_stop_rgba(0.0, *white + (0.6,))
 
228
        lin.add_color_stop_rgba(1.0, *white + (0.15,))
 
229
 
 
230
        cr.set_source(lin)
 
231
        cr.stroke()
 
232
        return
 
233
 
 
234
    def _paint_star_interactive_in(self, cr, widget, state, x, y, w, h, alpha):
 
235
        shape = self.shape
 
236
 
 
237
        # define the color palate
 
238
        #~ black = color_floats(widget.style.black)
 
239
 
 
240
        if self.fill == self.FILL_FULL:
 
241
            white = color_floats(widget.style.white)
 
242
            dark = color_floats('#B54D00') # brownish
 
243
            darker = alpha_composite(dark+(0.65,), (0,0,0))
 
244
            grad0 = grad1 = alpha_composite(self.fg_color+(0.9,), dark)
 
245
 
 
246
        else:
 
247
            theme = self.theme
 
248
            white = theme.light_line[state].floats()
 
249
            dark = darker = theme.dark_line[state].floats()
 
250
            grad0, grad1 = theme.gradients[state]
 
251
            grad0 = grad0.floats()
 
252
            grad1 = grad1.floats()
 
253
 
 
254
        cr.set_line_join(cairo.LINE_CAP_ROUND)
 
255
 
 
256
        shape.layout(cr, x+1, y+1, w-2, h-2)
 
257
        cr.set_source_rgba(*dark+(0.35,))
 
258
 
 
259
        cr.set_line_width(3)
 
260
        cr.stroke_preserve()
 
261
 
 
262
        cr.set_source_rgba(*dark+(0.8,))
 
263
        cr.set_line_width(2)
 
264
        cr.stroke()
 
265
 
 
266
        self.shape.layout(cr, x+1, y+1, w-2, h-2)
 
267
 
 
268
        lin = cairo.LinearGradient(x, y, x, y+h)
 
269
        lin.add_color_stop_rgb(0.0, *grad0)
 
270
        lin.add_color_stop_rgb(1.0, *grad1)
 
271
 
 
272
        cr.set_source(lin)
 
273
        cr.fill()
 
274
 
 
275
        cr.set_line_width(2)
 
276
        self.shape.layout(cr, x+1, y+1, w-2, h-2)
 
277
 
 
278
        lin = cairo.LinearGradient(x, y, x, y+h)
 
279
        lin.add_color_stop_rgba(0.0, *darker+(0.175,))
 
280
        lin.add_color_stop_rgba(1.0, *darker+(0.05,))
 
281
        cr.set_source(lin)
 
282
 
 
283
        #~ cr.set_source_rgba(*darker+(0.1*alpha,))
 
284
        cr.stroke()
 
285
        return
 
286
 
 
287
    def _paint_star_big(self, cr, widget, state, x, y, w, h, alpha):
 
288
        if self.fill == self.FILL_FULL:
 
289
            white = (1,1,1)
 
290
            dark = color_floats('#B54D00') # brownish
 
291
            fill = self.fg_color
 
292
 
 
293
        else:
 
294
            white = color_floats(widget.style.white)
 
295
            dark = self.theme.dark_line[state].floats()
 
296
            grad0, grad1 = self.theme.gradients[state]
 
297
            fill = alpha_composite(grad0.floats()+(0.5,),
 
298
                                   grad1.floats())
 
299
 
 
300
        cr.set_line_join(cairo.LINE_CAP_ROUND)
 
301
        self.shape.layout(cr, x+2, y+2, w-4, h-4)
 
302
 
 
303
        cr.set_source_rgba(*white+(0.2,))
 
304
        cr.set_line_width(8)
 
305
        cr.stroke_preserve()
 
306
 
 
307
        cr.set_source_rgba(*white+(0.5,))
 
308
        cr.set_line_width(6)
 
309
        cr.stroke_preserve()
 
310
 
 
311
        cr.set_source_rgba(*dark+(0.8,))
 
312
        cr.set_line_width(4)
 
313
        cr.stroke_preserve()
 
314
 
 
315
        cr.set_source_rgb(*fill)
 
316
        cr.set_line_width(2)
 
317
        cr.stroke_preserve()
 
318
 
 
319
        cr.fill_preserve()
 
320
 
 
321
        lin = cairo.LinearGradient(x, y, x, y+h)
 
322
        lin.add_color_stop_rgba(0.0, *white+(0.45,))
 
323
        lin.add_color_stop_rgba(1.0, *white+(0.0,))
 
324
        cr.set_source(lin)
 
325
 
 
326
        cr.fill()
245
327
        return
246
328
 
247
329
    def set_paint_style(self, paint_style):
368
450
                          self.n_stars)
369
451
        return
370
452
 
 
453
    def queue_draw(self):
 
454
        a = self.allocation
 
455
        self.queue_draw_area(a.x-5, a.y-5, a.width+10, a.height+10)
 
456
        return
 
457
 
371
458
    def set_max_stars(self, max_stars):
372
459
        self.max_stars = max_stars
373
460
        self._calc_size(max_stars)
474
561
        star.set_state(gtk.STATE_NORMAL)
475
562
        gobject.timeout_add(100, self._hover_check_cb)
476
563
        a = star.allocation
477
 
        star.queue_draw_area(a.x-2, a.y-2, a.width+4, a.height+4)
 
564
        star.queue_draw()
478
565
        return
479
566
 
480
567
    def _on_press(self, star, event):
484
571
 
485
572
        star.set_state(gtk.STATE_ACTIVE)
486
573
        a = star.allocation
487
 
        star.queue_draw_area(a.x-2, a.y-2, a.width+4, a.height+4)
 
574
        star.queue_draw()
488
575
        return
489
576
 
490
577
    def _on_release(self, star, event):
491
578
        self.set_rating(star.position+1)
492
579
        star.set_state(gtk.STATE_PRELIGHT)
493
580
        a = star.allocation
494
 
        star.queue_draw_area(a.x-2, a.y-2, a.width+4, a.height+4)
 
581
        star.queue_draw()
495
582
        return
496
583
 
497
584
    def _on_focus_in(self, star, event):
528
615
            self.set_rating(star.position+1)
529
616
            star.set_state(gtk.STATE_NORMAL)
530
617
            a = star.allocation
531
 
            star.queue_draw_area(a.x-2, a.y-2, a.width+4, a.height+4)
 
618
            star.queue_draw()
532
619
        return
533
620
 
534
621
    def _connect_signals(self, star):
550
637
                self.caption.set_markup(self.RATING_WORDS[self.rating])
551
638
        return
552
639
 
 
640
    def queue_draw(self):
 
641
        a = self.allocation
 
642
        self.queue_draw_area(a.x-5, a.y-5, a.width+10, a.height+10)
 
643
        return
 
644
 
553
645
    def set_caption_widget(self, caption_widget):
554
646
        caption_widget.set_markup(self.RATING_WORDS[0])
555
647
        self.caption = caption_widget
562
654
            else:
563
655
                star.border = StarPainter.BORDER_OFF
564
656
        a = self.allocation
565
 
        self.queue_draw_area(a.x-2, a.y-2, a.width+4, a.height+4)
 
657
        self.queue_draw()
566
658
        return
567
659
 
568
660
 
591
683
 
592
684
class ReviewStatsContainer(gtk.VBox):
593
685
 
 
686
    SIZE = (2*EM, 2*EM)
 
687
 
594
688
    def __init__(self):
595
689
        gtk.VBox.__init__(self, spacing=4)
596
 
        self.star_rating = StarRating(star_size=(3*EM,3*EM))
597
 
        #~ self.star_rating.set_shadow_type(gtk.SHADOW_ETCHED_OUT)
598
 
 
 
690
        self.star_rating = StarRating(star_size=self.SIZE)
 
691
        self.star_rating.set_paint_style(StarPainter.STYLE_BIG)
599
692
        self.label = gtk.Label()
600
693
        self.pack_start(self.star_rating, False)
601
694
        self.pack_start(self.label, False, False)
 
695
 
602
696
    def set_avg_rating(self, avg_rating):
603
697
        self.star_rating.set_rating(avg_rating)
 
698
 
604
699
    def set_nr_reviews(self, nr_reviews):
605
700
        self.nr_reviews = nr_reviews
606
701
        self._update_nr_reviews()
 
702
 
607
703
    # internal stuff
608
704
    def _update_nr_reviews(self):
609
705
        s = gettext.ngettext(
1212
1308
 
1213
1309
 
1214
1310
if __name__ == "__main__":
1215
 
    w = StarRatingSelector(star_size=(6*EM,6*EM))
 
1311
    w = StarRatingSelector()
1216
1312
    #~ w.set_avg_rating(3.5)
1217
1313
    #~ w.set_nr_reviews(101)
1218
1314