Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Workshop.activity/Rating.py
blob: a13e5a2faa82409761685c1a06201ffd2156289a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import gtk
from gtk import gdk
import logging

class Rating(gtk.Widget):
    """
    Controls that display the rating of a tutorial using colored stars
    """
    def __init__(self,tutorial,controller, rating=0,editable = False):
        """
        Constructor
        
        @param the controller to link the view with
        @param tutorial The tutorial for which this rating is
        @param rating The rating to show
        @param editable True if the rating may be edited
        """
        gtk.Widget.__init__(self)
        
        self.tutorial = tutorial
        self.controller = controller
        self.editable = editable
        self.rating = rating
        
        #star size is 24 pixels by 24 pixels
        self.image_length = 24
        
    def do_realize(self):
        self.set_flags(self.flags() | gtk.REALIZED)
        
        self.window = gtk.gdk.Window(
            self.get_parent_window(),
            width=self.allocation.width,
            height=self.allocation.height,
            window_type=gdk.WINDOW_CHILD,
            wclass=gdk.INPUT_OUTPUT,
            event_mask=self.get_events() | gtk.gdk.EXPOSURE_MASK
                | gtk.gdk.BUTTON_PRESS_MASK)
        
        self.window.set_user_data(self)
        
        self.style.attach(self.window)
        
        self.style.set_background(self.window, gtk.STATE_NORMAL)
        self.window.move_resize(*self.allocation)
        
        #load the stars
        pixbuf =  gtk.gdk.pixbuf_new_from_file('full_star.png')
        self.full_star,mask = pixbuf.render_pixmap_and_mask()
        
        pixbuf =  gtk.gdk.pixbuf_new_from_file('half_star.png')
        self.half_star,mask = pixbuf.render_pixmap_and_mask()
        
        image = gtk.Image()
        pixbuf =  gtk.gdk.pixbuf_new_from_file('grayed_star.png')
        self.empty_star,mask =pixbuf.render_pixmap_and_mask()
            
        self.gc = self.style.fg_gc[gtk.STATE_NORMAL]
        
    def do_unrealize(self):
        self.window.destroy()
    
    def do_size_request(self, requisition):
        requisition.height = self.image_length
        requisition.width = (self.image_length * 5)

    def do_size_allocate(self, allocation):
        self.allocation = allocation
        if self.flags() & gtk.REALIZED:
            self.window.move_resize(*allocation)
            
    def do_expose_event(self, event):
        """
        The widget is drawn here
        """
        value = self.rating
        stars = [0,0,0,0,0]
        if value > 0:
            for x in range(5):
                if value -1 > 0:
                    stars[x]=1
                elif value -1 == -0.5:
                    stars[x] = 0.5
                    break
                else:
                    stars[x]=1
                    break
                value -= 1
                
        for x in range(0,5):
            if stars[x] == 0:
                self.window.draw_drawable(self.gc, self.empty_star, 0, 0
                                                , x*self.image_length
                                                , 0,-1, -1)
            elif stars[x] == 0.5:
                self.window.draw_drawable(self.gc, self.half_star, 0, 0
                                                , x*self.image_length
                                                , 0,-1, -1)
            elif stars[x] == 1:
                self.window.draw_drawable(self.gc, self.full_star, 0, 0
                                                , x*self.image_length
                                                , 0,-1, -1)
            
    def do_button_press_event(self, event):
        """When the button is pressed"""
        
        # make sure it was the first button
        if self.editable:
            if event.button == 1:
                #check for new stars
                self.check_for_new_stars(event.x)
            
        return True
    
    def check_for_new_stars(self, xPos):
        """
        Computes the star number based on where the click was
        """
        
        new_stars = int(xPos / self.image_length)
        half_star = xPos % self.image_length
        
        logging.info("xpos: %d, new_stars: %d, half_star: %d",xPos,new_stars,half_star)
        if half_star > self.image_length/2:
            new_stars +=1
        else:
            new_stars = new_stars+0.5
        logging.info("rating: %f",new_stars)
        self.controller.rate_tutorial(self.tutorial,new_stars)
        
        self.set_value(new_stars)
        
    def set_value(self, value):
        """
        Sets the value and force a redraw
        """
        
        if (value >= 0):
            self.rating = value
            #check for the maximum
            if (self.rating > 5):
                self.rating = 5
            # redraw the widget
            self.queue_draw()