Group.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. #include "Group.h"
  2. #include "ui_Group.h"
  3. #include <QMouseEvent>
  4. #include <QSettings>
  5. #include <QVBoxLayout>
  6. #include <QLabel>
  7. #include <QTimer>
  8. #include "QDir"
  9. // 静态变量初始化
  10. Group* Group::m_pCurrentlySelectedGroup = nullptr;
  11. Group* Group::m_pPreviouslySelectedBlueGroup = nullptr;
  12. Group* Group::m_pLastClickedGroup = nullptr;
  13. int Group::m_stnLastClickedIndex = 0;
  14. int Group::m_stnLastSavedIndex = 1;
  15. Group::Group(int groupId,const CameraInfo& cameraInfo, ns_module::CViewInterface *cViewInterface,QList<QPair<QString, QString>> fileList,QWidget* parent)
  16. //Group::Group(const CameraInfo& cameraInfo, QWidget* parent)
  17. : QWidget(parent), ui(new Ui::Group), m_nCurrentGroupId(groupId)
  18. {
  19. ui->setupUi(this);
  20. m_stnCurrentComboBoxIndex = 0;
  21. m_pCViewInterface = cViewInterface;
  22. m_nGroupId = m_nCurrentGroupId;
  23. //初始化目录名称
  24. for (const auto& pair : fileList) {
  25. ui->DatacomboBox->addItem(pair.first, pair.second);
  26. }
  27. ui->DatacomboBox->setCurrentIndex(m_stnCurrentComboBoxIndex);
  28. ui->GroupButton->setStyleSheet(
  29. "QPushButton:hover {"
  30. " background-color: #45a049;"
  31. "}"
  32. "QPushButton:pressed {"
  33. " background-color: #3e8e41;"
  34. "}"
  35. );
  36. ui->Imagewidget_left->setProperty("groupId", groupId);
  37. ui->Imagewidget_right->setProperty("groupId", groupId);
  38. ui->rightBackground->setStyleSheet("");
  39. ui->leftBackground->setStyleSheet("");
  40. if (cameraInfo.eType == 1)
  41. {
  42. m_pWafer = new Wafer(0, ui->Imagewidget_right);
  43. if (m_pCViewInterface != nullptr)
  44. {
  45. std::vector<ns_mat::WAFER_MATRIX_POINT_INFO_STRUCT> veWafer;
  46. m_pCViewInterface->GetWaferAllPointInfo(veWafer);
  47. m_pWafer->UpdataVal(veWafer);
  48. }
  49. else
  50. {
  51. m_pWafer->UpdataGenerateTestData();
  52. }
  53. WaferWidget();
  54. }
  55. else if (cameraInfo.eType == 2)
  56. {
  57. m_pWaffle = new Waffle(0, ui->Imagewidget_right);
  58. if (m_pCViewInterface != nullptr)
  59. {
  60. std::vector<ns_mat::WAFFLE_MATRIX_POINT_STRUCT> veWaffle;
  61. m_pCViewInterface->GetWafflePackAllPointInfo(veWaffle);
  62. m_pWaffle->UpdataVal(veWaffle);
  63. }
  64. else
  65. {
  66. m_pWaffle->UpdataGenerateTestData();
  67. }
  68. WaffleWidget();
  69. }
  70. else if (cameraInfo.eType == 3)
  71. {
  72. m_pMaterialbox = new MaterialBox(0, ui->Imagewidget_right);
  73. MaterialBoxWidget();
  74. }
  75. else if (cameraInfo.eType == 4)
  76. {
  77. m_pBond = new Bond(ui->Imagewidget_right);
  78. if (m_pCViewInterface != nullptr)
  79. {
  80. std::vector<ns_mat::POINT_INFO_STRUCT> stPointInfo;
  81. m_pCViewInterface->GetBondAllPointInfo(stPointInfo);
  82. m_pBond->UpdataVal(stPointInfo);
  83. }
  84. else
  85. {
  86. m_pBond->UpdataGenerateTestData();
  87. }
  88. BondWidget();
  89. }
  90. saveGroupSettings(groupId, cameraInfo.eType);
  91. connect(ui->GroupButton, &QPushButton::clicked, this, &Group::onclickbutton);
  92. connect(ui->DatacomboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
  93. this, &Group::on_DatacomboBox_currentIndexChanged);
  94. initForm();
  95. connect(this, &Group::SendGroupSelectedSignals, this, &Group::GetGroupSelectedSlots);
  96. }
  97. Group::~Group()
  98. {
  99. delete ui;
  100. }
  101. void Group::initForm()
  102. {
  103. ui->Imagewidget_left->installEventFilter(this);
  104. ui->Imagewidget_right->installEventFilter(this);
  105. // MainWnd *mainWindow = new MainWnd();
  106. // connect(mainWindow,&MainWnd::styleChanged,this,&Group::Changedstyle);
  107. // QTimer *timer = new QTimer(this);
  108. // connect(timer, &QTimer::timeout, this, &Group::Changedstyle);
  109. // timer->start(100);
  110. }
  111. void Group::Changedstyle()
  112. {
  113. QSettings settings("YourCompany", "YourApplication_style");
  114. int flag = settings.value("Flag_Style", 0).toInt();
  115. if (flag == 0) {
  116. ui->DatacomboBox->setStyleSheet("background-color: #FFFFFF;");
  117. } else {
  118. ui->DatacomboBox->setStyleSheet("background-color: #4C4FA6;");
  119. }
  120. }
  121. bool Group::eventFilter(QObject *obj, QEvent *event)
  122. {
  123. if (event->type() == QEvent::MouseButtonDblClick)
  124. {
  125. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
  126. if (mouseEvent->button() == Qt::LeftButton)
  127. {
  128. int index = 0;
  129. if (obj == this->ui->Imagewidget_left)
  130. {
  131. index = 1;
  132. setEnableControls(true);
  133. }
  134. else if (obj == this->ui->Imagewidget_right)
  135. {
  136. index = 2;
  137. setEnableControls(false);
  138. }
  139. if (index == 0) {
  140. return QWidget::eventFilter(obj, event);
  141. }
  142. //获取当前点击的group
  143. int groupId = obj->property("groupId").toInt();
  144. QSettings lastSettings("YourCompany", "YourApplication_");
  145. int lastGroupId = lastSettings.value("GroupId", 0).toInt();
  146. int lastIndex = lastSettings.value("Index", 0).toInt();
  147. //对比判断点击是否当前的最新小窗口
  148. if ((lastGroupId == groupId) && (lastIndex == index)) {
  149. return QWidget::eventFilter(obj, event);
  150. }
  151. // 保存索引并调用检查函数
  152. QSettings settings("YourCompany", "YourApplication_");
  153. settings.setValue("GroupId", groupId);
  154. settings.setValue("Index", index);
  155. //改变小窗口边框颜色
  156. check_selected(index);
  157. //发送信号
  158. emit SetCurrentSelectSig(groupId, index);
  159. emit SendGroupSelectedSignals(this, index);
  160. emit sendUpdateGroupState(groupId, index);
  161. return true;
  162. }
  163. }
  164. return QWidget::eventFilter(obj, event);
  165. }
  166. void Group::check_selected(int index)
  167. {
  168. if (index == 1)
  169. {
  170. ui->leftBackground->setStyleSheet("border: 2px solid red;");
  171. }
  172. else if (index == 2)
  173. {
  174. ui->rightBackground->setStyleSheet("border: 2px solid red;");
  175. }
  176. }
  177. void Group::saveGroupSettings(int Id, int materialWndType)
  178. {
  179. QSettings settings("YourOrganization", "YourApplication");
  180. settings.beginGroup(QString::number(Id));
  181. //settings.setValue("ImagePath1", imagePath1);
  182. settings.setValue("MaterialWndType", materialWndType);
  183. //settings.setValue("TextList", textList);
  184. settings.endGroup();
  185. }
  186. void Group::WaferWidget()
  187. {
  188. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  189. m_pWafer->paintInitFrom(ui->Imagewidget_right);
  190. QLabel* pixmapLabel = new QLabel();
  191. pixmapLabel->setPixmap(m_pWafer->getGlobalPixmap());
  192. ui->Imagewidget_right->setLayout(layout2);
  193. ui->Imagewidget_right->setFixedSize(110, 110);
  194. layout2->setContentsMargins(0, 0, 0, 0);
  195. layout2->addWidget(pixmapLabel);
  196. }
  197. void Group::WaffleWidget()
  198. {
  199. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  200. m_pWaffle->paintInitFrom(ui->Imagewidget_right);
  201. QLabel* pixmapLabel = new QLabel();
  202. pixmapLabel->setPixmap(m_pWaffle->getGlobalPixmap());
  203. ui->Imagewidget_right->setLayout(layout2);
  204. ui->Imagewidget_right->setFixedSize(110, 110);
  205. layout2->setContentsMargins(0, 0, 0, 0);
  206. layout2->addWidget(pixmapLabel);
  207. }
  208. void Group::MaterialBoxWidget()
  209. {
  210. QVBoxLayout* layout2 = new QVBoxLayout(ui->Imagewidget_right);
  211. m_pMaterialbox->paintInitFrom(ui->Imagewidget_right);
  212. QLabel* pixmapLabel = new QLabel();
  213. pixmapLabel->setPixmap(m_pMaterialbox->getGlobalPixmap());
  214. ui->Imagewidget_right->setLayout(layout2);
  215. ui->Imagewidget_right->setFixedSize(110, 110);
  216. layout2->setContentsMargins(0, 0, 0, 0);
  217. layout2->addWidget(pixmapLabel);
  218. }
  219. void Group::BondWidget()
  220. {
  221. QVBoxLayout* layout2 = new QVBoxLayout(ui->Imagewidget_right);
  222. m_pBond->paintInitFrom(ui->Imagewidget_right);
  223. QLabel* pixmapLabel = new QLabel();
  224. pixmapLabel->setPixmap(m_pBond->getGlobalPixmap());
  225. ui->Imagewidget_right->setLayout(layout2);
  226. ui->Imagewidget_right->setFixedSize(110, 110);
  227. layout2->setContentsMargins(0, 0, 0, 0);
  228. layout2->addWidget(pixmapLabel);
  229. }
  230. void Group::onclickbutton(){
  231. QSettings settings("YourCompany", "YourApplication_Button");
  232. settings.setValue("GroupId_button", m_nCurrentGroupId);
  233. emit send_button_Signal();
  234. }
  235. void Group::setDatacomboBox(int index){
  236. ui->DatacomboBox->setCurrentIndex(index);
  237. }
  238. void Group::on_DatacomboBox_currentIndexChanged(int comboxIndex){
  239. send_ComboBox_singal(m_nCurrentGroupId,comboxIndex);
  240. }
  241. void Group::UpDataImageShowSlots(const QPixmap& imageData)
  242. {
  243. QSize size_left = ui->Imagewidget_left->size();
  244. QPixmap scaledPixmap = imageData.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  245. ui->Imagewidget_left->setPixmap(scaledPixmap);
  246. emit SetUpDataImageShowSig(imageData);
  247. }
  248. void Group::showEvent(QShowEvent *event) {
  249. QWidget::showEvent(event);
  250. loadBorderSettings();
  251. }
  252. void Group::hideEvent(QHideEvent *event) {
  253. QWidget::hideEvent(event);
  254. saveBorderSettings(); // 保存边框颜色信息
  255. }
  256. void Group::saveBorderSettings()
  257. {
  258. QSettings settings("YourOrganization", "YourApplication");
  259. settings.beginGroup(QString::number(m_nCurrentGroupId));
  260. QString leftStyle = ui->leftBackground->styleSheet();
  261. QString rightStyle = ui->rightBackground->styleSheet();
  262. if (leftStyle.contains("blue", Qt::CaseInsensitive)) {
  263. leftStyle = ""; // 清除样式
  264. // 记录该边框信息
  265. recordBorderInfo(m_nCurrentGroupId, "Left");
  266. }
  267. if (rightStyle.contains("blue", Qt::CaseInsensitive)) {
  268. rightStyle = ""; // 清除样式
  269. // 记录该边框信息
  270. recordBorderInfo(m_nCurrentGroupId, "Right");
  271. }
  272. settings.setValue("LeftBorderStyle", leftStyle);
  273. settings.setValue("RightBorderStyle", rightStyle);
  274. settings.setValue("LastClickedIndex", m_stnLastClickedIndex);
  275. settings.setValue("LastSavedIndex", m_stnLastSavedIndex);
  276. if (m_pCurrentlySelectedGroup == this) {
  277. settings.setValue("IsCurrentlySelected", true);
  278. }
  279. else {
  280. settings.setValue("IsCurrentlySelected", false);
  281. }
  282. settings.endGroup();
  283. }
  284. void Group::recordBorderInfo(int groupId, const QString& side)
  285. {
  286. QSettings settings("YourOrganization", "YourApplication");
  287. settings.beginGroup("ModifiedBorders");
  288. QString key = QString("%1_%2").arg(groupId).arg(side);
  289. settings.setValue(key, true);
  290. settings.endGroup();
  291. }
  292. void Group::loadBorderSettings()
  293. {
  294. QSettings settings("YourOrganization", "YourApplication");
  295. settings.beginGroup(QString::number(m_nCurrentGroupId));
  296. QString leftStyle = settings.value("LeftBorderStyle", "").toString();
  297. QString rightStyle = settings.value("RightBorderStyle", "").toString();
  298. m_stnLastClickedIndex = settings.value("LastClickedIndex", 0).toInt();
  299. m_stnLastSavedIndex = settings.value("LastSavedIndex", 1).toInt();
  300. bool isCurrentlySelected = settings.value("IsCurrentlySelected", false).toBool();
  301. QSettings settings2("OrganizationName__", "ApplicationName__");
  302. int Index = settings2.value("lastIndex", 1).toInt();
  303. if (Index == 2) {
  304. QSettings borderInfoSettings("YourOrganization", "YourApplication");
  305. borderInfoSettings.beginGroup("ModifiedBorders");
  306. QString leftKey = QString("%1_Left").arg(m_nCurrentGroupId);
  307. QString rightKey = QString("%1_Right").arg(m_nCurrentGroupId);
  308. // 检查左边框是否需要还原
  309. if (borderInfoSettings.contains(leftKey)) {
  310. leftStyle = getBlueBorderStyle();
  311. borderInfoSettings.remove(leftKey); // 移除记录,避免下次重复处理
  312. qDebug() << "Left border of group" << m_nCurrentGroupId << "restored to blue.";
  313. m_pPreviouslySelectedBlueGroup = this;
  314. // lastClickedGroup =this;
  315. }
  316. // 检查右边框是否需要还原
  317. if (borderInfoSettings.contains(rightKey)) {
  318. rightStyle = getBlueBorderStyle();
  319. borderInfoSettings.remove(rightKey);
  320. qDebug() << "Right border of group" << m_nCurrentGroupId << "restored to blue.";
  321. m_pPreviouslySelectedBlueGroup = this;
  322. // lastClickedGroup =this;
  323. }
  324. borderInfoSettings.endGroup();
  325. }
  326. ui->leftBackground->setStyleSheet(leftStyle);
  327. ui->rightBackground->setStyleSheet(rightStyle);
  328. if (isCurrentlySelected) {
  329. m_pCurrentlySelectedGroup = this;
  330. m_pLastClickedGroup = this;
  331. if (leftStyle.contains("red")) {
  332. m_stnLastClickedIndex = 1;
  333. qDebug() << "Left border of group" << m_nCurrentGroupId << "is red.";
  334. }
  335. else if (rightStyle.contains("red")) {
  336. m_stnLastClickedIndex = 2;
  337. qDebug() << "Right border of group" << m_nCurrentGroupId << "is red.";
  338. }
  339. }
  340. settings.endGroup();
  341. }
  342. QString Group::getBlueBorderStyle()
  343. {
  344. static const QString blueBorderStyle = "border: 2px solid blue;";
  345. return blueBorderStyle;
  346. }
  347. QString Group::getRedBorderStyle()
  348. {
  349. static const QString blueBorderStyle = "";
  350. return blueBorderStyle;
  351. }
  352. void Group::GetGroupSelectedSlots(Group* group, int index) {
  353. QSettings settings("OrganizationName__", "ApplicationName__");
  354. int currentLastSavedIndex = settings.value("lastIndex", 1).toInt();
  355. // 检查是否是同一个组的同一个索引被点击
  356. if (m_pLastClickedGroup == group && m_stnLastClickedIndex == index) {
  357. // 如果是同一个组的同一个索引被点击,则不更新边框
  358. return;
  359. }
  360. // 清除当前红色边框
  361. if (m_pCurrentlySelectedGroup != nullptr) {
  362. m_pCurrentlySelectedGroup->ui->leftBackground->setStyleSheet("");
  363. m_pCurrentlySelectedGroup->ui->rightBackground->setStyleSheet("");
  364. }
  365. // 清除当前蓝色边框
  366. if (m_pPreviouslySelectedBlueGroup != nullptr) {
  367. m_pPreviouslySelectedBlueGroup->ui->leftBackground->setStyleSheet("");
  368. m_pPreviouslySelectedBlueGroup->ui->rightBackground->setStyleSheet("");
  369. }
  370. if (currentLastSavedIndex == 1 || currentLastSavedIndex == 3) {
  371. // 仅当前选中设为红色,蓝色边框清空
  372. m_pPreviouslySelectedBlueGroup = nullptr;
  373. }
  374. else if (currentLastSavedIndex == 2) {
  375. // 当前选中设为红色,上一个选中设为蓝色
  376. if (m_pLastClickedGroup != nullptr) {
  377. QString blueBorderStyle = "border: 2px solid blue;";
  378. // 即使是同一个实例,只要索引不同就设置蓝色边框
  379. if (m_pLastClickedGroup == group && m_stnLastClickedIndex != index) {
  380. if (m_stnLastClickedIndex == 1) {
  381. m_pLastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
  382. }
  383. else if (m_stnLastClickedIndex == 2) {
  384. m_pLastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
  385. }
  386. m_pPreviouslySelectedBlueGroup = m_pLastClickedGroup;
  387. }
  388. else if (m_pLastClickedGroup != group) {
  389. if (m_stnLastClickedIndex == 1) {
  390. m_pLastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
  391. }
  392. else if (m_stnLastClickedIndex == 2) {
  393. m_pLastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
  394. }
  395. m_pPreviouslySelectedBlueGroup = m_pLastClickedGroup;
  396. }
  397. }
  398. }
  399. // 更新当前选中的 Group 实例
  400. m_pCurrentlySelectedGroup = group;
  401. // 将当前选中的边框设为红色
  402. group->check_selected(index);
  403. // 更新上一次点击的记录
  404. m_pLastClickedGroup = group;
  405. m_stnLastClickedIndex = index;
  406. }
  407. void Group::setEnableControls(bool enable) {
  408. ui->GroupButton->setEnabled(enable);
  409. ui->DatacomboBox->setEnabled(enable);
  410. if (enable == true) {
  411. ui->GroupButton->setStyleSheet(
  412. "QPushButton:hover {"
  413. " background-color: #45a049;"
  414. "}"
  415. "QPushButton:pressed {"
  416. " background-color: #3e8e41;"
  417. "}"
  418. );
  419. }
  420. else {
  421. ui->GroupButton->setStyleSheet("background-color: lightgray;");
  422. }
  423. }
  424. int Group::getCurrentComboBoxIndex(){
  425. return ui->DatacomboBox->currentIndex();
  426. }
  427. void Group::setCurrentCombox(const int index){
  428. m_stnCurrentComboBoxIndex = index;
  429. if(m_stnCurrentComboBoxIndex != 0){
  430. QString imagePath = ui->DatacomboBox->itemData(index).toString();
  431. QPixmap pixmap(imagePath);
  432. QSize size_left = ui->Imagewidget_left->size();
  433. QPixmap scaledPixmap = pixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  434. ui->Imagewidget_left->setPixmap(scaledPixmap);
  435. }
  436. }
  437. void Group::initBorderStyle(int index,int color) {
  438. if (index == 2) {
  439. if(color == 0){
  440. ui->rightBackground->setStyleSheet("border: 2px solid blue;");
  441. }
  442. else {
  443. ui->rightBackground->setStyleSheet("border: 2px solid red;");
  444. }
  445. }
  446. else {
  447. if (color == 0) {
  448. ui->leftBackground->setStyleSheet("border: 2px solid blue;");
  449. }
  450. else {
  451. ui->leftBackground->setStyleSheet("border: 2px solid red;");
  452. }
  453. }
  454. }