addnormalitem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 <QPainter>
  10. #include <QMouseEvent>
  11. #include "common/combobox/customcombobox.h"
  12. #include "LHQLogAPI.h"
  13. #include "TransmitterSwitchInfo.h"
  14. #include "common/SelectTime/timewidget.h"
  15. #include "ItemData.h"
  16. #include "OneShadowEffect.h"
  17. // #include "lhstylemanager.h"
  18. AddNormalItem::AddNormalItem(QWidget *parent) :
  19. QDialog(parent),
  20. ui(new Ui::AddNormalItem)
  21. {
  22. ui->setupUi(this);
  23. /* 设置无边框 */
  24. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  25. /* 设置底层样式表,让最底层的透明 */
  26. this->setAttribute(Qt::WA_TranslucentBackground);
  27. /* 加载QSS */
  28. // QFile file(":/QSS/QSS/AddNormalItem_Light.qss");
  29. // if(file.open(QIODevice::ReadOnly))
  30. // {
  31. // QString stylesheet = file.readAll();
  32. // this->setStyleSheet(stylesheet);
  33. // file.close();
  34. // } else
  35. // {
  36. // LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  37. // }
  38. OneShadowEffect *pShadowEffect = new OneShadowEffect(this);
  39. ui->widget_background->setGraphicsEffect(pShadowEffect);
  40. ui->label_timeWarn->hide();
  41. ui->label_devWarn->hide();
  42. ui->label_actionWarn->hide();
  43. /* 获取屏幕大小 */
  44. m_rectScreen = QApplication::desktop()->availableGeometry();
  45. this->resize(m_rectScreen.width(), m_rectScreen.height());
  46. /* 设置默认时间 */
  47. m_time.setHMS(0, 0, 0);
  48. /* 连接信号和槽 */
  49. connect(ui->pBtn_Close, &QPushButton::clicked, this, &QDialog::close);
  50. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &QDialog::close);
  51. connect(ui->pBtn_ok, &QPushButton::clicked, this, &AddNormalItem::do_ok);
  52. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &AddNormalItem::close);
  53. /* 设备选择 */
  54. connect(ui->comBox_devSelect, QOverload<const QString&>::of(&QComboBox::currentTextChanged), this, &AddNormalItem::do_selectDev);
  55. /* 动作选择 */
  56. connect(ui->comBox_actionSelect,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this, &AddNormalItem::do_selectAction);
  57. /* 打开时间选择器 */
  58. connect(ui->pBtn_selectTime, &QPushButton::clicked, this, &AddNormalItem::do_selectTime);
  59. /* 设置事件过滤器 */
  60. ui->comBox_actionSelect->installEventFilter(this);
  61. ui->comBox_devSelect->installEventFilter(this);
  62. ui->pBtn_Close->installEventFilter(this);
  63. }
  64. AddNormalItem::~AddNormalItem()
  65. {
  66. delete ui;
  67. }
  68. /* 设置父指针,时间选择器需要使用 */
  69. void AddNormalItem::setParentPointer(QWidget* p)
  70. {
  71. m_parent = p;
  72. // m_parent->installEventFilter(this);
  73. }
  74. /* 添加可选设备 */
  75. void AddNormalItem::setDevice(QMap<QString, DeviceInfo>& mapDev)
  76. {
  77. ui->comBox_devSelect->clear();
  78. for(const auto& it : mapDev)
  79. {
  80. ui->comBox_devSelect->addItem(it.devName);
  81. }
  82. /* 设置显示第一个设备,并设置可选的动作 */
  83. ui->comBox_devSelect->setCurrentIndex(0);
  84. m_devName = ui->comBox_devSelect->currentText();
  85. setAction(m_devName);
  86. }
  87. /* 设置周几 */
  88. void AddNormalItem::setWeekDay(int week)
  89. {
  90. if(week < 0 || week > 6)
  91. {
  92. return;
  93. }
  94. m_week = week;
  95. }
  96. /* 设置QSS */
  97. void AddNormalItem::setQSS(const QString& qssPath)
  98. {
  99. QString qssFile = qssPath + "/addnormalitem.qss";
  100. QFile file(qssFile);
  101. if(file.open(QIODevice::ReadOnly))
  102. {
  103. QString stylesheet = file.readAll();
  104. this->setStyleSheet(stylesheet);
  105. file.close();
  106. } else
  107. {
  108. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  109. }
  110. /* 设置comboBox阴影,需要先加载样式表,在设置这个阴影 */
  111. ui->comBox_devSelect->setViewShadowEffect();
  112. ui->comBox_actionSelect->setViewShadowEffect();
  113. }
  114. /* 进行查重和关闭页面 */
  115. void AddNormalItem::do_ok()
  116. {
  117. ui->label_timeWarn->hide();
  118. ui->label_devWarn->hide();
  119. ui->label_actionWarn->hide();
  120. setComboBoxWarning(ui->comBox_devSelect,false);
  121. setComboBoxWarning(ui->comBox_actionSelect, false);
  122. setTimeEditWarning(false);
  123. /* 检查设备是否为空 */
  124. if(ui->comBox_devSelect->currentText().isEmpty())
  125. {
  126. ui->label_devWarn->setText("不能为空!");
  127. ui->label_devWarn->show();
  128. setComboBoxWarning(ui->comBox_devSelect, true);
  129. return;
  130. }
  131. /* 检查动作是否为空 */
  132. if(ui->comBox_actionSelect->currentText().isEmpty())
  133. {
  134. ui->label_actionWarn->setText("不能为空!");
  135. ui->label_actionWarn->show();
  136. setComboBoxWarning(ui->comBox_actionSelect, true);
  137. }
  138. /* 检查时间是否为空 */
  139. if(m_time.isNull())
  140. {
  141. ui->label_timeWarn->setText("不能为空!");
  142. ui->label_timeWarn->show();
  143. setTimeEditWarning(true);
  144. return;
  145. }
  146. /* 进行设备查重 */
  147. // bool ret = m_p->judgeTimeRepetition(*m_p->m_vecItem[m_p->m_stack->currentIndex()],m_devName,m_time);
  148. bool ret = IData.judgeTimeRepetitionWithAdd(m_week, m_devName, m_time);
  149. if(ret)
  150. {
  151. ui->label_timeWarn->setText("一个时间点,单个设备仅能执行一个操作!");
  152. ui->label_timeWarn->show();
  153. setTimeEditWarning(true);
  154. return;
  155. }
  156. m_isAddDev = true;
  157. /* 添加一项计划 */
  158. LH_WRITE_LOG_DEBUG(QString("添加一项计划: 设备:%1, 动作:%2, 时间:%3").arg(m_devName).arg(m_action).arg(m_time.toString("hh:mm:ss")));
  159. /* 发送信号 */
  160. emit signal_addNormalItem(m_devName,m_action,m_time);
  161. close();
  162. }
  163. /* 选择了设备,设置其对应的动作 */
  164. void AddNormalItem::do_selectDev()
  165. {
  166. m_devName = ui->comBox_devSelect->currentText();
  167. setAction(m_devName);
  168. }
  169. /* 选择了动作 */
  170. void AddNormalItem::do_selectAction()
  171. {
  172. m_action = ui->comBox_actionSelect->currentText();
  173. m_actionID = ui->comBox_actionSelect->currentData().toInt();
  174. }
  175. /* 点击了时间选择按钮,打开时间选择器 */
  176. void AddNormalItem::do_selectTime()
  177. {
  178. // LH_WRITE_LOG_DEBUG("选择时间");
  179. std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(this, TimeWidget::ShowType::Dialog);
  180. /* 设置图标 */
  181. tw->setIcon(":/ICON/ICON/Time.png");
  182. tw->setIconShow(true);
  183. tw->setIconSize(16, 16);
  184. /* 重新设置大小 */
  185. tw->setEditLine(ui->pBtn_selectTime->width(), ui->pBtn_selectTime->height());
  186. /* 设置选择框大小 */
  187. tw->setTimeAreaWidth(140);
  188. /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */
  189. auto pos = ui->pBtn_selectTime->mapTo(this, QPoint(0, 0));
  190. tw->move(pos);
  191. /* 设置默认的时间 */
  192. tw->setTime(m_time);
  193. tw->execShow();
  194. m_time = tw->getTime();
  195. // LH_WRITE_LOG_DEBUG(QString("选择时间:%1").arg(m_time.toString("hh:mm:ss")));
  196. ui->pBtn_selectTime->setText(m_time.toString("hh:mm:ss"));
  197. }
  198. /* 设置选择框报警 */
  199. void AddNormalItem::setComboBoxWarning(QComboBox* bo, bool flag)
  200. {
  201. if(flag)
  202. {
  203. bo->setProperty("Warn", true);
  204. }
  205. else
  206. {
  207. bo->setProperty("Warn", false);
  208. }
  209. bo->style()->unpolish(bo);
  210. bo->style()->polish(bo);
  211. }
  212. /* 设置时间报警 */
  213. void AddNormalItem::setTimeEditWarning(bool flag)
  214. {
  215. if(flag)
  216. {
  217. ui->pBtn_selectTime->setProperty("Warn", true);
  218. }
  219. else
  220. {
  221. ui->pBtn_selectTime->setProperty("Warn", false);
  222. }
  223. ui->pBtn_selectTime->style()->unpolish(ui->pBtn_selectTime);
  224. ui->pBtn_selectTime->style()->polish(ui->pBtn_selectTime);
  225. }
  226. /* 设置动作 */
  227. void AddNormalItem::setAction(const QString& devName)
  228. {
  229. QMap<int, QString> devAction;
  230. if(!DeviceContainer.getDevAction(devName, devAction))
  231. {
  232. return;
  233. }
  234. ui->comBox_actionSelect->clear();
  235. for(auto it = devAction.begin();it != devAction.end();it++)
  236. {
  237. ui->comBox_actionSelect->addItem(it.value(), it.key());
  238. }
  239. }
  240. /* 事件过滤器 */
  241. bool AddNormalItem::eventFilter(QObject *watched, QEvent *event)
  242. {
  243. if(watched == ui->comBox_devSelect)
  244. {
  245. if(event->type() == QEvent::Wheel)
  246. {
  247. return true;
  248. }
  249. }
  250. else if(watched == ui->comBox_actionSelect)
  251. {
  252. if(event->type() == QEvent::Wheel)
  253. {
  254. return true;
  255. }
  256. }
  257. else if(watched == ui->pBtn_Close)
  258. {
  259. if(event->type() == QEvent::Enter)
  260. {
  261. ui->pBtn_Close->setProperty("Hover", true);
  262. ui->pBtn_Close->style()->unpolish(ui->pBtn_Close);
  263. ui->pBtn_Close->style()->polish(ui->pBtn_Close);
  264. return true;
  265. }else if(event->type() == QEvent::Leave)
  266. {
  267. ui->pBtn_Close->setProperty("Hover", false);
  268. ui->pBtn_Close->style()->unpolish(ui->pBtn_Close);
  269. ui->pBtn_Close->style()->polish(ui->pBtn_Close);
  270. return true;
  271. }
  272. }
  273. return QDialog::eventFilter(watched, event);
  274. }
  275. /* 绘画事件 */
  276. // void AddNormalItem::paintEvent(QPaintEvent *event)
  277. // {
  278. // QPainter painter(this);
  279. // painter.setRenderHint(QPainter::Antialiasing);
  280. // }
  281. /* 鼠标点击事件 */
  282. void AddNormalItem::mousePressEvent(QMouseEvent *event)
  283. {
  284. m_lastPos = event->globalPos();
  285. event->accept();
  286. }
  287. /* 鼠标移动事件 */
  288. void AddNormalItem::mouseMoveEvent(QMouseEvent *event)
  289. {
  290. // QRect rect = this->geometry();
  291. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  292. QRect rect(point, ui->widget_Top->size());
  293. // LH_WRITE_LOG(QString("rect: %1, %2, %3, %4").arg(rect.left()).arg(rect.top()).arg(rect.right()).arg(rect.bottom()));
  294. // LH_WRITE_LOG(QString("lastPos: %1, %2").arg(m_lastPos.x()).arg(m_lastPos.y()));
  295. // rect.setBottom(rect.top()+50);
  296. if(!rect.contains(m_lastPos))
  297. {
  298. event->accept();
  299. return;
  300. }
  301. int dx = event->globalX() - m_lastPos.x();
  302. int dy = event->globalY() - m_lastPos.y();
  303. // move(x()+dx, y()+dy);
  304. ui->widget_background->move(ui->widget_background->x() + dx, ui->widget_background->y() + dy);
  305. // m_shadow->move(ui->widget_background->pos());
  306. m_lastPos = event->globalPos();
  307. event->accept();
  308. }
  309. /* 鼠标释放事件 */
  310. void AddNormalItem::mouseReleaseEvent(QMouseEvent *event)
  311. {
  312. event->accept();
  313. }