qt/LedOK/wProgramManager/wEditProgram/wpageattr.cpp
2022-01-04 18:11:48 +08:00

136 lines
5.3 KiB
C++

#include "wpageattr.h"
#include "ui_wpageattr.h"
#include "wpagelist.h"
#include "wpageitem.h"
#include "wpageitemwidget.h"
#include <LoUIClass/x_uimsgboxok.h>
wPageAttr::wPageAttr(wPageItem *page, QWidget *parent) :
QWidget(parent),
ui(new Ui::wPageAttr),
m_list(page->listWidget())
{
setAttribute(Qt::WA_DeleteOnClose);
ui->setupUi(this);
setProperty("ssBackground", "dark");
connect(ui->wValidDate, SIGNAL(toggled(bool)), ui->wDateStart, SLOT(setEnabled(bool)));
connect(ui->wValidDate, SIGNAL(toggled(bool)), ui->wDateEnd, SLOT(setEnabled(bool)));
connect(ui->wValidDate, SIGNAL(toggled(bool)), ui->bnDateStart, SLOT(setEnabled(bool)));
connect(ui->wValidDate, SIGNAL(toggled(bool)), ui->bnDateEnd, SLOT(setEnabled(bool)));
connect(ui->bnDateStart, SIGNAL(sDateSelected(const QDate &)), ui->wDateStart, SLOT(setDate(const QDate &)));
connect(ui->bnDateEnd, SIGNAL(sDateSelected(const QDate &)), ui->wDateEnd, SLOT(setDate(const QDate &)));
connect(ui->bnAddPlan, SIGNAL(clicked(bool)), ui->wPlans, SLOT(onAddPlan()));
connect(ui->bnClearPlan, SIGNAL(clicked(bool)), ui->wPlans, SLOT(clear()));
QJsonDocument jRoot = page->jRoot();
ui->wPageName ->setText(jRoot["name"].toString());
ui->wPlayTimes->setValue(jRoot["repeat"].toInt());
bool bDateVaile=jRoot["validDate"]["isValid"].toBool();
ui->wValidDate->setChecked(bDateVaile);
if(bDateVaile)
{
ui->wDateStart->setEnabled(true);
ui->wDateEnd->setEnabled(true);
ui->bnDateStart->setEnabled(true);
ui->bnDateEnd->setEnabled(true);
}
else {
ui->wDateStart->setEnabled(false);
ui->wDateEnd->setEnabled(false);
ui->bnDateStart->setEnabled(false);
ui->bnDateEnd->setEnabled(false);
}
QString strStartDate=jRoot["validDate"]["start"].toString();
QString strEndDate=jRoot["validDate"]["end"].toString();
ui->wDateStart->setDate(QDate::fromString(strStartDate, "yyyy-MM-dd"));
ui->wDateEnd ->setDate(QDate::fromString(strEndDate, "yyyy-MM-dd"));
ui->wPlans ->onRestorePlan(jRoot["plans"].toArray());
wPageItemWidget *w = page->wPage(false);
connect(ui->wPageName, SIGNAL(textEdited(const QString&)), this, SLOT(onNameChanged(const QString&)));
connect(ui->wPlayTimes, SIGNAL(valueChanged(int)), this, SLOT(updateJson()));
connect(ui->wValidDate, SIGNAL(toggled(bool)), this, SLOT(updateJson()));
connect(ui->wDateStart, SIGNAL(dateChanged(const QDate&)), this, SLOT(onDateStartChanged(const QDate&)));
connect(ui->wDateEnd, SIGNAL(dateChanged(const QDate&)), this, SLOT(onDateEndChanged(const QDate&)));
connect(ui->wPlans, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(updateJson()));
connect(ui->wPlayTimes, SIGNAL(valueChanged(int)), w, SLOT(onRepeatChanged(int)));
connect(this, SIGNAL(sigAttrUpdated(const QJsonObject &)), page, SLOT(onAttrUpdated(const QJsonObject &)));
}
wPageAttr::~wPageAttr()
{
delete ui;
}
void wPageAttr::onNameChanged(const QString &name)
{
bool isDuplicate = isNameDuplicate(name);
if(isDuplicate) {
//ui->wPageName->setText(tr("New") + QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
ui->wPageName->setText(tr("New") + QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
}
updateJson();
}
void wPageAttr::onDateStartChanged(const QDate &date)
{
QDate date0 = date;
QDate dateN = ui->wDateEnd->date();
if(!date0.isValid() || date0 > dateN) {
ui->wDateStart->setDate(QDate(date));
X_UIMsgBoxOk *dlg=new X_UIMsgBoxOk(tr("The end time can not be earlier than the start time"),tr("The end time can not be earlier than the start time"),this,0);
dlg->exec();
//QMessageBox::warning(this, "Title", "The end time can not be earlier than the start time");
}
updateJson();
}
void wPageAttr::onDateEndChanged(const QDate &date)
{
QDate date0 = ui->wDateStart->date();
QDate dateN = date;
if(!dateN.isValid() || date0 > dateN) {
X_UIMsgBoxOk *dlg=new X_UIMsgBoxOk(tr("The end time can not be earlier than the start time"),tr("The end time can not be earlier than the start time"),this,0);
dlg->exec();
}
updateJson();
}
void wPageAttr::updateJson()
{
QJsonObject obj_root;
QJsonObject obj_date;
obj_date["isValid"] = QJsonValue(ui->wValidDate->isChecked());
obj_date["start"] = QJsonValue(ui->wDateStart->date().toString("yyyy-MM-dd"));
obj_date["end"] = QJsonValue(ui->wDateEnd->date().toString("yyyy-MM-dd"));
obj_root["name"] = QJsonValue(ui->wPageName->text());
obj_root["repeat"] = QJsonValue(ui->wPlayTimes->value());
obj_root["validDate"] = obj_date;
obj_root["plans"] = ui->wPlans->plansJson();
emit sigAttrUpdated(obj_root);
}
bool wPageAttr::isNameDuplicate(const QString &name)
{
bool res = false;
QListWidget *list = m_list;
if(nullptr != list) {
int n = list->count();
for(int i=0; i<n; i++) {
wPageItem *item = static_cast<wPageItem*>(list->item(i));
if(name == item->name()) {
res = true;
break;
}
}
} else {
res = true;
}
return res;
}