o
    b                      @   s   d dl mZ d dl mZ d dlmZ dd ZdddZ	 		dd
dZdd ZdddZ	dd Z
dd Zdd Zdd ZdddZdS )    )GObject)Gtk)xmlutilc                 C   s6   |   }|  }zt|W S  ty   |  Y S w )zH
    Safely get spin button contents, converting to int if possible
    )get_adjustmentget_textint	Exception	get_value)widgetadjtxt r   1/usr/share/virt-manager/virtManager/lib/uiutil.pyspin_get_helper   s   
r   Fc                 C   sj   |r|   sdS t| dr#|  }| \}}|du rdS || }|S |  }|dkr-dS |  | }|S )zJ
    Helper to simplify getting the selected row in a list/tree/combo
    Nget_selection)get_visiblehasattrr   get_selected
get_active	get_model)r
   check_visible	selectionmodeltreeiterrowidxr   r   r   get_list_selected_row   s   
r   Tc                 C   sF   t | |d}|dur|| S |r!t| dr!|  r!|    S dS )a4  
    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.

    :param check_entry: If True, attempt to check the widget's text entry
        using the logic described above.
    )r   Nget_has_entry)r   r   r   	get_childr   strip)r
   columnr   check_entryr   r   r   r   get_list_selection6   s   
r#   c                 C   s0   t |}|  }|  | | || dS )zA
    Helper to set list selection from the passed row number
    N)strr   unselect_all
set_cursorselect_path)r
   rownumpathr   r   r   r   set_list_selection_by_numberO   s
   
r*   c                 C   s   |   }d}|D ]}|| |kr|j} nq|s/t| dr+|  r+|  |p(d n| }t| dr<|  }|j}n| }|j	}|rG|| |
d dS )z
    Set a list or tree selection given the passed key, expected to
    be stored at the specified column.

    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.
    Nr    r   changed)r   iterr   r   r   set_textget_iter_firstr   select_iterset_active_iteremit)r
   valuer!   r   _iterr   r   cbr   r   r   set_list_selection[   s&   
r6   c                 C   s*   t  }|t j | ||| | S )zY
    Wrapper for child_get_property, which pygobject doesn't properly
    introspect
    )r   ValueinitTYPE_INTchild_get_propertyget_int)parentchildpropnamer3   r   r   r   r:      s   r:   c                 C   s^   |   }t|tjstdt| t|| d}| D ]}t||d|kr,|	| qdS )z
    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
    zparent must be grid, not %sz
top-attachN)

get_parent
isinstancer   Gridr   DevErrortyper:   get_childrenset_visible)r=   visibler<   r   cr   r   r   set_grid_row_visible   s   
rH   c                 C   s<   |   r| | dS t }| |d | |d| |S )z
    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
    TtextN)r   set_entry_text_columnr   CellRendererText
pack_startadd_attribute)combocolrI   r   r   r   init_combo_text_column   s   
rP   c                 C   s(   t | } | dkrd| d  S d| d  S )Ni   z	%2.2f GiBg      0Az	%2.0f MiBg      @)r   )valr   r   r   
pretty_mem   s   rR   Nc                 C   s|   t tt}| | t| d |r|dt jj |D ]\}}|	||g q|r1t
| | dS t|r<| d dS dS )zF
    Helper to build a combo with model schema [xml value, label]
       r   N)r   	ListStoreobjectr$   	set_modelrP   set_sort_column_idSortType	ASCENDINGappendr6   len
set_active)rN   valuesdefault_valuesortr   xmlvallabelr   r   r   build_simple_combo   s   

rb   )F)r   FT)r   )NT)gi.repositoryr   r   virtinstr   r   r   r#   r*   r6   r:   rH   rP   rR   rb   r   r   r   r   <module>   s   


$