addnormalitem.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 "common/combobox/customcombobox.h"
  11. #include "LHQLogAPI.h"
  12. #include "transmitterswitchinfo.h"
  13. #include "common/SelectTime/timewidget.h"
  14. #include "ItemData.h"
  15. #include "oneshadow.h"
  16. #include "lhstylemanager.h"
  17. AddNormalItem::AddNormalItem(QWidget *parent) :
  18. QDialog(parent),
  19. ui(new Ui::AddNormalItem)
  20. {
  21. ui->setupUi(this);
  22. /* 设置无边框 */
  23. setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
  24. /* 设置底层样式表,让最底层的透明 */
  25. this->setAttribute(Qt::WA_TranslucentBackground);
  26. /* 加载QSS */
  27. // QFile file(":/QSS/QSS/AddNormalItem_Light.qss");
  28. // if(file.open(QIODevice::ReadOnly))
  29. // {
  30. // QString stylesheet = file.readAll();
  31. // this->setStyleSheet(stylesheet);
  32. // file.close();
  33. // } else
  34. // {
  35. // LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  36. // }
  37. // LHStyleManager::Instance()->AddWidget(this);
  38. setQSS();
  39. /* 创建阴影 */
  40. m_shadow = new OneShadow(ui->widget_background->size(), 16);
  41. m_shadow->image();
  42. /* 设置comboBox阴影 */
  43. ui->comBox_devSelect->setViewShadowEffect();
  44. ui->comBox_actionSelect->setViewShadowEffect();
  45. ui->label_timeWarn->hide();
  46. ui->label_devWarn->hide();
  47. ui->label_actionWarn->hide();
  48. /* 获取屏幕大小 */
  49. m_rectScreen = QApplication::desktop()->availableGeometry();
  50. this->resize(m_rectScreen.width(), m_rectScreen.height());
  51. /* 设置默认时间 */
  52. m_time.setHMS(0, 0, 0);
  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, &AddNormalItem::do_ok);
  57. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &AddNormalItem::close);
  58. /* 设备选择 */
  59. connect(ui->comBox_devSelect, QOverload<const QString&>::of(&QComboBox::currentTextChanged), this, &AddNormalItem::do_selectDev);
  60. /* 动作选择 */
  61. connect(ui->comBox_actionSelect,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this, &AddNormalItem::do_selectAction);
  62. /* 打开时间选择器 */
  63. connect(ui->pBtn_selectTime, &QPushButton::clicked, this, &AddNormalItem::do_selectTime);
  64. /* 设置事件过滤器 */
  65. ui->comBox_actionSelect->installEventFilter(this);
  66. ui->comBox_devSelect->installEventFilter(this);
  67. }
  68. AddNormalItem::~AddNormalItem()
  69. {
  70. delete ui;
  71. }
  72. /* 设置父指针,时间选择器需要使用 */
  73. void AddNormalItem::setParentPointer(QWidget* p)
  74. {
  75. m_parent = p;
  76. // m_parent->installEventFilter(this);
  77. }
  78. /* 添加可选设备 */
  79. void AddNormalItem::setDevice(QMap<QString, DeviceInfo>& mapDev)
  80. {
  81. ui->comBox_devSelect->clear();
  82. for(const auto& it : mapDev)
  83. {
  84. ui->comBox_devSelect->addItem(it.devName);
  85. }
  86. /* 设置显示第一个设备,并设置可选的动作 */
  87. ui->comBox_devSelect->setCurrentIndex(0);
  88. m_devName = ui->comBox_devSelect->currentText();
  89. setAction(m_devName);
  90. }
  91. /* 设置周几 */
  92. void AddNormalItem::setWeekDay(int week)
  93. {
  94. if(week < 0 || week > 6)
  95. {
  96. return;
  97. }
  98. m_week = week;
  99. }
  100. /* 设置QSS */
  101. void AddNormalItem::setQSS()
  102. {
  103. auto appPath = QApplication::applicationDirPath();
  104. QString qssPath;
  105. if(LHStyleManager::Instance()->GetCurSkinStyle() == eWhiteStyle)
  106. {
  107. qssPath = appPath + "/white/addnormalitem.qss";
  108. }else if(LHStyleManager::Instance()->GetCurSkinStyle() == eBlackStyle)
  109. {
  110. qssPath = appPath + "/black/addnormalitem.qss";
  111. }
  112. QFile file(qssPath);
  113. if(file.open(QIODevice::ReadOnly))
  114. {
  115. QString stylesheet = file.readAll();
  116. this->setStyleSheet(stylesheet);
  117. file.close();
  118. } else
  119. {
  120. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  121. }
  122. }
  123. /* 进行查重和关闭页面 */
  124. void AddNormalItem::do_ok()
  125. {
  126. ui->label_timeWarn->hide();
  127. ui->label_devWarn->hide();
  128. ui->label_actionWarn->hide();
  129. setComboBoxWarning(ui->comBox_devSelect,false);
  130. setComboBoxWarning(ui->comBox_actionSelect, false);
  131. setTimeEditWarning(false);
  132. /* 检查设备是否为空 */
  133. if(ui->comBox_devSelect->currentText().isEmpty())
  134. {
  135. ui->label_devWarn->setText("不能为空!");
  136. ui->label_devWarn->show();
  137. setComboBoxWarning(ui->comBox_devSelect, true);
  138. return;
  139. }
  140. /* 检查动作是否为空 */
  141. if(ui->comBox_actionSelect->currentText().isEmpty())
  142. {
  143. ui->label_actionWarn->setText("不能为空!");
  144. ui->label_actionWarn->show();
  145. setComboBoxWarning(ui->comBox_actionSelect, true);
  146. }
  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 = m_p->judgeTimeRepetition(*m_p->m_vecItem[m_p->m_stack->currentIndex()],m_devName,m_time);
  157. bool ret = IData.judgeTimeRepetitionWithAdd(m_week, m_devName, m_time);
  158. if(ret)
  159. {
  160. ui->label_timeWarn->setText("一个时间点,单个设备仅能执行一个操作!");
  161. ui->label_timeWarn->show();
  162. setTimeEditWarning(true);
  163. return;
  164. }
  165. m_isAddDev = true;
  166. /* 添加一项计划 */
  167. LH_WRITE_LOG_DEBUG(QString("添加一项计划: 设备:%1, 动作:%2, 时间:%3").arg(m_devName).arg(m_action).arg(m_time.toString("hh:mm:ss")));
  168. /* 发送信号 */
  169. emit signal_addNormalItem(m_devName,m_action,m_time);
  170. close();
  171. }
  172. /* 选择了设备,设置其对应的动作 */
  173. void AddNormalItem::do_selectDev()
  174. {
  175. m_devName = ui->comBox_devSelect->currentText();
  176. setAction(m_devName);
  177. }
  178. /* 选择了动作 */
  179. void AddNormalItem::do_selectAction()
  180. {
  181. m_action = ui->comBox_actionSelect->currentText();
  182. }
  183. /* 点击了时间选择按钮,打开时间选择器 */
  184. void AddNormalItem::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 AddNormalItem::setComboBoxWarning(QComboBox* bo, bool flag)
  208. {
  209. if(flag)
  210. {
  211. bo->setProperty("Warn", true);
  212. }
  213. else
  214. {
  215. bo->setProperty("Warn", false);
  216. }
  217. bo->style()->unpolish(bo);
  218. bo->style()->polish(bo);
  219. }
  220. /* 设置时间报警 */
  221. void AddNormalItem::setTimeEditWarning(bool flag)
  222. {
  223. if(flag)
  224. {
  225. ui->pBtn_selectTime->setProperty("Warn", true);
  226. }
  227. else
  228. {
  229. ui->pBtn_selectTime->setProperty("Warn", false);
  230. }
  231. ui->pBtn_selectTime->style()->unpolish(ui->pBtn_selectTime);
  232. ui->pBtn_selectTime->style()->polish(ui->pBtn_selectTime);
  233. }
  234. /* 设置动作 */
  235. void AddNormalItem::setAction(const QString& devName)
  236. {
  237. QMap<int, QString> devAction;
  238. if(!DeviceContainer.getDevAction(devName, devAction))
  239. {
  240. return;
  241. }
  242. ui->comBox_actionSelect->clear();
  243. for(auto it = devAction.begin();it != devAction.end();it++)
  244. {
  245. ui->comBox_actionSelect->addItem(it.value(), it.key());
  246. }
  247. }
  248. /* 事件过滤器 */
  249. bool AddNormalItem::eventFilter(QObject *watched, QEvent *event)
  250. {
  251. if(watched == ui->comBox_devSelect)
  252. {
  253. if(event->type() == QEvent::Wheel)
  254. {
  255. return true;
  256. }
  257. }
  258. else if(watched == ui->comBox_actionSelect)
  259. {
  260. if(event->type() == QEvent::Wheel)
  261. {
  262. return true;
  263. }
  264. }
  265. return QDialog::eventFilter(watched, event);
  266. }
  267. /* 绘画事件 */
  268. void AddNormalItem::paintEvent(QPaintEvent *event)
  269. {
  270. QPainter painter(this);
  271. painter.setRenderHint(QPainter::Antialiasing);
  272. /* 移动到方框下面 */
  273. QPoint pos = ui->widget_background->pos();
  274. pos.setX(pos.x() - 16);
  275. pos.setY(pos.y() - 16);
  276. painter.drawImage(pos, m_shadow->image());
  277. }