diff getan/states.py @ 405:150180e972d3

Adds ability to adjust the length of (saved) entries.
author Bernhard Reiter <bernhard@intevation.de>
date Fri, 10 Feb 2017 15:01:07 +0100
parents 527168c08ae4
children 84f9b1eeb8b6
line wrap: on
line diff
--- a/getan/states.py	Fri Feb 03 15:01:06 2017 +0100
+++ b/getan/states.py	Fri Feb 10 15:01:07 2017 +0100
@@ -547,8 +547,16 @@
             entry = self.view.item_in_focus()
             if entry:
                 self.set_next_state(AdjustEntryState(self.projectlist_state,
-                                                   self.controller, self.view,
-                                                   entry))
+                                                     self.controller, self.view,
+                                                     entry))
+            return True
+
+        if keys.get_entry_length() in key:
+            entry = self.view.item_in_focus()
+            if entry:
+                self.set_next_state(LengthEntryState(self.projectlist_state,
+                                                     self.controller, self.view,
+                                                     entry))
             return True
 
         return False
@@ -829,6 +837,71 @@
         self.controller.view.set_focus("entries")
         self.view.frame.set_focus("footer")
 
+class LengthEntryState(HandleUserInputState):
+
+    messages = {
+        'adjust_length_entry': _('Adjust length of entry: '),
+    }
+
+    def __init__(self, state, controller, view, entry):
+        view.set_footer_text(self.msg('adjust_length_entry'),
+                             'question', True)
+        super(LengthEntryState, self).__init__(controller, view,
+                                             None, view.footer)
+
+        # format current duration as string that is also accepted by enter()
+        total_minutes = int(entry.get_duration().total_seconds()/60)
+        hours = int(total_minutes // 60)
+
+        if hours > 0:
+            self.footer.set_edit_text(
+                "{:d}:{:d}".format(hours, int(total_minutes % 60)))
+        else:
+            self.footer.set_edit_text(int(minutes))
+
+        self.footer.set_edit_pos(len(self.footer.edit_text))
+        self.entry = entry
+        self.state = state
+        logger.debug("LengthEntryState: Entry %s" % entry)
+
+    def enter(self):
+        """Changed the length of an entry.
+
+        Works for total minutes or HH:MM.
+        """
+        entry_duration = self.footer.get_edit_text()
+
+        # avoid unexpected behavior if minus signs are given in the new length
+        if '-' in entry_duration:
+            return self
+
+        if ':' in entry_duration:
+            hours, minutes = entry_duration.split(':')
+        else:
+            hours = 0
+            minutes = entry_duration
+
+        try:
+            duration = timedelta(minutes=int(minutes), hours=int(hours))
+        except:
+            return self
+
+        entry = self.entry
+        entry.end = entry.start + duration
+
+        self.controller.update_entry(entry)
+        self.view.node_in_focus().update()
+        return self.exit()
+
+    def exit(self):
+        self.view.set_footer_text("", 'entry_footer', False)
+        self.set_next_state(DefaultEntryListState(self.state, self.controller,
+                                                  self.view))
+        return True
+
+    def set_focus(self):
+        self.controller.view.set_focus("entries")
+        self.view.frame.set_focus("footer")
 
 
 
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)