#include "lds_datetimeedit_pop.h"
#include "ui_lds_datetimeedit_pop.h"
#include <QtDebug>
#include "lds_datetimeedit_year.h"
#include <QCalendarWidget>
#include "lds_datetimeedit_key.h"
lds_datetimeedit_pop::lds_datetimeedit_pop(QDateTimeEdit *parent) :
QWidget(parent),
ui(new Ui::lds_datetimeedit_pop),
_dtedit(parent)
{
ui->setupUi(this);
_curdt=_dtedit->dateTime();
//0
pop_year=new lds_datetimeedit_year;
// pop_year->setcenter_year(_curdt.date().year());
connect(pop_year,SIGNAL(signal_year_check(int)),this,SLOT(update_year(int)));
ui->stackedWidget->addWidget(pop_year);
//1.1
calender=new QCalendarWidget;
calender->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
calender->setNavigationBarVisible(false);
// calender->setSelectedDate(_curdt.date());
connect(calender,SIGNAL(clicked(QDate)),this,SLOT(update_date(QDate)));
ui->stackedWidget->addWidget(calender);
//1.2
pop_key=new lds_datetimeedit_key;
connect(pop_key, SIGNAL(signal_num_check(int)), this,SLOT(update_fromkey(int)));
ui->stackedWidget->addWidget(pop_key);
//2
connect(ui->radioButton_hh,SIGNAL(toggled(bool)),this,SLOT(center(bool)));
connect(ui->radioButton_mm,SIGNAL(toggled(bool)),this,SLOT(center(bool)));
connect(ui->radioButton_MM_dd,SIGNAL(toggled(bool)),this,SLOT(center(bool)));
connect(ui->radioButton_ss,SIGNAL(toggled(bool)),this,SLOT(center(bool)));
connect(ui->radioButton_yyyy,SIGNAL(toggled(bool)),this,SLOT(center(bool)));
connect(ui->pushButton_7,SIGNAL(clicked()),this,SLOT(tocancel()));
connect(ui->pushButton_8,SIGNAL(clicked()),this,SLOT(took()));
//h
//m
//s
//hh:mm
//mm:ss
//hh:mm:ss
//3
ui->radioButton_hh->setVisible(_dtedit->displayFormat().contains("h")||_dtedit->displayFormat().contains("H"));
ui->radioButton_mm->setVisible(_dtedit->displayFormat().contains("m"));
ui->radioButton_ss->setVisible(_dtedit->displayFormat().contains("s"));
int timeshow=!ui->radioButton_hh->isHidden()+!ui->radioButton_mm->isHidden()+!ui->radioButton_ss->isHidden();
if(timeshow==0||timeshow==1)
{ui->label_t_1->setVisible(false);ui->label_t_2->setVisible(false);}
if(timeshow==1)
{
if(!ui->radioButton_hh->isHidden())ui->radioButton_hh->setChecked(true);
if(!ui->radioButton_mm->isHidden())ui->radioButton_mm->setChecked(true);
if(!ui->radioButton_ss->isHidden())ui->radioButton_ss->setChecked(true);
}
if(timeshow==2&&!ui->radioButton_hh->isHidden()&&!ui->radioButton_mm->isHidden())
{ui->label_t_2->setVisible(false);ui->radioButton_hh->setChecked(true);}
if(timeshow==2&&!ui->radioButton_mm->isHidden()&&!ui->radioButton_ss->isHidden())
{ui->label_t_1->setVisible(false);ui->radioButton_mm->setChecked(true);}
if(timeshow==3)
{ui->radioButton_hh->setChecked(true);}
//yyyy
//MM-dd
//yyyy-MM-dd
ui->radioButton_yyyy->setVisible(_dtedit->displayFormat().contains("y"));
ui->radioButton_MM_dd->setVisible(_dtedit->displayFormat().contains("M"));
int dateshow=!ui->radioButton_yyyy->isHidden()+!ui->radioButton_MM_dd->isHidden();
if(dateshow==0)
{ui->label_d_1->setVisible(false);}
if(dateshow==1){
ui->label_d_1->setVisible(false);
if(!ui->radioButton_MM_dd->isHidden())ui->radioButton_MM_dd->setChecked(true);
}
if(!ui->radioButton_yyyy->isHidden())ui->radioButton_yyyy->setChecked(true);
ui->label->setText(_dtedit->displayFormat());
}
lds_datetimeedit_pop::~lds_datetimeedit_pop()
{
delete ui;
}
void lds_datetimeedit_pop::update_date(const QDate &dt)
{
update_year(dt.year());
update_Md(dt.month(), dt.day());
}
void lds_datetimeedit_pop::update_year(int y)
{
_curdt.setDate(QDate(y, _curdt.date().month(), _curdt.date().day()));
ui->radioButton_yyyy->setText(QString::number(y));
}
void lds_datetimeedit_pop::update_Md(int M, int d)
{
_curdt.setDate(QDate(_curdt.date().year(), M, d));
ui->radioButton_MM_dd->setText(QString::number(M)+"-"+QString::number(d));
}
void lds_datetimeedit_pop::update_hms(const QTime &t)
{
update_h(t.hour());
update_m(t.minute());
update_s(t.second());
}
void lds_datetimeedit_pop::update_h(int h)
{
_curdt.setTime(QTime(h, _curdt.time().minute(), _curdt.time().second()));
ui->radioButton_hh->setText(QString::number(h));
}
void lds_datetimeedit_pop::update_m(int m)
{
_curdt.setTime(QTime(_curdt.time().hour(), m, _curdt.time().second()));
ui->radioButton_mm->setText(QString::number(m));
}
void lds_datetimeedit_pop::update_s(int s)
{
_curdt.setTime(QTime(_curdt.time().hour(), _curdt.time().minute(), s));
ui->radioButton_ss->setText(QString::number(s));
}
void lds_datetimeedit_pop::update_fromkey(int k)
{
if(_rb==0) return;
int curt=_rb->text().toInt();
int value;
int index=-1;
curt=qAbs(curt);
if(_rb==ui->radioButton_hh){
index=0;
} else if(_rb==ui->radioButton_mm){
index=1;
} else if(_rb==ui->radioButton_ss){
index=2;
}
if(k==-1){
_rb->setText(lds_datetimeedit_key::geth_m_s(curt+1, index));
} else {
if(k==-2){
_rb->setText(lds_datetimeedit_key::geth_m_s(curt-1, index));
} else {
_rb->setText(lds_datetimeedit_key::geth_m_s(curt%10*10+k, index));
}
}
}
void lds_datetimeedit_pop::center(bool flag)
{
if(flag){
_rb=0;
if(ui->radioButton_hh->isChecked()){
_rb=ui->radioButton_hh;
ui->stackedWidget->setCurrentIndex(2);
} else if(ui->radioButton_mm->isChecked()){
_rb=ui->radioButton_mm;
ui->stackedWidget->setCurrentIndex(2);
} else if(ui->radioButton_MM_dd->isChecked()){
_rb=ui->radioButton_MM_dd;
ui->stackedWidget->setCurrentIndex(1);
} else if(ui->radioButton_ss->isChecked()){
_rb=ui->radioButton_ss;
ui->stackedWidget->setCurrentIndex(2);
} else if(ui->radioButton_yyyy->isChecked()){
_rb=ui->radioButton_yyyy;
ui->stackedWidget->setCurrentIndex(0);
}
pop_year->setcenter_year(_curdt.date().year());
calender->setSelectedDate(_curdt.date());
}
}
void lds_datetimeedit_pop::took()
{
emit signal_dt(_curdt);
this->close();
}
void lds_datetimeedit_pop::tocancel()
{
this->close();
}
void lds_datetimeedit_pop::showEvent(QShowEvent *event)
{
ui->radioButton_yyyy->setText(_dtedit->dateTime().toString("yyyy"));
ui->radioButton_MM_dd->setText(_dtedit->dateTime().toString("MM-dd"));
ui->radioButton_hh->setText(_dtedit->dateTime().toString("hh"));
ui->radioButton_mm->setText(_dtedit->dateTime().toString("mm"));
ui->radioButton_ss->setText(_dtedit->dateTime().toString("ss"));
QWidget::showEvent(event);
}