addspecialitem.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. }
  182. /* 点击了时间选择按钮,打开时间选择器 */
  183. void AddSpecialItem::do_selectTime()
  184. {
  185. // LH_WRITE_LOG_DEBUG("选择时间");
  186. std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(this, TimeWidget::ShowType::Dialog);
  187. /* 设置图标 */
  188. tw->setIcon(":/ICON/ICON/Time.png");
  189. tw->setIconShow(true);
  190. tw->setIconSize(16, 16);
  191. /* 重新设置大小 */
  192. tw->setEditLine(ui->pBtn_selectTime->width(), ui->pBtn_selectTime->height());
  193. /* 设置选择框大小 */
  194. tw->setTimeAreaWidth(140);
  195. /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */
  196. auto pos = ui->pBtn_selectTime->mapTo(this, QPoint(0, 0));
  197. tw->move(pos);
  198. /* 设置默认的时间 */
  199. tw->setTime(m_time);
  200. tw->execShow();
  201. m_time = tw->getTime();
  202. // LH_WRITE_LOG_DEBUG(QString("选择时间:%1").arg(m_time.toString("hh:mm:ss")));
  203. ui->pBtn_selectTime->setText(m_time.toString("hh:mm:ss"));
  204. }
  205. /* 修改了日期 */
  206. void AddSpecialItem::do_selectDate(const QDate &date)
  207. {
  208. // LH_WRITE_LOG_DEBUG(QString("选择日期:%1").arg(date.toString("yyyy-MM-dd")));
  209. }
  210. /* 设置选择框报警 */
  211. void AddSpecialItem::setComboBoxWarning(QComboBox* bo, bool flag)
  212. {
  213. if(flag)
  214. {
  215. bo->setProperty("Warn", true);
  216. }
  217. else
  218. {
  219. bo->setProperty("Warn", false);
  220. }
  221. bo->style()->unpolish(bo);
  222. bo->style()->polish(bo);
  223. }
  224. /* 设置时间报警 */
  225. void AddSpecialItem::setTimeEditWarning(bool flag)
  226. {
  227. if(flag)
  228. {
  229. ui->pBtn_selectTime->setProperty("Warn", true);
  230. }
  231. else
  232. {
  233. ui->pBtn_selectTime->setProperty("Warn", false);
  234. }
  235. ui->pBtn_selectTime->style()->unpolish(ui->pBtn_selectTime);
  236. ui->pBtn_selectTime->style()->polish(ui->pBtn_selectTime);
  237. }
  238. /* 设置动作 */
  239. void AddSpecialItem::setAction(const QString& devName)
  240. {
  241. QMap<int, QString> devAction;
  242. if(!DeviceContainer.getDevAction(devName, devAction))
  243. {
  244. return;
  245. }
  246. ui->comBox_actionSelect->clear();
  247. for(auto it = devAction.begin();it != devAction.end();it++)
  248. {
  249. ui->comBox_actionSelect->addItem(it.value(), it.key());
  250. }
  251. }
  252. /* 事件过滤器 */
  253. bool AddSpecialItem::eventFilter(QObject *watched, QEvent *event)
  254. {
  255. if(watched == ui->comBox_devSelect)
  256. {
  257. if(event->type() == QEvent::Wheel)
  258. {
  259. return true;
  260. }
  261. }
  262. else if(watched == ui->comBox_actionSelect)
  263. {
  264. if(event->type() == QEvent::Wheel)
  265. {
  266. return true;
  267. }
  268. }
  269. else if(watched == ui->dateEdit)
  270. {
  271. if(event->type() == QEvent::Wheel)
  272. {
  273. return true;
  274. }
  275. }
  276. else if(watched == ui->pBtn_Close)
  277. {
  278. if(event->type() == QEvent::Enter)
  279. {
  280. ui->pBtn_Close->setProperty("Hover", true);
  281. ui->pBtn_Close->style()->unpolish(ui->pBtn_Close);
  282. ui->pBtn_Close->style()->polish(ui->pBtn_Close);
  283. return true;
  284. }else if(event->type() == QEvent::Leave)
  285. {
  286. ui->pBtn_Close->setProperty("Hover", false);
  287. ui->pBtn_Close->style()->unpolish(ui->pBtn_Close);
  288. ui->pBtn_Close->style()->polish(ui->pBtn_Close);
  289. return true;
  290. }
  291. }
  292. return QDialog::eventFilter(watched, event);
  293. }
  294. /* 绘画事件 */
  295. // void AddSpecialItem::paintEvent(QPaintEvent *event)
  296. // {
  297. // QPainter painter(this);
  298. // painter.setRenderHint(QPainter::Antialiasing);
  299. // /* 移动到方框下面 */
  300. // QPoint pos = ui->widget_background->pos();
  301. // pos.setX(pos.x() - 16);
  302. // pos.setY(pos.y() - 16);
  303. // painter.drawImage(pos, m_shadow->image());
  304. // }
  305. /* 鼠标点击事件 */
  306. void AddSpecialItem::mousePressEvent(QMouseEvent *event)
  307. {
  308. m_lastPos = event->globalPos();
  309. event->accept();
  310. }
  311. /* 鼠标移动事件 */
  312. void AddSpecialItem::mouseMoveEvent(QMouseEvent *event)
  313. {
  314. // QRect rect = this->geometry();
  315. auto point = ui->widget_Top->mapToGlobal(QPoint(0, 0));
  316. QRect rect(point, ui->widget_Top->size());
  317. // LH_WRITE_LOG(QString("rect: %1, %2, %3, %4").arg(rect.left()).arg(rect.top()).arg(rect.right()).arg(rect.bottom()));
  318. // LH_WRITE_LOG(QString("lastPos: %1, %2").arg(m_lastPos.x()).arg(m_lastPos.y()));
  319. // rect.setBottom(rect.top()+50);
  320. if(!rect.contains(m_lastPos))
  321. {
  322. event->accept();
  323. return;
  324. }
  325. int dx = event->globalX() - m_lastPos.x();
  326. int dy = event->globalY() - m_lastPos.y();
  327. // move(x()+dx, y()+dy);
  328. ui->widget_background->move(ui->widget_background->x() + dx, ui->widget_background->y() + dy);
  329. // m_shadow->move(ui->widget_background->pos());
  330. m_lastPos = event->globalPos();
  331. event->accept();
  332. }
  333. /* 鼠标释放事件 */
  334. void AddSpecialItem::mouseReleaseEvent(QMouseEvent *event)
  335. {
  336. event->accept();
  337. }