def _setup_entry_slave(self, box=None): widget = ProxyEntry() # Try to simulate insensitive appearance for non-editable entries # while keeping them selectable widget.set_editable(self.sensitive) if not self.sensitive: style = widget.get_style() widget.modify_text(gtk.STATE_NORMAL, style.text[gtk.STATE_INSENSITIVE]) widget.modify_base(gtk.STATE_NORMAL, style.base[gtk.STATE_INSENSITIVE]) widget.data_type = unicode widget.model_attribute = "field_value" self.proxy.add_widget("field_value", widget) if box is None: self.container.add(widget) else: box.pack_start(widget) widget.show() widget.connect('validate', self._on_entry__validate) widget.connect('validation-changed', self._on_entry__validation_changed) self._entry = widget
def _setup_entry_slave(self, box=None): widget = ProxyEntry() # Try to simulate insensitive appearance for non-editable entries # while keeping them selectable widget.set_editable(self.sensitive) if not self.sensitive: style = widget.get_style() widget.modify_text( gtk.STATE_NORMAL, style.text[gtk.STATE_INSENSITIVE]) widget.modify_base( gtk.STATE_NORMAL, style.base[gtk.STATE_INSENSITIVE]) widget.data_type = unicode widget.model_attribute = "field_value" self.proxy.add_widget("field_value", widget) if box is None: self.container.add(widget) else: box.pack_start(widget) widget.show() widget.connect('validate', self._on_entry__validate) widget.connect('validation-changed', self._on_entry__validation_changed) self._entry = widget
def test_attach(self): entry = ProxyEntry() entry.data_type = currency self.assertEqual(entry.get_property('secondary-icon-pixbuf'), None) calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) pixbuf_pixels = calc.render_icon(STOQ_CALC, gtk.ICON_SIZE_MENU).get_pixels() self.assertEqual( entry.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels) entry.set_sensitive(False) self.assertEqual(entry.get_property('secondary-icon-pixbuf'), None) entry.set_sensitive(True) self.assertEqual( entry.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels) spinbutton = ProxySpinButton() spinbutton.data_type = currency self.assertEqual(spinbutton.get_property('secondary-icon-pixbuf'), None) calc = CalculatorPopup(spinbutton, CalculatorPopup.MODE_SUB) pixbuf_pixels = calc.render_icon(STOQ_CALC, gtk.ICON_SIZE_MENU).get_pixels() self.assertEqual( spinbutton.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels) spinbutton.set_sensitive(False) self.assertEqual(spinbutton.get_property('secondary-icon-pixbuf'), None) spinbutton.set_sensitive(True) self.assertEqual( spinbutton.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels)
def test_popup(self): entry = ProxyEntry() entry.data_type = currency entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) event = Gdk.Event.new(Gdk.EventType.BUTTON_PRESS) event.window = Gdk.get_default_root_window() with mock.patch.object(calc, 'popup') as popup: entry.emit('icon-press', Gtk.EntryIconPosition.PRIMARY, event) self.assertEqual(popup.call_count, 0) entry.emit('icon-press', Gtk.EntryIconPosition.SECONDARY, event) self.assertEqual(popup.call_count, 1)
def test_popup(self): entry = ProxyEntry() entry.data_type = currency entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) event = gtk.gdk.Event(gtk.gdk.BUTTON_PRESS) event.window = gtk.gdk.get_default_root_window() with mock.patch.object(calc, 'popup') as popup: entry.emit('icon-press', gtk.ENTRY_ICON_PRIMARY, event) self.assertEqual(popup.call_count, 0) entry.emit('icon-press', gtk.ENTRY_ICON_SECONDARY, event) popup.assert_called_once()
def test_apply(self): entry = ProxyEntry() entry.data_type = currency entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) # calc.popup will not work here, so call _update_ui directly calc._update_ui() calc._entry.set_text('10%') event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) event.keyval = gtk.keysyms.Return event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) calc.emit('key-press-event', event) self.assertEqual(entry.read(), 135)
def test_apply(self): entry = ProxyEntry() entry.data_type = currency entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) # calc.popup will not work here, so call validate_popup directly calc.validate_popup() calc._entry.set_text('10%') event = Gdk.Event.new(Gdk.EventType.KEY_PRESS) event.keyval = Gdk.KEY_Return event.window = Gdk.get_default_root_window() calc.emit('key-press-event', event) calc.emit('key-press-event', event) self.assertEqual(entry.read(), 135)
def _setup_entry_slave(self, box=None): widget = ProxyEntry() widget.props.sensitive = self.sensitive widget.data_type = unicode widget.model_attribute = "field_value" self.proxy.add_widget("field_value", widget) if box is None: self.container.add(widget) else: box.pack_start(widget) widget.show() widget.connect("validate", self._on_entry__validate) widget.connect("validation-changed", self._on_entry__validation_changed) self._entry = widget
def _create_entry(self, mandatory=False): entry = ProxyEntry() entry.data_type = str # Set as empty or kiwi will return ValueUnset on entry.read() # and we would have to take that in consideration everywhere here entry.update(u'') entry.mandatory = mandatory self.setup_entry(entry) entry.connect_after('content-changed', self._after_entry__content_changed) entry.connect_after('changed', self._after_entry__changed) entry.connect('validate', self._on_entry__validate) entry.connect('activate', self._on_entry__activate) return entry
def _setup_entry_slave(self, box=None): widget = ProxyEntry() widget.props.sensitive = self.sensitive widget.data_type = unicode widget.model_attribute = "field_value" self.proxy.add_widget("field_value", widget) if box is None: self.container.add(widget) else: box.pack_start(widget) widget.show() widget.connect('validate', self._on_entry__validate) widget.connect('validation-changed', self._on_entry__validation_changed) self._entry = widget
def _create_entry(self, mandatory=False): entry = ProxyEntry() entry.data_type = unicode # Set as empty or kiwi will return ValueUnset on entry.read() # and we would have to take that in consideration everywhere here entry.update(u'') entry.mandatory = mandatory self.setup_entry(entry) entry.connect_after('content-changed', self._after_entry__content_changed) entry.connect_after('changed', self._after_entry__changed) entry.connect('validate', self._on_entry__validate) entry.connect('activate', self._on_entry__activate) return entry
def test_popdown(self): entry = ProxyEntry() entry.data_type = currency entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) with contextlib.nested( mock.patch.object(calc, '_maybe_apply_new_value'), mock.patch.object(calc, 'popdown')) as (manv, popdown): # Those keys should try to apply the value for keyval in [ gtk.keysyms.Return, gtk.keysyms.KP_Enter, gtk.keysyms.Tab ]: event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) event.keyval = keyval event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) self.assertEqual(manv.call_count, 1) self.assertEqual(popdown.call_count, 0) manv.reset_mock() popdown.reset_mock() event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # Escape should popdown the popup event.keyval = gtk.keysyms.Escape event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) self.assertEqual(popdown.call_count, 1) self.assertEqual(manv.call_count, 0) manv.reset_mock() popdown.reset_mock() event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # Any other should not do anything event.keyval = gtk.keysyms.A event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) self.assertEqual(manv.call_count, 0) self.assertEqual(popdown.call_count, 0)
def test_popdown(self): entry = ProxyEntry() entry.data_type = currency entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) with contextlib.nested( mock.patch.object(calc, '_maybe_apply_new_value'), mock.patch.object(calc, 'popdown')) as (manv, popdown): # Those keys should try to apply the value for keyval in [gtk.keysyms.Return, gtk.keysyms.KP_Enter, gtk.keysyms.Tab]: event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) event.keyval = keyval event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) self.assertEqual(manv.call_count, 1) self.assertEqual(popdown.call_count, 0) manv.reset_mock() popdown.reset_mock() event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # Escape should popdown the popup event.keyval = gtk.keysyms.Escape event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) self.assertEqual(popdown.call_count, 1) self.assertEqual(manv.call_count, 0) manv.reset_mock() popdown.reset_mock() event = gtk.gdk.Event(gtk.gdk.KEY_PRESS) # Any other should not do anything event.keyval = gtk.keysyms.A event.window = gtk.gdk.get_default_root_window() calc.emit('key-press-event', event) self.assertEqual(manv.call_count, 0) self.assertEqual(popdown.call_count, 0)
def testDataMode(self): entry = ProxyEntry() entry.data_type = str entry.set_exact_completion() items = {'xxx': object(), 'yyy': object()} entry.prefill([(k, v) for k, v in items.items()]) entry.set_text('xxx') self.assertIs(entry.read(), items['xxx']) entry.set_text('x') self.assertIs(entry.read(), None) entry.set_text('xxxxx') self.assertIs(entry.read(), None) entry.set_text('yyy') self.assertIs(entry.read(), items['yyy']) entry.set_text('y') self.assertIs(entry.read(), None) entry.set_text('yyyyy') self.assertIs(entry.read(), None)
def _setup_entry_slave(self, box=None): widget = ProxyEntry() # Try to simulate insensitive appearance for non-editable entries # while keeping them selectable widget.set_editable(self.sensitive) if not self.sensitive: sc = widget.get_style_context() sc.add_class('visualmode') widget.data_type = str widget.model_attribute = "field_value" self.proxy.add_widget("field_value", widget) if box is None: self.container.add(widget) else: box.pack_start(widget, True, True, 0) widget.show() widget.connect('validate', self._on_entry__validate) widget.connect('validation-changed', self._on_entry__validation_changed) self._entry = widget
def test_validate(self): def validate_entry(entry, value): if value == 100: return ValidationError() # FIXME: For some reason, entry is not emitting 'changed' event # on set_text, not even if we call entry.emit('changed') by hand. # That only happens here on the test. Figure out why def update_entry(entry, value): entry.set_text(value) entry.validate(force=True) entry = ProxyEntry() entry.data_type = currency entry.connect('validate', validate_entry) entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) # calc.popup will not work here, so call _update_ui directly calc._update_ui() self.assertValid(calc, ['_entry']) self.assertNotVisible(calc, ['_warning']) for value in ['abc', '+10%', '-10%', '+10', '-10']: update_entry(calc._entry, value) self.assertInvalid(calc, ['_entry']) self.assertNotVisible(calc, ['_warning']) update_entry(calc._entry, '40') self.assertValid(calc, ['_entry']) self.assertNotVisible(calc, ['_warning']) # 50 of discount will make the value invalid on entry # (see validate_entry above) update_entry(calc._entry, '50') self.assertValid(calc, ['_entry']) self.assertVisible(calc, ['_warning'])
def test_validate(self): def validate_entry(entry, value): if value == 100: return ValidationError() # FIXME: For some reason, entry is not emitting 'changed' event # on set_text, not even if we call entry.emit('changed') by hand. # That only happens here on the test. Figure out why def update_entry(entry, value): entry.set_text(value) entry.validate(force=True) entry = ProxyEntry() entry.data_type = currency entry.connect('validate', validate_entry) entry.set_text('150') calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) # calc.popup will not work here, so call validate_popup directly calc.validate_popup() self.assertValid(calc, ['_entry']) self.assertNotVisible(calc, ['_warning']) for value in ['abc', '+10%', '-10%', '+10', '-10']: update_entry(calc._entry, value) self.assertInvalid(calc, ['_entry']) self.assertNotVisible(calc, ['_warning']) update_entry(calc._entry, '40') self.assertValid(calc, ['_entry']) self.assertNotVisible(calc, ['_warning']) # 50 of discount will make the value invalid on entry # (see validate_entry above) update_entry(calc._entry, '50') self.assertValid(calc, ['_entry']) self.assertVisible(calc, ['_warning'])
def test_attach(self): entry = ProxyEntry() entry.data_type = currency self.assertEqual(entry.get_property('secondary-icon-pixbuf'), None) calc = CalculatorPopup(entry, CalculatorPopup.MODE_SUB) pixbuf_pixels = calc.render_icon(STOQ_CALC, Gtk.IconSize.MENU).get_pixels() self.assertEqual( entry.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels) entry.set_sensitive(False) self.assertEqual(entry.get_property('secondary-icon-pixbuf'), None) entry.set_sensitive(True) self.assertEqual( entry.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels) spinbutton = ProxySpinButton() spinbutton.data_type = currency self.assertEqual(spinbutton.get_property('secondary-icon-pixbuf'), None) calc = CalculatorPopup(spinbutton, CalculatorPopup.MODE_SUB) pixbuf_pixels = calc.render_icon(STOQ_CALC, Gtk.IconSize.MENU).get_pixels() self.assertEqual( spinbutton.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels) spinbutton.set_sensitive(False) self.assertEqual(spinbutton.get_property('secondary-icon-pixbuf'), None) spinbutton.set_sensitive(True) self.assertEqual( spinbutton.get_property('secondary-icon-pixbuf').get_pixels(), pixbuf_pixels)
from kiwi.ui.widgets.entry import ProxyEntry def on_entry_activate(entry): print 'You selected:', entry.read() gtk.main_quit() win = gtk.Window() win.connect('delete-event', gtk.main_quit) vbox = gtk.VBox() win.add(vbox) # Normal entry entry = ProxyEntry() entry.data_type = str entry.connect('activate', on_entry_activate) entry.prefill(['Belo Horizonte', u'São Carlos', u'São Paulo', u'Båstad', u'Örnsköldsvik', 'sanca', 'sampa']) vbox.pack_start(entry) entry = ProxyEntry() entry.data_type = int entry.connect('activate', on_entry_activate) entry.prefill([('Brazil', 186), ('Sweden', 9), ('China', 1306)]) vbox.pack_start(entry) win.show_all()
def on_entry_activate(entry): print('You selected:', entry.read()) Gtk.main_quit() win = Gtk.Window() win.connect('delete-event', Gtk.main_quit) vbox = Gtk.VBox() win.add(vbox) # Normal entry entry = ProxyEntry() entry.data_type = str entry.connect('activate', on_entry_activate) entry.prefill([ 'Belo Horizonte', 'São Carlos', 'São Paulo', 'Båstad', 'Örnsköldsvik', 'sanca', 'sampa' ]) vbox.pack_start(entry, True, True, 0) entry = ProxyEntry() entry.data_type = int entry.connect('activate', on_entry_activate) entry.prefill([('Brazil', 186), ('Sweden', 9), ('China', 1306)]) vbox.pack_start(entry, True, True, 0) win.show_all() Gtk.main()