plancard.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #include "plancard.h"
  2. #include "ui_plancard.h"
  3. #include <QPainter>
  4. #include <QEventLoop>
  5. #include "UIStyleManager.h"
  6. #include "LHQLogAPI.h"
  7. PlanInfo::PlanInfo()
  8. {
  9. onWeekDay = enum_WeekDay::WeekDay_Mon;
  10. onDateTime = QDateTime::fromString("1970-01-01 00:00:00", "yyyy-MM-dd hh:mm:ss");
  11. offWeekDay = enum_WeekDay::WeekDay_Mon;
  12. offDateTime = QDateTime::fromString("1970-01-01 00:00:00", "yyyy-MM-dd hh:mm:ss");
  13. }
  14. PlanInfo::PlanInfo(const PlanInfo& info)
  15. {
  16. onWeekDay = info.onWeekDay;
  17. onDateTime = info.onDateTime;
  18. offWeekDay = info.offWeekDay;
  19. offDateTime = info.offDateTime;
  20. }
  21. PlanInfo& PlanInfo::operator=(const PlanInfo& info)
  22. {
  23. if(this != &info)
  24. {
  25. onWeekDay = info.onWeekDay;
  26. onDateTime = info.onDateTime;
  27. offWeekDay = info.offWeekDay;
  28. offDateTime = info.offDateTime;
  29. }
  30. return *this;
  31. }
  32. bool PlanInfo::operator==(const PlanInfo& info) const
  33. {
  34. if(onWeekDay == info.onWeekDay && onDateTime == info.onDateTime &&
  35. offWeekDay == info.offWeekDay && offDateTime == info.offDateTime)
  36. {
  37. return true;
  38. }
  39. return false;
  40. }
  41. /* 重载比较函数 */
  42. bool CSortModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
  43. {
  44. /* 先比较是正常日还是特殊日 */
  45. auto leftDay = sourceModel()->data(left, UserRole_WeekDay).toInt();
  46. auto rightDay = sourceModel()->data(right, UserRole_WeekDay).toInt();
  47. if(leftDay != rightDay)
  48. {
  49. return leftDay < rightDay; /* 比较周几 */
  50. }
  51. /* 日期类型相等,再比较时间 */
  52. auto leftTime = sourceModel()->data(left, UserRole_Time).toDateTime();
  53. auto rightTime = sourceModel()->data(right, UserRole_Time).toDateTime();
  54. if(leftDay == static_cast<int>(enum_WeekDay::WeekDay_Special))
  55. {
  56. /* 特殊日,比较日期和时间 */
  57. return leftTime < rightTime;
  58. }else {
  59. /* 正常日,只比较时间 */
  60. return leftTime.time() < rightTime.time();
  61. }
  62. }
  63. PlanCard::PlanCard(QWidget *parent) :
  64. QWidget(parent),
  65. ui(new Ui::PlanCard)
  66. {
  67. ui->setupUi(this);
  68. /* 设置最小宽度 */
  69. this->setMinimumWidth(300);
  70. /* 初始化视图模型 */
  71. m_model = new QStandardItemModel(this);
  72. m_model->setColumnCount(2); /* 两列 */
  73. m_model->clear();
  74. m_sortModel = new CSortModel(this);
  75. m_sortModel->setSourceModel(m_model);
  76. m_sortModel->sort(0, Qt::AscendingOrder); /* 默认升序 */
  77. /* 设置排序数据 */
  78. m_sortModel->setSortRole(QtUserRole::UserRole_Time); /* 设置排序角色 */
  79. ui->tableView->setModel(m_sortModel);
  80. /* 设置每列的高度 */
  81. ui->tableView->verticalHeader()->setDefaultSectionSize(34);
  82. /* 设置去掉标题 */
  83. ui->tableView->horizontalHeader()->setVisible(false);
  84. /* 去掉列标题 */
  85. ui->tableView->verticalHeader()->setVisible(false);
  86. /* 去掉网格线 */
  87. ui->tableView->setShowGrid(false);
  88. /* 设置单元格不可编辑 */
  89. ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
  90. /* 禁止显示横向滚动条 */
  91. ui->tableView->horizontalHeader()->setStretchLastSection(true); /* 设置最后一列拉伸 */
  92. /* 设置空白图片和文字的位置 */
  93. setSpaceImageAndTextRect();
  94. /* 注册事件过滤器 */
  95. // ui->tableView->installEventFilter(this);
  96. ui->tableView->viewport()->installEventFilter(this);
  97. /* 连接信号和槽 */
  98. connect(&EPUIStyle, &UIStyleManager::signal_qssChanged, this, &PlanCard::do_setUIStyle);
  99. connect(ui->tableView, &QTableView::clicked, this, &PlanCard::do_tableView_clicked);
  100. /* 设置默认的皮肤 */
  101. do_setUIStyle();
  102. }
  103. PlanCard::~PlanCard()
  104. {
  105. delete ui;
  106. }
  107. /* 设置频率信息 */
  108. void PlanCard::setChannelInfo(const ChannelInfo& info)
  109. {
  110. m_channelInfo = info;
  111. /* 设置标题 */
  112. ui->label_title->setText(m_channelInfo.ChannelName);
  113. }
  114. /* 添加一项 */
  115. void PlanCard::addPlanItem(const PlanInfo& info)
  116. {
  117. auto strStart = getDateString(info.onWeekDay, info.onDateTime);
  118. auto strEnd = getDateString(info.offWeekDay, info.offDateTime);
  119. /* 创建一个item */
  120. QStandardItem* itemStart = new QStandardItem();
  121. itemStart->setText(strStart);
  122. itemStart->setData(static_cast<int>(info.onWeekDay), UserRole_WeekDay); /* 设置周几 */
  123. itemStart->setData(info.onDateTime, UserRole_Time); /* 设置时间 */
  124. QStandardItem* itemEnd = new QStandardItem();
  125. itemEnd->setText(strEnd);
  126. itemEnd->setData(static_cast<int>(info.offWeekDay), UserRole_WeekDay); /* 设置周几 */
  127. itemEnd->setData(info.offDateTime, UserRole_Time); /* 设置时间 */
  128. /* 添加一个项 */
  129. m_model->appendRow(QList<QStandardItem*>{itemStart, itemEnd});
  130. m_isSpace = false; /* 有数据了 */
  131. // LH_WRITE_LOG(QString("计划数目: %1").arg(m_model->rowCount()));
  132. // LH_WRITE_LOG(QString("UI: %1:%2").arg(ui->tableView->width()).arg(ui->tableView->height()));
  133. }
  134. /* 删除一项 */
  135. void PlanCard::deletePlanItem(const enum_WeekDay weekDay, const QDateTime& time, const enum_OnOff onOff)
  136. {
  137. for(auto& it : m_model->findItems(QString(), Qt::MatchContains, 0))
  138. {
  139. auto item = it->data(UserRole_WeekDay).toInt();
  140. if(item == static_cast<int>(weekDay) && it->data(UserRole_Time).toDateTime() == time)
  141. {
  142. /* 删除该行 */
  143. m_model->removeRow(it->row());
  144. break;
  145. }
  146. }
  147. /* 判断数据有没有空 */
  148. if(m_model->rowCount() == 0)
  149. {
  150. m_isSpace = true; /* 没有数据了 */
  151. }
  152. }
  153. /* 删除一项,传入行号 */
  154. PlanInfo PlanCard::deletePlanItem(const int row)
  155. {
  156. if(row < 0 || row >= m_model->rowCount())
  157. {
  158. return PlanInfo(); /* 没有选中行 */
  159. }
  160. /* 获取原始模型的行号 */
  161. auto sourceRow = m_sortModel->mapToSource(m_sortModel->index(row, 0)).row();
  162. /* 获取该行计划 */
  163. auto item1 = m_model->item(sourceRow, 0);
  164. auto item2 = m_model->item(sourceRow, 1);
  165. PlanInfo info;
  166. info.onWeekDay = static_cast<enum_WeekDay>(item1->data(UserRole_WeekDay).toInt());
  167. info.onDateTime = item1->data(UserRole_Time).toDateTime();
  168. info.offWeekDay = static_cast<enum_WeekDay>(item2->data(UserRole_WeekDay).toInt());
  169. info.offDateTime = item2->data(UserRole_Time).toDateTime();
  170. /* 删除该行 */
  171. m_model->removeRow(sourceRow);
  172. /* 判断数据有没有空 */
  173. if(m_model->rowCount() == 0)
  174. {
  175. m_isSpace = true; /* 没有数据了 */
  176. }
  177. return info; /* 返回删除的计划 */
  178. }
  179. /* 修改一项 */
  180. PlanInfo PlanCard::modifyPlanItem(const int row, const PlanInfo& info)
  181. {
  182. if(row < 0 || row >= m_model->rowCount())
  183. {
  184. return PlanInfo(); /* 没有选中行 */
  185. }
  186. /* 获取原始模型的行号 */
  187. auto sourceRow = m_sortModel->mapToSource(m_sortModel->index(row, 0)).row();
  188. /* 获取该行计划 */
  189. auto item1 = m_model->item(sourceRow, 0);
  190. auto item2 = m_model->item(sourceRow, 1);
  191. PlanInfo oldInfo;
  192. oldInfo.onWeekDay = static_cast<enum_WeekDay>(item1->data(UserRole_WeekDay).toInt());
  193. oldInfo.onDateTime = item1->data(UserRole_Time).toDateTime();
  194. oldInfo.offWeekDay = static_cast<enum_WeekDay>(item2->data(UserRole_WeekDay).toInt());
  195. oldInfo.offDateTime = item2->data(UserRole_Time).toDateTime();
  196. // LH_WRITE_LOG(QString("修改前的行: %1").arg(item1->row()));
  197. /* 修改该行 */
  198. item1->setText(getDateString(info.onWeekDay, info.onDateTime));
  199. item1->setData(static_cast<int>(info.onWeekDay), UserRole_WeekDay); /* 设置周几 */
  200. item1->setData(info.onDateTime, UserRole_Time); /* 设置时间 */
  201. item2->setText(getDateString(info.offWeekDay, info.offDateTime));
  202. item2->setData(static_cast<int>(info.offWeekDay), UserRole_WeekDay); /* 设置周几 */
  203. item2->setData(info.offDateTime, UserRole_Time); /* 设置时间 */
  204. // LH_WRITE_LOG(QString("修改后的行: %1").arg(item1->row()));
  205. /* 依旧选中修改完成后的行 */
  206. // ui->tableView->selectRow(item1->row());
  207. return oldInfo; /* 返回修改前的计划 */
  208. }
  209. /* 清空列表 */
  210. void PlanCard::clearPlanItem()
  211. {
  212. for(auto& it : m_model->findItems(QString(), Qt::MatchContains, 0))
  213. {
  214. m_model->removeRow(it->row());
  215. }
  216. /* 判断数据有没有空 */
  217. if(m_model->rowCount() == 0)
  218. {
  219. m_isSpace = true; /* 没有数据了 */
  220. }
  221. }
  222. /* 取出所有的计划信息,用于给WebAPI发送数据 */
  223. void PlanCard::getAllPlanInfo(QList<OnePlanItemInfo>& list)
  224. {
  225. int rowCount = m_model->rowCount();
  226. for(int i = 0; i < rowCount; i++)
  227. {
  228. auto item1 = m_model->item(i, 0);
  229. auto item2 = m_model->item(i, 1);
  230. if(item1 == nullptr || item2 == nullptr)
  231. {
  232. continue;
  233. }
  234. OnePlanItemInfo info1;
  235. info1.ChannelID = m_channelInfo.ChannelID;
  236. info1.ChannelName = m_channelInfo.ChannelName;
  237. info1.onWeekDay = static_cast<enum_WeekDay>(item1->data(UserRole_WeekDay).toInt());
  238. info1.onDateTime = item1->data(UserRole_Time).toDateTime();
  239. info1.offWeekDay = static_cast<enum_WeekDay>(item2->data(UserRole_WeekDay).toInt());
  240. info1.offDateTime = item2->data(UserRole_Time).toDateTime();
  241. list.append(info1);
  242. }
  243. }
  244. /* 设置计划列表 */
  245. void PlanCard::setPlanList(const QList<OnePlanItemInfo>& list)
  246. {
  247. /* 清空当前的计划列表 */
  248. clearPlanItem();
  249. for(const auto& it : list)
  250. {
  251. if(GInfo.isOnlineDB())
  252. {
  253. if(it.ChannelID != m_channelInfo.ChannelID)
  254. {
  255. /* 不是当前频率的计划 */
  256. LH_WRITE_ERROR(QString("频率ID不匹配,当前频率ID:%1, 计划频率ID:%2").arg(m_channelInfo.ChannelID).arg(it.ChannelID));
  257. continue;
  258. }
  259. }
  260. /* 添加计划 */
  261. PlanInfo info;
  262. info.onWeekDay = it.onWeekDay;
  263. info.onDateTime = it.onDateTime;
  264. info.offWeekDay = it.offWeekDay;
  265. info.offDateTime = it.offDateTime;
  266. addPlanItem(info);
  267. }
  268. }
  269. /* 获取计划列表,适用于PlanCard之间传输数据 */
  270. void PlanCard::getPlanList(QList<PlanInfo>& list)
  271. {
  272. for(int i = 0; i < m_model->rowCount(); i++)
  273. {
  274. auto item1 = m_model->item(i, 0);
  275. auto item2 = m_model->item(i, 1);
  276. PlanInfo info;
  277. info.onWeekDay = static_cast<enum_WeekDay>(item1->data(UserRole_WeekDay).toInt());
  278. info.onDateTime = item1->data(UserRole_Time).toDateTime();
  279. info.offWeekDay = static_cast<enum_WeekDay>(item2->data(UserRole_WeekDay).toInt());
  280. info.offDateTime = item2->data(UserRole_Time).toDateTime();
  281. list.append(info);
  282. }
  283. }
  284. /* 设置计划列表,适用于PlanCard之间传输数据 */
  285. void PlanCard::setPlanList(const QList<PlanInfo>& list)
  286. {
  287. /* 清空列表 */
  288. clearPlanItem();
  289. for(const auto& it : list)
  290. {
  291. addPlanItem(it);
  292. }
  293. }
  294. /**
  295. * @brief 获取选中的行
  296. *
  297. * @return int
  298. */
  299. int PlanCard::getSelectedRow() const
  300. {
  301. return ui->tableView->currentIndex().row();
  302. }
  303. PlanInfo PlanCard::getSelectedPlan()
  304. {
  305. auto index = ui->tableView->currentIndex();
  306. if(!index.isValid())
  307. {
  308. return PlanInfo(); /* 没有选中行 */
  309. }
  310. auto item1 = m_model->item(index.row(), 0);
  311. auto item2 = m_model->item(index.row(), 1);
  312. PlanInfo info;
  313. info.onWeekDay = static_cast<enum_WeekDay>(item1->data(UserRole_WeekDay).toInt());
  314. info.onDateTime = item1->data(UserRole_Time).toDateTime();
  315. info.offWeekDay = static_cast<enum_WeekDay>(item2->data(UserRole_WeekDay).toInt());
  316. info.offDateTime = item2->data(UserRole_Time).toDateTime();
  317. return info;
  318. }
  319. /* 设置自身被选中 */
  320. void PlanCard::setSelected(bool isSelected)
  321. {
  322. if(isSelected)
  323. {
  324. ui->widget->setProperty("Selected", true);
  325. }else {
  326. ui->widget->setProperty("Selected", false);
  327. }
  328. ui->widget->style()->unpolish(ui->widget);
  329. ui->widget->style()->polish(ui->widget);
  330. }
  331. /* 自身被点击了,给外部调用,发送点击信号 */
  332. void PlanCard::clickedCard()
  333. {
  334. emit signal_clickedCard(m_channelInfo.ChannelID);
  335. }
  336. /* 更改UI外观 */
  337. void PlanCard::do_setUIStyle()
  338. {
  339. this->setStyleSheet(EPUIStyle.StrQSS_PlanCard);
  340. /* 打开图片 */
  341. if(EPUIStyle.getUIStyle() == enum_UIStyle::UI_Light)
  342. {
  343. m_spaceImage = QPixmap(":/ICON/ICON/space_light.png");
  344. }else {
  345. m_spaceImage = QPixmap(":/ICON/ICON/space_dark.png");
  346. }
  347. }
  348. /* 点击了一个单元格,设置高亮一行 */
  349. void PlanCard::do_tableView_clicked(const QModelIndex& index)
  350. {
  351. if(index.isValid())
  352. {
  353. /* 设置高亮一行 */
  354. ui->tableView->selectRow(index.row());
  355. }
  356. /* 发送信号 */
  357. auto item1 = m_model->item(index.row(), 0);
  358. auto item2 = m_model->item(index.row(), 1);
  359. PlanInfo info;
  360. info.onWeekDay = static_cast<enum_WeekDay>(item1->data(UserRole_WeekDay).toInt());
  361. info.onDateTime = item1->data(UserRole_Time).toDateTime();
  362. info.offWeekDay = static_cast<enum_WeekDay>(item2->data(UserRole_WeekDay).toInt());
  363. info.offDateTime = item2->data(UserRole_Time).toDateTime();
  364. emit signal_clickedItem(info); /* 发送信号 */
  365. // emit signal_clickedCard(m_channelInfo.ChannelID);
  366. }
  367. /* 设置空白图片和文字的位置 */
  368. void PlanCard::setSpaceImageAndTextRect()
  369. {
  370. /* 设置图片大小 */
  371. m_rectSpaceImage.setSize(QSize(108, 87));
  372. /* 设置文本区域大小 */
  373. m_rectSpaceText.setSize(QSize(72, 19));
  374. /* 设置空白图片位置 */
  375. QPoint pointTable = ui->tableView->mapToParent(QPoint(0, 0));
  376. /* 设置空白图片位置,居中显示 */
  377. int x = pointTable.x() + (ui->tableView->width() - m_rectSpaceImage.width()) / 2;
  378. int y = pointTable.y() + (ui->tableView->height() - m_rectSpaceImage.height() - m_rectSpaceText.height() - 5) / 2;
  379. m_rectSpaceImage.moveTo(x, y);
  380. /* 设置空白文字位置 */
  381. x = pointTable.x() + (ui->tableView->width() - m_rectSpaceText.width()) / 2;
  382. y = m_rectSpaceImage.y() + m_rectSpaceImage.height() + 5;
  383. m_rectSpaceText.moveTo(x, y);
  384. }
  385. /* 根据日期获取字符串 */
  386. QString PlanCard::getDateString(const enum_WeekDay weekDay, const QDateTime& time)
  387. {
  388. QString strTime;
  389. switch(weekDay)
  390. {
  391. case enum_WeekDay::WeekDay_Mon:
  392. strTime = QString("星期一 %1").arg(time.toString("hh:mm:ss"));
  393. break;
  394. case enum_WeekDay::WeekDay_Tue:
  395. strTime = QString("星期二 %1").arg(time.toString("hh:mm:ss"));
  396. break;
  397. case enum_WeekDay::WeekDay_Wed:
  398. strTime = QString("星期三 %1").arg(time.toString("hh:mm:ss"));
  399. break;
  400. case enum_WeekDay::WeekDay_Thu:
  401. strTime = QString("星期四 %1").arg(time.toString("hh:mm:ss"));
  402. break;
  403. case enum_WeekDay::WeekDay_Fri:
  404. strTime = QString("星期五 %1").arg(time.toString("hh:mm:ss"));
  405. break;
  406. case enum_WeekDay::WeekDay_Sat:
  407. strTime = QString("星期六 %1").arg(time.toString("hh:mm:ss"));
  408. break;
  409. case enum_WeekDay::WeekDay_Sun:
  410. strTime = QString("星期天 %1").arg(time.toString("hh:mm:ss"));
  411. break;
  412. case enum_WeekDay::WeekDay_Special:
  413. strTime = QString("%1").arg(time.toString("yy-MM-dd hh:mm:ss"));
  414. break;
  415. default:
  416. strTime = QString("未知 %1").arg(time.toString("hh:mm:ss"));
  417. break;
  418. }
  419. return strTime;
  420. }
  421. /* 设置表格大小 */
  422. void PlanCard::setTableViewSize()
  423. {
  424. /* 设置两列一样宽 */
  425. auto horWidth = ui->tableView->width();
  426. ui->tableView->horizontalHeader()->setDefaultSectionSize(horWidth / 2);
  427. // LH_WRITE_LOG(QString("表格大小: %1:%2").arg(ui->tableView->width()).arg(ui->tableView->height()));
  428. // LH_WRITE_LOG(QString("标题宽度: %1").arg(ui->widget_tableHeader->width()));
  429. }
  430. /* 绘制事件 */
  431. void PlanCard::paintEvent(QPaintEvent *event)
  432. {
  433. if(m_isSpace)
  434. {
  435. QPainter painter(this);
  436. /* 绘制图片 */
  437. painter.drawPixmap(m_rectSpaceImage, m_spaceImage);
  438. /* 绘制文字 */
  439. QFont font = painter.font();
  440. font.setPixelSize(12);
  441. painter.setFont(font);
  442. painter.drawText(m_rectSpaceText, Qt::AlignCenter, "暂无相关信息");
  443. }
  444. QWidget::paintEvent(event);
  445. }
  446. /* 大小缩放事件 */
  447. void PlanCard::resizeEvent(QResizeEvent *event)
  448. {
  449. QWidget::resizeEvent(event);
  450. /* 设置空白图片和文字的位置 */
  451. setSpaceImageAndTextRect();
  452. setTableViewSize();
  453. }
  454. /* 显示事件,这里获取到的控件大小信息是正确的 */
  455. void PlanCard::showEvent(QShowEvent *event)
  456. {
  457. QWidget::showEvent(event);
  458. /* 设置空白图片和文字的位置 */
  459. setSpaceImageAndTextRect();
  460. setTableViewSize();
  461. }
  462. /* 鼠标点击事件 */
  463. void PlanCard::mousePressEvent(QMouseEvent *event)
  464. {
  465. QWidget::mousePressEvent(event);
  466. /* 发送信号 */
  467. emit signal_clickedCard(m_channelInfo.ChannelID);
  468. }
  469. /* 事件过滤器 */
  470. bool PlanCard::eventFilter(QObject *watched, QEvent *event)
  471. {
  472. if(watched == ui->tableView->viewport())
  473. {
  474. if(event->type() == QEvent::MouseButtonPress)
  475. {
  476. emit signal_clickedCard(m_channelInfo.ChannelID);
  477. }
  478. }
  479. return QWidget::eventFilter(watched, event);
  480. }