Add new components, DateSlidePickerBox and DateSelectDialog.

This commit is contained in:
Krad
2022-04-01 15:31:38 +08:00
parent 01bcd510e6
commit 3835339758
4 changed files with 247 additions and 0 deletions

37
src/DateSelectDialog.cpp Normal file
View File

@@ -0,0 +1,37 @@
//
// Created by Krad on 2022/3/24.
//
#include "DateSelectDialog.h"
#include <QVBoxLayout>
#include "components/DateSlidePickerBox.h"
#include <QDate>
DateSelectDialog::DateSelectDialog(QWidget *parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) {
this->setFixedSize(460, 380);
QVBoxLayout* layout = new QVBoxLayout(formWidget);
box = new DateSlidePickerBox(formWidget);
box->setObjectName("slider_one");
box->setSelectedValue("1990-01-01");
layout->addWidget(box);
}
DateSelectDialog::~DateSelectDialog() {
}
QString DateSelectDialog::getSelectedValue() {
return box->getSelectedValue();
}
void DateSelectDialog::setSelectedValue(const QString &val) {
box->setSelectedValue(val);
box->resizeLabel();
}
bool DateSelectDialog::updateReferenceData() {
return true;
}
void DateSelectDialog::showEvent(QShowEvent * event) {
QDialog::showEvent(event);
}

24
src/DateSelectDialog.h Normal file
View File

@@ -0,0 +1,24 @@
//
// Created by Krad on 2022/3/24.
//
#ifndef GUI_DATESELECTDIALOG_H
#define GUI_DATESELECTDIALOG_H
#include "GUIFormBaseDialog.h"
class DateSlidePickerBox;
class DateSelectDialog:public GUIFormBaseDialog{
Q_OBJECT
public:
explicit DateSelectDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
~DateSelectDialog() override;
QString getSelectedValue();
void setSelectedValue(const QString& val);
void showEvent(QShowEvent *) override;
protected:
bool updateReferenceData() override;
DateSlidePickerBox* box;
};
#endif //GUI_DATESELECTDIALOG_H

View File

@@ -0,0 +1,152 @@
//
// Created by Krad on 2022/3/24.
//
#include "DateSlidePickerBox.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QDate>
DateSlidePickerBox::DateSlidePickerBox(QWidget *parent) {
layout = new QHBoxLayout(this);
myear = new SlidePickerBox(this);
QStringList centry;
centry<<"19"<<"20";
myear->setFixedWidth(56);
myear->setItems(centry);
layout->addWidget(myear);
tyear = new SlidePickerBox(this);
QStringList m_centry;
m_centry << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9";
tyear->setItems(m_centry);
tyear->setFixedWidth(50);
layout->addWidget(tyear);
year = new SlidePickerBox(this);
year->setItems(m_centry);
year->setFixedWidth(50);
layout->addWidget(year);
QLabel* label = new QLabel(this);
label->setText("/");
// label->setFixedHeight(110);
label->setAlignment(Qt::AlignCenter);
label->setFixedWidth(50);
label->setObjectName("sliderSpliterLabel");
layout->addWidget(label);
month = new SlidePickerBox(this);
QStringList m_mouth;
m_mouth << "01" << "02" << "03" << "04" << "05" << "06" << "07" << "08" << "09"<<"10"<<"11"<<"12";
month->setItems(m_mouth);
month->setFixedWidth(56);
layout->addWidget(month);
QLabel* label2 = new QLabel(this);
label2->setText("/");
// label2->setFixedHeight(110);
label2->setAlignment(Qt::AlignCenter);
// label2->setFixedHeight(80);
label2->setFixedWidth(50);
label2->setObjectName("sliderSpliterLabel");
layout->addWidget(label2);
day = new SlidePickerBox(this);
QStringList days;
days << "01" << "02" << "03" << "04" << "05" << "06" << "07" << "08" << "09";
for (int i=10;i<=31;i++){
days<<QString("%1").arg(i);
}
day->setFixedWidth(56);
day->setItems(days);
layout->addWidget(day);
bigMonth << "01" << "03" << "05" << "07" << "08" << "10" << "12";
connect(myear,&SlidePickerBox::valueChanged,this,&DateSlidePickerBox::leapYearAndFebruaryAdjust);
connect(tyear,&SlidePickerBox::valueChanged,this,&DateSlidePickerBox::leapYearAndFebruaryAdjust);
connect(year,&SlidePickerBox::valueChanged,this,&DateSlidePickerBox::leapYearAndFebruaryAdjust);
connect(month,&SlidePickerBox::valueChanged,this,&DateSlidePickerBox::leapYearAndFebruaryAdjust);
}
void DateSlidePickerBox::resizeLabel() {
myear->resizeLabelWidth();
tyear->resizeLabelWidth();
year->resizeLabelWidth();
month->resizeLabelWidth();
day->resizeLabelWidth();
}
QString DateSlidePickerBox::getSelectedValue() {
return QString("%1%2%3-%4-%5").arg(myear->getSelectedValue()).
arg(tyear->getSelectedValue()).arg(year->getSelectedValue())
.arg(month->getSelectedValue()).arg(day->getSelectedValue());
}
void DateSlidePickerBox::setSelectedValue(const QString &val) {
myear->setSelectedValue(val.left(2));
tyear->setSelectedValue(val.left(3).right(1));
year->setSelectedValue(val.left(4).right(1));
QStringList sary = val.split('-');
month->setSelectedValue(sary[1]);
day->setSelectedValue(sary[2]);
}
int DateSlidePickerBox::getYear() {
return QString("%1%2%3").arg(myear->getSelectedValue()).
arg(tyear->getSelectedValue()).arg(year->getSelectedValue()).toInt();
}
void DateSlidePickerBox::leapYearAndFebruaryAdjust() {
QString d = day->getSelectedValue();
//闰年且二月
if (QDate::isLeapYear(getYear()) && month->getSelectedValue() == "02") {
//seq is important, must enable item firstly
day->enableItem("29");
if (d.toInt() > 29) {
day->setSelectedValue("29");
}
day->disableItem("30");
day->disableItem("31");
}
//普通年份2月或其他月份
else{
//普通年份2月
if (month->getSelectedValue() == "02") {
//seq is important, must set selected value firstly
if (d.toInt() > 28) {
day->setSelectedValue("28");
}
day->disableItem("29");
day->disableItem("30");
day->disableItem("31");
}
//其他月份
else{
day->enableItem("29");
day->enableItem("30");
if (isBigMonth()){
day->enableItem("31");
}
else{
//seq is important, must set selected value firstly
if (d == "31") {
day->setSelectedValue("30");
}
day->disableItem("31");
}
}
}
// day->setSelectedValue(d);
}
bool DateSlidePickerBox::isBigMonth() {
return bigMonth.contains(month->getSelectedValue());
}

View File

@@ -0,0 +1,34 @@
//
// Created by Krad on 2022/3/24.
//
#ifndef GUI_DATESLIDEPICKERBOX_H
#define GUI_DATESLIDEPICKERBOX_H
#include <QWidget>
#include "SlidePickerBox.h"
#include <QDate>
class QHBoxLayout;
class DateSlidePickerBox:public QWidget {
Q_OBJECT
public:
explicit DateSlidePickerBox(QWidget *parent = nullptr);
QString getSelectedValue();
void setSelectedValue(const QString& val);
void resizeLabel();
SlidePickerBox* myear;
SlidePickerBox* tyear;
SlidePickerBox* year;
SlidePickerBox* month;
SlidePickerBox* day;
public Q_SLOTS:
void leapYearAndFebruaryAdjust();
private:
int getYear();
QStringList bigMonth;
bool isBigMonth();
QHBoxLayout* layout;
};
#endif //GUI_DATESLIDEPICKERBOX_H