ShowTempPage.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "ShowTempPage.h"
  2. #include "ui_ShowTempPage.h"
  3. #include <QTimer>
  4. #include <set>
  5. #include "ShowTemperatureWnd.h"
  6. #include "ShowTemperatureListNumberWnd.h"
  7. ShowTempPage::ShowTempPage(QWidget *parent)
  8. : QWidget(parent)
  9. , ui(new Ui::ShowTempPage)
  10. {
  11. ui->setupUi(this);
  12. ui->praTabWidget->tabBar()->hide();
  13. QTimer::singleShot(800, this, [&]() { Init(); });
  14. UpDataParameter();
  15. }
  16. ShowTempPage::~ShowTempPage()
  17. {
  18. delete ui;
  19. }
  20. void ShowTempPage::Init()
  21. {
  22. m_manageDB = ns_db::CManageDB::GetInstance();
  23. if (m_manageDB == nullptr)
  24. {
  25. return;
  26. }
  27. m_pProduct = m_manageDB->GetCProduct();
  28. if (m_pProduct == nullptr)
  29. {
  30. return;
  31. }
  32. setStyleSheet(
  33. //"QWidget { background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #F1F4FD, stop: 1 #E5E4F6); }"
  34. "QDoubleSpinBox { background: #FFFFFF; border: 1px solid #BABBDC; border-radius: 6px; padding: 2px 5px; }"
  35. "QSpinBox { background: #FFFFFF; border: 1px solid #BABBDC; border-radius: 6px; padding: 2px 5px; }"
  36. "QLineEdit { background: #FFFFFF; border: 1px solid #BABBDC; border-radius: 6px; padding: 2px 5px; }"
  37. "QCheckBox::indicator { width: 20px; height: 20px; }"
  38. "QCheckBox::indicator:unchecked { background-color: #FFFFFF; border-radius: 2px; }"
  39. "QComboBox { background: #FFFFFF; border: 1px solid #BABBDC; border-radius: 6px; padding: 2px 5px; }"
  40. "QComboBox::drop-down { width: 20px; }"
  41. "QPushButton { background: #D0D0E8; border: 1px solid #BABBDC; border-radius: 6px; padding: 2px 5px; }" // Button background color
  42. "QPushButton:hover { background-color: #B0B0D8; }" // Lighter color on hover
  43. "QPushButton:pressed { background-color: #A0A0C8; }" // Darker color on press
  44. );
  45. InitSerialNumberList();
  46. }
  47. std::vector<ns_db::TEMPERATURE_LIST_STRUCT> ShowTempPage::FilterTemperatures(
  48. const std::vector<ns_db::TEMPERATURE_LIST_STRUCT>& originalList)
  49. {
  50. std::vector<ns_db::TEMPERATURE_LIST_STRUCT> filteredList;
  51. // 使用 std::map 来存储每个 iId 对应的所有 TEMPERATURE_LIST_STRUCT
  52. // map<iId, vector<TEMPERATURE_LIST_STRUCT>>
  53. std::map<int, std::vector<ns_db::TEMPERATURE_LIST_STRUCT>> groupedById;
  54. // 第一次遍历:按 iId 分组
  55. for (const auto& item : originalList)
  56. {
  57. groupedById[item.iId].push_back(item);
  58. }
  59. // 遍历每个 iId 组
  60. for (const auto& pair : groupedById)
  61. {
  62. const int currentId = pair.first;
  63. const std::vector<ns_db::TEMPERATURE_LIST_STRUCT>& currentGroup = pair.second;
  64. // 检查该 iId 组中是否有多个不同的 iSerialNumber
  65. std::set<int> serialNumbersInGroup;
  66. for (const auto& item : currentGroup) {
  67. serialNumbersInGroup.insert(item.iSerialNumber);
  68. }
  69. // 如果这个 iId 组包含多于一个不同的 iSerialNumber,则将该组的所有元素添加到结果中
  70. if (serialNumbersInGroup.size() > 1) {
  71. for (const auto& item : currentGroup) {
  72. filteredList.push_back(item);
  73. }
  74. }
  75. }
  76. return filteredList;
  77. }
  78. void ShowTempPage::UpdataUi(int nIndex, int ncurrentGroupIndex, bool isClear/* = false*/)
  79. {
  80. //if (isClear)
  81. {
  82. ui->serialNumberList->clear();
  83. }
  84. // 释放
  85. int nI = 0;
  86. // 筛选出来了
  87. for (const auto& pair : m_groupedById)
  88. {
  89. if (nI == nIndex)
  90. {
  91. const int currentId = pair.first;
  92. const std::vector<ns_db::TEMPERATURE_LIST_STRUCT>& currentGroup = pair.second;
  93. for (int i = 0; i < currentGroup.size(); i++)
  94. {
  95. AddListItem<ShowTemperatureListNumberWnd>(currentGroup[i], i, ui->serialNumberList, QSize(370, 48));
  96. }
  97. UpDataParameter(false, currentGroup[ncurrentGroupIndex]);
  98. break; //只要一个
  99. }
  100. nI++;
  101. }
  102. }
  103. void ShowTempPage::AddOrDel(int iId, int iSerialNumber, bool isDel)
  104. {
  105. // 释放
  106. ui->serialNumberList->clear();
  107. int nI = 0;
  108. // 筛选出来了
  109. for (auto& pair : m_groupedById)
  110. {
  111. if (nI == iId)
  112. {
  113. std::vector<ns_db::TEMPERATURE_LIST_STRUCT>& currentGroup = pair.second;
  114. if (isDel)
  115. {
  116. for (int i = 0; i < currentGroup.size(); i++)
  117. {
  118. if (currentGroup[i].iSerialNumber == iSerialNumber)
  119. {
  120. currentGroup.erase(currentGroup.begin() + i);
  121. break;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. int nSize = currentGroup.size();
  128. ns_db::TEMPERATURE_LIST_STRUCT _list = {};
  129. if (nSize > 0)
  130. {
  131. //最大减一
  132. _list = currentGroup[nSize - 1];
  133. }
  134. else
  135. {
  136. _list.iId = ui->idListComboBox->currentText().toInt();
  137. }
  138. _list.iSerialNumber += 1;
  139. currentGroup.push_back(_list);
  140. }
  141. for (int i = 0; i < currentGroup.size(); i++)
  142. {
  143. AddListItem<ShowTemperatureListNumberWnd>(currentGroup[i], i, ui->serialNumberList, QSize(370, 48));
  144. }
  145. UpDataParameter(true);
  146. break; //只要一个
  147. }
  148. nI++;
  149. }
  150. }
  151. void ShowTempPage::InitSerialNumberList()
  152. {
  153. std::vector <ns_db::TEMPERATURE_LIST_STRUCT> vecCurrentTemperatureList = m_pProduct->GetAllTemperatureList();
  154. int n = 0;
  155. // 模拟数据
  156. //if (vecCurrentTemperatureList.size() == 0)
  157. {
  158. for (int i = 0; i < 111; i++)
  159. {
  160. ns_db::TEMPERATURE_LIST_STRUCT _a = {};
  161. if (n < 6)
  162. {
  163. _a.iId = i + 1;
  164. }
  165. else
  166. {
  167. n = 0;
  168. }
  169. n++;
  170. _a.iSerialNumber = i + 1;
  171. vecCurrentTemperatureList.push_back(_a);
  172. }
  173. }
  174. // 第一次遍历:按 iId 分组
  175. for (const auto& item : vecCurrentTemperatureList)
  176. {
  177. m_groupedById[item.iId].push_back(item);
  178. }
  179. UpdataUi(0,0);
  180. for (int i = 0; i < m_groupedById.size(); i++)
  181. {
  182. ui->idListComboBox->addItem(QString::number(i));
  183. }
  184. // const std::vector<ns_db::TEMPERATURE_LIST_STRUCT>& currentGroup = m_groupedById[0].second;
  185. }
  186. void ShowTempPage::UpDataParameter(bool isDel /*= false*/, const ns_db::TEMPERATURE_LIST_STRUCT temperaturePar /*= {}*/)
  187. {
  188. if (isDel)
  189. {
  190. ui->praTabWidget->removeTab(0);
  191. m_ShowTemperatureWnd->deleteLater();
  192. m_ShowTemperatureWnd = nullptr;
  193. }
  194. else
  195. {
  196. if (m_ShowTemperatureWnd)
  197. {
  198. UpDataParameter(true);
  199. }
  200. m_ShowTemperatureWnd = new ShowTemperatureWnd();
  201. m_ShowTemperatureWnd->SetTemperaturePar(temperaturePar);
  202. connect(m_ShowTemperatureWnd, &ShowTemperatureWnd::SendModifyTemperatureListSingals,
  203. this, &ShowTempPage::GetModifyTemperatureListSlots);
  204. ui->praTabWidget->addTab(m_ShowTemperatureWnd, "Parameter Page");
  205. }
  206. }
  207. template <typename Y>
  208. void ShowTempPage::AddListItem(const ns_db::TEMPERATURE_LIST_STRUCT& temperaturePar,
  209. int nIndex,QListWidget* listWidget,const QSize& sizeHint)
  210. {
  211. Y* pWnd = new Y(this);
  212. pWnd->setAttribute(Qt::WA_DeleteOnClose);
  213. connect(pWnd, &Y::SendDelTemperatureListSingals, this, &ShowTempPage::GetDelTemperatureListSlots);
  214. //connect(pWnd, &Y::SendAddTemperatureListSingals, this, &ShowTempPage::GetAddTemperatureListSlots);
  215. //connect(pWnd, &Y::SendModifyTemperatureListSingals, this, &ShowTempPage::GetModifyTemperatureListSlots);
  216. pWnd->SetTemperaturePar(temperaturePar, nIndex);
  217. QListWidgetItem* item = new QListWidgetItem();
  218. item->setSizeHint(sizeHint);
  219. listWidget->addItem(item);
  220. listWidget->setItemWidget(item, pWnd);
  221. }
  222. void ShowTempPage::on_idListComboBox_currentIndexChanged(int index)
  223. {
  224. UpdataUi(index, 0);
  225. }
  226. void ShowTempPage::on_serialNumberList_itemSelectionChanged()
  227. {
  228. QListWidgetItem* selectedItem = ui->serialNumberList->currentItem();
  229. if (selectedItem)
  230. {
  231. int row = ui->serialNumberList->row(selectedItem);
  232. UpdataUi(ui->idListComboBox->currentText().toInt(), row);
  233. }
  234. }
  235. void ShowTempPage::on_addBut_clicked()
  236. {
  237. AddOrDel(ui->idListComboBox->currentText().toInt(), -1, false);
  238. }
  239. void ShowTempPage::on_saveAllBut_clicked()
  240. {
  241. //保存所有
  242. //m_pProduct->GetAllTemperatureList();
  243. }
  244. void ShowTempPage::GetDelTemperatureListSlots(int iId, int iSerialNumber)
  245. {
  246. AddOrDel(iId, iSerialNumber, true);
  247. }
  248. void ShowTempPage::GetModifyTemperatureListSlots(const ns_db::TEMPERATURE_LIST_STRUCT& temperaturePar)
  249. {
  250. int nI = 0;
  251. for (auto& pair : m_groupedById)
  252. {
  253. if (nI == temperaturePar.iId)
  254. {
  255. std::vector<ns_db::TEMPERATURE_LIST_STRUCT>& currentGroup = pair.second;
  256. for (int i = 0; i < currentGroup.size(); i++)
  257. {
  258. if (currentGroup[i].iSerialNumber == temperaturePar.iSerialNumber)
  259. {
  260. // 直接全部替换
  261. currentGroup[i] = temperaturePar;
  262. break;
  263. }
  264. }
  265. break; //只要一个
  266. }
  267. nI++;
  268. }
  269. }