131 lines
4.2 KiB
C++
131 lines
4.2 KiB
C++
#include "DateSlidePickerBox.h"
|
|
#include <QHBoxLayout>
|
|
#include <QLabel>
|
|
#include <QDate>
|
|
DateSlidePickerBox::DateSlidePickerBox(QWidget *parent)
|
|
: QWidget(parent)
|
|
, mLayout(new QHBoxLayout(this))
|
|
, mHundredYearBox(new SlidePickerBox(this))
|
|
, mTenYearBox(new SlidePickerBox(this))
|
|
, mYearBox(new SlidePickerBox(this))
|
|
, mMonthBox(new SlidePickerBox(this))
|
|
, mDayBox(new SlidePickerBox(this)) {
|
|
initBox(mHundredYearBox, {"19", "20"}, 56);
|
|
|
|
QStringList tenYearValues{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
|
|
mTenYearBox->setLoop(true);
|
|
mYearBox->setLoop(true);
|
|
|
|
initBox(mTenYearBox, tenYearValues);
|
|
initBox(mYearBox, tenYearValues);
|
|
|
|
addSplitLabel();
|
|
|
|
mMonthBox->setLoop(true);
|
|
QStringList monthValues{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
|
|
initBox(mMonthBox, monthValues, 56);
|
|
|
|
addSplitLabel();
|
|
|
|
QStringList days(monthValues);
|
|
for (int i = 13; i <= 31; i++) {
|
|
days << QString("%1").arg(i, 2);
|
|
}
|
|
mDayBox->setLoop(true);
|
|
initBox(mDayBox, days, 56);
|
|
|
|
mBigMonthList.append({"01", "03", "05", "07", "08", "10", "12"});
|
|
}
|
|
|
|
void DateSlidePickerBox::addSplitLabel() {
|
|
QLabel* splitLabel = new QLabel(this);
|
|
splitLabel->setText("/");
|
|
splitLabel->setAlignment(Qt::AlignCenter);
|
|
splitLabel->setFixedWidth(50);
|
|
splitLabel->setObjectName("sliderSpliterLabel");
|
|
mLayout->addWidget(splitLabel);
|
|
}
|
|
|
|
void DateSlidePickerBox::initBox(SlidePickerBox* aBox, const QStringList& aItemsValues, int width) {
|
|
aBox->setItems(aItemsValues);
|
|
mLayout->addWidget(aBox);
|
|
aBox->setFixedWidth(width);
|
|
connect(aBox, &SlidePickerBox::valueChanged, this, &DateSlidePickerBox::leapYearAndFebruaryAdjust);
|
|
}
|
|
|
|
void DateSlidePickerBox::resizeLabel() {
|
|
mHundredYearBox->resizeLabelWidth();
|
|
mTenYearBox->resizeLabelWidth();
|
|
mYearBox->resizeLabelWidth();
|
|
mMonthBox->resizeLabelWidth();
|
|
mDayBox->resizeLabelWidth();
|
|
}
|
|
|
|
QString DateSlidePickerBox::getSelectedValue() const{
|
|
return QString("%1%2%3-%4-%5").arg(mHundredYearBox->getSelectedValue()).
|
|
arg(mTenYearBox->getSelectedValue()).arg(mYearBox->getSelectedValue())
|
|
.arg(mMonthBox->getSelectedValue()).arg(mDayBox->getSelectedValue());
|
|
}
|
|
|
|
void DateSlidePickerBox::setSelectedValue(const QString &val) {
|
|
mHundredYearBox->setSelectedValue(val.left(2));
|
|
mTenYearBox->setSelectedValue(val.left(3).right(1));
|
|
mYearBox->setSelectedValue(val.left(4).right(1));
|
|
QStringList splitList = val.split('-');
|
|
if (splitList.length()<3) return;
|
|
mMonthBox->setSelectedValue(splitList[1]);
|
|
mDayBox->setSelectedValue(splitList[2]);
|
|
}
|
|
|
|
int DateSlidePickerBox::getYear() {
|
|
return QString("%1%2%3").arg(mHundredYearBox->getSelectedValue()).
|
|
arg(mTenYearBox->getSelectedValue()).arg(mYearBox->getSelectedValue()).toInt();
|
|
}
|
|
|
|
void DateSlidePickerBox::leapYearAndFebruaryAdjust() {
|
|
QString d = mDayBox->getSelectedValue();
|
|
//闰年且二月
|
|
if (QDate::isLeapYear(getYear()) && mMonthBox->getSelectedValue() == "02") {
|
|
//seq is important, must enable item firstly
|
|
mDayBox->enableItem("29");
|
|
if (d.toInt() > 29) {
|
|
mDayBox->setSelectedValue("29");
|
|
}
|
|
|
|
mDayBox->disableItem("30");
|
|
mDayBox->disableItem("31");
|
|
}
|
|
//普通年份2月或其他月份
|
|
else{
|
|
//普通年份2月
|
|
if (mMonthBox->getSelectedValue() == "02") {
|
|
//seq is important, must set selected value firstly
|
|
if (d.toInt() > 28) {
|
|
mDayBox->setSelectedValue("28");
|
|
}
|
|
mDayBox->disableItem("29");
|
|
mDayBox->disableItem("30");
|
|
mDayBox->disableItem("31");
|
|
}
|
|
//其他月份
|
|
else{
|
|
mDayBox->enableItem("29");
|
|
mDayBox->enableItem("30");
|
|
if (isBigMonth()){
|
|
mDayBox->enableItem("31");
|
|
}
|
|
else{
|
|
//seq is important, must set selected value firstly
|
|
if (d == "31") {
|
|
mDayBox->setSelectedValue("30");
|
|
}
|
|
mDayBox->disableItem("31");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool DateSlidePickerBox::isBigMonth() const {
|
|
return mBigMonthList.contains(mMonthBox->getSelectedValue());
|
|
}
|