Group.cpp 15 KB

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