File: cellrendererinteger.py

package info (click to toggle)
tryton-client 7.0.27-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,476 kB
  • sloc: python: 27,180; sh: 37; makefile: 18
file content (33 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download | duplicates (3)
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
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import locale

from gi.repository import GObject

from .cellrenderertext import CellRendererText


class CellRendererInteger(CellRendererText):

    def on_editing_started(self, editable, path):
        super().on_editing_started(editable, path)
        editable.set_alignment(1.0)
        editable.connect('insert_text', self.sig_insert_text)

    def _can_insert_text(self, entry, new_text, position):
        value = entry.get_text()
        new_value = value[:position] + new_text + value[position:]
        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):
        position = entry.get_position()
        if not self._can_insert_text(entry, new_text, position):
            entry.stop_emission_by_name('insert-text')


GObject.type_register(CellRendererInteger)