Group.cpp 15 KB

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