plancard.cpp 18 KB

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