File: calendar.py

package info (click to toggle)
python-pyqtgraph 0.13.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,068 kB
  • sloc: python: 54,043; makefile: 129; ansic: 40; sh: 2
file content (56 lines) | stat: -rw-r--r-- 1,988 bytes parent folder | download
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
from ...Qt import QtCore, QtWidgets
from ..Parameter import Parameter
from .basetypes import WidgetParameterItem


class CalendarParameterItem(WidgetParameterItem):
    def makeWidget(self):
        self.asSubItem = True
        w = QtWidgets.QCalendarWidget()
        w.setMaximumHeight(200)
        w.sigChanged = w.selectionChanged
        w.value = w.selectedDate
        w.setValue = w.setSelectedDate
        self.hideWidget = False
        self.param.opts.setdefault('default', QtCore.QDate.currentDate())
        return w


class CalendarParameter(Parameter):
    """
    Displays a Qt calendar whose date is specified by a 'format' option.

    ============== ========================================================
    **Options:**
    format         Format for displaying the date and converting from a string. Can be any value accepted by
                   `QDate.toString` and `fromString`, or a stringified version of a QDateFormat enum, i.e. 'ISODate',
                   'TextDate' (default), etc.
    ============== ========================================================
    """

    itemClass = CalendarParameterItem

    def __init__(self, **opts):
        opts.setdefault('format', 'TextDate')
        super().__init__(**opts)

    def _interpretFormat(self, fmt=None):
        fmt = fmt or self.opts.get('format')
        if hasattr(QtCore.Qt.DateFormat, fmt):
            fmt = getattr(QtCore.Qt.DateFormat, fmt)
        return fmt

    def _interpretValue(self, v):
        if isinstance(v, str):
            fmt = self._interpretFormat()
            if fmt is None:
                raise ValueError('Cannot parse date string without a set format')
            v = QtCore.QDate.fromString(v, fmt)
        return v

    def saveState(self, filter=None):
        state = super().saveState(filter)
        fmt = self._interpretFormat()
        if state.get('value', None) is not None:
            state['value'] = state['value'].toString(fmt)
        return state