addspecialitem.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "common/combobox/customcombobox.h"
  11. #include "LHQLogAPI.h"
  12. #include "transmitterswitchinfo.h"
  13. #include "common/SelectTime/timewidget.h"
  14. #include "common/date/calendardtedit.h"
  15. #include "ItemData.h"
  16. #include "oneshadow.h"
  17. #include "lhstylemanager.h"
  18. AddSpecialItem::AddSpecialItem(QWidget *parent) :
  19. QDialog(parent),
  20. ui(new Ui::AddSpecialItem)
  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/AddSpecialItem_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. setQSS();
  39. /* 创建弹窗阴影 */
  40. m_shadow = new OneShadow(ui->widget_background->size(), 16);
  41. /* 设置comboBox阴影 */
  42. ui->comBox_devSelect->setViewShadowEffect();
  43. ui->comBox_actionSelect->setViewShadowEffect();
  44. ui->label_timeWarn->hide();
  45. ui->label_devWarn->hide();
  46. ui->label_actionWarn->hide();
  47. /* 获取屏幕大小 */
  48. m_rectScreen = QApplication::desktop()->availableGeometry();
  49. this->resize(m_rectScreen.width(), m_rectScreen.height());
  50. /* 设置默认时间 */
  51. m_time.setHMS(0, 0, 0);
  52. m_date = QDate::currentDate();
  53. ui->dateEdit->setDate(m_date);
  54. ui->dateEdit->setDisplayFormat("yyyy-MM-dd");
  55. /* 连接信号和槽 */
  56. connect(ui->pBtn_Close, &QPushButton::clicked, this, &QDialog::close);
  57. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &QDialog::close);
  58. connect(ui->pBtn_ok, &QPushButton::clicked, this, &AddSpecialItem::do_ok);
  59. connect(ui->pBtn_cancel, &QPushButton::clicked, this, &AddSpecialItem::close);
  60. /* 设备选择 */
  61. connect(ui->comBox_devSelect, QOverload<const QString&>::of(&QComboBox::currentTextChanged), this, &AddSpecialItem::do_selectDev);
  62. /* 动作选择 */
  63. connect(ui->comBox_actionSelect,QOverload<const QString&>::of(&QComboBox::currentTextChanged),this, &AddSpecialItem::do_selectAction);
  64. /* 打开时间选择器 */
  65. connect(ui->pBtn_selectTime, &QPushButton::clicked, this, &AddSpecialItem::do_selectTime);
  66. connect(ui->pBtn_iconTime, &QPushButton::clicked, this, &AddSpecialItem::do_selectTime);
  67. /* 设置事件过滤器 */
  68. ui->comBox_actionSelect->installEventFilter(this);
  69. ui->comBox_devSelect->installEventFilter(this);
  70. }
  71. AddSpecialItem::~AddSpecialItem()
  72. {
  73. delete ui;
  74. }
  75. /* 设置父指针,时间选择器需要使用 */
  76. void AddSpecialItem::setParentPointer(QWidget* p)
  77. {
  78. m_parent = p;
  79. // m_parent->installEventFilter(this);
  80. }
  81. /* 添加可选设备 */
  82. void AddSpecialItem::setDevice(QMap<QString, DeviceInfo>& mapDev)
  83. {
  84. ui->comBox_devSelect->clear();
  85. for(const auto& it : mapDev)
  86. {
  87. ui->comBox_devSelect->addItem(it.devName);
  88. }
  89. /* 设置显示第一个设备,并设置可选的动作 */
  90. ui->comBox_devSelect->setCurrentIndex(0);
  91. m_devName = ui->comBox_devSelect->currentText();
  92. setAction(m_devName);
  93. }
  94. /* 设置周几 */
  95. void AddSpecialItem::setWeekDay(int week)
  96. {
  97. if(week != 7)
  98. {
  99. return;
  100. }
  101. m_week = week;
  102. }
  103. /* 设置QSS */
  104. void AddSpecialItem::setQSS()
  105. {
  106. auto appPath = QApplication::applicationDirPath();
  107. QString qssPath;
  108. if(LHStyleManager::Instance()->GetCurSkinStyle() == eWhiteStyle)
  109. {
  110. qssPath = appPath + "/white/addspecialitem.qss";
  111. }else if(LHStyleManager::Instance()->GetCurSkinStyle() == eBlackStyle)
  112. {
  113. qssPath = appPath + "/black/addspecialitem.qss";
  114. }
  115. QFile file(qssPath);
  116. if(file.open(QIODevice::ReadOnly))
  117. {
  118. QString stylesheet = file.readAll();
  119. this->setStyleSheet(stylesheet);
  120. file.close();
  121. } else
  122. {
  123. LH_WRITE_ERROR(QString("打开文件失败:%1").arg(file.fileName()));
  124. }
  125. }
  126. /* 进行查重和关闭页面 */
  127. void AddSpecialItem::do_ok()
  128. {
  129. ui->label_timeWarn->hide();
  130. ui->label_devWarn->hide();
  131. ui->label_actionWarn->hide();
  132. setComboBoxWarning(ui->comBox_devSelect,false);
  133. setComboBoxWarning(ui->comBox_actionSelect, false);
  134. setTimeEditWarning(false);
  135. /* 检查设备是否为空 */
  136. if(ui->comBox_devSelect->currentText().isEmpty())
  137. {
  138. ui->label_devWarn->setText("不能为空!");
  139. ui->label_devWarn->show();
  140. setComboBoxWarning(ui->comBox_devSelect, true);
  141. return;
  142. }
  143. /* 检查动作是否为空 */
  144. if(ui->comBox_actionSelect->currentText().isEmpty())
  145. {
  146. ui->label_actionWarn->setText("不能为空!");
  147. ui->label_actionWarn->show();
  148. setComboBoxWarning(ui->comBox_actionSelect, true);
  149. }
  150. /* 赋值日期 */
  151. m_date = ui->dateEdit->date();
  152. /* 检查时间是否为空 */
  153. if(m_time.isNull())
  154. {
  155. ui->label_timeWarn->setText("不能为空!");
  156. ui->label_timeWarn->show();
  157. setTimeEditWarning(true);
  158. return;
  159. }
  160. /* 进行设备查重 */
  161. bool ret = IData.judgeDateTimeRepetitionWithAdd(m_week, m_devName, m_date, m_time);
  162. if(ret)
  163. {
  164. ui->label_timeWarn->setText("一个时间点,单个设备仅能执行一个操作!");
  165. ui->label_timeWarn->show();
  166. setTimeEditWarning(true);
  167. return;
  168. }
  169. m_isAddDev = true;
  170. /* 添加一项计划 */
  171. 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")));
  172. /* 发送信号 */
  173. emit signal_AddSpecialItem(m_devName, m_action, m_date, m_time);
  174. close();
  175. }
  176. /* 选择了设备,设置其对应的动作 */
  177. void AddSpecialItem::do_selectDev()
  178. {
  179. m_devName = ui->comBox_devSelect->currentText();
  180. setAction(m_devName);
  181. }
  182. /* 选择了动作 */
  183. void AddSpecialItem::do_selectAction()
  184. {
  185. m_action = ui->comBox_actionSelect->currentText();
  186. }
  187. /* 点击了时间选择按钮,打开时间选择器 */
  188. void AddSpecialItem::do_selectTime()
  189. {
  190. // LH_WRITE_LOG_DEBUG("选择时间");
  191. std::shared_ptr<TimeWidget> tw = std::make_shared<TimeWidget>(this, TimeWidget::ShowType::Dialog);
  192. /* 设置图标 */
  193. tw->setIcon(":/ICON/ICON/Time.png");
  194. tw->setIconShow(true);
  195. tw->setIconSize(16, 16);
  196. /* 重新设置大小 */
  197. tw->setEditLine(ui->pBtn_selectTime->width(), ui->pBtn_selectTime->height());
  198. /* 设置选择框大小 */
  199. tw->setTimeAreaWidth(140);
  200. /* 移动位置,覆盖显示时间的按钮,获取的坐标是相对于Dialog的位置 */
  201. auto pos = ui->pBtn_selectTime->mapTo(this, QPoint(0, 0));
  202. tw->move(pos);
  203. /* 设置默认的时间 */
  204. tw->setTime(m_time);
  205. tw->execShow();
  206. m_time = tw->getTime();
  207. // LH_WRITE_LOG_DEBUG(QString("选择时间:%1").arg(m_time.toString("hh:mm:ss")));
  208. ui->pBtn_selectTime->setText(m_time.toString("hh:mm:ss"));
  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. return QDialog::eventFilter(watched, event);
  270. }
  271. /* 绘画事件 */
  272. void AddSpecialItem::paintEvent(QPaintEvent *event)
  273. {
  274. QPainter painter(this);
  275. painter.setRenderHint(QPainter::Antialiasing);
  276. /* 移动到方框下面 */
  277. QPoint pos = ui->widget_background->pos();
  278. pos.setX(pos.x() - 16);
  279. pos.setY(pos.y() - 16);
  280. painter.drawImage(pos, m_shadow->image());
  281. }