Skip to content

Commit

Permalink
InputBox: Add functionality to enter a number with max value
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlesat authored and Huevos committed Aug 26, 2023
1 parent cc9fcd4 commit ce6e39d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions lib/python/Components/Input.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ class Input(VariableText, GUIComponent, NumericalTextInput):
PIN = 1
NUMBER = 2

def __init__(self, text="", maxSize=False, visible_width=False, type=TEXT, currPos=0, allMarked=True):
def __init__(self, text="", maxSize=False, visible_width=False, type=TEXT, currPos=0, allMarked=True, maxValue=False):
NumericalTextInput.__init__(self, self.right)
GUIComponent.__init__(self)
VariableText.__init__(self)
self.type = type
self.allmarked = allMarked and (text != "") and (type != self.PIN)
self.maxSize = maxSize
self.currPos = currPos
self.maxValue= maxValue
self.visible_width = visible_width
self.offset = 0
self.overwrite = maxSize
self.overwrite = maxSize or self.type == self.NUMBER
self.setText(text)

def __len__(self):
Expand Down Expand Up @@ -154,6 +155,17 @@ def end(self):
self.currPos = len(self.Text)
self.update()

def checkNumber(self):
if self.type == self.NUMBER:
if self.maxValue and self.Text and int(self.Text) > self.maxValue:
self.Text = self.Text[:self.currPos]
elif not self.Text:
self.Text = "0"
self.currPos = 0
elif len(self.Text) > 1 and self.Text.startswith('0'):
self.Text = self.Text[1:]
self.currPos = self.currPos - 1

def insertChar(self, ch, pos=False, owr=False, ins=False):
if isinstance(ch, bytes):
ch = six.ensure_text(ch, errors='ignore')
Expand All @@ -167,6 +179,7 @@ def insertChar(self, ch, pos=False, owr=False, ins=False):
self.Text = self.Text[0:pos] + ch + self.Text[pos:-1]
else:
self.Text = self.Text[0:pos] + ch + self.Text[pos:]
self.checkNumber()

def deleteChar(self, pos):
if not self.maxSize:
Expand All @@ -175,10 +188,13 @@ def deleteChar(self, pos):
self.Text = self.Text[0:pos] + " " + self.Text[pos + 1:]
else:
self.Text = self.Text[0:pos] + self.Text[pos + 1:] + " "
self.checkNumber()

def deleteAllChars(self):
if self.maxSize:
self.Text = " " * len(self.Text)
elif self.type == self.NUMBER:
self.Text = "0"
else:
self.Text = ""
self.currPos = 0
Expand Down

0 comments on commit ce6e39d

Please sign in to comment.