~openerp-dev/openobject-client/trunk-m2o_with_selection-rga

« back to all changes in this revision

Viewing changes to bin/widget_search/date_widget.py

  • Committer: RGA(OpenERP)
  • Date: 2011-04-19 08:42:28 UTC
  • mfrom: (1831.2.47 trunk)
  • Revision ID: rga@tinyerp.com-20110419084228-uk5isv1h2m1ej5xo
Merge with trunk client

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#
20
20
##############################################################################
21
21
 
 
22
 
22
23
import gobject
23
24
import pango
24
25
import gtk
25
26
import re
26
 
 
 
27
from datetime import datetime
27
28
import tools
 
29
from datetime import datetime
28
30
import tools.datetime_util
29
31
import time
30
32
 
 
33
 
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])
41
 
            
42
 
        self.small_text = self.initial_value 
 
44
 
 
45
        self.small_text = self.initial_value
43
46
        self.set_text(self.initial_value)
44
47
        self.regex = re.compile(self.regex)
45
48
 
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`
71
74
        """
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)
75
78
 
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): 
77
 
                
 
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):
78
81
            return '%Y/%m/%d'
 
82
 
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'
82
 
            
 
86
 
83
87
 
84
88
        return fmt
85
89
 
89
93
            return datetime.strptime(text, self.format)
90
94
        except:
91
95
            pass
92
 
            
 
96
 
93
97
        try:
94
98
            return datetime.strptime(text, self.small_format)
95
99
        except:
96
100
            self.valid = False
97
101
            return False
98
 
        
99
 
        
 
102
 
 
103
 
100
104
    def _on_insert_text(self, editable, value, length, position):
101
105
        if not self._interactive_input:
102
106
            return
104
108
        self.stop_emission('insert-text')
105
109
        if self.mode_cmd:
106
110
            if self.callback: self.callback(value)
107
 
            return 
108
 
        
109
 
        text = self.get_text()  
 
111
            return
 
112
 
 
113
        text = self.get_text()
110
114
        current_pos = self.get_position()
111
 
        
 
115
 
112
116
        if(length + current_pos > len(text)):
113
117
            return
114
 
        
 
118
 
115
119
        pos = (current_pos < 10  or text != self.initial_value) and current_pos or 0
116
120
 
117
121
        if length != 1:
156
160
        if self.mode_cmd:
157
161
            self.mode_cmd = False
158
162
            if self.callback_process: self.callback_process(False, self, False)
159
 
            
 
163
 
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
168
172
        try:
 
173
            self.set_max_length(len(text))
169
174
            gtk.Entry.set_text(self, text)
170
175
            date = self.isvalid_date(text);
171
176
            if(date):
172
177
                self.small_text = date.strftime(self.small_format)
173
 
            
 
178
            elif(text==self.initial_value):
 
179
                self.small_text = self.initial_value
 
180
 
174
181
        finally:
175
182
            self._interactive_input = True
176
 
            
 
183
 
 
184
    def get_text(self):
 
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)
 
189
 
 
190
 
 
191
    def get_value(self):
 
192
        date = self.isvalid_date(self.small_text)
 
193
        if(date):
 
194
            return date.strftime(self.format)
 
195
        return gtk.Entry.get_text(self)
 
196
 
177
197
    def date_set(self, dt):
178
198
        if 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)
254
274
 
255
 
    def clear(self):
256
 
        self.widget.clear()
257
 
 
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]
287
304
            self.widget_cmd.set_text('')
288
305
            self.widget_cmd.hide()
289
306
 
 
307
    def clear(self):
 
308
        self.widget.clear()
 
309
 
290
310
if __name__ == '__main__':
291
311
    import sys
292
312
    def _(s):