Stop key-press-event and insert decimal point
On Windows editing the keyval of the event does not work anymore. So we must insert ourself the decimal point in the entry buffer. issue10956 review374311002 (grafted from 8fe7c0da72026de62ecd988e638cb2a410c5d5d3) --HG-- branch : 6.0
This commit is contained in:
1 parent
f601fc621b
commit
6c3986cda7
3 files changed
+41
-23
No files matched your search
@@ -17,29 +17,39 @@ class CellRendererFloat(CellRendererInteger):
|
||||
super().on_editing_started(editable, path)
|
||||
editable.connect('key-press-event', self.key_press_event)
|
||||
|
||||
@property
|
||||
def __decimal_point(self):
|
||||
return locale.localeconv()['decimal_point']
|
||||
|
||||
@property
|
||||
def __thousands_sep(self):
|
||||
return locale.localeconv()['thousands_sep']
|
||||
|
||||
def key_press_event(self, widget, event):
|
||||
for name in ('KP_Decimal', 'KP_Separator'):
|
||||
if event.keyval == Gdk.keyval_from_name(name):
|
||||
event.keyval = int(Gdk.unicode_to_keyval(
|
||||
ord(locale.localeconv()['decimal_point'])))
|
||||
text = self.__decimal_point
|
||||
position = widget.props.cursor_position
|
||||
if self._can_insert_text(widget, text, position):
|
||||
buffer_ = widget.get_buffer()
|
||||
buffer_.insert_text(position, text, len(text))
|
||||
widget.set_position(
|
||||
widget.props.cursor_position + len(text))
|
||||
return True
|
||||
|
||||
def sig_insert_text(self, entry, new_text, new_text_length, position):
|
||||
def _can_insert_text(self, entry, new_text, position):
|
||||
value = entry.get_text()
|
||||
position = entry.get_position()
|
||||
new_value = value[:position] + new_text + value[position:]
|
||||
decimal_point = locale.localeconv()['decimal_point']
|
||||
|
||||
if new_value in ('-', decimal_point):
|
||||
return
|
||||
|
||||
try:
|
||||
value = locale.atof(new_value)
|
||||
except ValueError:
|
||||
entry.stop_emission_by_name('insert-text')
|
||||
return
|
||||
|
||||
if self.digits and not (round(value, self.digits[1]) == float(value)):
|
||||
entry.stop_emission_by_name('insert-text')
|
||||
if new_value not in {'-', self.__decimal_point, self.__thousands_sep}:
|
||||
try:
|
||||
value = locale.atof(new_value)
|
||||
except ValueError:
|
||||
return False
|
||||
if (self.digits
|
||||
and not (round(value, self.digits[1]) == float(value))):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
GObject.type_register(CellRendererFloat)
|
||||
@@ -14,15 +14,19 @@ class CellRendererInteger(CellRendererText):
|
||||
editable.set_alignment(1.0)
|
||||
editable.connect('insert_text', self.sig_insert_text)
|
||||
|
||||
def sig_insert_text(self, entry, new_text, new_text_length, position):
|
||||
def _can_insert_text(self, entry, new_text, position):
|
||||
value = entry.get_text()
|
||||
position = entry.get_position()
|
||||
new_value = value[:position] + new_text + value[position:]
|
||||
if new_value == '-':
|
||||
return
|
||||
try:
|
||||
locale.atoi(new_value)
|
||||
except ValueError:
|
||||
if new_value != '-':
|
||||
try:
|
||||
locale.atoi(new_value)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
def sig_insert_text(self, entry, new_text, new_text_length, position):
|
||||
if not self._can_insert_text(entry, new_text, position):
|
||||
entry.stop_emission_by_name('insert-text')
|
||||
|
||||
|
||||
|
||||
@@ -64,7 +64,11 @@ class NumberEntry(Gtk.Entry, Gtk.Editable):
|
||||
def __key_press_event(self, event):
|
||||
for name in ['KP_Decimal', 'KP_Separator']:
|
||||
if event.keyval == Gdk.keyval_from_name(name):
|
||||
event.keyval = Gdk.unicode_to_keyval(ord(self.__decimal_point))
|
||||
text = self.__decimal_point
|
||||
self.do_insert_text(
|
||||
text, len(text), self.props.cursor_position)
|
||||
self.set_position(self.props.cursor_position + len(text))
|
||||
return True
|
||||
|
||||
|
||||
GObject.type_register(NumberEntry)
|
||||
|
||||
Reference in new issue
Block a user