def _reset_size_widgets(self, selected_size): """ Adjust size scale with selected size and unit """ unit = self.selected_unit self._scale.set_range(self.min_size.convert_to(unit), self.max_size.convert_to(unit)) self._scale.clear_marks() increment, digits = get_size_precision(self.min_size.convert_to(unit), self.max_size.convert_to(unit)) self._scale.set_increments(increment, increment * 10) self._scale.set_digits(digits) self._scale.add_mark(0, Gtk.PositionType.BOTTOM, format(self.min_size.convert_to(unit), "." + str(digits) + "f") + " " + size.unit_str(unit)) self._scale.add_mark(float(self.max_size.convert_to(unit)), Gtk.PositionType.BOTTOM, format(self.max_size.convert_to(unit), "." + str(digits) + "f") + " " + size.unit_str(unit)) self._spin.set_range(self.min_size.convert_to(unit), self.max_size.convert_to(unit)) self._spin.set_increments(increment, increment * 10) self._spin.set_digits(digits) if selected_size > self.max_size: self.selected_size = self.max_size elif selected_size < self.min_size: self.selected_size = self.min_size else: self.selected_size = selected_size
def test_unit_change(self): original_size = self.size_area.selected_size for idx, unit in enumerate(list(UNITS.keys())): self.size_area._unit_chooser.set_active(idx) self.assertEqual(unit.upper(), unit_str(self.size_area.selected_unit).upper()) # kB vs KB new_size = Size(str(self.size_area._spin.get_value()) + " " + unit) self.assertEqual(original_size, new_size)
def _on_unit_changed(self, combo): """ On-change action for unit combo """ new_unit = UNITS[combo.get_active_text()] old_unit = self.selected_unit self.selected_unit = new_unit selected_size = size.Size(str(self._scale.get_value()) + " " + size.unit_str(old_unit)) self._reset_size_widgets(selected_size) if self.unit_change_handler is not None: self.unit_change_handler.method(self.selected_unit, *self.unit_change_handler.args)
def _on_unit_changed(self, combo): """ On-change action for unit combo """ new_unit = UNITS[combo.get_active_text()] old_unit = self.selected_unit self.selected_unit = new_unit selected_size = size.Size(str(self._scale.get_value()) + " " + size.unit_str(old_unit)) self._reset_size_widgets(selected_size) for handler in self._unit_change_handlers: handler.method(self.selected_unit, *handler.args)
def _scale_precision(self, unit): """ Get number of decimal places to be displayed for selected unit and step for the scale. We should allow one decimal place for GB and bigger (step 0.1) and no decimal values for MiB and smaller (step 1). :param unit: selected size unit :type unit: size unit constant (e.g. blivet.size.KiB) """ if size.Size("1 " + size.unit_str(unit)) >= size.Size("1 GB"): return (1, 0.1) else: return (0, 1)
def _set_size_widgets(self): """ Configure size widgets (Gtk.Scale, Gtk.SpinButton) """ scale = self._builder.get_object("scale_size") spin = self._builder.get_object("spinbutton_size") default_unit = self.default_unit adjustment = Gtk.Adjustment( value=0, lower=self.min_size.convert_to(default_unit), upper=self.max_size.convert_to(default_unit), step_increment=1, page_increment=10, page_size=0) scale.set_adjustment(adjustment) spin.set_adjustment(adjustment) digits, increment = self._scale_precision(default_unit) scale.set_increments(increment, increment * 10) scale.set_digits(digits) spin.set_increments(increment, increment * 10) spin.set_digits(digits) scale.add_mark(self.min_size.convert_to(default_unit), Gtk.PositionType.BOTTOM, str(self.min_size)) scale.add_mark(self.max_size.convert_to(default_unit), Gtk.PositionType.BOTTOM, str(self.max_size)) combobox_size = self._builder.get_object("combobox_size") for unit in self.available_units: combobox_size.append_text(size.unit_str(unit)) # set default unit self.selected_unit = default_unit combobox_size.set_active(self.available_units.index(default_unit)) combobox_size.connect("changed", self._on_unit_changed) scale.connect("value-changed", self._on_scale_moved, spin) spin.connect("value-changed", self._on_spin_moved, scale) return scale, spin, combobox_size
def _set_size_widgets(self): """ Configure size widgets (Gtk.Scale, Gtk.SpinButton) """ scale = self._builder.get_object("scale_size") spin = self._builder.get_object("spinbutton_size") default_unit = self.default_unit adjustment = Gtk.Adjustment(value=0, lower=self.min_size.convert_to(default_unit), upper=self.max_size.convert_to(default_unit), step_increment=1, page_increment=10, page_size=0) scale.set_adjustment(adjustment) spin.set_adjustment(adjustment) digits, increment = self._scale_precision(default_unit) scale.set_increments(increment, increment * 10) scale.set_digits(digits) spin.set_increments(increment, increment * 10) spin.set_digits(digits) scale.add_mark(self.min_size.convert_to(default_unit), Gtk.PositionType.BOTTOM, str(self.min_size)) scale.add_mark(self.max_size.convert_to(default_unit), Gtk.PositionType.BOTTOM, str(self.max_size)) combobox_size = self._builder.get_object("combobox_size") for unit in self.available_units: combobox_size.append_text(size.unit_str(unit)) # set default unit self.selected_unit = default_unit combobox_size.set_active(self.available_units.index(default_unit)) combobox_size.connect("changed", self._on_unit_changed) scale.connect("value-changed", self._on_scale_moved, spin) spin.connect("value-changed", self._on_spin_moved, scale) return scale, spin, combobox_size
def selected_size(self): return size.Size( str(self._scale.get_value()) + " " + size.unit_str(self.selected_unit))
def selected_size(self): return size.Size(str(self._scale.get_value()) + " " + size.unit_str(self.selected_unit))
def test_get_size(self): selected_size = Size(str(self.size_area._spin.get_value()) + " " + unit_str(self.size_area.selected_unit)) self.assertEqual(selected_size, self.size_area.selected_size)