1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
# Copyright (C) 2009, 2013, 2014 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.
from gi.repository import GObject
from gi.repository import Gtk
from virtinst import xmlutil
#####################
# UI getter helpers #
#####################
def spin_get_helper(widget):
"""
Safely get spin button contents, converting to int if possible
"""
adj = widget.get_adjustment()
txt = widget.get_text()
try:
return int(txt)
except Exception:
return adj.get_value()
def get_list_selected_row(widget, check_visible=False):
"""
Helper to simplify getting the selected row in a list/tree/combo
"""
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]
return row
def get_list_selection(widget, column=0, check_visible=False, check_entry=True):
"""
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.
"""
row = get_list_selected_row(widget, check_visible=check_visible)
if row is not None:
return row[column]
if check_entry and hasattr(widget, "get_has_entry"):
if widget.get_has_entry():
return widget.get_child().get_text().strip()
return None
#####################
# UI setter helpers #
#####################
def set_list_selection_by_number(widget, rownum):
"""
Helper to set list selection from the passed row number
"""
path = str(rownum)
selection = widget.get_selection()
selection.unselect_all()
widget.set_cursor(path)
selection.select_path(path)
def set_list_selection(widget, value, column=0):
"""
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.
"""
model = widget.get_model()
_iter = None
for row in model:
if row[column] == value:
_iter = row.iter
break
if not _iter:
if hasattr(widget, "get_has_entry") and widget.get_has_entry():
widget.get_child().set_text(value or "")
else:
_iter = model.get_iter_first()
if hasattr(widget, "get_selection"):
selection = widget.get_selection()
cb = selection.select_iter
else:
selection = widget
cb = selection.set_active_iter
if _iter:
cb(_iter)
selection.emit("changed")
##################
# Misc functions #
##################
def child_get_property(parent, child, propname):
"""
Wrapper for child_get_property, which pygobject doesn't properly
introspect
"""
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):
"""
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 interaction
"""
parent = child.get_parent()
if not isinstance(parent, Gtk.Grid):
raise xmlutil.DevError("parent must be grid, not %s" % type(parent))
row = child_get_property(parent, child, "top-attach")
for c in parent.get_children():
if child_get_property(parent, c, "top-attach") == row:
c.set_visible(visible)
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
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)
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)
|