Group.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  132. waffle->paintInitFrom(ui->Imagewidget_right);
  133. QLabel* pixmapLabel = new QLabel();
  134. pixmapLabel->setPixmap(waffle->getGlobalPixmap());
  135. ui->Imagewidget_right->setLayout(layout2);
  136. ui->Imagewidget_right->setFixedSize(110, 110);
  137. layout2->setContentsMargins(0, 0, 0, 0);
  138. layout2->addWidget(pixmapLabel);
  139. }
  140. void Group::MaterialBoxWidget()
  141. {
  142. QVBoxLayout *layout2 = new QVBoxLayout(ui->Imagewidget_right);
  143. ui->Imagewidget_right->setLayout(layout2);
  144. ui->Imagewidget_right->setFixedSize(110, 110);
  145. layout2->setContentsMargins(0, 0, 0, 0);
  146. layout2->addWidget(materialbox);
  147. }
  148. void Group::onclickbutton(){
  149. QSettings settings("YourCompany", "YourApplication_Button");
  150. settings.setValue("GroupId_button", groupId);
  151. emit send_button_Signal();
  152. }
  153. void Group::setDatacomboBox(int index){
  154. ui->DatacomboBox->setCurrentIndex(index);
  155. }
  156. void Group::on_DatacomboBox_currentIndexChanged(int index){
  157. send_ComboBox_singal(currentGroupId,index);
  158. }
  159. void Group::onGroupSelected(Group* group, int index)
  160. {
  161. QSettings settings("OrganizationName__", "ApplicationName__");
  162. int currentLastSavedIndex = settings.value("lastIndex", 1).toInt();
  163. // 清除当前红色边框
  164. if (currentlySelectedGroup != nullptr) {
  165. currentlySelectedGroup->ui->leftBackground->setStyleSheet("");
  166. currentlySelectedGroup->ui->rightBackground->setStyleSheet("");
  167. }
  168. // 清除当前蓝色边框
  169. if (previouslySelectedBlueGroup != nullptr) {
  170. previouslySelectedBlueGroup->ui->leftBackground->setStyleSheet("");
  171. previouslySelectedBlueGroup->ui->rightBackground->setStyleSheet("");
  172. }
  173. if (currentLastSavedIndex == 1 || currentLastSavedIndex == 3) {
  174. // 仅当前选中设为红色,蓝色边框清空
  175. previouslySelectedBlueGroup = nullptr;
  176. } else if (currentLastSavedIndex == 2) {
  177. // 当前选中设为红色,上一个选中设为蓝色
  178. if (lastClickedGroup != nullptr) {
  179. QString blueBorderStyle = "border: 2px solid blue;";
  180. // 即使是同一个实例,只要索引不同就设置蓝色边框
  181. if (lastClickedGroup == group && lastClickedIndex != index) {
  182. if (lastClickedIndex == 1) {
  183. lastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
  184. } else if (lastClickedIndex == 2) {
  185. lastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
  186. }
  187. previouslySelectedBlueGroup = lastClickedGroup;
  188. } else if (lastClickedGroup != group) {
  189. if (lastClickedIndex == 1) {
  190. lastClickedGroup->ui->leftBackground->setStyleSheet(blueBorderStyle);
  191. } else if (lastClickedIndex == 2) {
  192. lastClickedGroup->ui->rightBackground->setStyleSheet(blueBorderStyle);
  193. }
  194. previouslySelectedBlueGroup = lastClickedGroup;
  195. }
  196. }
  197. }
  198. // 更新当前选中的 Group 实例
  199. currentlySelectedGroup = group;
  200. // 将当前选中的边框设为红色
  201. group->check_selected(index);
  202. // 更新上一次点击的记录
  203. lastClickedGroup = group;
  204. lastClickedIndex = index;
  205. }
  206. void Group::showEvent(QShowEvent *event) {
  207. QWidget::showEvent(event);
  208. loadBorderSettings();
  209. }
  210. void Group::hideEvent(QHideEvent *event) {
  211. QWidget::hideEvent(event);
  212. saveBorderSettings(); // 保存边框颜色信息
  213. }
  214. void Group::saveBorderSettings()
  215. {
  216. QSettings settings("YourOrganization", "YourApplication");
  217. settings.beginGroup(QString::number(groupId));
  218. QString leftStyle = ui->leftBackground->styleSheet();
  219. if (leftStyle.contains("blue", Qt::CaseInsensitive)) {
  220. leftStyle = ""; // 清除样式
  221. // 记录该边框信息
  222. recordBorderInfo(groupId, "Left");
  223. }
  224. settings.setValue("LeftBorderStyle", leftStyle);
  225. QString rightStyle = ui->rightBackground->styleSheet();
  226. if (rightStyle.contains("blue", Qt::CaseInsensitive)) {
  227. rightStyle = ""; // 清除样式
  228. // 记录该边框信息
  229. recordBorderInfo(groupId, "Right");
  230. }
  231. settings.setValue("RightBorderStyle", rightStyle);
  232. settings.setValue("LastClickedIndex", lastClickedIndex);
  233. settings.setValue("LastSavedIndex", lastSavedIndex);
  234. if (currentlySelectedGroup == this) {
  235. settings.setValue("IsCurrentlySelected", true);
  236. } else {
  237. settings.setValue("IsCurrentlySelected", false);
  238. }
  239. settings.endGroup();
  240. }
  241. void Group::recordBorderInfo(int groupId, const QString& side)
  242. {
  243. QSettings settings("YourOrganization", "YourApplication");
  244. settings.beginGroup("ModifiedBorders");
  245. QString key = QString("%1_%2").arg(groupId).arg(side);
  246. settings.setValue(key, true);
  247. settings.endGroup();
  248. }
  249. void Group::loadBorderSettings()
  250. {
  251. QSettings settings("YourOrganization", "YourApplication");
  252. settings.beginGroup(QString::number(groupId));
  253. QString leftStyle = settings.value("LeftBorderStyle", "").toString();
  254. QString rightStyle = settings.value("RightBorderStyle", "").toString();
  255. lastClickedIndex = settings.value("LastClickedIndex", 0).toInt();
  256. lastSavedIndex = settings.value("LastSavedIndex", 1).toInt();
  257. bool isCurrentlySelected = settings.value("IsCurrentlySelected", false).toBool();
  258. QSettings settings2("OrganizationName__", "ApplicationName__");
  259. int Index = settings2.value("lastIndex", 1).toInt();
  260. if (Index == 2) {
  261. QSettings borderInfoSettings("YourOrganization", "YourApplication");
  262. borderInfoSettings.beginGroup("ModifiedBorders");
  263. QString leftKey = QString("%1_Left").arg(groupId);
  264. QString rightKey = QString("%1_Right").arg(groupId);
  265. // 检查左边框是否需要还原
  266. if (borderInfoSettings.contains(leftKey)) {
  267. leftStyle = getBlueBorderStyle();
  268. borderInfoSettings.remove(leftKey); // 移除记录,避免下次重复处理
  269. }
  270. // 检查右边框是否需要还原
  271. if (borderInfoSettings.contains(rightKey)) {
  272. rightStyle = getBlueBorderStyle();
  273. borderInfoSettings.remove(rightKey);
  274. }
  275. borderInfoSettings.endGroup();
  276. }
  277. ui->leftBackground->setStyleSheet(leftStyle);
  278. ui->rightBackground->setStyleSheet(rightStyle);
  279. if (isCurrentlySelected) {
  280. currentlySelectedGroup = this;
  281. if (leftStyle.contains("red")) {
  282. lastClickedIndex = 1;
  283. } else if (rightStyle.contains("red")) {
  284. lastClickedIndex = 2;
  285. }
  286. }
  287. settings.endGroup();
  288. }
  289. QString Group::getBlueBorderStyle()
  290. {
  291. static const QString blueBorderStyle = "border: 2px solid blue;";
  292. return blueBorderStyle;
  293. }