Group.cpp 11 KB

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