datepicker.py (9104B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\eve\client\script\ui\control\datepicker.py 2 import datetime 3 import time 4 import blue 5 import calendar 6 import datetimeutils 7 import gametime 8 import localization 9 from carbonui import uiconst 10 from carbonui.control.combo import Combo 11 from carbonui.primitives.container import Container 12 from dateutil import relativedelta 13 from eve.common.lib import appConst as const 14 from localization.formatters import FormatTimeIntervalShortWritten 15 16 class DatePicker(Container): 17 __guid__ = 'xtriui.DatePicker' 18 __nonpersistvars__ = [] 19 months = [localization.GetByLabel('UI/Common/Months/January'), 20 localization.GetByLabel('UI/Common/Months/February'), 21 localization.GetByLabel('UI/Common/Months/March'), 22 localization.GetByLabel('UI/Common/Months/April'), 23 localization.GetByLabel('UI/Common/Months/May'), 24 localization.GetByLabel('UI/Common/Months/June'), 25 localization.GetByLabel('UI/Common/Months/July'), 26 localization.GetByLabel('UI/Common/Months/August'), 27 localization.GetByLabel('UI/Common/Months/September'), 28 localization.GetByLabel('UI/Common/Months/October'), 29 localization.GetByLabel('UI/Common/Months/November'), 30 localization.GetByLabel('UI/Common/Months/December')] 31 32 def Startup(self, now = None, withTime = False, timeparts = 4, startYear = None, yearRange = None, set_value = None): 33 self.timeparts = timeparts 34 if now is None: 35 now = time.gmtime() 36 if startYear is None: 37 startYear = const.calendarStartYear 38 year = max(startYear, now[0]) 39 month = now[1] 40 day = now[2] 41 left = 0 42 if yearRange is None: 43 yRange = year - startYear + 1 44 else: 45 yRange = yearRange 46 self.yearBase = year 47 yearops = [ (localization.formatters.FormatNumeric(startYear + i, decimalPlaces=0), startYear + i) for i in xrange(yRange) ] 48 self.ycombo = Combo(parent=self, label=localization.GetByLabel('UI/Common/DateWords/Year'), options=yearops, name='year', select=set_value[0] if set_value else year, callback=self.OnComboChange, pos=(left, 49 0, 50 0, 51 0), align=uiconst.TOPLEFT, adjustWidth=True) 52 left += self.ycombo.width + 4 53 self.monthBase = month 54 monthops = [ (self.months[i], i + 1) for i in xrange(12) ] 55 self.mcombo = Combo(parent=self, label=localization.GetByLabel('UI/Common/DateWords/Month'), options=monthops, name='month', select=set_value[1] if set_value else month, callback=self.OnComboChange, pos=(left, 56 0, 57 0, 58 0), align=uiconst.TOPLEFT, adjustWidth=True) 59 left += self.mcombo.width + 4 60 self.dayBase = day 61 firstday, numdays = calendar.monthrange(year, month) 62 dayops = [ (localization.formatters.FormatNumeric(i + 1, decimalPlaces=0), i + 1) for i in xrange(numdays) ] 63 self.dcombo = Combo(parent=self, label=localization.GetByLabel('UI/Common/DateWords/Day'), options=dayops, name='day', select=set_value[2] if set_value else day, callback=self.OnComboChange, pos=(left, 64 0, 65 0, 66 0), align=uiconst.TOPLEFT, adjustWidth=True) 67 self.width = self.dcombo.left + self.dcombo.width 68 self.height = self.ycombo.height 69 if withTime: 70 if set_value: 71 index = self._GetTimeIndexFromHourAndMin(set_value[3], set_value[4]) 72 else: 73 index = self.GetTimeIndex(now) 74 hourops = self.GetTimeOptions() 75 self.hourBase = now[3] 76 self.minuteBase = now[4] 77 left += self.dcombo.width + 4 78 self.hcombo = Combo(parent=self, label=localization.GetByLabel('UI/Common/DateWords/Time'), options=hourops, name='time', select=index, callback=self.OnComboChange, pos=(left, 79 0, 80 0, 81 0), align=uiconst.TOPLEFT, adjustWidth=True) 82 self.width = self.hcombo.left + self.hcombo.width 83 self.height = self.hcombo.height 84 85 def OnComboChange(self, combo, header, value, *args): 86 self.CheckDays() 87 if hasattr(self, 'OnDateChange'): 88 self.OnDateChange() 89 90 def _ResetCombosToDefault(self): 91 currentSelection = self.mcombo.selectedValue 92 self.mcombo.LoadOptions([ (self.months[i], i + 1) for i in xrange(12) ]) 93 self.mcombo.SelectItemByValue(currentSelection) 94 currentSelection = self.dcombo.selectedValue 95 firstday, numdays = calendar.monthrange(self.ycombo.GetValue(), self.mcombo.GetValue()) 96 self.dcombo.LoadOptions([ (localization.formatters.FormatNumeric(i + 1, decimalPlaces=0), i + 1) for i in xrange(numdays) ]) 97 self.dcombo.SelectItemByValue(currentSelection) 98 currentValue = self.GetValueAsDateTime() 99 currentIndex = self._GetTimeIndexFromHourAndMin(currentValue.hour, currentValue.minute - 1) 100 hourops = self.GetTimeOptions() 101 self.hcombo.LoadOptions(hourops) 102 self.hcombo.SelectItemByValue(currentIndex) 103 104 def CheckDays(self): 105 sely = self.ycombo.GetValue() 106 selm = self.mcombo.GetValue() 107 seld = self.dcombo.GetValue() 108 firstday, numdays = calendar.monthrange(sely, selm) 109 dayops = [ (str(i + 1), i + 1) for i in xrange(numdays) ] 110 self.dcombo.LoadOptions(dayops, min(seld, numdays)) 111 112 def SetValueFromNow(self, seconds = 0, minutes = 0, hours = 0, days = 0, weeks = 0, months = 0, years = 0): 113 now = datetime.datetime.utcnow() 114 time_to_advance = relativedelta.relativedelta(seconds=seconds, minutes=minutes, hours=hours, days=days, weeks=weeks, months=months, years=years) 115 target_time = now + time_to_advance 116 target_time_tuple = target_time.timetuple() 117 year = target_time_tuple.tm_year 118 month = target_time_tuple.tm_mon 119 day = target_time_tuple.tm_mday 120 hour = target_time_tuple.tm_hour 121 minute = target_time_tuple.tm_min 122 self._ResetCombosToDefault() 123 self.ycombo.SetValue(year) 124 self.mcombo.SetValue(month) 125 self.dcombo.SetValue(day) 126 if getattr(self, 'hcombo', None) is not None: 127 index = self._GetTimeIndexFromHourAndMin(hour, minute) 128 self.hcombo.SetValue(index) 129 130 def SetValue(self, gametime): 131 time_datetime = datetimeutils.filetime_to_datetime(gametime) 132 time_tuple = time_datetime.timetuple() 133 year = time_tuple.tm_year 134 month = time_tuple.tm_mon 135 day = time_tuple.tm_mday 136 hour = time_tuple.tm_hour 137 minute = time_tuple.tm_min 138 self.ycombo.SetValue(year) 139 self.mcombo.SetValue(month) 140 self.dcombo.SetValue(day) 141 if getattr(self, 'hcombo', None) is not None: 142 index = self._GetTimeIndexFromHourAndMin(hour, minute) 143 self.hcombo.SetValue(index) 144 self.OnComboChange(None, None, None, None) 145 146 def GetValue(self): 147 y = self.ycombo.GetValue() 148 m = self.mcombo.GetValue() 149 d = self.dcombo.GetValue() 150 h = 0 151 min = 0 152 if getattr(self, 'hcombo', None) is not None: 153 time = self.hcombo.GetValue() 154 h = time / self.timeparts 155 interval = 60 / self.timeparts 156 min = time % self.timeparts * interval 157 return blue.os.GetTimeFromParts(y, m, d, h, min, 0, 0) 158 159 def GetValueAsDateTime(self): 160 return datetimeutils.filetime_to_datetime(self.GetValue()) 161 162 def GetDurationFromNow(self): 163 return relativedelta.relativedelta(self.GetValueAsDateTime(), datetime.datetime.utcnow()) 164 165 def HasDatePassed(self): 166 wallclock_now = gametime.GetWallclockTimeNow() 167 wallclock_destination = self.GetValue() 168 if wallclock_destination - wallclock_now < 0: 169 return True 170 171 def GetDurationFromNowString(self, show_from = 'year', show_to = 'minute'): 172 wallclock_now = gametime.GetWallclockTimeNow() 173 wallclock_destination = self.GetValue() 174 if wallclock_destination - wallclock_now < 0: 175 return None 176 return FormatTimeIntervalShortWritten(wallclock_destination - wallclock_now, showFrom=show_from, showTo=show_to) 177 178 def GetTimeOptions(self, *args): 179 hours = [] 180 counter = 0 181 for h in xrange(0, 24): 182 interval = 60 / self.timeparts 183 for m in xrange(0, self.timeparts): 184 min = m * interval 185 timeStr = '%.2d:%.2d' % (h, min) 186 hours.append((timeStr, counter)) 187 counter += 1 188 189 return hours 190 191 def GetTimeIndex(self, now, *args): 192 if len(now) > 3: 193 hour = now[3] 194 else: 195 hour = 12 196 if len(now) > 4: 197 min = now[4] 198 else: 199 min = 0 200 return self._GetTimeIndexFromHourAndMin(hour, min) 201 202 def _GetTimeIndexFromHourAndMin(self, hour, min): 203 interval = 60 / self.timeparts 204 index = hour * self.timeparts + min / interval 205 if min % interval * 2 > interval: 206 index += 1 207 return index