Commits

fak3 committed 6b3d842

use simple ChoiceBox for MAX_EXEC setting

  • Participants
  • Parent commits 11c5f9e

Comments (0)

Files changed (3)

 import utils
 import conf
 
-from gui_utils import HBox, VBox, TreeItem, TreeWidget, TimeBox, TimeDeltaBox, DownloadFileDialog, ActionDialog, StatusDialog, TextButton
+from gui_utils import HBox, VBox, TreeItem, TreeWidget, TimeBox, ChoiceBox, DownloadFileDialog, ActionDialog, StatusDialog, TextButton
 
 from main_utils import *
 
         end = datetime(2012, 03, 05, 8, 0) # 8:00 (5 марта)
         delta = timedelta(hours = 1)
         
-        self.tbox = TimeBox(self, start, end, delta, datetime.strptime(self.value, '%d.%m.%Y %H:%M'))
+        initial = datetime.strptime(self.value, '%d.%m.%Y %H:%M')
+        
+        self.tbox = TimeBox(utils.iter_times(start, end, delta), initial)
         self.tbox.currentIndexChanged.connect(self.change_time)
         
     def change_time(self, index):
-        self.settings[self.key] = self.tbox.current_time().strftime('%d.%m.%Y %H:%M')
+        self.settings[self.key] = self.tbox.selected().strftime('%d.%m.%Y %H:%M')
+        
         
 class FetchTimeSetting(TimeSetting):
     def construct_tbox(self):
-        start = timedelta(days = 30)
-        end = timedelta(days = 120)
-        delta = timedelta(days = 30)
-        
-        self.tbox = TimeDeltaBox(self, start, end, delta, timedelta(days = self.value))
+        self.tbox = ChoiceBox([5,10,20,30,40,60,90,120], initial = self.value)
         self.tbox.currentIndexChanged.connect(self.change_time)
         
     def change_time(self, index):
-        self.settings[self.key] = self.tbox.current_time().days
+        self.settings[self.key] = int(self.tbox.selected())
     
     sizeHint = lambda self: QSize(560, 230)
     
         end = datetime.strptime(settings['max_time'], '%d.%m.%Y %H:%M')
         delta = timedelta(hours = 1)
         
-        self.cbox_start = TimeBox(self, start, end, delta)
-        self.cbox_end = TimeBox(self, start, end, delta, current = end)
+        self.cbox_start = TimeBox( utils.iter_times(start, end, delta))
+        self.cbox_end = TimeBox( utils.iter_times(start, end, delta), initial = end)
         
         layout = HBox(self, [self.start_lbl, self.cbox_start, self.end_lbl, self.cbox_end])
         layout.setSizeConstraint(QLayout.SetMinAndMaxSize)
             if item.checkState(order_col) == Qt.Checked:
                 uiks.append(item.uikdata)
                 
-        start = self.spanbox.cbox_start.current_time()
-        end = self.spanbox.cbox_end.current_time()
+        start = self.spanbox.cbox_start.selected()
+        end = self.spanbox.cbox_end.selected()
         self.dlg_val = uiks, start, end
         
         self.done(1)
         self.tclicked.emit(self.text())
 
 
-class TimeBox(QComboBox):
-    def __init__(self, parent, start, end, delta, current = None):
-        QComboBox.__init__(self, parent)
-        self.times = []
-        t = start
-        while t <= end:
-            self.times.append(t)
-            self.addItem(t.strftime('%d %b %H:00').decode(locale.getpreferredencoding()))
-            if current == t:
-                self.setCurrentIndex(len(self.times)-1)
-            t += delta
+class ChoiceBox(QComboBox):
+    def __init__(self, choices, initial = None, *args, **kwargs):
+        QComboBox.__init__(self, *args, **kwargs)
+        self.choices = list(choices)
+        for n, c in enumerate(self.choices):
+            self.addItem(self.to_str(c))
+            if initial == c:
+                self.setCurrentIndex(n)
         
-    def current_time(self):
-        return self.times[self.currentIndex()]
+    def selected(self):
+        return self.choices[self.currentIndex()]
+    
+    def to_str(self, c):
+        return str(c)
+    
+    
+class TimeBox(ChoiceBox):
+    def to_str(self, dt):
+        return dt.strftime('%d %b %H:00').decode(locale.getpreferredencoding())
 
 
-class TimeDeltaBox(QComboBox):
-    def __init__(self, parent, start, end, delta, current = None):
-        QComboBox.__init__(self, parent)
-        self.times = []
-        t = start
-        while t <= end:
-            self.times.append(t)
-            self.addItem(str(t.days))
-            if current == t:
-                self.setCurrentIndex(len(self.times)-1)
-            t += delta
-        
-    def current_time(self):
-        return self.times[self.currentIndex()]
-    
-
 class TreeWidget(QTreeWidget):
     def walk_items(self, only_leaves = False):
         for i in range(self.topLevelItemCount()):
     return wrapped
 
 
+def iter_times(start, end, delta):
+    t = start
+    while t <= end:
+        yield t
+        t += delta
+
+
 def safe_path(path):
     if not isinstance(path, unicode):
         path = unicode(path, sys.getfilesystemencoding())