addspecialitem.cpp 11 KB

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