提交 64e6b69b 编写于 作者: 之一Yo's avatar 之一Yo

添加日期时间选择器格式化器 `PickerColumnFormatter`

上级 be4d2d03
......@@ -62,69 +62,6 @@
<translation>取消</translation>
</message>
</context>
<context>
<name>DatePicker</name>
<message>
<source>month</source>
<translation></translation>
</message>
<message>
<source>day</source>
<translation></translation>
</message>
<message>
<source>year</source>
<translation></translation>
</message>
<message>
<source>January</source>
<translation>一月</translation>
</message>
<message>
<source>February</source>
<translation>二月</translation>
</message>
<message>
<source>March</source>
<translation>三月</translation>
</message>
<message>
<source>April</source>
<translation>四月</translation>
</message>
<message>
<source>May</source>
<translation>五月</translation>
</message>
<message>
<source>June</source>
<translation>六月</translation>
</message>
<message>
<source>July</source>
<translation>七月</translation>
</message>
<message>
<source>August</source>
<translation>八月</translation>
</message>
<message>
<source>September</source>
<translation>九月</translation>
</message>
<message>
<source>October</source>
<translation>十月</translation>
</message>
<message>
<source>November</source>
<translation>十一月</translation>
</message>
<message>
<source>December</source>
<translation>十二月</translation>
</message>
</context>
<context>
<name>FolderListDialog</name>
<message>
......
# coding:utf-8
from PyQt5.QtCore import Qt
from qfluentwidgets import DatePicker, TimePicker, AMTimePicker
from qfluentwidgets import DatePicker, TimePicker, AMTimePicker, ZhDatePicker
from .gallery_interface import GalleryInterface
from ..common.translator import Translator
......@@ -26,7 +26,7 @@ class DateTimeInterface(GalleryInterface):
self.addExampleCard(
title=self.tr('A DatePicker in another format'),
widget=DatePicker(self, DatePicker.YYYY_MM_DD),
widget=ZhDatePicker(self),
sourcePath='https://github.com/zhiyiYo/PyQt-Fluent-Widgets/blob/master/examples/date_time/demo.py'
)
......
......@@ -5,7 +5,18 @@ from PyQt5.QtCore import QDate, Qt, QTime
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from qfluentwidgets import TimePicker, AMTimePicker, DatePicker, setTheme, Theme
from qfluentwidgets import TimePicker, AMTimePicker, DatePicker, ZhDatePicker, setTheme, Theme, PickerColumnFormatter
class SecondsFormatter(PickerColumnFormatter):
""" Seconds formatter """
def encode(self, value):
return str(value) + "秒"
def decode(self, value: str):
return int(value[:-1])
class Demo(QWidget):
......@@ -18,26 +29,32 @@ class Demo(QWidget):
self.vBoxLayout = QVBoxLayout(self)
self.picker0 = DatePicker(self, isMonthTight=True)
self.picker1 = AMTimePicker(self)
self.picker2 = TimePicker(self)
self.picker3 = TimePicker(self, True)
self.datePicker1 = DatePicker(self)
self.datePicker2 = ZhDatePicker(self)
self.timePicker1 = AMTimePicker(self)
self.timePicker2 = TimePicker(self)
self.timePicker3 = TimePicker(self, showSeconds=True)
# customize column format
self.timePicker3.setColumnFormatter(2, SecondsFormatter())
self.picker0.dateChanged.connect(lambda t: print(t.toString()))
self.picker1.timeChanged.connect(lambda t: print(t.toString()))
self.picker2.timeChanged.connect(lambda t: print(t.toString()))
self.picker3.timeChanged.connect(lambda t: print(t.toString()))
self.datePicker1.dateChanged.connect(lambda t: print(t.toString()))
self.datePicker2.dateChanged.connect(lambda t: print(t.toString()))
self.timePicker1.timeChanged.connect(lambda t: print(t.toString()))
self.timePicker2.timeChanged.connect(lambda t: print(t.toString()))
self.timePicker3.timeChanged.connect(lambda t: print(t.toString()))
# set current date/time
# self.picker0.setDate(QDate.currentDate())
# self.picker1.setTime(QTime(13, 15))
# self.picker2.setTime(QTime(13, 15))
# self.datePicker1.setDate(QDate.currentDate())
# self.timePicker1.setTime(QTime(13, 15))
# self.timePicker2.setTime(QTime(13, 15))
self.resize(500, 500)
self.vBoxLayout.addWidget(self.picker0, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.picker1, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.picker2, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.picker3, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.datePicker1, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.datePicker2, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.timePicker1, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.timePicker2, 0, Qt.AlignHCenter)
self.vBoxLayout.addWidget(self.timePicker3, 0, Qt.AlignHCenter)
if __name__ == '__main__':
......
......@@ -12,7 +12,7 @@ Examples are available at https://github.com/zhiyiYo/PyQt-Fluent-Widgets/tree/ma
:license: GPLv3, see LICENSE for more details.
"""
__version__ = "0.7.0"
__version__ = "0.7.1"
from .components import *
from .common import *
......
......@@ -99,11 +99,14 @@ PickerBase:disabled {
border-right: transparent;
}
#pickerButton[enter=true]:enabled,
#pickerButton[hasValue=true]:enabled {
#pickerButton[enter=true]:enabled {
color: rgba(0, 0, 0, 0.896);
}
#pickerButton[hasValue=true]:enabled{
color: rgb(0, 0, 0);
}
#pickerButton[pressed=true] {
color: rgba(0, 0, 0, 0.6);
}
......
此差异已折叠。
from .date_picker import DatePickerBase, DatePicker
from .picker_base import PickerBase, PickerPanel
from .date_picker import DatePickerBase, DatePicker, ZhDatePicker
from .picker_base import PickerBase, PickerPanel, PickerColumnFormatter
from .time_picker import TimePicker, AMTimePicker
\ No newline at end of file
# coding:utf-8
from PyQt5.QtGui import QFontMetrics, QFont
from PyQt5.QtCore import Qt, pyqtSignal, QDate, QCalendar
from .picker_base import PickerBase, PickerPanel
from .picker_base import PickerBase, PickerPanel, PickerColumnFormatter, DigitFormatter
class DatePickerBase(PickerBase):
......@@ -14,11 +13,51 @@ class DatePickerBase(PickerBase):
super().__init__(parent)
self.date = QDate()
self.calendar = QCalendar()
self._yearFormatter = None
self._monthFormatter = None
self._dayFormatter = None
def setDate(self, date: QDate):
""" set current date """
raise NotImplementedError
def setYearFormatter(self, formatter: PickerColumnFormatter):
self._yearFormatter = formatter
def setMonthFormatter(self, formatter: PickerColumnFormatter):
self._monthFormatter = formatter
def setDayFormatter(self, formatter: PickerColumnFormatter):
self._dayFormatter = formatter
def yearFormatter(self):
return self._yearFormatter or DigitFormatter()
def dayFormatter(self):
return self._dayFormatter or DigitFormatter()
def monthFormatter(self):
return self._monthFormatter or MonthFormatter()
class MonthFormatter(PickerColumnFormatter):
""" Month formatter """
def __init__(self):
super().__init__()
self.months = [
self.tr('January'), self.tr('February'), self.tr('March'),
self.tr('April'), self.tr('May'), self.tr('June'),
self.tr('July'), self.tr('August'), self.tr('September'),
self.tr('October'), self.tr('November'), self.tr('December')
]
def encode(self, month):
return self.months[int(month) - 1]
def decode(self, value):
return self.months.index(value) + 1
class DatePicker(DatePickerBase):
""" Date picker """
......@@ -40,13 +79,11 @@ class DatePicker(DatePickerBase):
is the month column tight
"""
super().__init__(parent=parent)
self.MONTH = self.tr('month')
self.YEAR = self.tr('year')
self.DAY = self.tr('day')
self.isMonthTight = isMonthTight
self.months = [
self.tr('January'), self.tr('February'), self.tr('March'),
self.tr('April'), self.tr('May'), self.tr('June'),
self.tr('July'), self.tr('August'), self.tr('September'),
self.tr('October'), self.tr('November'), self.tr('December')
]
self.setDateFormat(format)
def setDateFormat(self, format: int):
......@@ -59,30 +96,42 @@ class DatePicker(DatePickerBase):
"""
self.clearColumns()
y = QDate.currentDate().year()
w = self._monthColumnWidth()
self.dateFormat = format
if format == self.MM_DD_YYYY:
self.monthIndex = 0
self.dayIndex = 1
self.yearIndex = 2
self.addColumn(self.tr('month'), self.months, w, Qt.AlignLeft)
self.addColumn(self.tr('day'), range(1, 32), 80)
self.addColumn(self.tr('year'), range(y-100, y+101), 80)
self.addColumn(self.MONTH, range(1, 13),
80, Qt.AlignLeft, self.monthFormatter())
self.addColumn(self.DAY, range(1, 32),
80, formatter=self.dayFormatter())
self.addColumn(self.YEAR, range(y-100, y+101),
80, formatter=self.yearFormatter())
elif format == self.YYYY_MM_DD:
self.yearIndex = 0
self.monthIndex = 1
self.dayIndex = 2
self.addColumn(self.tr('year'), range(y-100, y+101), 80)
self.addColumn(self.tr('month'), self.months, w)
self.addColumn(self.tr('day'), range(1, 32), 80)
self.addColumn(self.YEAR, range(y-100, y+101),
80, formatter=self.yearFormatter())
self.addColumn(self.MONTH, range(1, 13),
80, formatter=self.monthFormatter())
self.addColumn(self.DAY, range(1, 32), 80,
formatter=self.dayFormatter())
self.setColumnWidth(self.monthIndex, self._monthColumnWidth())
def panelInitialValue(self):
if any(self.value()):
return self.value()
# initialize date
date = self.date.currentDate()
self.columns[self.monthIndex].value = self.months[date.month() - 1]
self.columns[self.dayIndex].value = date.day()
self.columns[self.yearIndex].value = date.year()
date = QDate.currentDate()
y = self.encodeValue(self.yearIndex, date.year())
m = self.encodeValue(self.monthIndex, date.month())
d = self.encodeValue(self.dayIndex, date.day())
return [y, m, d] if self.dateFormat == self.YYYY_MM_DD else [m, d, y]
def setMonthTight(self, isTight: bool):
""" set whether the month column is tight """
......@@ -94,10 +143,11 @@ class DatePicker(DatePickerBase):
def _monthColumnWidth(self):
fm = self.fontMetrics()
wm = max(fm.width(i) for i in self.months) + 20
wm = max(fm.width(i)
for i in self.columns[self.monthIndex].items()) + 20
# don't use tight layout for english
if self.tr('month') == 'month':
if self.MONTH == 'month':
return wm + 49
return max(80, wm) if self.isMonthTight else wm + 49
......@@ -107,23 +157,26 @@ class DatePicker(DatePickerBase):
return
# get days number in month
month = panel.columnValue(self.monthIndex)
month = self.months.index(month) + 1
year = int(panel.columnValue(self.yearIndex))
month = self.decodeValue(
self.monthIndex, panel.columnValue(self.monthIndex))
year = self.decodeValue(
self.yearIndex, panel.columnValue(self.yearIndex))
days = self.calendar.daysInMonth(month, year)
# update days
c = panel.column(self.dayIndex)
day = c.currentItem().text()
c.setItems(range(1, days + 1))
self.setColumnItems(self.dayIndex, range(1, days + 1))
c.setItems(self.columns[self.dayIndex].items())
c.setSelectedItem(day)
self.columns[self.dayIndex].items = list(range(1, days + 1))
def _onConfirmed(self, value: list):
month = self.months.index(value[self.monthIndex]) + 1
year = self.decodeValue(self.yearIndex, value[self.yearIndex])
month = self.decodeValue(self.monthIndex, value[self.monthIndex])
day = self.decodeValue(self.dayIndex, value[self.dayIndex])
date = QDate(int(value[self.yearIndex]), month, int(value[self.dayIndex]))
od = self.date
date, od = QDate(year, month, day), self.date
self.setDate(date)
if od != date:
......@@ -134,7 +187,51 @@ class DatePicker(DatePickerBase):
return
self.date = date
self.setColumnValue(self.monthIndex, self.months[date.month() - 1])
self.setColumnValue(self.monthIndex, date.month())
self.setColumnValue(self.dayIndex, date.day())
self.setColumnValue(self.yearIndex, date.year())
self.columns[self.dayIndex].items = list(range(1, date.daysInMonth() + 1))
\ No newline at end of file
self.setColumnItems(self.dayIndex, range(1, date.daysInMonth() + 1))
class ZhFormatter(PickerColumnFormatter):
""" Chinese date formatter """
suffix = ""
def encode(self, value):
return str(value) + self.suffix
def decode(self, value: str):
return int(value[:-1])
class ZhYearFormatter(ZhFormatter):
""" Chinese year formatter """
suffix = "年"
class ZhMonthFormatter(ZhFormatter):
""" Chinese month formatter """
suffix = "月"
class ZhDayFormatter(ZhFormatter):
""" Chinese day formatter """
suffix = "日"
class ZhDatePicker(DatePicker):
""" Chinese date picker """
def __init__(self, parent=None):
super().__init__(parent, DatePicker.YYYY_MM_DD)
self.MONTH = "月"
self.YEAR = "年"
self.DAY = "日"
self.setDayFormatter(ZhDayFormatter())
self.setYearFormatter(ZhYearFormatter())
self.setMonthFormatter(ZhMonthFormatter())
self.setDateFormat(self.YYYY_MM_DD)
\ No newline at end of file
# coding:utf-8
from typing import Iterable, List
from PyQt5.QtCore import Qt, pyqtSignal, QSize, QRectF, QPoint, QPropertyAnimation, QEasingCurve
from PyQt5.QtGui import QColor, QPainter, QCursor, QRegion, QFont, QFontMetrics
from PyQt5.QtCore import Qt, pyqtSignal, QSize, QRectF, QPoint, QPropertyAnimation, QEasingCurve, QObject
from PyQt5.QtGui import QColor, QPainter, QCursor, QRegion
from PyQt5.QtWidgets import (QApplication, QWidget, QFrame, QVBoxLayout, QHBoxLayout,
QGraphicsDropShadowEffect, QSizePolicy, QPushButton, QListWidgetItem)
......@@ -84,24 +84,108 @@ class ItemMaskWidget(QWidget):
painter.drawText(rect, align, item.text())
class PickerColumn:
""" Picker column """
class PickerColumnFormatter(QObject):
""" Picker column formatter """
def __init__(self, name: str, items: list, width: int, align=Qt.AlignLeft):
self.name = name
self.items = items
self.width = width
self.align = align
def __init__(self):
super().__init__()
def encode(self, value):
""" convert original value to formatted value """
return str(value)
def decode(self, value: str):
""" convert formatted value to original value """
return str(value)
class DigitFormatter(PickerColumnFormatter):
""" Digit formatter """
def decode(self, value):
return int(value)
class PickerColumnButton(QPushButton):
""" Picker column button """
def __init__(self, name: str, items: Iterable, width: int, align=Qt.AlignLeft, formatter=None, parent=None):
super().__init__(text=name, parent=parent)
self._name = name
self._value = None # type: str
self.isVisible = True
@property
def value(self):
return self._value
self.setItems(items)
self.setAlignment(align)
self.setFormatter(formatter)
self.setFixedSize(width, 30)
self.setObjectName('pickerButton')
self.setProperty('hasBorder', False)
self.setAttribute(Qt.WA_TransparentForMouseEvents)
def align(self):
return self._align
def setAlignment(self, align=Qt.AlignCenter):
""" set the text alignment """
if align == Qt.AlignLeft:
self.setProperty('align', 'left')
elif align == Qt.AlignRight:
self.setProperty('align', 'right')
else:
self.setProperty('align', 'center')
self._align = align
self.setStyle(QApplication.style())
def value(self) -> str:
if self._value is None:
return None
return self.formatter().encode(self._value)
def setValue(self, v):
self._value = v
if v is None:
self.setText(self.name())
self.setProperty('hasValue', False)
else:
self.setText(self.value())
self.setProperty('hasValue', True)
self.setStyle(QApplication.style())
def items(self):
return [self._formatter.encode(i) for i in self._items]
def setItems(self, items: Iterable):
self._items = list(items)
def formatter(self):
return self._formatter
def setFormatter(self, formatter):
self._formatter = formatter or PickerColumnFormatter()
def name(self):
return self._name
def setName(self, name: str):
if self.text() == self.name():
self.setText(name)
self._name = name
def checkColumnIndex(func):
""" check whether the index is out of range """
def wrapper(picker, index: int, *args, **kwargs):
if not 0 <= index < len(picker.columns):
return
return func(picker, index, *args, **kwargs)
@value.setter
def value(self, v):
self._value = str(v)
return wrapper
class PickerBase(QPushButton):
......@@ -109,9 +193,7 @@ class PickerBase(QPushButton):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.columns = [] # type: List[PickerColumn]
self.columnMap = {}
self.buttons = [] # type: List[QPushButton]
self.columns = [] # type: List[PickerColumnButton]
self.hBoxLayout = QHBoxLayout(self)
......@@ -122,7 +204,8 @@ class PickerBase(QPushButton):
FluentStyleSheet.TIME_PICKER.apply(self)
self.clicked.connect(self._showPanel)
def addColumn(self, name: str, items: Iterable, width: int, align=Qt.AlignCenter):
def addColumn(self, name: str, items: Iterable, width: int, align=Qt.AlignCenter,
formatter: PickerColumnFormatter = None):
""" add column
Parameters
......@@ -138,86 +221,76 @@ class PickerBase(QPushButton):
align: Qt.AlignmentFlag
the text alignment of button
"""
if name in self.columnMap:
return
# create column
column = PickerColumn(name, list(items), width, align)
self.columns.append(column)
self.columnMap[name] = column
# create button
button = QPushButton(name, self)
button.setFixedSize(width, 30)
button.setObjectName('pickerButton')
button.setProperty('hasBorder', False)
button.setAttribute(Qt.WA_TransparentForMouseEvents)
formatter: PickerColumnFormatter
the formatter of column
"""
# create column button
button = PickerColumnButton(name, items, width, align, formatter, self)
self.columns.append(button)
self.hBoxLayout.addWidget(button, 0, Qt.AlignLeft)
self.buttons.append(button)
self._setButtonAlignment(button, align)
# update the style of buttons
for btn in self.buttons[:-1]:
for btn in self.columns[:-1]:
btn.setProperty('hasBorder', True)
btn.setStyle(QApplication.style())
def _setButtonAlignment(self, button: QPushButton, align=Qt.AlignCenter):
""" set the text alignment of button """
if align == Qt.AlignLeft:
button.setProperty('align', 'left')
elif align == Qt.AlignRight:
button.setProperty('align', 'right')
else:
button.setProperty('align', 'center')
@checkColumnIndex
def setColumnAlignment(self, index: int, align=Qt.AlignCenter):
""" set the text alignment of specified column """
if not 0 <= index < len(self.columns):
return
self.columns[index].align = align
self._setButtonAlignment(self.buttons[index], align)
self.columns[index].setTextAlignment(align)
@checkColumnIndex
def setColumnWidth(self, index: int, width: int):
""" set the width of specified column """
if not 0 <= index < len(self.columns):
return
self.columns[index].width = width
self.buttons[index].setFixedWidth(width)
self.columns[index].setFixedWidth(width)
@checkColumnIndex
def setColumnTight(self, index: int):
""" make the specified column to be tight """
if not 0 <= index < len(self.columns):
return
fm = self.fontMetrics()
w = max(fm.width(i) for i in self.columns[index].items) + 30
self.setColumnWidth(index, w)
@checkColumnIndex
def setColumnVisible(self, index: int, isVisible: bool):
""" set the text alignment of specified column """
if not 0 <= index < len(self.columns):
return
self.columns[index].isVisible = isVisible
self.buttons[index].setVisible(isVisible)
self.columns[index].setVisible(isVisible)
def value(self):
return [c.value for c in self.columns if c.isVisible]
return [c.value() for c in self.columns if c.isVisible()]
def initialValue(self):
return [c.initialValue() for c in self.columns if c.isVisible()]
@checkColumnIndex
def setColumnValue(self, index: int, value):
if not 0 <= index < len(self.columns):
return
self.columns[index].setValue(value)
@checkColumnIndex
def setColumnInitialValue(self, index: int, value):
self.columns[index].setInitialValue(value)
@checkColumnIndex
def setColumnFormatter(self, index: int, formatter: PickerColumnFormatter):
self.columns[index].setFormatter(formatter)
value = str(value)
self.columns[index].value = value
self.buttons[index].setText(value)
self._setButtonProperty('hasValue', True)
@checkColumnIndex
def setColumnItems(self, index: int, items: Iterable):
self.columns[index].setItems(items)
@checkColumnIndex
def encodeValue(self, index: int, value):
""" convert original value to formatted value """
return self.columns[index].formatter().encode(value)
@checkColumnIndex
def decodeValue(self, index: int, value):
""" convert formatted value to origin value """
return self.columns[index].formatter().decode(value)
@checkColumnIndex
def setColumn(self, index: int, name: str, items: Iterable, width: int, align=Qt.AlignCenter):
""" set column
......@@ -238,26 +311,15 @@ class PickerBase(QPushButton):
align: Qt.AlignmentFlag
the text alignment of button
"""
if not 0 <= index < len(self.columns):
return
column = self.columns[index]
self.columnMap.pop(column.name)
column = PickerColumn(name, items, width, align)
self.columns[index] = column
self.columnMap[name] = column
self.buttons[index].setText(name)
self.buttons[index].setFixedWidth(width)
self._setButtonAlignment(self.buttons[index], align)
button = self.columns[index]
button.setText(name)
button.setFixedWidth(width)
button.setAlignment(align)
def clearColumns(self):
""" clear columns """
self.columns.clear()
self.columnMap.clear()
while self.buttons:
btn = self.buttons.pop()
while self.columns:
btn = self.columns.pop()
btn.deleteLater()
def enterEvent(self, e):
......@@ -276,18 +338,22 @@ class PickerBase(QPushButton):
def _setButtonProperty(self, name, value):
""" send event to picker buttons """
for button in self.buttons:
for button in self.columns:
button.setProperty(name, value)
button.setStyle(QApplication.style())
def panelInitialValue(self):
""" initial value of panel """
return self.value()
def _showPanel(self):
""" show panel """
panel = PickerPanel(self)
for column in self.columns:
if column.isVisible:
panel.addColumn(column.items, column.width, column.align)
if column.isVisible():
panel.addColumn(column.items(), column.width(), column.align())
panel.setValue(self.value())
panel.setValue(self.panelInitialValue())
panel.confirmed.connect(self._onConfirmed)
panel.columnValueChanged.connect(
......
......@@ -5,7 +5,7 @@ from PyQt5.QtCore import Qt, pyqtSignal, QSize, QTime
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget
from .picker_base import PickerBase
from .picker_base import PickerBase, PickerColumnFormatter, DigitFormatter
class TimePickerBase(PickerBase):
......@@ -33,6 +33,40 @@ class TimePickerBase(PickerBase):
raise NotImplementedError
class MiniuteFormatter(DigitFormatter):
""" Minute formatter """
def encode(self, minute):
return str(minute).zfill(2)
class AMHourFormatter(DigitFormatter):
""" AM/PM Hour formatter """
def encode(self, hour):
hour = int(hour)
if hour in [0, 12]:
return "12"
return str(hour % 12)
class AMPMFormatter(PickerColumnFormatter):
""" AM/PM formatter """
def __init__(self):
super().__init__()
self.AM = self.tr('AM')
self.PM = self.tr('PM')
def encode(self, hour):
if not str(hour).isdigit():
return str(hour)
hour = int(hour)
return self.AM if hour < 12 else self.PM
class TimePicker(TimePickerBase):
""" 24 hours time picker """
......@@ -40,14 +74,16 @@ class TimePicker(TimePickerBase):
super().__init__(parent, showSeconds)
# add hour column
w = 80 if showSeconds else 120
self.addColumn(self.tr('hour'), range(0, 24), w)
self.addColumn(self.tr('hour'), range(0, 24),
w, formatter=DigitFormatter())
# add minute column
minute = [str(i).zfill(2) for i in range(0, 60)]
self.addColumn(self.tr('minute'), minute, w)
self.addColumn(self.tr('minute'), range(0, 60),
w, formatter=MiniuteFormatter())
# add seconds column
self.addColumn(self.tr('second'), minute, w)
self.addColumn(self.tr('second'), range(0, 60),
w, formatter=MiniuteFormatter())
self.setColumnVisible(2, showSeconds)
def setTime(self, time):
......@@ -56,20 +92,21 @@ class TimePicker(TimePickerBase):
self.time = time
self.setColumnValue(0, time.hour())
self.setColumnValue(1, str(time.minute()).zfill(2))
self.setColumnValue(2, str(time.second()).zfill(2))
self.setColumnValue(1, time.minute())
self.setColumnValue(2, time.second())
def setSecondVisible(self, isVisible: bool):
self.setColumnVisible(2, isVisible)
w = 80 if isVisible else 120
for column, button in zip(self.columns, self.buttons):
for button in self.columns:
button.setFixedWidth(w)
column.width = w
def _onConfirmed(self, value: list):
super()._onConfirmed(value)
h, m = int(value[0]), int(value[1])
s = 0 if len(value) == 2 else int(value[2])
h = self.decodeValue(0, value[0])
m = self.decodeValue(1, value[1])
s = 0 if len(value) == 2 else self.decodeValue(2, value[2])
time = QTime(h, m, s)
ot = self.time
self.setTime(time)
......@@ -77,6 +114,16 @@ class TimePicker(TimePickerBase):
if ot != time:
self.timeChanged.emit(time)
def panelInitialValue(self):
if any(self.value()):
return self.value()
time = QTime.currentTime()
h = self.encodeValue(0, time.hour())
m = self.encodeValue(1, time.minute())
s = self.encodeValue(2, time.second())
return [h, m, s] if self.showSeconds else [h, m]
class AMTimePicker(TimePickerBase):
""" AM/PM time picker """
......@@ -87,18 +134,21 @@ class AMTimePicker(TimePickerBase):
self.PM = self.tr('PM')
# add hour column
self.addColumn(self.tr('hour'), range(1, 13), 80)
self.addColumn(self.tr('hour'), range(1, 13),
80, formatter=AMHourFormatter())
# add minute column
minute = [str(i).zfill(2) for i in range(0, 60)]
self.addColumn(self.tr('minute'), minute, 80)
self.addColumn(self.tr('minute'), range(0, 60),
80, formatter=MiniuteFormatter())
# add second column
self.addColumn(self.tr('second'), minute, 80)
self.addColumn(self.tr('second'), range(0, 60),
80, formatter=MiniuteFormatter())
self.setColumnVisible(2, showSeconds)
# add AM/PM column
self.addColumn(self.AM, [self.AM, self.PM], 80)
self.addColumn(self.AM, [self.AM, self.PM],
80, formatter=AMPMFormatter())
def setSecondVisible(self, isVisible: bool):
self.setColumnVisible(2, isVisible)
......@@ -108,16 +158,10 @@ class AMTimePicker(TimePickerBase):
return
self.time = time
h = time.hour()
if h in [0, 12]:
self.setColumnValue(0, 12)
else:
self.setColumnValue(0, h % 12)
self.setColumnValue(1, str(time.minute()).zfill(2))
self.setColumnValue(2, str(time.second()).zfill(2))
self.setColumnValue(3, self.AM if h < 12 else self.PM)
self.setColumnValue(0, time.hour())
self.setColumnValue(1, time.minute())
self.setColumnValue(2, time.second())
self.setColumnValue(3, time.hour())
def _onConfirmed(self, value: list):
super()._onConfirmed(value)
......@@ -127,8 +171,10 @@ class AMTimePicker(TimePickerBase):
s = 0
else:
h, m, s, p = value
s = self.decodeValue(2, s)
h, m, s = int(h), int(m), int(s)
h = self.decodeValue(0, h)
m = self.decodeValue(1, m)
if p == self.AM:
h = 0 if h == 12 else h
......@@ -141,3 +187,14 @@ class AMTimePicker(TimePickerBase):
if ot != time:
self.timeChanged.emit(time)
def panelInitialValue(self):
if any(self.value()):
return self.value()
time = QTime.currentTime()
h = self.encodeValue(0, time.hour())
m = self.encodeValue(1, time.minute())
s = self.encodeValue(2, time.second())
p = self.encodeValue(3, time.hour())
return [h, m, s, p] if self.showSeconds else [h, m, p]
......@@ -6,7 +6,7 @@ with open('README.md', encoding='utf-8') as f:
setuptools.setup(
name="PyQt-Fluent-Widgets",
version="0.7.0",
version="0.7.1",
keywords="pyqt fluent widgets",
author="zhiyiYo",
author_email="shokokawaii@outlook.com",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册