Ticket #4384: 0001-New-IconProgress-widget-SL.patch
File 0001-New-IconProgress-widget-SL.patch, 7.3 KB (added by manuq, 11 years ago) |
---|
-
src/sugar3/graphics/Makefile.am
From ac3d71c97b4f61233c957d228a87bd76888a6e00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= <manuq@laptop.org> Date: Wed, 16 Jan 2013 14:36:24 -0300 Subject: [PATCH toolkit] New IconProgress widget - SL #? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mail-Followup-To: <sugar-devel@lists.sugarlabs.org> A new icon widget useful to display progress. It is compatible with sugar3.graphics.icon.Icon . The progress is represented filling the icon. Signed-off-by: Manuel Quiñones <manuq@laptop.org> --- src/sugar3/graphics/Makefile.am | 1 + src/sugar3/graphics/iconprogress.py | 101 ++++++++++++++++++++++++++++++++++++ tests/graphics/iconprogress.py | 66 +++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 src/sugar3/graphics/iconprogress.py create mode 100644 tests/graphics/iconprogress.py diff --git a/src/sugar3/graphics/Makefile.am b/src/sugar3/graphics/Makefile.am index a4f9629..2c2d8bb 100644
a b sugar_PYTHON = \ 6 6 combobox.py \ 7 7 iconentry.py \ 8 8 icon.py \ 9 iconprogress.py \ 9 10 __init__.py \ 10 11 menuitem.py \ 11 12 notebook.py \ -
new file src/sugar3/graphics/iconprogress.py
diff --git a/src/sugar3/graphics/iconprogress.py b/src/sugar3/graphics/iconprogress.py new file mode 100644 index 0000000..9f1c5a4
- + 1 # Copyright (C) 2013, One Laptop Per Child 2 # 3 # This library is free software; you can redistribute it and/or 4 # modify it under the terms of the GNU Lesser General Public 5 # License as published by the Free Software Foundation; either 6 # version 2 of the License, or (at your option) any later version. 7 # 8 # This library is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 # Lesser General Public License for more details. 12 # 13 # You should have received a copy of the GNU Lesser General Public 14 # License along with this library; if not, write to the 15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 # Boston, MA 02111-1307, USA. 17 18 from gi.repository import Gtk 19 from sugar3.graphics.icon import get_surface 20 from sugar3.graphics import style 21 22 23 class IconProgress(Gtk.DrawingArea): 24 """Display the progress filling the icon. 25 26 This class is compatible with the sugar3.graphics.icon.Icon class. 27 28 Call update(progress) with the new progress to update the icon. 29 30 The direction defaults to 'vertical', in which case the icon is 31 filled from bottom to top. If direction is set to 'horizontal', 32 it will be filled from right to left or from left to right, 33 depending on the system's language RTL setting. 34 35 """ 36 def __init__(self, icon_name, pixel_size, stroke_color, fill_color, 37 direction=None): 38 Gtk.DrawingArea.__init__(self) 39 40 self._icon_name = icon_name 41 if direction == None: 42 direction = 'vertical' 43 self._direction = direction 44 self._progress = 0 45 46 self._stroke = get_surface( 47 icon_name=icon_name, width=pixel_size, height=pixel_size, 48 stroke_color=stroke_color, 49 fill_color=style.COLOR_TRANSPARENT.get_svg()) 50 51 self._fill = get_surface( 52 icon_name=icon_name, width=pixel_size, height=pixel_size, 53 stroke_color=style.COLOR_TRANSPARENT.get_svg(), 54 fill_color=fill_color) 55 56 self.connect("draw", self._draw_cb) 57 58 def _draw_cb(self, widget, cr): 59 allocation = widget.get_allocation() 60 61 # Center the graphic in the allocated space. 62 margin_x = (allocation.width - self._stroke.get_width()) / 2 63 margin_y = (allocation.height - self._stroke.get_height()) / 2 64 cr.translate(margin_x, margin_y) 65 66 # Paint the fill, clipping it by the progress. 67 x_, y_ = 0, 0 68 width, height = self._stroke.get_width(), self._stroke.get_height() 69 if self._direction == 'vertical': # vertical direction, bottom to top 70 y_ = self._stroke.get_height() 71 height *= self._progress * -1 72 else: 73 rtl_direction = \ 74 Gtk.Widget.get_default_direction() == Gtk.TextDirection.RTL 75 if rtl_direction: # horizontal direction, right to left 76 x_ = self._stroke.get_width() 77 width *= self._progress * -1 78 else: # horizontal direction, left to right 79 width *= self._progress 80 81 cr.rectangle(x_, y_, width, height) 82 cr.clip() 83 cr.set_source_surface(self._fill, 0, 0) 84 cr.paint() 85 86 # Paint the stroke over the fill. 87 cr.reset_clip() 88 cr.set_source_surface(self._stroke, 0, 0) 89 cr.paint() 90 91 def do_get_preferred_width(self): 92 width = self._stroke.get_width() 93 return (width, width) 94 95 def do_get_preferred_height(self): 96 height = self._stroke.get_height() 97 return (height, height) 98 99 def update(self, progress): 100 self._progress = progress 101 self.queue_draw() -
new file tests/graphics/iconprogress.py
diff --git a/tests/graphics/iconprogress.py b/tests/graphics/iconprogress.py new file mode 100644 index 0000000..99fe74a
- + 1 # Copyright (C) 2013, One Laptop Per Child 2 # 3 # This library is free software; you can redistribute it and/or 4 # modify it under the terms of the GNU Lesser General Public 5 # License as published by the Free Software Foundation; either 6 # version 2 of the License, or (at your option) any later version. 7 # 8 # This library is distributed in the hope that it will be useful, 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 # Lesser General Public License for more details. 12 # 13 # You should have received a copy of the GNU Lesser General Public 14 # License along with this library; if not, write to the 15 # Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 # Boston, MA 02111-1307, USA. 17 18 """ 19 Test the sugar3.graphics.iconprogress.IconProgress widget. 20 """ 21 22 23 from gi.repository import GObject 24 25 from sugar3.graphics.iconprogress import IconProgress 26 from sugar3.graphics.icon import Icon, get_surface 27 from sugar3.graphics import style 28 29 import common 30 31 test = common.Test() 32 test.show() 33 34 icon = IconProgress( 35 pixel_size=style.LARGE_ICON_SIZE, 36 icon_name='computer-xo', 37 stroke_color=style.COLOR_BUTTON_GREY.get_svg(), 38 fill_color=style.COLOR_WHITE.get_svg()) 39 test.pack_start(icon, True, True, 0) 40 icon.show() 41 42 icon2 = IconProgress( 43 pixel_size=style.LARGE_ICON_SIZE, 44 icon_name='computer-xo', 45 stroke_color=style.COLOR_BUTTON_GREY.get_svg(), 46 fill_color=style.COLOR_WHITE.get_svg(), 47 direction='horizontal') 48 test.pack_start(icon2, True, True, 0) 49 icon2.show() 50 51 progress = 0 52 53 54 def timeout_cb(): 55 global progress 56 progress += 0.05 57 icon.update(progress) 58 icon2.update(progress) 59 if progress >= 1: 60 return False 61 return True 62 63 GObject.timeout_add(50, timeout_cb) 64 65 if __name__ == '__main__': 66 common.main(test)