2014-01-27 03:15:50 +04:00
#
# Copyright (C) 2009, 2013, 2014 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#
from gi . repository import GObject
from gi . repository import Gtk
2015-05-20 01:07:19 +03:00
#####################
# UI getter helpers #
#####################
2014-01-27 03:15:50 +04:00
def spin_get_helper ( widget ) :
2014-01-27 05:21:12 +04:00
"""
Safely get spin button contents , converting to int if possible
"""
2014-01-27 03:15:50 +04:00
adj = widget . get_adjustment ( )
txt = widget . get_text ( )
try :
return int ( txt )
except :
return adj . get_value ( )
2015-05-20 00:17:53 +03:00
def get_list_selected_row ( widget , check_visible = False ) :
2014-01-27 05:21:12 +04:00
"""
2015-05-20 00:17:53 +03:00
Helper to simplify getting the selected row in a list / tree / combo
2014-01-27 05:21:12 +04:00
"""
if check_visible and not widget . get_visible ( ) :
return None
if hasattr ( widget , " get_selection " ) :
selection = widget . get_selection ( )
model , treeiter = selection . get_selected ( )
if treeiter is None :
return None
row = model [ treeiter ]
else :
idx = widget . get_active ( )
if idx == - 1 :
return None
row = widget . get_model ( ) [ idx ]
2014-01-27 03:15:50 +04:00
2015-05-20 00:17:53 +03:00
return row
2015-06-21 22:47:31 +03:00
def get_list_selection ( widget , column = 0 ,
check_visible = False , check_entry = True ) :
2015-05-20 00:17:53 +03:00
"""
2015-05-20 00:54:53 +03:00
Helper to simplify getting the selected row and value in a list / tree / combo .
If nothing is selected , and the widget is a combo box with a text entry ,
return the value of that .
2015-06-21 22:47:31 +03:00
: param check_entry : If True , attempt to check the widget ' s text entry
using the logic described above .
2015-05-20 00:17:53 +03:00
"""
row = get_list_selected_row ( widget , check_visible = check_visible )
2015-05-20 00:54:53 +03:00
if row is not None :
return row [ column ]
2015-06-21 22:47:31 +03:00
if check_entry and hasattr ( widget , " get_has_entry " ) :
2015-05-20 00:54:53 +03:00
if widget . get_has_entry ( ) :
return widget . get_child ( ) . get_text ( ) . strip ( )
return None
2014-01-27 03:15:50 +04:00
2015-05-20 01:07:19 +03:00
#####################
# UI setter helpers #
#####################
2015-05-20 01:13:33 +03:00
def set_list_selection_by_number ( widget , rownum ) :
2014-01-27 05:21:12 +04:00
"""
Helper to set list selection from the passed row number
"""
2014-01-27 03:15:50 +04:00
path = str ( rownum )
selection = widget . get_selection ( )
selection . unselect_all ( )
widget . set_cursor ( path )
selection . select_path ( path )
2015-05-20 01:13:33 +03:00
def set_list_selection ( widget , value , column = 0 ) :
2014-01-27 05:21:12 +04:00
"""
2015-05-20 01:07:19 +03:00
Set a list or tree selection given the passed key , expected to
be stored at the specified column .
2015-05-20 01:22:49 +03:00
If the passed value is not found , and the widget is a combo box with
a text entry , set the text entry to the passed value .
2014-01-27 05:21:12 +04:00
"""
2015-05-20 01:07:19 +03:00
model = widget . get_model ( )
2014-01-27 03:15:50 +04:00
_iter = None
2015-05-20 01:11:00 +03:00
for row in model :
if row [ column ] == value :
_iter = row . iter
break
2015-05-20 01:22:49 +03:00
2014-01-27 03:15:50 +04:00
if not _iter :
2015-05-20 01:22:49 +03:00
if hasattr ( widget , " get_has_entry " ) and widget . get_has_entry ( ) :
widget . get_child ( ) . set_text ( value or " " )
else :
_iter = model . get_iter_first ( )
2014-01-27 03:15:50 +04:00
2015-05-20 01:07:19 +03:00
if hasattr ( widget , " get_selection " ) :
selection = widget . get_selection ( )
2014-01-27 03:15:50 +04:00
cb = selection . select_iter
else :
2015-05-20 01:07:19 +03:00
selection = widget
2014-01-27 03:15:50 +04:00
cb = selection . set_active_iter
if _iter :
cb ( _iter )
selection . emit ( " changed " )
2015-05-20 01:07:19 +03:00
##################
# Misc functions #
##################
2014-01-27 03:15:50 +04:00
def child_get_property ( parent , child , propname ) :
2014-01-27 05:21:12 +04:00
"""
Wrapper for child_get_property , which pygobject doesn ' t properly
introspect
"""
2014-01-27 03:15:50 +04:00
value = GObject . Value ( )
value . init ( GObject . TYPE_INT )
parent . child_get_property ( child , propname , value )
return value . get_int ( )
def set_grid_row_visible ( child , visible ) :
2014-01-27 05:21:12 +04:00
"""
For the passed widget , find its parent GtkGrid , and hide / show all
elements that are in the same row as it . Simplifies having to name
every element in a row when we want to dynamically hide things
based on UI interraction
"""
2014-01-27 03:15:50 +04:00
parent = child . get_parent ( )
2016-04-18 23:42:12 +03:00
if type ( parent ) is not Gtk . Grid :
2014-01-27 03:15:50 +04:00
raise RuntimeError ( " Programming error, parent must be grid, "
" not %s " % type ( parent ) )
row = child_get_property ( parent , child , " top-attach " )
for child in parent . get_children ( ) :
if child_get_property ( parent , child , " top-attach " ) == row :
child . set_visible ( visible )
2015-05-20 01:07:19 +03:00
def init_combo_text_column ( combo , col ) :
"""
Set the text column of the passed combo to ' col ' . Does the
right thing whether it ' s a plain combo or a comboboxentry. Saves
some typing .
: returns : If we added a cell renderer , returns it . Otherwise return None
"""
if combo . get_has_entry ( ) :
combo . set_entry_text_column ( col )
else :
text = Gtk . CellRendererText ( )
combo . pack_start ( text , True )
combo . add_attribute ( text , ' text ' , col )
return text
return None