Group.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "Group.h"
  2. #include "ui_Group.h"
  3. #include <QDebug>
  4. #include <QMouseEvent>
  5. #include <QSettings>
  6. #include <QVBoxLayout>
  7. #include <QLabel>
  8. // 静态变量初始化
  9. Group* Group::currentlySelectedGroup = nullptr;
  10. Group* Group::previouslySelectedBlueGroup = nullptr;
  11. Group* Group::lastClickedGroup = nullptr;
  12. int Group::lastClickedIndex = 0;
  13. int Group::lastSavedIndex = 1;
  14. Group::Group(int Id, const QString& imagePath1, int MaterialWindowType, const QStringList& textList, QWidget* parent)
  15. : QWidget(parent), ui(new Ui::Group), groupId(Id)
  16. {
  17. ui->setupUi(this);
  18. QPixmap pixmap1(imagePath1);
  19. ui->Imagewidget_left->setPixmap(pixmap1);
  20. ui->DatacomboBox->addItems(textList);
  21. ui->GroupButton->setStyleSheet(
  22. "QPushButton:hover {"
  23. " background-color: #45a049;"
  24. "}"
  25. "QPushButton:pressed {"
  26. " background-color: #3e8e41;"
  27. "}"
  28. );
  29. ui->Imagewidget_left->setProperty("groupId", Id);
  30. ui->Imagewidget_right->setProperty("groupId", Id);
  31. if (MaterialWindowType == 1) {
  32. wafer = new Wafer(0, ui->Imagewidget_right);
  33. WaferWidget();
  34. } else if (MaterialWindowType == 2) {
  35. waffle = new Waffle(0, ui->Imagewidget_right);
  36. WaffleWidget();
  37. } else if (MaterialWindowType == 3) {
  38. materialbox = new MaterialBox(0, ui->Imagewidget_right);
  39. MaterialBoxWidget();
  40. }
  41. saveGroupSettings(Id, imagePath1, MaterialWindowType, textList);
  42. connect(ui->GroupButton, &QPushButton::clicked, this, &Group::onclickbutton);
  43. initForm();
  44. connect(this, &Group::groupSelected, this, &Group::onGroupSelected);
  45. }
  46. Group::~Group()
  47. {
  48. delete ui;
  49. }
  50. void Group::initForm()
  51. {
  52. ui->Imagewidget_left->installEventFilter(this);
  53. ui->Imagewidget_right->installEventFilter(this);
  54. }
  55. bool Group::eventFilter(QObject *obj, QEvent *event)
  56. {
  57. if (event->type() == QEvent::MouseButtonDblClick) {
  58. QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
  59. if (mouseEvent->button() == Qt::LeftButton) {
  60. int groupId = obj->property("groupId").toInt();
  61. QSettings settings("YourCompany", "YourApplication_");
  62. settings.setValue("GroupId", groupId);
  63. int index = 0;
  64. if (obj == this->ui->Imagewidget_left) {
  65. index = 1;
  66. settings.setValue("Index", 1);
  67. check_selected(1);
  68. } else if (obj == this->ui->Imagewidget_right) {
  69. index = 2;
  70. settings.setValue("Index", 2);
  71. check_selected(2);
  72. }
  73. emit groupSelected(this, index);
  74. return true;
  75. }
  76. }
  77. return QWidget::eventFilter(obj, event);
  78. }
  79. void Group::check_selected(int index)
  80. {
  81. if (index == 1) {
  82. ui->leftBackground->setStyleSheet("border: 2px solid red;");
  83. } else if (index == 2) {
  84. ui->rightBackground->setStyleSheet("border: 2px solid red;");
  85. }
  86. }
  87. void Group::saveGroupSettings(int Id, const QString& imagePath1, int materialWndType, const QStringList& textList)
  88. {
  89. QSettings settings("YourOrganization", "YourApplication");
  90. settings.beginGroup(QString::number(Id));
  91. settings.setValue("ImagePath1", imagePath1);
  92. settings.setValue("MaterialWndType", materialWndType);
  93. settings.setValue("TextList", textList);
  94. settings.endGroup();
  95. }
  96. void Group::WaferWidget()
  97. {
  98. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  99. wafer->paintInitFrom(ui->Imagewidget_right);
  100. QLabel* pixmapLabel = new QLabel();
  101. pixmapLabel->setPixmap(wafer->getGlobalPixmap());
  102. ui->Imagewidget_right->setLayout(layout2);
  103. ui->Imagewidget_right->setFixedSize(110, 110);
  104. layout2->setContentsMargins(0, 0, 0, 0);
  105. layout2->addWidget(pixmapLabel);
  106. }
  107. void Group::WaffleWidget()
  108. {
  109. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  110. ui->Imagewidget_right->setLayout(layout2);
  111. ui->Imagewidget_right->setFixedSize(110, 110);
  112. layout2->setContentsMargins(0, 0, 0, 0);
  113. layout2->addWidget(waffle);
  114. }
  115. void Group::MaterialBoxWidget()
  116. {
  117. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  118. ui->Imagewidget_right->setLayout(layout2);
  119. ui->Imagewidget_right->setFixedSize(110, 110);
  120. layout2->setContentsMargins(0, 0, 0, 0);
  121. layout2->addWidget(materialbox);
  122. }
  123. void Group::onclickbutton(){
  124. QSettings settings("YourCompany", "YourApplication_Button");
  125. settings.setValue("GroupId_button", groupId);
  126. emit send_button_Signal();
  127. }
  128. void Group::setDatacomboBox(int index){
  129. ui->DatacomboBox->setCurrentIndex(index);
  130. }
  131. void Group::on_DatacomboBox_currentIndexChanged(int index){
  132. send_ComboBox_singal(currentGroupId,index);
  133. }
  134. void Group::onGroupSelected(Group* group, int index)
  135. {
  136. QSettings settings("OrganizationName__", "ApplicationName__");
  137. int currentLastSavedIndex = settings.value("lastIndex", 1).toInt();
  138. // 清除当前红色边框
  139. if (currentlySelectedGroup != nullptr) {
  140. currentlySelectedGroup->ui->leftBackground->setStyleSheet("");
  141. currentlySelectedGroup->ui->rightBackground->setStyleSheet("");
  142. }
  143. // 清除当前蓝色边框
  144. if (previouslySelectedBlueGroup != nullptr) {
  145. previouslySelectedBlueGroup->ui->leftBackground->setStyleSheet("");
  146. previouslySelectedBlueGroup->ui->rightBackground->setStyleSheet("");
  147. }
  148. if (currentLastSavedIndex == 1 || currentLastSavedIndex == 3) {
  149. // 仅当前选中设为红色,蓝色边框清空
  150. previouslySelectedBlueGroup = nullptr;
  151. } else if (currentLastSavedIndex == 2) {
  152. // 当前选中设为红色,上一个选中设为蓝色
  153. if (lastClickedGroup != nullptr) {
  154. QString blueBorderStyle = "border: 2px solid blue;";
  155. // 即使是同一个实例,只要索引不同就设置蓝色边框
  156. if (lastClickedGroup == group && lastClickedIndex != index) {
  157. if (lastClickedIndex == 1) {
  158. lastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
  159. } else if (lastClickedIndex == 2) {
  160. lastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
  161. }
  162. previouslySelectedBlueGroup = lastClickedGroup;
  163. } else if (lastClickedGroup != group) {
  164. if (lastClickedIndex == 1) {
  165. lastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
  166. } else if (lastClickedIndex == 2) {
  167. lastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
  168. }
  169. previouslySelectedBlueGroup = lastClickedGroup;
  170. }
  171. }
  172. }
  173. // 更新当前选中的 Group 实例
  174. currentlySelectedGroup = group;
  175. // 将当前选中的边框设为红色
  176. group->check_selected(index);
  177. // 更新上一次点击的记录
  178. lastClickedGroup = group;
  179. lastClickedIndex = index;
  180. }
  181. void Group::showEvent(QShowEvent *event) {
  182. QWidget::showEvent(event);
  183. loadBorderSettings();
  184. }
  185. void Group::hideEvent(QHideEvent *event) {
  186. QWidget::hideEvent(event);
  187. saveBorderSettings(); // 保存边框颜色信息
  188. }
  189. void Group::saveBorderSettings()
  190. {
  191. QSettings settings("YourOrganization", "YourApplication");
  192. settings.beginGroup(QString::number(groupId));
  193. QString leftStyle = ui->leftBackground->styleSheet();
  194. if (leftStyle.contains("blue", Qt::CaseInsensitive)) {
  195. leftStyle = ""; // 清除样式
  196. // 记录该边框信息
  197. recordBorderInfo(groupId, "Left");
  198. }
  199. settings.setValue("LeftBorderStyle", leftStyle);
  200. QString rightStyle = ui->rightBackground->styleSheet();
  201. if (rightStyle.contains("blue", Qt::CaseInsensitive)) {
  202. rightStyle = ""; // 清除样式
  203. // 记录该边框信息
  204. recordBorderInfo(groupId, "Right");
  205. }
  206. settings.setValue("RightBorderStyle", rightStyle);
  207. settings.setValue("LastClickedIndex", lastClickedIndex);
  208. settings.setValue("LastSavedIndex", lastSavedIndex);
  209. if (currentlySelectedGroup == this) {
  210. settings.setValue("IsCurrentlySelected", true);
  211. } else {
  212. settings.setValue("IsCurrentlySelected", false);
  213. }
  214. settings.endGroup();
  215. }
  216. void Group::recordBorderInfo(int groupId, const QString& side)
  217. {
  218. QSettings settings("YourOrganization", "YourApplication");
  219. settings.beginGroup("ModifiedBorders");
  220. QString key = QString("%1_%2").arg(groupId).arg(side);
  221. settings.setValue(key, true);
  222. settings.endGroup();
  223. }
  224. void Group::loadBorderSettings()
  225. {
  226. QSettings settings("YourOrganization", "YourApplication");
  227. settings.beginGroup(QString::number(groupId));
  228. QString leftStyle = settings.value("LeftBorderStyle", "").toString();
  229. QString rightStyle = settings.value("RightBorderStyle", "").toString();
  230. lastClickedIndex = settings.value("LastClickedIndex", 0).toInt();
  231. lastSavedIndex = settings.value("LastSavedIndex", 1).toInt();
  232. bool isCurrentlySelected = settings.value("IsCurrentlySelected", false).toBool();
  233. QSettings settings2("OrganizationName__", "ApplicationName__");
  234. int Index = settings2.value("lastIndex", 1).toInt();
  235. if (Index == 2) {
  236. QSettings borderInfoSettings("YourOrganization", "YourApplication");
  237. borderInfoSettings.beginGroup("ModifiedBorders");
  238. QString leftKey = QString("%1_Left").arg(groupId);
  239. QString rightKey = QString("%1_Right").arg(groupId);
  240. // 检查左边框是否需要还原
  241. if (borderInfoSettings.contains(leftKey)) {
  242. leftStyle = getBlueBorderStyle();
  243. borderInfoSettings.remove(leftKey); // 移除记录,避免下次重复处理
  244. }
  245. // 检查右边框是否需要还原
  246. if (borderInfoSettings.contains(rightKey)) {
  247. rightStyle = getBlueBorderStyle();
  248. borderInfoSettings.remove(rightKey);
  249. }
  250. borderInfoSettings.endGroup();
  251. }
  252. ui->leftBackground->setStyleSheet(leftStyle);
  253. ui->rightBackground->setStyleSheet(rightStyle);
  254. if (isCurrentlySelected) {
  255. currentlySelectedGroup = this;
  256. if (leftStyle.contains("red")) {
  257. lastClickedIndex = 1;
  258. } else if (rightStyle.contains("red")) {
  259. lastClickedIndex = 2;
  260. }
  261. }
  262. settings.endGroup();
  263. }
  264. QString Group::getBlueBorderStyle()
  265. {
  266. static const QString blueBorderStyle = "border: 2px solid blue;";
  267. return blueBorderStyle;
  268. }