Blame platform-demos/C/samples/widget_drawing.py

Packit 1470ea
from gi.repository import Gtk
Packit 1470ea
from gi.repository import cairo
Packit 1470ea
import sys
Packit 1470ea
import math
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyWindow(Gtk.ApplicationWindow):
Packit 1470ea
Packit 1470ea
    def __init__(self, app):
Packit 1470ea
        Gtk.Window.__init__(self, title="Choose an angle", application=app)
Packit 1470ea
        self.set_default_size(400, 400)
Packit 1470ea
        self.set_border_width(10)
Packit 1470ea
Packit 1470ea
        # a default angle
Packit 1470ea
        self.angle = 360
Packit 1470ea
Packit 1470ea
        grid = Gtk.Grid()
Packit 1470ea
Packit 1470ea
        # a spinbutton that takes the value of an angle
Packit 1470ea
        ad = Gtk.Adjustment(360, 0, 360, 1, 0, 0)
Packit 1470ea
        self.spin = Gtk.SpinButton(adjustment=ad, climb_rate=1, digits=0)
Packit 1470ea
        self.spin.connect("value-changed", self.get_angle)
Packit 1470ea
Packit 1470ea
        # a drawing area for drawing whatever we want
Packit 1470ea
        self.darea = Gtk.DrawingArea()
Packit 1470ea
        # that we describe in the method draw(), connected to the signal "draw"
Packit 1470ea
        self.darea.connect("draw", self.draw)
Packit 1470ea
        # we have to request a minimum size of the drawing area, or it will
Packit 1470ea
        # disappear
Packit 1470ea
        self.darea.set_size_request(300, 300)
Packit 1470ea
Packit 1470ea
        grid.attach(self.spin, 0, 0, 1, 1)
Packit 1470ea
        grid.attach(self.darea, 0, 1, 1, 1)
Packit 1470ea
Packit 1470ea
        self.add(grid)
Packit 1470ea
Packit 1470ea
    # whenever we get a new angle in the spinbutton
Packit 1470ea
    def get_angle(self, event):
Packit 1470ea
        self.angle = self.spin.get_value_as_int()
Packit 1470ea
        # redraw what is in the drawing area
Packit 1470ea
        self.darea.queue_draw()
Packit 1470ea
Packit 1470ea
    def draw(self, darea, cr):
Packit 1470ea
        # a 10-pixels-wide line
Packit 1470ea
        cr.set_line_width(10)
Packit 1470ea
        # red
Packit 1470ea
        cr.set_source_rgba(0.5, 0.0, 0.0, 1.0)
Packit 1470ea
Packit 1470ea
        # get the width and height of the drawing area
Packit 1470ea
        w = self.darea.get_allocated_width()
Packit 1470ea
        h = self.darea.get_allocated_height()
Packit 1470ea
Packit 1470ea
        # move to the center of the drawing area
Packit 1470ea
        # (translate from the top left corner to w/2, h/2)
Packit 1470ea
        cr.translate(w / 2, h / 2)
Packit 1470ea
        # draw a line to (55, 0)
Packit 1470ea
        cr.line_to(55, 0)
Packit 1470ea
        # and get back to (0, 0)
Packit 1470ea
        cr.line_to(0, 0)
Packit 1470ea
        # draw an arc centered in the origin, 50 pixels wide, from the angle 0
Packit 1470ea
        # (in radians) to the angle given by the spinbutton (in degrees)
Packit 1470ea
        cr.arc(0, 0, 50, 0, self.angle * (math.pi / 180))
Packit 1470ea
        # draw a line back to the origin
Packit 1470ea
        cr.line_to(0, 0)
Packit 1470ea
        # drawing the path, and keeping the path for future use
Packit 1470ea
        cr.stroke_preserve()
Packit 1470ea
Packit 1470ea
        # set a colour
Packit 1470ea
        cr.set_source_rgba(0.0, 0.5, 0.5, 1.0)
Packit 1470ea
        # and use it to fill the path (that we had kept)
Packit 1470ea
        cr.fill()
Packit 1470ea
Packit 1470ea
Packit 1470ea
class MyApplication(Gtk.Application):
Packit 1470ea
Packit 1470ea
    def __init__(self):
Packit 1470ea
        Gtk.Application.__init__(self)
Packit 1470ea
Packit 1470ea
    def do_activate(self):
Packit 1470ea
        win = MyWindow(self)
Packit 1470ea
        win.show_all()
Packit 1470ea
Packit 1470ea
    def do_startup(self):
Packit 1470ea
        Gtk.Application.do_startup(self)
Packit 1470ea
Packit 1470ea
app = MyApplication()
Packit 1470ea
exit_status = app.run(sys.argv)
Packit 1470ea
sys.exit(exit_status)