addnormalitem.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "addnormalitem.h"
  2. #include "ui_addnormalitem.h"
  3. #include <memory>
  4. #include <QDebug>
  5. #include <QListView>
  6. #include <QFile>
  7. #include <QApplication>
  8. #include <QDesktopWidget>
  9. #include "common/combobox/customcombobox.h"
  10. #include "LHQLogAPI.h"
  11. #include "transmitterswitchinfo.h"
  12. #include "common/SelectTime/timewidget.h"
  13. #include "ItemData.h"
  14. AddNormalItem::AddNormalItem(QWidget *parent) :
  15. QDialog(parent),
  16. ui(new Ui::AddNormalItem)
  17. {
  18. ui->setupUi(this);
  19. /* 设置无边框 */
  20. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  21. /* 设置底层样式表,让最底层的透明 */
  22. this->setAttribute(Qt::WA_TranslucentBackground);
  23. /* 加载QSS */
  24. QFile file(":/QSS/QSS/AddNormalItem_Light.qss");
  25. if(file.open(QIODevice::ReadOnly))
  26. {
  27. QString stylesheet = file.readAll();
  28. this->setStyleSheet(stylesheet);
  29. file.close();
  30. } else
  31. {
  32. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  33. }
  34. /* 设置comboBox阴影 */
  35. ui->comBox_devSelect->setViewShadowEffect();
  36. ui->comBox_actionSelect->setViewShadowEffect();
  37. ui->label_timeWarn->hide();
  38. ui->label_devWarn->hide();
  39. ui->label_actionWarn->hide();
  40. /* 获取屏幕大小 */
  41. m_rectScreen = QApplication::desktop()->availableGeometry();
  42. this->resize(m_rectScreen.width(), m_rectScreen.height());
  43. /* 设置默认时间 */
  44. m_time.setHMS(0, 0, 0);
  45. /* 连接信号和槽 */
  46. connect(ui->pBtn_Close, &QPushButton::clicked, this, &QDialog::close);
  47. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &QDialog::close);
  48. connect(ui->pBtn_ok, &QPushButton::clicked, this, &AddNormalItem::do_ok);
  49. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &AddNormalItem::close);
  50. /* 设备选择 */
  51. connect(ui->comBox_devSelect, QOverload<const QString&>::of(&QComboBox::currentTextChanged), this, &AddNormalItem::do_selectDev);
  52. /* 动作选择 */
  53. connect(ui->comBox_actionSelect,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this, &AddNormalItem::do_selectAction);
  54. /* 打开时间选择器 */
  55. connect(ui->pBtn_selectTime, &QPushButton::clicked, this, &AddNormalItem::do_selectTime);
  56. /* 设置事件过滤器 */
  57. ui->comBox_actionSelect->installEventFilter(this);
  58. ui->comBox_devSelect->installEventFilter(this);
  59. }
  60. AddNormalItem::~AddNormalItem()
  61. {
  62. delete ui;
  63. }
  64. /* 设置父指针,时间选择器需要使用 */
  65. void AddNormalItem::setParentPointer(QWidget* p)
  66. {
  67. m_parent = p;
  68. // m_parent->installEventFilter(this);
  69. }
  70. /* 添加可选设备 */
  71. void AddNormalItem::setDevice(QMap<QString, DeviceInfo>& mapDev)
  72. {
  73. ui->comBox_devSelect->clear();
  74. for(const auto& it : mapDev)
  75. {
  76. ui->comBox_devSelect->addItem(it.devName);
  77. }
  78. /* 设置显示第一个设备,并设置可选的动作 */
  79. ui->comBox_devSelect->setCurrentIndex(0);
  80. m_devName = ui->comBox_devSelect->currentText();
  81. setAction(m_devName);
  82. }
  83. /* 设置周几 */
  84. void AddNormalItem::setWeekDay(int week)
  85. {
  86. if(week < 0 || week > 6)
  87. {
  88. return;
  89. }
  90. m_week = week;
  91. }
  92. /* 进行查重和关闭页面 */
  93. void AddNormalItem::do_ok()
  94. {
  95. ui->label_timeWarn->hide();
  96. ui->label_devWarn->hide();
  97. ui->label_actionWarn->hide();
  98. setComboBoxWarning(ui->comBox_devSelect,false);
  99. setComboBoxWarning(ui->comBox_actionSelect, false);
  100. setTimeEditWarning(false);
  101. /* 检查设备是否为空 */
  102. if(ui->comBox_devSelect->currentText().isEmpty())
  103. {
  104. ui->label_devWarn->setText("不能为空!");
  105. ui->label_devWarn->show();
  106. setComboBoxWarning(ui->comBox_devSelect, true);
  107. return;
  108. }
  109. /* 检查动作是否为空 */
  110. if(ui->comBox_actionSelect->currentText().isEmpty())
  111. {
  112. ui->label_actionWarn->setText("不能为空!");
  113. ui->label_actionWarn->show();
  114. setComboBoxWarning(ui->comBox_actionSelect, true);
  115. }
  116. /* 检查时间是否为空 */
  117. if(m_time.isNull())
  118. {
  119. ui->label_timeWarn->setText("不能为空!");
  120. ui->label_timeWarn->show();
  121. setTimeEditWarning(true);
  122. return;
  123. }
  124. /* 进行设备查重 */
  125. // bool ret = m_p->judgeTimeRepetition(*m_p->m_vecItem[m_p->m_stack->currentIndex()],m_devName,m_time);
  126. bool ret = IData.judgeTimeRepetitionWithAdd(m_week, m_devName, m_time);
  127. if(ret)
  128. {
  129. ui->label_timeWarn->setText("一个时间点,单个设备仅能执行一个操作!");
  130. ui->label_timeWarn->show();
  131. setTimeEditWarning(true);
  132. return;
  133. }
  134. m_isAddDev = true;
  135. /* 添加一项计划 */
  136. LH_WRITE_LOG_DEBUG(QString("添加一项计划: 设备:%1, 动作:%2, 时间:%3").arg(m_devName).arg(m_action).arg(m_time.toString("hh:mm:ss")));
  137. /* 发送信号 */
  138. emit signal_addNormalItem(m_devName,m_action,m_time);
  139. close();
  140. }
  141. /* 选择了设备,设置其对应的动作 */
  142. void AddNormalItem::do_selectDev()
  143. {
  144. m_devName = ui->comBox_devSelect->currentText();
  145. setAction(m_devName);
  146. }
  147. /* 选择了动作 */
  148. void AddNormalItem::do_selectAction()
  149. {
  150. m_action = ui->comBox_actionSelect->currentText();
  151. }
  152. /* 点击了时间选择按钮,打开时间选择器 */
  153. void AddNormalItem::do_selectTime()
  154. {
  155. // LH_WRITE_LOG_DEBUG("选择时间");
  156. std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(this, TimeWidget::ShowType::Dialog);
  157. /* 设置图标 */
  158. tw->setIcon(":/ICON/ICON/Time.png");
  159. tw->setIconShow(true);
  160. tw->setIconSize(16, 16);
  161. /* 重新设置大小 */
  162. tw->setEditLine(ui->pBtn_selectTime->width(), ui->pBtn_selectTime->height());
  163. /* 设置选择框大小 */
  164. tw->setTimeAreaWidth(140);
  165. /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */
  166. auto pos = ui->pBtn_selectTime->mapTo(this, QPoint(0, 0));
  167. tw->move(pos);
  168. /* 设置默认的时间 */
  169. tw->setTime(m_time);
  170. tw->execShow();
  171. m_time = tw->getTime();
  172. // LH_WRITE_LOG_DEBUG(QString("选择时间:%1").arg(m_time.toString("hh:mm:ss")));
  173. ui->pBtn_selectTime->setText(m_time.toString("hh:mm:ss"));
  174. }
  175. /* 设置选择框报警 */
  176. void AddNormalItem::setComboBoxWarning(QComboBox* bo, bool flag)
  177. {
  178. if(flag)
  179. {
  180. bo->setProperty("Warn", true);
  181. }
  182. else
  183. {
  184. bo->setProperty("Warn", false);
  185. }
  186. bo->style()->unpolish(bo);
  187. bo->style()->polish(bo);
  188. }
  189. /* 设置时间报警 */
  190. void AddNormalItem::setTimeEditWarning(bool flag)
  191. {
  192. if(flag)
  193. {
  194. ui->pBtn_selectTime->setProperty("Warn", true);
  195. }
  196. else
  197. {
  198. ui->pBtn_selectTime->setProperty("Warn", false);
  199. }
  200. ui->pBtn_selectTime->style()->unpolish(ui->pBtn_selectTime);
  201. ui->pBtn_selectTime->style()->polish(ui->pBtn_selectTime);
  202. }
  203. /* 设置动作 */
  204. void AddNormalItem::setAction(const QString& devName)
  205. {
  206. QMap<int, QString> devAction;
  207. if(!DeviceContainer.getDevAction(devName, devAction))
  208. {
  209. return;
  210. }
  211. ui->comBox_actionSelect->clear();
  212. for(auto it = devAction.begin();it != devAction.end();it++)
  213. {
  214. ui->comBox_actionSelect->addItem(it.value(), it.key());
  215. }
  216. }
  217. /* 事件过滤器 */
  218. bool AddNormalItem::eventFilter(QObject *watched, QEvent *event)
  219. {
  220. if(watched == ui->comBox_devSelect)
  221. {
  222. if(event->type() == QEvent::Wheel)
  223. {
  224. return true;
  225. }
  226. }
  227. else if(watched == ui->comBox_actionSelect)
  228. {
  229. if(event->type() == QEvent::Wheel)
  230. {
  231. return true;
  232. }
  233. }
  234. return QDialog::eventFilter(watched, event);
  235. }