Group.cpp 12 KB

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