2014-01-26 18:15:50 -05:00
# Copyright (C) 2009, 2013, 2014 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
2018-04-04 14:35:41 +01:00
# This work is licensed under the GNU GPLv2 or later.
2018-03-20 15:00:02 -04:00
# See the COPYING file in the top-level directory.
2014-01-26 18:15:50 -05:00
from gi . repository import GObject
from gi . repository import Gtk
2020-08-20 13:34:01 -04:00
from virtinst import xmlutil
2014-01-26 18:15:50 -05:00
2015-05-19 18:07:19 -04:00
#####################
# UI getter helpers #
#####################
2014-01-26 18:15:50 -05:00
def spin_get_helper ( widget ) :
2014-01-26 20:21:12 -05:00
"""
Safely get spin button contents , converting to int if possible
"""
2014-01-26 18:15:50 -05:00
adj = widget . get_adjustment ( )
txt = widget . get_text ( )
try :
return int ( txt )
2017-07-24 09:26:48 +01:00
except Exception :
2014-01-26 18:15:50 -05:00
return adj . get_value ( )
2015-05-19 17:17:53 -04:00
def get_list_selected_row ( widget , check_visible = False ) :
2014-01-26 20:21:12 -05:00
"""
2015-05-19 17:17:53 -04:00
Helper to simplify getting the selected row in a list / tree / combo
2014-01-26 20:21:12 -05: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-26 18:15:50 -05:00
2015-05-19 17:17:53 -04:00
return row
2015-06-21 15:47:31 -04:00
def get_list_selection ( widget , column = 0 ,
check_visible = False , check_entry = True ) :
2015-05-19 17:17:53 -04:00
"""
2015-05-19 17:54:53 -04: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 15:47:31 -04:00
: param check_entry : If True , attempt to check the widget ' s text entry
using the logic described above .
2015-05-19 17:17:53 -04:00
"""
row = get_list_selected_row ( widget , check_visible = check_visible )
2015-05-19 17:54:53 -04:00
if row is not None :
return row [ column ]
2015-06-21 15:47:31 -04:00
if check_entry and hasattr ( widget , " get_has_entry " ) :
2015-05-19 17:54:53 -04:00
if widget . get_has_entry ( ) :
return widget . get_child ( ) . get_text ( ) . strip ( )
return None
2014-01-26 18:15:50 -05:00
2015-05-19 18:07:19 -04:00
#####################
# UI setter helpers #
#####################
2015-05-19 18:13:33 -04:00
def set_list_selection_by_number ( widget , rownum ) :
2014-01-26 20:21:12 -05:00
"""
Helper to set list selection from the passed row number
"""
2014-01-26 18:15:50 -05:00
path = str ( rownum )
selection = widget . get_selection ( )
selection . unselect_all ( )
widget . set_cursor ( path )
selection . select_path ( path )
2015-05-19 18:13:33 -04:00
def set_list_selection ( widget , value , column = 0 ) :
2014-01-26 20:21:12 -05:00
"""
2015-05-19 18:07:19 -04:00
Set a list or tree selection given the passed key , expected to
be stored at the specified column .
2015-05-19 18:22:49 -04: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-26 20:21:12 -05:00
"""
2015-05-19 18:07:19 -04:00
model = widget . get_model ( )
2014-01-26 18:15:50 -05:00
_iter = None
2015-05-19 18:11:00 -04:00
for row in model :
if row [ column ] == value :
_iter = row . iter
break
2015-05-19 18:22:49 -04:00
2014-01-26 18:15:50 -05:00
if not _iter :
2015-05-19 18:22:49 -04: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-26 18:15:50 -05:00
2015-05-19 18:07:19 -04:00
if hasattr ( widget , " get_selection " ) :
selection = widget . get_selection ( )
2014-01-26 18:15:50 -05:00
cb = selection . select_iter
else :
2015-05-19 18:07:19 -04:00
selection = widget
2014-01-26 18:15:50 -05:00
cb = selection . set_active_iter
if _iter :
cb ( _iter )
selection . emit ( " changed " )
2015-05-19 18:07:19 -04:00
##################
# Misc functions #
##################
2014-01-26 18:15:50 -05:00
def child_get_property ( parent , child , propname ) :
2014-01-26 20:21:12 -05:00
"""
Wrapper for child_get_property , which pygobject doesn ' t properly
introspect
"""
2014-01-26 18:15:50 -05: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-26 20:21:12 -05: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
2024-10-13 15:26:08 +02:00
based on UI interaction
2014-01-26 20:21:12 -05:00
"""
2014-01-26 18:15:50 -05:00
parent = child . get_parent ( )
2017-10-11 12:35:41 +01:00
if not isinstance ( parent , Gtk . Grid ) :
2020-08-20 13:34:01 -04:00
raise xmlutil . DevError ( " parent must be grid, not %s " % type ( parent ) )
2014-01-26 18:15:50 -05:00
row = child_get_property ( parent , child , " top-attach " )
2017-06-15 08:18:26 -04:00
for c in parent . get_children ( ) :
if child_get_property ( parent , c , " top-attach " ) == row :
c . set_visible ( visible )
2015-05-19 18:07:19 -04: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
2019-06-07 17:19:23 -04:00
def pretty_mem ( val ) :
val = int ( val )
if val > ( 10 * 1024 * 1024 ) :
return " %2.2f GiB " % ( val / ( 1024.0 * 1024.0 ) )
else :
return " %2.0f MiB " % ( val / 1024.0 )
2020-09-07 18:29:20 -04:00
def build_simple_combo ( combo , values , default_value = None , sort = True ) :
"""
Helper to build a combo with model schema [ xml value , label ]
"""
model = Gtk . ListStore ( object , str )
combo . set_model ( model )
init_combo_text_column ( combo , 1 )
if sort :
model . set_sort_column_id ( 1 , Gtk . SortType . ASCENDING )
for xmlval , label in values :
model . append ( [ xmlval , label ] )
if default_value :
set_list_selection ( combo , default_value )
elif len ( model ) :
combo . set_active ( 0 )