-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatepicker.py
498 lines (381 loc) · 14.8 KB
/
datepicker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/python
# -*- coding: utf-8 -*-
###########################################################
# TMG 3-23-19 modification of :
#
# KivyCalendar (X11/MIT License)
# Calendar & Date picker widgets for Kivy (http://kivy.org)
# https://bitbucket.org/xxblx/kivycalendar
#
# Oleg Kozlov (xxblx), 2015
# https://xxblx.bitbucket.org/
###########################################################
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.properties import NumericProperty, ReferenceListProperty
###########################################################
Builder.load_string("""
<ArrowButton>:
background_normal: ""
background_down: ""
background_color: 1, 1, 1, 0
size_hint: .1, .1
<MonthYearLabel>:
pos_hint: {"top": 1, "center_x": .5}
size_hint: None, 0.1
halign: "center"
<MonthsManager>:
pos_hint: {"top": .9}
size_hint: 1, .9
<ButtonsGrid>:
cols: 7
rows: 7
size_hint: 1, 1
pos_hint: {"top": 1}
<DayAbbrLabel>:
text_size: self.size[0], None
halign: "center"
<DayAbbrWeekendLabel>:
color: 1, 0, 0, 1
<DayButton>:
group: "day_num"
<DayNumWeekendButton>:
background_color: 1, 0, 0, 1
<SpacerButton>:
background_color:0,0,0,1
""")
###########################################################
class DatePicker(TextInput):
"""
Date picker is a textinput, if it focused shows popup with calendar
which allows you to define the popup dimensions using pHint_x, pHint_y,
and the pHint lists, for example in kv:
DatePicker:
pHint: 0.7,0.4
would result in a size_hint of 0.7,0.4 being used to create the popup
"""
# keep a somewhat-calendar-like aspect ratio: hints will be different
# based on screen aspect ratio - calculate at popup time instead of here
# in case window has been resized after opening
pHint_x = NumericProperty(0.7)
pHint_y = NumericProperty(0.7)
pHint = ReferenceListProperty(pHint_x ,pHint_y)
def __init__(self, touch_switch=False, *args, **kwargs):
super(DatePicker, self).__init__(*args, **kwargs)
self.touch_switch = touch_switch
self.init_ui()
def init_ui(self):
self.text = today_date()
# Calendar
self.cal = CalendarWidget(as_popup=True,
touch_switch=self.touch_switch,
dp=self)
# Popup
# don't assume that dismiss should update the value; dismiss via escape
# should not update the value; explicitly update the value then dismiss
# when a date is clicked
self.popup = Popup(content=self.cal,
title="")
self.cal.parent_popup = self.popup
self.bind(focus=self.show_popup)
def show_popup(self, inst, val):
"""
Open popup if textinput focused,
and regardless update the popup size_hint
"""
self.popup.size_hint=None,None
self.popup.width=min(Window.width*0.9,600)
self.popup.height=self.popup.width*0.75 # always use a 4:3 aspect ratio
if val:
# Automatically dismiss the keyboard
# that results from the textInput
Window.release_all_keyboards()
self.popup.open()
def update_value(self, inst):
""" Update textinput value on popup close """
# self.text = "%s-%s-%s" % tuple(self.cal.active_date)
[d,m,y]=self.cal.active_date
# self.text = "%s. %s. %s, %s" % (self.cal.days_abrs[datetime(y,m,d).weekday()],month_abbr[m],d,y)
self.text=get_date_str(y,m,d)
self.focus = False
class CalendarWidget(RelativeLayout):
""" Basic calendar widget """
def __init__(self, as_popup=False, touch_switch=False, dp=None, *args, **kwargs):
super(CalendarWidget, self).__init__(*args, **kwargs)
self.as_popup = as_popup
self.touch_switch = touch_switch
self.prepare_data()
self.init_ui()
self.dp=dp
def init_ui(self):
self.left_arrow = ArrowButton(text="<", on_press=self.go_prev,
pos_hint={"top": 1, "left": 0})
self.right_arrow = ArrowButton(text=">", on_press=self.go_next,
pos_hint={"top": 1, "right": 1})
self.add_widget(self.left_arrow)
self.add_widget(self.right_arrow)
# Title
self.title_label = MonthYearLabel(text=self.title)
self.add_widget(self.title_label)
# ScreenManager
self.sm = MonthsManager()
self.add_widget(self.sm)
self.create_month_scr(self.quarter[1], toogle_today=True)
def create_month_scr(self, month, toogle_today=False):
""" Screen with calendar for one month """
scr = Screen()
m = self.month_names_eng[self.active_date[1] - 1]
scr.name = "%s-%s" % (m, self.active_date[2]) # like march-2015
# Grid for days
grid_layout = ButtonsGrid()
scr.add_widget(grid_layout)
# Days abbrs
for i in range(7):
if i >= 5: # weekends
l = DayAbbrWeekendLabel(text=self.days_abrs[i])
else: # work days
l = DayAbbrLabel(text=self.days_abrs[i])
grid_layout.add_widget(l)
# Buttons with days numbers
for week in month:
for day in week:
if day[1] >= 5: # weekends
tbtn = DayNumWeekendButton(text=str(day[0]))
else: # work days
tbtn = DayNumButton(text=str(day[0]))
tbtn.bind(on_press=self.get_btn_value)
if toogle_today:
# Down today button
if day[0] == self.active_date[0] and day[2] == 1:
tbtn.state = "down"
# Use spacer buttons for days from other months
if day[2] == 0:
tbtn.disabled = True
grid_layout.add_widget(SpacerButton())
else:
grid_layout.add_widget(tbtn)
self.sm.add_widget(scr)
def prepare_data(self):
""" Prepare data for showing on widget loading """
# Get days abbrs and month names lists
self.month_names = get_month_names()
self.month_names_eng = get_month_names_eng()
self.days_abrs = get_days_abbrs()
# Today date
self.active_date = today_date_list()
# Set title
self.title = "%s - %s" % (self.month_names[self.active_date[1] - 1],
self.active_date[2])
# Quarter where current month in the self.quarter[1]
self.get_quarter()
def get_quarter(self):
""" Get caledar and months/years nums for quarter """
self.quarter_nums = calc_quarter(self.active_date[2],
self.active_date[1])
self.quarter = get_quarter(self.active_date[2],
self.active_date[1])
def get_btn_value(self, inst):
""" Get day value from pressed button """
self.active_date[0] = int(inst.text)
if self.as_popup:
self.dp.update_value(self)
self.parent_popup.dismiss()
def go_prev(self, inst):
""" Go to screen with previous month """
# Change active date
self.active_date = [self.active_date[0], self.quarter_nums[0][1],
self.quarter_nums[0][0]]
# Name of prev screen
n = self.quarter_nums[0][1] - 1
prev_scr_name = "%s-%s" % (self.month_names_eng[n],
self.quarter_nums[0][0])
# If it's doen't exitst, create it
if not self.sm.has_screen(prev_scr_name):
self.create_month_scr(self.quarter[0])
self.sm.current = prev_scr_name
self.sm.transition.direction = "right"
self.get_quarter()
self.title = "%s - %s" % (self.month_names[self.active_date[1] - 1],
self.active_date[2])
self.title_label.text = self.title
def go_next(self, inst):
""" Go to screen with next month """
# Change active date
self.active_date = [self.active_date[0], self.quarter_nums[2][1],
self.quarter_nums[2][0]]
# Name of prev screen
n = self.quarter_nums[2][1] - 1
next_scr_name = "%s-%s" % (self.month_names_eng[n],
self.quarter_nums[2][0])
# If it's doen't exitst, create it
if not self.sm.has_screen(next_scr_name):
self.create_month_scr(self.quarter[2])
self.sm.current = next_scr_name
self.sm.transition.direction = "left"
self.get_quarter()
self.title = "%s - %s" % (self.month_names[self.active_date[1] - 1],
self.active_date[2])
self.title_label.text = self.title
def on_touch_move(self, touch):
""" Switch months pages by touch move """
if self.touch_switch:
# Left - prev
if touch.dpos[0] < -30:
self.go_prev(None)
# Right - next
elif touch.dpos[0] > 30:
self.go_next(None)
class ArrowButton(Button):
pass
class MonthYearLabel(Label):
pass
class MonthsManager(ScreenManager):
pass
class ButtonsGrid(GridLayout):
pass
class DayAbbrLabel(Label):
pass
class DayAbbrWeekendLabel(DayAbbrLabel):
pass
class DayButton(ToggleButton):
pass
class DayNumButton(DayButton):
pass
class DayNumWeekendButton(DayButton):
pass
class SpacerButton(DayButton):
pass
#!/usr/bin/python
# -*- coding: utf-8 -*-
###########################################################
# KivyCalendar (X11/MIT License)
# Calendar & Date picker widgets for Kivy (http://kivy.org)
# https://bitbucket.org/xxblx/kivycalendar
#
# Oleg Kozlov (xxblx), 2015
# https://xxblx.bitbucket.org/
###########################################################
from calendar import month_name, month_abbr, day_abbr, Calendar, monthrange
from datetime import datetime
from locale import getdefaultlocale
def get_month_names():
""" Return list with months names """
result = []
# If it possible get months names in system language
try:
with TimeEncoding("%s.%s" % getdefaultlocale()) as time_enc:
for i in range(1, 13):
result.append(month_name[i].decode(time_enc))
return result
except:
return get_month_names_eng()
def get_month_names_eng():
""" Return list with months names in english """
result = []
for i in range(1, 13):
result.append(month_name[i])
return result
def get_days_abbrs():
""" Return list with days abbreviations """
result = []
# If it possible get days abbrs in system language
try:
with TimeEncoding("%s.%s" % getdefaultlocale()) as time_enc:
for i in range(7):
result.append(day_abbr[i].decode(time_enc))
except:
for i in range(7):
result.append(day_abbr[i])
return result
def calc_quarter(y, m):
""" Calculate previous and next month """
# Previous / Next month's year number and month number
prev_y = y
prev_m = m - 1
next_y = y
next_m = m + 1
if m == 1:
prev_m = 12
prev_y = y - 1
elif m == 12:
next_m = 1
next_y = y + 1
return [(prev_y, prev_m), (y, m), (next_y, next_m)]
def get_month(y, m):
"""
Return list of month's weeks, which day
is a turple (<month day number>, <weekday number>)
"""
cal = Calendar()
month = cal.monthdays2calendar(y, m)
# Add additional num to every day which mark from
# this or from other day that day numer
for week in range(len(month)):
for day in range(len(month[week])):
_day = month[week][day]
if _day[0] == 0:
this = 0
else:
this = 1
_day = (_day[0], _day[1], this)
month[week][day] = _day
# Days numbers of days from preious and next monthes
# marked as 0 (zero), replace it with correct numbers
# If month include 4 weeks it hasn't any zero
if len(month) == 4:
return month
quater = calc_quarter(y, m)
# Zeros in first week
fcount = 0
for i in month[0]:
if i[0] == 0:
fcount += 1
# Zeros in last week
lcount = 0
for i in month[-1]:
if i[0] == 0:
lcount += 1
if fcount:
# Last day of prev month
n = monthrange(quater[0][0], quater[0][1])[1]
for i in range(fcount):
month[0][i] = (n - (fcount - 1 - i), i, 0)
if lcount:
# First day of next month
n = 1
for i in range(lcount):
month[-1][-lcount + i] = (n + i, 7 - lcount + i, 0)
return month
def get_quarter(y, m):
""" Get quarter where m is a middle month """
result = []
quarter = calc_quarter(y, m)
for i in quarter:
result.append(get_month(i[0], i[1]))
return result
def get_date_str(y,m,d):
# do not use commas here as they will cause the line to be wrapped in
# double quotes if written to a csv file
return "%s %s %s %s" % (get_days_abbrs()[datetime(y,m,d).weekday()],month_abbr[m],d,y)
def today_date_list():
""" Return list with today date """
return [datetime.now().day, datetime.now().month, datetime.now().year]
def today_date():
""" Return today date dd.mm.yyyy like 28.02.2015 """
# return datetime.now().strftime("%d/%m/%Y")
# return datetime.now().strftime("%b. %#d, %Y")
# return '{month_abbr[dt.month]}. {dt.day}, {dt.year}'.format(dt = datetime.now())
today=datetime.today()
return get_date_str(today.year,today.month,today.day)
if __name__ == "__main__":
from kivy.base import runTouchApp
c = DatePicker()
runTouchApp(c)