Write documentation for sugar3.graphics.combobox

This commit is contained in:
Sam Parkinson 2015-11-15 07:00:39 +11:00
parent ec7f723819
commit a334888eb9
2 changed files with 61 additions and 21 deletions

View File

@ -3,27 +3,20 @@ from gi.repository import Gtk
from sugar3.graphics.combobox import ComboBox
def _destroy_cb(widget, data=None):
Gtk.main_quit()
def __combo_changed_cb(widget, data=None):
print 'combo-changed'
def __combo_changed_cb(combo):
print 'Combo changed to %r' % combo.get_value()
w = Gtk.Window()
w.connect("destroy", _destroy_cb)
box = Gtk.VBox()
w.add(box)
w.connect("destroy", Gtk.main_quit)
combo = ComboBox()
combo.append_item(0, 'one')
combo.append_item(1, 'two', 'go-next')
combo.append_item(2, 'three')
combo.append_item(True, 'one')
combo.append_item(2, 'two', 'go-next')
combo.append_item('3', 'three')
# This will make 'two' active (zero indexed)
combo.set_active(1)
combo.connect('changed', __combo_changed_cb)
box.pack_start(combo, False, False, 0)
w.add(combo)
w.show_all()
Gtk.main()

View File

@ -15,9 +15,15 @@
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
"""
STABLE.
"""
'''
The combobox module provides a combo box; a button like widget which
creates a list popup when clicked. It's best used outside of a
toolbar when the user needs to choose from a *short* list of items.
Example:
.. literalinclude:: ../examples/combobox.py
'''
from gi.repository import GObject
from gi.repository import Gtk
@ -25,7 +31,11 @@ from gi.repository import GdkPixbuf
class ComboBox(Gtk.ComboBox):
'''
This class provides a simple wrapper based on the :class:`Gtk.ComboBox`.
This lets you make a list of items, with a value, label and optionally an
icon.
'''
__gtype_name__ = 'SugarComboBox'
def __init__(self):
@ -43,6 +53,13 @@ class ComboBox(Gtk.ComboBox):
self.set_row_separator_func(self._is_separator, None)
def get_value(self):
'''
The value of the currently selected item; the same as the `value`
argument that was passed to the `append_item` func.
Returns:
object, value of selected item
'''
row = self.get_active_item()
if not row:
return None
@ -61,7 +78,22 @@ class ComboBox(Gtk.ComboBox):
del info
return fname
def append_item(self, action_id, text, icon_name=None, file_name=None):
def append_item(self, value, text, icon_name=None, file_name=None):
'''
This function adds another item to the bottom of the combo box list.
If either `icon_name` or `file_name` are supplied and icon column
will be added to the combo box list.
Args:
value (object): the value that will be returned by `get_value`,
when this item is selected
text (str): the user visible label for the item
icon_name (str): the name of the icon in the theme to use for
this item, optional and conflicting with `file_name`
file_name (str): the path to a sugar (svg) icon to use for this
item, optional and conflicting with `icon_name`
'''
if not self._icon_renderer and (icon_name or file_name):
self._icon_renderer = Gtk.CellRendererPixbuf()
@ -93,12 +125,24 @@ class ComboBox(Gtk.ComboBox):
else:
pixbuf = None
self._model.append([action_id, text, pixbuf, False])
self._model.append([value, text, pixbuf, False])
def append_separator(self):
'''
Add a separator to the bottom of the combo box list. The separator
can not be selected.
'''
self._model.append([0, None, None, True])
def get_active_item(self):
'''
Get the row of data for the currently selected item.
Returns:
row of data in the format::
[value, text, pixbuf, is_separator]
'''
index = self.get_active()
if index == -1:
index = 0
@ -109,6 +153,9 @@ class ComboBox(Gtk.ComboBox):
return self._model[row]
def remove_all(self):
'''
Remove all list items from the combo box.
'''
self._model.clear()
def _is_separator(self, model, row, data):