20
20
##############################################################################
27
from datetime import datetime
29
from datetime import datetime
28
30
import tools.datetime_util
31
34
class DateEntry(gtk.Entry):
32
35
def __init__(self, format, callback=None, callback_process=None):
33
36
super(DateEntry, self).__init__()
38
41
for key,val in tools.datetime_util.date_mapping.items():
39
42
self.regex = self.regex.replace(key, val[1])
40
43
self.initial_value = self.initial_value.replace(key, val[0])
42
self.small_text = self.initial_value
45
self.small_text = self.initial_value
43
46
self.set_text(self.initial_value)
44
47
self.regex = re.compile(self.regex)
69
72
"""If format string doesn't contain
70
73
any of the `%Y, %m or %d` then returns default datetime format `%Y/%m/%d`
72
for x,y in [('%y','%Y'),('%B','%m'),('%A',''), ('%I','%H'), ('%p',''), ('%j',''), ('%a',''), ('%b',''),
75
for x,y in [('%y','%Y'),('%B','%m'),('%A',''), ('%I','%H'), ('%p',''), ('%j',''), ('%a',''), ('%b','%m'),
73
76
('%U',''), ('%W','')]:
74
77
fmt = fmt.replace(x, y)
76
if (not (fmt.count('%Y') >= 1 and fmt.count('%m') >= 1 and fmt.count('%d') >= 1) or fmt.count('%x') >= 1) and (fmt.count('%H') == 0 and fmt.count('%M') == 0 and fmt.count('%S') == 0 and fmt.count('%X') == 0):
79
if (not (fmt.count('%Y') >= 1 and fmt.count('%m') >= 1 and fmt.count('%d') >= 1) or fmt.count('%x') >= 1) \
80
and (fmt.count('%H') == 0 and fmt.count('%M') == 0 and fmt.count('%S') == 0 and fmt.count('%X') == 0):
79
83
elif not (fmt.count('%Y') >= 1 and fmt.count('%m') >= 1 and fmt.count('%d') >= 1) \
80
84
or (fmt.count('%x') >=1 or fmt.count('%c') >= 1):
81
85
return '%Y/%m/%d %H:%M:%S'
89
93
return datetime.strptime(text, self.format)
94
98
return datetime.strptime(text, self.small_format)
96
100
self.valid = False
100
104
def _on_insert_text(self, editable, value, length, position):
101
105
if not self._interactive_input:
104
108
self.stop_emission('insert-text')
105
109
if self.mode_cmd:
106
110
if self.callback: self.callback(value)
109
text = self.get_text()
113
text = self.get_text()
110
114
current_pos = self.get_position()
112
116
if(length + current_pos > len(text)):
115
119
pos = (current_pos < 10 or text != self.initial_value) and current_pos or 0
156
160
if self.mode_cmd:
157
161
self.mode_cmd = False
158
162
if self.callback_process: self.callback_process(False, self, False)
160
164
def _focus_in(self, args, args2):
161
165
self.set_text(self.small_text)
162
166
if self.mode_cmd:
166
170
def set_text(self, text):
167
171
self._interactive_input = False
173
self.set_max_length(len(text))
169
174
gtk.Entry.set_text(self, text)
170
175
date = self.isvalid_date(text);
172
177
self.small_text = date.strftime(self.small_format)
178
elif(text==self.initial_value):
179
self.small_text = self.initial_value
175
182
self._interactive_input = True
185
date = self.isvalid_date(self.small_text)
186
if(date and not self.is_focus()):
187
return date.strftime(self.format)
188
return gtk.Entry.get_text(self)
192
date = self.isvalid_date(self.small_text)
194
return date.strftime(self.format)
195
return gtk.Entry.get_text(self)
177
197
def date_set(self, dt):
179
199
self.set_text( dt.strftime(self.format) )
252
272
self.pack_start(self.widget, expand=True, fill=True)
253
273
self.pack_start(self.widget_cmd, expand=False, fill=True)
258
275
def _date_cb(self, event):
259
276
if event.keyval in (gtk.keysyms.BackSpace,):
260
277
text = self.widget_cmd.get_text()[:-1]