Group.cpp 14 KB

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