oneitem.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "oneitem.h"
  2. #include "ui_oneitem.h"
  3. #include <QFile>
  4. #include "LHQLogAPI.h"
  5. OneItem::OneItem(QWidget *parent, bool isSpecial) :
  6. QWidget(parent), m_isSpecial(isSpecial),
  7. ui(new Ui::OneItem)
  8. {
  9. ui->setupUi(this);
  10. /* 加载QSS */
  11. QFile file(":/QSS/QSS/OneItem_dark.qss");
  12. if(file.open(QIODevice::ReadOnly))
  13. {
  14. QString stylesheet = file.readAll();
  15. this->setStyleSheet(stylesheet);
  16. file.close();
  17. } else
  18. {
  19. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  20. }
  21. /* 设置定时器 */
  22. m_warnTimer.setTimerType(Qt::PreciseTimer);
  23. m_warnTimer.setSingleShot(true);
  24. connect(&m_warnTimer,&QTimer::timeout,this,[this](){
  25. ui->label_warnning->hide();
  26. });
  27. /* 根据是否是特殊日移动位置 */
  28. layoutInit();
  29. /* 注册事件过滤器 */
  30. ui->comboBox_devName->installEventFilter(this);
  31. ui->comboBox_action->installEventFilter(this);
  32. /* 连接信号和槽 */
  33. connect(ui->comboBox_devName,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this,&OneItem::do_devChanged);
  34. connect(ui->comboBox_action,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this,&OneItem::do_actionChanged);
  35. connect(ui->pBtn_execTime,&QPushButton::clicked,this,&OneItem::do_pBtn_execTime_clicked);
  36. connect(ui->pBtn_execDate,&QPushButton::clicked,this,&OneItem::do_pBtn_execDate_clicked);
  37. }
  38. OneItem::~OneItem()
  39. {
  40. delete ui;
  41. }
  42. /* 设置序号 */
  43. void OneItem::setNum(int num)
  44. {
  45. ui->label_num->setText(QString::number(num));
  46. }
  47. /* 获取序号 */
  48. int OneItem::getNum()
  49. {
  50. return ui->label_num->text().toInt();
  51. }
  52. /* 设置日期 */
  53. void OneItem::setDate(const QDate& date)
  54. {
  55. ui->pBtn_execDate->setText(date.toString("yyyy-MM-dd"));
  56. }
  57. /* 获取日期 */
  58. QDate OneItem::getDate()
  59. {
  60. return QDate::fromString(ui->pBtn_execDate->text(),"yyyy-MM-dd");
  61. }
  62. /* 设置执行时间 */
  63. void OneItem::setExecTime(const QTime& time)
  64. {
  65. ui->pBtn_execTime->setText(time.toString("hh:mm:ss"));
  66. }
  67. /* 获取执行时间 */
  68. QTime OneItem::getExecTime()
  69. {
  70. return QTime::fromString(ui->pBtn_execTime->text(),"hh:mm:ss");
  71. }
  72. /* 设置设备名称 */
  73. void OneItem::setDevName(const QString& dev)
  74. {
  75. ui->comboBox_devName->setCurrentText(dev);
  76. }
  77. /* 获取设备名称 */
  78. QString OneItem::getDevName()
  79. {
  80. return ui->comboBox_devName->currentText();
  81. }
  82. /* 通过动作名称设置动作 */
  83. void OneItem::setActionName(const QString &action)
  84. {
  85. ui->comboBox_action->setCurrentText(action);
  86. }
  87. /* 获取动作名称 */
  88. QString OneItem::getActionName()
  89. {
  90. return ui->comboBox_action->currentText();
  91. }
  92. /* 获取动作类型 */
  93. int OneItem::getActionNum()
  94. {
  95. return ui->comboBox_action->currentIndex();
  96. }
  97. /* 添加设备 */
  98. void OneItem::addDevice(const QMap<QString, DeviceInfo>& mapDev)
  99. {
  100. ui->comboBox_devName->clear();
  101. for(const auto& it : mapDev)
  102. {
  103. ui->comboBox_devName->addItem(it.devName);
  104. }
  105. /* 设置显示第一个设备,并设置可选的动作 */
  106. ui->comboBox_devName->setCurrentIndex(0);
  107. setActionName(ui->comboBox_devName->currentText());
  108. }
  109. /* 获取日期类型 */
  110. bool OneItem::getDateType()
  111. {
  112. return m_isSpecial;
  113. }
  114. /* 设置警告 */
  115. void OneItem::setWarningText(QString str, int ms)
  116. {
  117. ui->label_warnning->setText(str);
  118. ui->label_warnning->show();
  119. if(ms > 0)
  120. {
  121. m_warnTimer.start(ms);
  122. }
  123. }
  124. /* 隐藏警告 */
  125. void OneItem::hideWarn()
  126. {
  127. ui->label_warnning->hide();
  128. }
  129. /* 设备改变了 */
  130. void OneItem::do_devChanged(QString devName)
  131. {
  132. /* 根据选择的设备,设置该设备的动作 */
  133. ui->comboBox_action->clear();
  134. auto it = DeviceContainer.getDevice(devName);
  135. if(it.PTTypeCode > 0)
  136. {
  137. m_devInfo = it;
  138. for(auto it1 = m_devInfo.DevType.devAction.begin();it1 != m_devInfo.DevType.devAction.end();it1++)
  139. {
  140. ui->comboBox_action->addItem(it1.value(), it1.key());
  141. }
  142. }else
  143. {
  144. LH_WRITE_ERROR(QString("没有这个设备:%1").arg(devName));
  145. return;
  146. }
  147. emit signal_devChanged();
  148. }
  149. /* 动作改变了 */
  150. void OneItem::do_actionChanged(QString action)
  151. {
  152. }
  153. /* 执行时间被点击了 */
  154. void OneItem::do_pBtn_execTime_clicked()
  155. {
  156. emit signal_execTimeClicked(getExecTime());
  157. }
  158. /* 执行日期被点击了 */
  159. void OneItem::do_pBtn_execDate_clicked()
  160. {
  161. emit signal_dateClicked(getDate());
  162. }
  163. /* 根据日期类型布局 */
  164. void OneItem::layoutInit()
  165. {
  166. /* 正常日,日期隐藏 */
  167. if(!m_isSpecial)
  168. {
  169. ui->pBtn_execDate->hide();
  170. ui->pBtn_iconDate->hide();
  171. /* 序号 */
  172. ui->label_num->move(18, 0);
  173. /* 设备名称 */
  174. ui->comboBox_devName->move(114, 0);
  175. /* 执行时间 */
  176. ui->pBtn_execTime->move(390, 40);
  177. /* 执行时间图标 */
  178. ui->pBtn_iconTime->move(498, 8);
  179. /* 动作 */
  180. ui->comboBox_action->move(538, 0);
  181. /* 关闭 */
  182. ui->pBtn_close->move(814, 0);
  183. }else
  184. {
  185. ui->pBtn_execDate->show();
  186. ui->pBtn_iconDate->show();
  187. /* 序号 */
  188. ui->label_num->move(18, 0);
  189. /* 设备名称 */
  190. ui->comboBox_devName->move(114, 0);
  191. /* 执行日期 */
  192. ui->pBtn_execDate->move(390, 0);
  193. /* 执行日期图标 */
  194. ui->pBtn_iconDate->move(498, 8);
  195. /* 执行时间 */
  196. ui->pBtn_execTime->move(539, 40);
  197. /* 执行时间图标 */
  198. ui->pBtn_iconTime->move(646, 8);
  199. /* 动作 */
  200. ui->comboBox_action->move(686, 0);
  201. /* 关闭 */
  202. ui->pBtn_close->move(962, 0);
  203. }
  204. }
  205. /* 事件过滤器 */
  206. bool OneItem::eventFilter(QObject *watched, QEvent *event)
  207. {
  208. if(watched == ui->comboBox_action)
  209. {
  210. if(event->type() == QEvent::Wheel)
  211. {
  212. return true;
  213. }
  214. }
  215. else if(watched == ui->comboBox_devName)
  216. {
  217. if(event->type() == QEvent::Wheel)
  218. {
  219. return true;
  220. }
  221. }
  222. return QWidget::eventFilter(watched,event);
  223. }