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
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 | ||
0 | 16 | import math |
1 | 17 | |
2 | 18 | from gi.repository import Gtk |
3 | 19 | |
4 | 20 | from sugar3.graphics import style |
5 | 21 | |
22 | _BORDER_DEFAULT = style.LINE_WIDTH | |
23 | ||
6 | 24 | |
7 | 25 | class RoundBox(Gtk.HBox): |
8 | 26 | __gtype_name__ = 'RoundBox' |
9 | 27 | |
10 | _BORDER_DEFAULT = style.LINE_WIDTH | |
11 | ||
12 | 28 | def __init__(self, **kwargs): |
13 | 29 | Gtk.HBox.__init__(self, **kwargs) |
14 | ||
15 | self._radius = style.zoom(10) | |
16 | self.border = self._BORDER_DEFAULT | |
30 | self._radius = style.zoom(15) | |
17 | 31 | self.border_color = style.COLOR_BLACK |
32 | self.tail = None | |
18 | 33 | 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) | |
22 | 38 | |
23 | 39 | def __add_cb(self, child, params): |
24 | 40 | child.set_border_width(style.zoom(5)) |
25 | 41 | |
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 | ||
32 | 42 | 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 | |
37 | 52 | |
38 | 53 | cr.move_to(x + self._radius, y) |
39 | 54 | cr.arc(x + width - self._radius, y + self._radius, |
40 | 55 | 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) | |
45 | 79 | cr.arc(x + self._radius, y + self._radius, self._radius, |
46 | 80 | math.pi, math.pi * 1.5) |
47 | 81 | cr.close_path() |
48 | 82 | |
49 | 83 | 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() | |
51 | 85 | cr.set_source_rgb(r, g, b) |
52 | 86 | cr.fill_preserve() |
53 | 87 | |
54 | 88 | 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() | |
56 | 90 | cr.set_source_rgb(r, g, b) |
57 | cr.set_line_width(self.border) | |
91 | cr.set_line_width(_BORDER_DEFAULT) | |
58 | 92 | cr.stroke() |
59 | #cr.restore() | |
60 | ||
93 | return False | |
94 | ||
95 | ||
61 | 96 | if __name__ == '__main__': |
62 | 97 | |
63 | 98 | win = Gtk.Window() |
64 | 99 | win.connect('destroy', Gtk.main_quit) |
65 | win.set_default_size(450, 550) | |
100 | win.set_default_size(450, 450) | |
66 | 101 | vbox = Gtk.VBox() |
67 | 102 | |
68 | 103 | box1 = RoundBox() |
104 | box1.tail = 'right' | |
69 | 105 | vbox.add(box1) |
70 | label1 = Gtk.Label(label="Test 1") | |
106 | label1 = Gtk.Label("Test 1") | |
71 | 107 | box1.add(label1) |
72 | 108 | |
73 | 109 | rbox = RoundBox() |
110 | rbox.tail = 'left' | |
74 | 111 | rbox.background_color = style.Color('#FF0000') |
75 | 112 | vbox.add(rbox) |
76 | label2 = Gtk.Label(label="Test 2") | |
113 | label2 = Gtk.Label("Test 2") | |
77 | 114 | rbox.add(label2) |
78 | 115 | |
79 | 116 | bbox = RoundBox() |
81 | 118 | bbox.border_color = style.Color('#ff3300') |
82 | 119 | vbox.add(bbox) |
83 | 120 | |
84 | win.add(vbox) | |
121 | win.add(vbox) | |
85 | 122 | win.show_all() |
86 | 123 | Gtk.main() |