addspecialitem.cpp 11 KB

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