Codebase list sugar-memorize-activity / da54538
Fix flake8 errors and warnings by resynchronising roundbox.py Merge changes to roundbox.py made in other activities; Poll, Speak, Chat, and Words. James Cameron 6 years ago
1 changed file(s) with 68 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
0 # Copyright 2014, Sugar Labs
1 #
2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation; either version 2 of the License, or
5 # (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 # GNU General Public License for more details.
11 #
12 # You should have received a copy of the GNU General Public License
13 # along with this program; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15
016 import math
117
218 from gi.repository import Gtk
319
420 from sugar3.graphics import style
521
22 _BORDER_DEFAULT = style.LINE_WIDTH
23
624
725 class RoundBox(Gtk.HBox):
826 __gtype_name__ = 'RoundBox'
927
10 _BORDER_DEFAULT = style.LINE_WIDTH
11
1228 def __init__(self, **kwargs):
1329 Gtk.HBox.__init__(self, **kwargs)
14
15 self._radius = style.zoom(10)
16 self.border = self._BORDER_DEFAULT
30 self._radius = style.zoom(15)
1731 self.border_color = style.COLOR_BLACK
32 self.tail = None
1833 self.background_color = None
19 self.connect("draw", self.__draw_cb)
20 self.connect("size-allocate", self.__size_allocate_cb)
21 self.connect("add", self.__add_cb)
34 self.set_resize_mode(Gtk.ResizeMode.PARENT)
35 self.set_reallocate_redraws(True)
36 self.connect('draw', self.__draw_cb)
37 self.connect('add', self.__add_cb)
2238
2339 def __add_cb(self, child, params):
2440 child.set_border_width(style.zoom(5))
2541
26 def __size_allocate_cb(self, widget, allocation):
27 self._x = allocation.x
28 self._y = allocation.y
29 self._width = allocation.width
30 self._height = allocation.height
31
3242 def __draw_cb(self, widget, cr):
33 x = self._x + self._BORDER_DEFAULT / 2
34 y = self._y + self._BORDER_DEFAULT / 2
35 width = self._width - self._BORDER_DEFAULT
36 height = self._height - self._BORDER_DEFAULT
43 rect = self.get_allocation()
44 hmargin = style.zoom(15)
45 x = hmargin
46 y = 0
47 width = rect.width - _BORDER_DEFAULT * 2. - hmargin * 2
48 if self.tail is None:
49 height = rect.height - _BORDER_DEFAULT * 2.
50 else:
51 height = rect.height - _BORDER_DEFAULT * 2. - self._radius
3752
3853 cr.move_to(x + self._radius, y)
3954 cr.arc(x + width - self._radius, y + self._radius,
4055 self._radius, math.pi * 1.5, math.pi * 2)
41 cr.arc(x + width - self._radius, y + height - self._radius,
42 self._radius, 0, math.pi * 0.5)
43 cr.arc(x + self._radius, y + height - self._radius,
44 self._radius, math.pi * 0.5, math.pi)
56 tail_height = style.zoom(5)
57 if self.tail == 'right':
58 cr.arc(x + width - self._radius, y + height - self._radius * 2,
59 self._radius, 0, math.pi * 0.5)
60 cr.line_to(x + width - self._radius, y + height)
61 cr.line_to(x + width - tail_height * self._radius,
62 y + height - self._radius)
63 cr.arc(x + self._radius, y + height - self._radius * 2,
64 self._radius, math.pi * 0.5, math.pi)
65 elif self.tail == 'left':
66 cr.arc(x + width - self._radius, y + height - self._radius * 2,
67 self._radius, 0, math.pi * 0.5)
68 cr.line_to(x + self._radius * tail_height,
69 y + height - self._radius)
70 cr.line_to(x + self._radius, y + height)
71 cr.line_to(x + self._radius, y + height - self._radius)
72 cr.arc(x + self._radius, y + height - self._radius * 2,
73 self._radius, math.pi * 0.5, math.pi)
74 else:
75 cr.arc(x + width - self._radius, y + height - self._radius,
76 self._radius, 0, math.pi * 0.5)
77 cr.arc(x + self._radius, y + height - self._radius,
78 self._radius, math.pi * 0.5, math.pi)
4579 cr.arc(x + self._radius, y + self._radius, self._radius,
4680 math.pi, math.pi * 1.5)
4781 cr.close_path()
4882
4983 if self.background_color is not None:
50 r, g, b, a = self.background_color.get_rgba()
84 r, g, b, __ = self.background_color.get_rgba()
5185 cr.set_source_rgb(r, g, b)
5286 cr.fill_preserve()
5387
5488 if self.border_color is not None:
55 r, g, b, a = self.border_color.get_rgba()
89 r, g, b, __ = self.border_color.get_rgba()
5690 cr.set_source_rgb(r, g, b)
57 cr.set_line_width(self.border)
91 cr.set_line_width(_BORDER_DEFAULT)
5892 cr.stroke()
59 #cr.restore()
60
93 return False
94
95
6196 if __name__ == '__main__':
6297
6398 win = Gtk.Window()
6499 win.connect('destroy', Gtk.main_quit)
65 win.set_default_size(450, 550)
100 win.set_default_size(450, 450)
66101 vbox = Gtk.VBox()
67102
68103 box1 = RoundBox()
104 box1.tail = 'right'
69105 vbox.add(box1)
70 label1 = Gtk.Label(label="Test 1")
106 label1 = Gtk.Label("Test 1")
71107 box1.add(label1)
72108
73109 rbox = RoundBox()
110 rbox.tail = 'left'
74111 rbox.background_color = style.Color('#FF0000')
75112 vbox.add(rbox)
76 label2 = Gtk.Label(label="Test 2")
113 label2 = Gtk.Label("Test 2")
77114 rbox.add(label2)
78115
79116 bbox = RoundBox()
81118 bbox.border_color = style.Color('#ff3300')
82119 vbox.add(bbox)
83120
84 win.add(vbox)
121 win.add(vbox)
85122 win.show_all()
86123 Gtk.main()