Beispiel #1
0
    def expose(self, widget, event):
        if self.is_sensitive():
            alpha = 1
        else:
            alpha = 0.3

        w = self.get_allocated_width()
        h = self.get_allocated_height()

        cr = widget.get_property('window').cairo_create()

        def set_color(c):
            return cr.set_source_rgba(c.red_float, c.green_float, c.blue_float,
                                      alpha)

        if (self.use_bitmaps == False):
            linewidth = 4
            x = y = linewidth / 2
            w2 = w - linewidth
            h2 = h - linewidth
            x_center = x + w2 / 2
            y_center = y + h2 / 2
            degrees = math.pi / 180.0

            cr.new_sub_path()
            cr.arc(x + w2 - self.corner_radius, y + self.corner_radius,
                   self.corner_radius, -90 * degrees, 0 * degrees)
            cr.arc(x + w2 - self.corner_radius, y + h2 - self.corner_radius,
                   self.corner_radius, 0 * degrees, 90 * degrees)
            cr.arc(x + self.corner_radius, y + h2 - self.corner_radius,
                   self.corner_radius, 90 * degrees, 180 * degrees)
            cr.arc(x + self.corner_radius, y + self.corner_radius,
                   self.corner_radius, 180 * degrees, 270 * degrees)
            cr.close_path()

            if (self.light_is_on):
                color = self.light_on_color
                if self.mouseover:
                    color = Gdk.color_from_hsv(color.hue,
                                               color.saturation * .5,
                                               color.value * 1.5)
                color2 = Gdk.color_from_hsv(color.hue, color.saturation * .25,
                                            color.value)
                linecolor = Gdk.color_from_hsv(color.hue, color.saturation,
                                               color.value * .95)
            else:
                color = self.light_off_color
                if self.mouseover:
                    color = Gdk.color_from_hsv(color.hue,
                                               color.saturation * .5,
                                               color.value * 1.5)
                if (self.dual_color):
                    color2 = Gdk.color_from_hsv(color.hue,
                                                color.saturation * .25,
                                                color.value)
                    linecolor = Gdk.color_from_hsv(color.hue, color.saturation,
                                                   color.value * .95)
                else:
                    color1 = Gdk.color_from_hsv(color.hue,
                                                color.saturation * .50,
                                                color.value * 2)
                    color2 = Gdk.color_from_hsv(color.hue, color.saturation,
                                                color.value * .50)
                    linecolor = Gdk.color_from_hsv(color.hue,
                                                   color.saturation * .50,
                                                   color.value * .75)

            cr.set_line_width(linewidth)
            set_color(linecolor)
            cr.stroke_preserve()

            if (self.light_is_on == True) or (self.dual_color == True):
                gradient_radius = (w2 + h2)
                g1 = cairo.RadialGradient(x_center, y_center, 0, x_center,
                                          y_center, gradient_radius)
                g1.add_color_stop_rgb(0.0, color2.red_float,
                                      color2.green_float, color2.blue_float)
                g1.add_color_stop_rgb(1.0, color.red_float, color.green_float,
                                      color.blue_float)
            else:
                g1 = cairo.LinearGradient(x, y, x, y + h2)
                g1.add_color_stop_rgb(0.0, color1.red_float,
                                      color1.green_float, color1.blue_float)
                g1.add_color_stop_rgb(0.0, color.red_float, color.green_float,
                                      color.blue_float)
                g1.add_color_stop_rgb(1.0, color2.red_float,
                                      color2.green_float, color2.blue_float)
            cr.set_source(g1)
            cr.fill()

        #Using bitmaps is not implemented. Bitmaps should not be scaled unless you figure out a way to make them scale nicely
        #The main reason to use bitmaps would be if someone wants a different look from the default one
        #The code below is the basic way to do it.  update_widget_size() should use the button size instead of text size
        #and there should be properties to set image filenames for each state (light on, light off, mouseover, etc)
        else:
            cr.save()
            image = cairo.ImageSurface.create_from_png('resources/k_green.png')
            img_w = image.get_width()
            img_h = image.get_height()
            print(float(w) / img_w, float(h) / img_h)
            cr.set_source_surface(image, 0, 0)
            cr.paint()
            cr.restore()

        # write text
        _layout = self.default_pangolayout
        if (self.light_is_on):
            set_color(self.font_on_color)
            if (not self.button_on_text == ""):
                _layout = self.on_pangolayout
        else:
            set_color(self.font_off_color)

        fontw, fonth = _layout.get_pixel_size()
        cr.move_to((w - fontw) / 2, (h - fonth) / 2)
        cr.update_layout(_layout)
        cr.show_layout(_layout)
        return False