1#!/usr/bin/env python
2
3#============================================================================#
4# (Re)Implementation of QDateEdit and QDateTimeEdit. These classes force the #
5# use of the calendar popup.                                                 #
6#----------------------------------------------------------------------------#
7# Copyright (c) 2008 by Denviso GmbH, <ulrich.berning@denviso.de>            #
8#                                                                            #
9# All Rights Reserved                                                        #
10#                                                                            #
11# Permission to use, copy, modify, and distribute this software and its      #
12# documentation for any purpose and without fee is hereby granted,           #
13# provided that the above copyright notice appear in all copies and that     #
14# both that copyright notice and this permission notice appear in            #
15# supporting documentation.                                                  #
16#                                                                            #
17# DENVISO DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS                       #
18# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY              #
19# AND FITNESS, IN NO EVENT SHALL DENVISO BE LIABLE FOR ANY                   #
20# SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES                  #
21# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,                    #
22# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER                      #
23# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE              #
24# OR PERFORMANCE OF THIS SOFTWARE.                                           #
25#----------------------------------------------------------------------------#
26
27from PyQt5.QtCore import pyqtProperty, Qt
28from PyQt5.QtWidgets import (QApplication, QCalendarWidget, QDateEdit,
29        QDateTimeEdit, QHBoxLayout, QWidget)
30
31
32#============================================================================#
33# PyDateEdit                                                                 #
34#----------------------------------------------------------------------------#
35class PyDateEdit(QDateEdit):
36    #
37    # Initialize base class
38    # Force use of the calendar popup
39    # Set default values for calendar properties
40    #
41    def __init__(self, *args):
42        super(PyDateEdit, self).__init__(*args)
43
44        self.setCalendarPopup(True)
45        self.__cw = None
46        self.__firstDayOfWeek = Qt.Monday
47        self.__gridVisible = False
48        self.__horizontalHeaderFormat = QCalendarWidget.ShortDayNames
49        self.__verticalHeaderFormat = QCalendarWidget.ISOWeekNumbers
50        self.__navigationBarVisible = True
51
52    #
53    # Call event handler of base class
54    # Get the calendar widget, if not already done
55    # Set the calendar properties
56    #
57    def mousePressEvent(self, event):
58        super(PyDateEdit, self).mousePressEvent(event)
59
60        if not self.__cw:
61            self.__cw = self.findChild(QCalendarWidget)
62            if self.__cw:
63                self.__cw.setFirstDayOfWeek(self.__firstDayOfWeek)
64                self.__cw.setGridVisible(self.__gridVisible)
65                self.__cw.setHorizontalHeaderFormat(self.__horizontalHeaderFormat)
66                self.__cw.setVerticalHeaderFormat(self.__verticalHeaderFormat)
67                self.__cw.setNavigationBarVisible(self.__navigationBarVisible)
68
69    #
70    # Make sure, the calendarPopup property is invisible in Designer
71    #
72    def getCalendarPopup(self):
73        return True
74    calendarPopup = pyqtProperty(bool, fget=getCalendarPopup)
75
76    #
77    # Property firstDayOfWeek: Qt::DayOfWeek
78    # Get: getFirstDayOfWeek()
79    # Set: setFirstDayOfWeek()
80    # Reset: resetFirstDayOfWeek()
81    #
82    def getFirstDayOfWeek(self):
83        return self.__firstDayOfWeek
84    def setFirstDayOfWeek(self, dayOfWeek):
85        if dayOfWeek != self.__firstDayOfWeek:
86            self.__firstDayOfWeek = dayOfWeek
87            if self.__cw:
88                self.__cw.setFirstDayOfWeek(dayOfWeek)
89    def resetFirstDayOfWeek(self):
90        if self.__firstDayOfWeek != Qt.Monday:
91            self.__firstDayOfWeek = Qt.Monday
92            if self.__cw:
93                self.__cw.setFirstDayOfWeek(Qt.Monday)
94    firstDayOfWeek = pyqtProperty(Qt.DayOfWeek,
95                                         fget=getFirstDayOfWeek,
96                                         fset=setFirstDayOfWeek,
97                                         freset=resetFirstDayOfWeek)
98
99    #
100    # Property gridVisible: bool
101    # Get: isGridVisible()
102    # Set: setGridVisible()
103    # Reset: resetGridVisible()
104    #
105    def isGridVisible(self):
106        return self.__gridVisible
107    def setGridVisible(self, show):
108        if show != self.__gridVisible:
109            self.__gridVisible = show
110            if self.__cw:
111                self.__cw.setGridVisible(show)
112    def resetGridVisible(self):
113        if self.__gridVisible != False:
114            self.__gridVisible = False
115            if self.__cw:
116                self.__cw.setGridVisible(False)
117    gridVisible = pyqtProperty(bool,
118                                      fget=isGridVisible,
119                                      fset=setGridVisible,
120                                      freset=resetGridVisible)
121
122    #
123    # Property horizontalHeaderFormat: QCalendarWidget::HorizontalHeaderFormat
124    # Get: getHorizontalHeaderFormat()
125    # Set: setHorizontalHeaderFormat()
126    # Reset: resetHorizontalHeaderFormat()
127    #
128    def getHorizontalHeaderFormat(self):
129        return self.__horizontalHeaderFormat
130    def setHorizontalHeaderFormat(self, format):
131        if format != self.__horizontalHeaderFormat:
132            self.__horizontalHeaderFormat = format
133            if self.__cw:
134                self.__cw.setHorizontalHeaderFormat(format)
135    def resetHorizontalHeaderFormat(self):
136        if self.__horizontalHeaderFormat != QCalendarWidget.ShortDayNames:
137            self.__horizontalHeaderFormat = QCalendarWidget.ShortDayNames
138            if self.__cw:
139                self.__cw.setHorizontalHeaderFormat(QCalendarWidget.ShortDayNames)
140    horizontalHeaderFormat = pyqtProperty(QCalendarWidget.HorizontalHeaderFormat,
141                                                 fget=getHorizontalHeaderFormat,
142                                                 fset=setHorizontalHeaderFormat,
143                                                 freset=resetHorizontalHeaderFormat)
144
145    #
146    # Property verticalHeaderFormat: QCalendarWidget::VerticalHeaderFormat
147    # Get: getVerticalHeaderFormat()
148    # Set: setVerticalHeaderFormat()
149    # Reset: resetVerticalHeaderFormat()
150    #
151    def getVerticalHeaderFormat(self):
152        return self.__verticalHeaderFormat
153    def setVerticalHeaderFormat(self, format):
154        if format != self.__verticalHeaderFormat:
155            self.__verticalHeaderFormat = format
156            if self.__cw:
157                self.__cw.setVerticalHeaderFormat(format)
158    def resetVerticalHeaderFormat(self):
159        if self.__verticalHeaderFormat != QCalendarWidget.ISOWeekNumbers:
160            self.__verticalHeaderFormat = QCalendarWidget.ISOWeekNumbers
161            if self.__cw:
162                self.__cw.setVerticalHeaderFormat(QCalendarWidget.ISOWeekNumbers)
163    verticalHeaderFormat = pyqtProperty(QCalendarWidget.VerticalHeaderFormat,
164                                               fget=getVerticalHeaderFormat,
165                                               fset=setVerticalHeaderFormat,
166                                               freset=resetVerticalHeaderFormat)
167
168    #
169    # Property navigationBarVisible: bool
170    # Get: isNavigationBarVisible()
171    # Set: setNavigationBarVisible()
172    # Reset: resetNavigationBarVisible()
173    #
174    def isNavigationBarVisible(self):
175        return self.__navigationBarVisible
176    def setNavigationBarVisible(self, visible):
177        if visible != self.__navigationBarVisible:
178            self.__navigationBarVisible = visible
179            if self.__cw:
180                self.__cw.setNavigationBarVisible(visible)
181    def resetNavigationBarVisible(self):
182        if self.__navigationBarVisible != True:
183            self.__navigationBarVisible = True
184            if self.__cw:
185                self.__cw.setNavigationBarVisible(True)
186    navigationBarVisible = pyqtProperty(bool,
187                                               fget=isNavigationBarVisible,
188                                               fset=setNavigationBarVisible,
189                                               freset=resetNavigationBarVisible)
190
191
192#============================================================================#
193# PyDateTimeEdit                                                             #
194#----------------------------------------------------------------------------#
195class PyDateTimeEdit(QDateTimeEdit):
196    #
197    # Initialize base class
198    # Force use of the calendar popup
199    # Set default values for calendar properties
200    #
201    def __init__(self, *args):
202        super(PyDateTimeEdit, self).__init__(*args)
203
204        self.setCalendarPopup(True)
205        self.__cw = None
206        self.__firstDayOfWeek = Qt.Monday
207        self.__gridVisible = False
208        self.__horizontalHeaderFormat = QCalendarWidget.ShortDayNames
209        self.__verticalHeaderFormat = QCalendarWidget.ISOWeekNumbers
210        self.__navigationBarVisible = True
211
212    #
213    # Call event handler of base class
214    # Get the calendar widget, if not already done
215    # Set the calendar properties
216    #
217    def mousePressEvent(self, event):
218        super(PyDateTimeEdit, self).mousePressEvent(event)
219
220        if not self.__cw:
221            self.__cw = self.findChild(QCalendarWidget)
222            if self.__cw:
223                self.__cw.setFirstDayOfWeek(self.__firstDayOfWeek)
224                self.__cw.setGridVisible(self.__gridVisible)
225                self.__cw.setHorizontalHeaderFormat(self.__horizontalHeaderFormat)
226                self.__cw.setVerticalHeaderFormat(self.__verticalHeaderFormat)
227                self.__cw.setNavigationBarVisible(self.__navigationBarVisible)
228
229    #
230    # Make sure, the calendarPopup property is invisible in Designer
231    #
232    def getCalendarPopup(self):
233        return True
234    calendarPopup = pyqtProperty(bool, fget=getCalendarPopup)
235
236    #
237    # Property firstDayOfWeek: Qt::DayOfWeek
238    # Get: getFirstDayOfWeek()
239    # Set: setFirstDayOfWeek()
240    # Reset: resetFirstDayOfWeek()
241    #
242    def getFirstDayOfWeek(self):
243        return self.__firstDayOfWeek
244    def setFirstDayOfWeek(self, dayOfWeek):
245        if dayOfWeek != self.__firstDayOfWeek:
246            self.__firstDayOfWeek = dayOfWeek
247            if self.__cw:
248                self.__cw.setFirstDayOfWeek(dayOfWeek)
249    def resetFirstDayOfWeek(self):
250        if self.__firstDayOfWeek != Qt.Monday:
251            self.__firstDayOfWeek = Qt.Monday
252            if self.__cw:
253                self.__cw.setFirstDayOfWeek(Qt.Monday)
254    firstDayOfWeek = pyqtProperty(Qt.DayOfWeek,
255                                         fget=getFirstDayOfWeek,
256                                         fset=setFirstDayOfWeek,
257                                         freset=resetFirstDayOfWeek)
258
259    #
260    # Property gridVisible: bool
261    # Get: isGridVisible()
262    # Set: setGridVisible()
263    # Reset: resetGridVisible()
264    #
265    def isGridVisible(self):
266        return self.__gridVisible
267    def setGridVisible(self, show):
268        if show != self.__gridVisible:
269            self.__gridVisible = show
270            if self.__cw:
271                self.__cw.setGridVisible(show)
272    def resetGridVisible(self):
273        if self.__gridVisible != False:
274            self.__gridVisible = False
275            if self.__cw:
276                self.__cw.setGridVisible(False)
277    gridVisible = pyqtProperty(bool,
278                                      fget=isGridVisible,
279                                      fset=setGridVisible,
280                                      freset=resetGridVisible)
281
282    #
283    # Property horizontalHeaderFormat: QCalendarWidget::HorizontalHeaderFormat
284    # Get: getHorizontalHeaderFormat()
285    # Set: setHorizontalHeaderFormat()
286    # Reset: resetHorizontalHeaderFormat()
287    #
288    def getHorizontalHeaderFormat(self):
289        return self.__horizontalHeaderFormat
290    def setHorizontalHeaderFormat(self, format):
291        if format != self.__horizontalHeaderFormat:
292            self.__horizontalHeaderFormat = format
293            if self.__cw:
294                self.__cw.setHorizontalHeaderFormat(format)
295    def resetHorizontalHeaderFormat(self):
296        if self.__horizontalHeaderFormat != QCalendarWidget.ShortDayNames:
297            self.__horizontalHeaderFormat = QCalendarWidget.ShortDayNames
298            if self.__cw:
299                self.__cw.setHorizontalHeaderFormat(QCalendarWidget.ShortDayNames)
300    horizontalHeaderFormat = pyqtProperty(QCalendarWidget.HorizontalHeaderFormat,
301                                                 fget=getHorizontalHeaderFormat,
302                                                 fset=setHorizontalHeaderFormat,
303                                                 freset=resetHorizontalHeaderFormat)
304
305    #
306    # Property verticalHeaderFormat: QCalendarWidget::VerticalHeaderFormat
307    # Get: getVerticalHeaderFormat()
308    # Set: setVerticalHeaderFormat()
309    # Reset: resetVerticalHeaderFormat()
310    #
311    def getVerticalHeaderFormat(self):
312        return self.__verticalHeaderFormat
313    def setVerticalHeaderFormat(self, format):
314        if format != self.__verticalHeaderFormat:
315            self.__verticalHeaderFormat = format
316            if self.__cw:
317                self.__cw.setVerticalHeaderFormat(format)
318    def resetVerticalHeaderFormat(self):
319        if self.__verticalHeaderFormat != QCalendarWidget.ISOWeekNumbers:
320            self.__verticalHeaderFormat = QCalendarWidget.ISOWeekNumbers
321            if self.__cw:
322                self.__cw.setVerticalHeaderFormat(QCalendarWidget.ISOWeekNumbers)
323    verticalHeaderFormat = pyqtProperty(QCalendarWidget.VerticalHeaderFormat,
324                                               fget=getVerticalHeaderFormat,
325                                               fset=setVerticalHeaderFormat,
326                                               freset=resetVerticalHeaderFormat)
327
328    #
329    # Property navigationBarVisible: bool
330    # Get: isNavigationBarVisible()
331    # Set: setNavigationBarVisible()
332    # Reset: resetNavigationBarVisible()
333    #
334    def isNavigationBarVisible(self):
335        return self.__navigationBarVisible
336    def setNavigationBarVisible(self, visible):
337        if visible != self.__navigationBarVisible:
338            self.__navigationBarVisible = visible
339            if self.__cw:
340                self.__cw.setNavigationBarVisible(visible)
341    def resetNavigationBarVisible(self):
342        if self.__navigationBarVisible != True:
343            self.__navigationBarVisible = True
344            if self.__cw:
345                self.__cw.setNavigationBarVisible(True)
346    navigationBarVisible = pyqtProperty(bool,
347                                               fget=isNavigationBarVisible,
348                                               fset=setNavigationBarVisible,
349                                               freset=resetNavigationBarVisible)
350
351
352if __name__ == "__main__":
353
354    import sys
355
356    app = QApplication(sys.argv)
357
358    w = QWidget()
359    lay = QHBoxLayout()
360
361    lay.addWidget(PyDateEdit())
362    lay.addWidget(PyDateTimeEdit())
363
364    w.setLayout(lay)
365    w.show()
366
367    sys.exit(app.exec_())
368
369#============================================================================#
370# EOF                                                                        #
371#----------------------------------------------------------------------------#
372