qt/LedOK/program/wplanlist.cpp

67 lines
2.0 KiB
C++
Raw Normal View History

2022-01-04 18:11:48 +08:00
#include "wplanlist.h"
#include "wplanitem.h"
2023-04-25 16:30:58 +08:00
#include <QJsonArray>
2022-01-04 18:11:48 +08:00
2023-04-25 16:30:58 +08:00
wPlanList::wPlanList(QWidget *parent) : QListWidget(parent) {
2022-01-04 18:11:48 +08:00
setAttribute(Qt::WA_DeleteOnClose);
setFrameShape(QListWidget::NoFrame);
}
2023-04-25 16:30:58 +08:00
void wPlanList::adjustItemNum() {
2022-01-04 18:11:48 +08:00
int n = count();
2023-04-25 16:30:58 +08:00
for(int i=0; i<n; i++) static_cast<wPlanItem*>(itemWidget(item(i)))->fdIdx->setNum(i+1);
2022-01-04 18:11:48 +08:00
}
2023-04-25 16:30:58 +08:00
QJsonArray wPlanList::plansJson() {
2022-01-04 18:11:48 +08:00
QJsonArray oRoot;
int n = count();
for(int i=0; i<n; i++) {
wPlanItem *w = static_cast<wPlanItem*>(itemWidget(item(i)));
oRoot.append(w->planJson());
}
return oRoot;
}
//添加时间计划表
void wPlanList::onAddPlan()
{
QListWidgetItem *item = new QListWidgetItem();
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
wPlanItem *widget = new wPlanItem(item);
addItem(item);
setItemWidget(item, widget);
setCurrentItem(item);
connect(widget, SIGNAL(sigItemChanged(QListWidgetItem*)), this, SIGNAL(itemChanged(QListWidgetItem*)));
connect(widget, SIGNAL(sigDelete(QListWidgetItem*)), this, SLOT(onDeletePlan(QListWidgetItem*)));
adjustItemNum();
emit itemChanged(item);
}
void wPlanList::onRestorePlan(const QJsonArray &jRoot)
{
clear();
int n = jRoot.count();
for(int i=0; i<n; i++) {
QListWidgetItem *item = new QListWidgetItem();
item->setFlags(item->flags() & ~Qt::ItemIsSelectable);
2023-04-25 16:30:58 +08:00
auto json = jRoot.at(i).toObject();
wPlanItem *widget = new wPlanItem(item, &json);
2022-01-04 18:11:48 +08:00
addItem(item);
setItemWidget(item, widget);
setCurrentItem(item);
connect(widget, SIGNAL(sigItemChanged(QListWidgetItem*)), this, SIGNAL(itemChanged(QListWidgetItem*)));
connect(widget, SIGNAL(sigDelete(QListWidgetItem*)), this, SLOT(onDeletePlan(QListWidgetItem*)));
}
adjustItemNum();
}
void wPlanList::onDeletePlan(QListWidgetItem *item)
{
int index = row(item);
if(index >= 0) {
removeItemWidget(item);
delete item;
emit itemChanged(nullptr);
}
adjustItemNum();
}