Changeset 837:739af4588b42


Ignore:
Timestamp:
04/17/17 07:02:11 (7 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Time data entry widget

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/mlx/gui/common.py

    r836 r837  
    88
    99import os
     10import time
    1011
    1112#-----------------------------------------------------------------------------
     
    283284                      else str(self._currentInteger))
    284285        self._selfSetting = False
     286
     287#------------------------------------------------------------------------------
     288
     289class TimeEntry(gtk.Entry):
     290    """Widget to display and edit a time value in HH:MM format."""
     291    def __init__(self):
     292        """Construct the entry"""
     293        super(TimeEntry, self).__init__(max = 5)
     294
     295        self.connect("insert-text", self._insertText)
     296        self.connect("delete-text", self._deleteText)
     297        self.connect("focus-out-event", self._focusOutEvent)
     298
     299    @property
     300    def hour(self):
     301        """Get the hour from the current text"""
     302        text = self.get_text()
     303        if not text or text==":":
     304            return 0
     305
     306        words = text.split(":")
     307        if len(words)==1:
     308            return 0
     309        elif len(words)>=2:
     310            return 0 if len(words[0])==0 else int(words[0])
     311        else:
     312            return 0
     313
     314    @property
     315    def minute(self):
     316        """Get the hour from the current text"""
     317        text = self.get_text()
     318        if not text or text==":":
     319            return 0
     320
     321        words = text.split(":")
     322        if len(words)==1:
     323            return 0 if len(words[0])==0 else int(words[0])
     324        elif len(words)>=2:
     325            return 0 if len(words[1])==0 else int(words[1])
     326        else:
     327            return 0
     328
     329    def setTimestamp(self, timestamp):
     330        """Set the hour and minute from the given timestamp in UTC."""
     331        tm = time.gmtime(timestamp)
     332        self.set_text("%02d:%02d" % (tm.tm_hour, tm.tm_min))
     333
     334    def _focusOutEvent(self, widget, event):
     335        """Reformat the text to match pattern HH:MM"""
     336        text = "%02d:%02d" % (self.hour, self.minute)
     337        if text!=self.get_text():
     338            self.set_text(text)
     339
     340    def _insertText(self, entry, text, length, position):
     341        """Called when some text is inserted into the entry."""
     342        text=text[:length]
     343        currentText = self.get_text()
     344        position = self.get_position()
     345        newText = currentText[:position] + text + currentText[position:]
     346        self._checkText(newText, "insert-text")
     347
     348    def _deleteText(self, entry, start, end):
     349        """Called when some text is erased from the entry."""
     350        currentText = self.get_text()
     351        newText = currentText[:start] + currentText[end:]
     352        self._checkText(newText, "delete-text")
     353
     354    def _checkText(self, newText, signal):
     355        """Check the given text.
     356
     357        If it is not suitable, stop the emission of the signal to prevent the
     358        change from appearing."""
     359        if not newText or newText==":":
     360            return
     361
     362        words = newText.split(":")
     363        if (len(words)==1 and
     364            len(words[0])<=2 and (len(words[0])==0 or
     365                                  (words[0].isdigit() and int(words[0])<60))) or \
     366           (len(words)==2 and
     367            len(words[0])<=2 and (len(words[0])==0 or
     368                                  (words[0].isdigit() and int(words[0])<24)) and
     369            len(words[1])<=2 and (len(words[1])==0 or
     370                                  (words[1].isdigit() and int(words[1])<60))):
     371            pass
     372        else:
     373            gtk.gdk.display_get_default().beep()
     374            self.stop_emission(signal)
    285375
    286376#------------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.