SingleCameraOperationWnd.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "SingleCameraOperationWnd.h"
  2. #include "ui_SingleCameraOperationWnd.h"
  3. #include <QSettings>
  4. #include <QCloseEvent>
  5. #include "Group.h"
  6. #include <QVBoxLayout>
  7. #include <QDebug>
  8. #include <QTimer>
  9. Demo001_3::Demo001_3(QWidget *parent)
  10. : QMainWindow(parent)
  11. , ui(new Ui::Demo001_3), scaleFactor(1.0)
  12. {
  13. ui->setupUi(this);
  14. initFrom();
  15. // connect(this, &Demo001_3::closeEvent, this, &Demo001_3::saveSliderStates);
  16. }
  17. Demo001_3::~Demo001_3()
  18. {
  19. delete ui;
  20. }
  21. void Demo001_3::initFrom() {
  22. QTimer *timer = new QTimer(this);
  23. connect(timer, &QTimer::timeout, this, &Demo001_3::checkSettings);
  24. timer->start(100);
  25. ui->label_Percentage->setAlignment(Qt::AlignCenter);
  26. // 设置右下部分
  27. loadSliderStates();
  28. initSliders();
  29. initLineEdits();
  30. connectSliderAndLineEdit(ui->verticalSlider, ui->lineEdit);
  31. connectSliderAndLineEdit(ui->verticalSlider_2, ui->lineEdit_2);
  32. connectSliderAndLineEdit(ui->verticalSlider_3, ui->lineEdit_3);
  33. connectSliderAndLineEdit(ui->verticalSlider_4, ui->lineEdit_4);
  34. connectSliderAndLineEdit(ui->verticalSlider_5, ui->lineEdit_5);
  35. connectSliderAndLineEdit(ui->verticalSlider_6, ui->lineEdit_6);
  36. // 设置右上部分
  37. QWidget *viewport = ui->scrollArea->viewport();
  38. QWidget *container = new QWidget(viewport);
  39. QVBoxLayout *layout = new QVBoxLayout(container);
  40. layout->setSpacing(16); // 设置Group之间的间隔距离
  41. layout->setMargin(0);
  42. QStringList dispmodel;
  43. dispmodel<<("P die align")<<("D device align");
  44. Group *widget = new Group(1, ":/images/test_image/image_1.png", ":/images/test_image/image_2.png", dispmodel, this);
  45. layout->addWidget(widget);
  46. QStringList dispmodel1;
  47. dispmodel1<<("D device align")<<("P die align");
  48. Group *widget_1 = new Group(2, ":/images/test_image/image_3.png", ":/images/test_image/image_4.png", dispmodel1, this);
  49. layout->addWidget(widget_1);
  50. QStringList dispmodel2;
  51. dispmodel2<<("B device align")<<("P die align");
  52. Group *widget_2 = new Group(3, ":/images/test_image/image_5.png", ":/images/test_image/image_6.png", dispmodel2, this);
  53. layout->addWidget(widget_2);
  54. QStringList dispmodel3;
  55. dispmodel3<<("U die align")<<("D device align");
  56. Group *widget_3 = new Group(4, ":/images/test_image/image_7.png", ":/images/test_image/image_8.png", dispmodel3, this);
  57. layout->addWidget(widget_3);
  58. Group *widget_4 = new Group(5, ":/images/test_image/image.png", ":/images/test_image/image_2.png", dispmodel, this);
  59. layout->addWidget(widget_4);
  60. widget->setMinimumHeight(162);
  61. widget_1->setMinimumHeight(162);
  62. widget_2->setMinimumHeight(162);
  63. widget_3->setMinimumHeight(162);
  64. widget_4->setMinimumHeight(162);
  65. widget->setMaximumWidth(244);
  66. widget_1->setMaximumWidth(244);
  67. widget_2->setMaximumWidth(244);
  68. widget_3->setMaximumWidth(244);
  69. widget_4->setMaximumWidth(244);
  70. container->setLayout(layout);
  71. ui->scrollArea->setWidget(container);
  72. ui->scrollArea->resize(261, 700);
  73. ui->Operatewidget->setMouseTracking(true);
  74. }
  75. void Demo001_3::closeEvent(QCloseEvent *event) {
  76. // 保存滑块状态
  77. saveSliderStates();
  78. // 确保调用基类的 closeEvent 处理其他关闭逻辑
  79. QMainWindow::closeEvent(event);
  80. }
  81. void Demo001_3::initSliders() {
  82. QList<QSlider*> sliders = {
  83. ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
  84. ui->verticalSlider_4, ui->verticalSlider_5, ui->verticalSlider_6
  85. };
  86. QList<int> initialValues = {11, 22, 9, 11, 0, 0};
  87. for (int i = 0; i < sliders.size(); ++i) {
  88. sliders[i]->setMinimum(0);
  89. sliders[i]->setMaximum(100);
  90. sliders[i]->setValue(initialValues[i]);
  91. }
  92. }
  93. void Demo001_3::initLineEdits() {
  94. QList<QLineEdit*> lineEdits = {
  95. ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
  96. ui->lineEdit_4, ui->lineEdit_5, ui->lineEdit_6
  97. };
  98. for (QLineEdit* lineEdit : lineEdits) {
  99. lineEdit->setAlignment(Qt::AlignCenter);
  100. }
  101. }
  102. void Demo001_3::saveSliderStates() {
  103. QSettings settings("YourCompany", "YourApp");
  104. settings.beginGroup("SliderStates");
  105. settings.setValue("verticalSlider", ui->verticalSlider->value());
  106. settings.setValue("verticalSlider_2", ui->verticalSlider_2->value());
  107. settings.setValue("verticalSlider_3", ui->verticalSlider_3->value());
  108. settings.setValue("verticalSlider_4", ui->verticalSlider_4->value());
  109. settings.setValue("verticalSlider_5", ui->verticalSlider_5->value());
  110. settings.setValue("verticalSlider_6", ui->verticalSlider_6->value());
  111. settings.endGroup();
  112. }
  113. void Demo001_3::loadSliderStates() {
  114. QSettings settings("YourCompany", "YourApp");
  115. settings.beginGroup("SliderStates");
  116. ui->verticalSlider->setValue(settings.value("verticalSlider", 11).toInt());
  117. ui->verticalSlider_2->setValue(settings.value("verticalSlider_2", 22).toInt());
  118. ui->verticalSlider_3->setValue(settings.value("verticalSlider_3", 9).toInt());
  119. ui->verticalSlider_4->setValue(settings.value("verticalSlider_4", 11).toInt());
  120. ui->verticalSlider_5->setValue(settings.value("verticalSlider_5", 0).toInt());
  121. ui->verticalSlider_6->setValue(settings.value("verticalSlider_6", 0).toInt());
  122. settings.endGroup();
  123. }
  124. // QSlider 和 QLineEdit 的联动
  125. void Demo001_3::connectSliderAndLineEdit(QSlider* slider, QLineEdit* lineEdit)
  126. {
  127. QIntValidator* validator = new QIntValidator(slider->minimum(), slider->maximum(), lineEdit);
  128. lineEdit->setValidator(validator);
  129. connect(slider, &QSlider::valueChanged, [lineEdit](int value) {
  130. lineEdit->setText(QString::number(value));
  131. });
  132. connect(lineEdit, &QLineEdit::textChanged, [slider](const QString &text) {
  133. bool ok;
  134. int value = text.toInt(&ok);
  135. if (ok && value >= slider->minimum() && value <= slider->maximum()) {
  136. slider->setValue(value);
  137. } else if (!text.isEmpty()) {
  138. int closestValue = qMin(qMax(text.toInt(&ok), slider->minimum()), slider->maximum());
  139. slider->setValue(closestValue);
  140. }
  141. });
  142. }
  143. void Demo001_3::checkSettings() {
  144. QSettings settings("YourCompany", "YourApplication_");
  145. int groupId = settings.value("GroupId", 0).toInt();
  146. int index = settings.value("Index", 0).toInt();
  147. if (groupId != lastGroupId || index != lastIndex) {
  148. lastGroupId = groupId;
  149. lastIndex = index;
  150. loadSettings();
  151. }
  152. }
  153. void Demo001_3::loadSettings()
  154. {
  155. QSettings settings("YourCompany", "YourApplication_");
  156. int groupId = settings.value("GroupId", 0).toInt();
  157. int index = settings.value("Index", 0).toInt();
  158. loadGroupSettings(groupId, index);
  159. }
  160. void Demo001_3::loadGroupSettings(int Id, int Index) {
  161. QSettings settings("YourOrganization", "YourApplication");
  162. settings.beginGroup(QString::number(Id));
  163. QString imagePath1 = settings.value("ImagePath1").toString();
  164. QString imagePath2 = settings.value("ImagePath2").toString();
  165. QStringList textList = settings.value("TextList").toStringList();
  166. settings.endGroup();
  167. QSize size = ui->Operatewidget->size();
  168. QPixmap newPixmap;
  169. if (Index == 1) {
  170. newPixmap = QPixmap(imagePath1);
  171. } else if (Index == 2) {
  172. newPixmap = QPixmap(imagePath2);
  173. } else {
  174. newPixmap = QPixmap(":/images/test_image/image.png");
  175. }
  176. QPixmap scaledPixmap = newPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  177. ui->Operatewidget->setPixmap(scaledPixmap);
  178. // 更新当前图片的成员变量
  179. currentPixmap = scaledPixmap;
  180. scaleFactor = 1.0;
  181. ui->label_Percentage->setText("100%");
  182. ui->DatacomboBox->clear();
  183. ui->DatacomboBox->addItems(textList);
  184. // qDebug() << "ComboBox items added:" << ui->DatacomboBox->count();
  185. // connect(ui->DatacomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Demo001_3::onComboBoxIndexChanged(int)));
  186. }
  187. void Demo001_3::on_ZoomUpButton_clicked() {
  188. if (scaleFactor < 3.0) {
  189. scaleFactor *= 1.1;
  190. }
  191. int newWidth = currentPixmap.width() * scaleFactor;
  192. int newHeight = currentPixmap.height() * scaleFactor;
  193. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  194. ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
  195. double percentage = (scaleFactor * 100);
  196. QString percentageStr = QString::number((int)percentage);
  197. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  198. }
  199. void Demo001_3::on_ZoomOutButton_clicked() {
  200. if (scaleFactor > 0.25) {
  201. scaleFactor *= 0.9;
  202. }
  203. int newWidth = currentPixmap.width() * scaleFactor;
  204. int newHeight = currentPixmap.height() * scaleFactor;
  205. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  206. ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
  207. double percentage = scaleFactor * 100;
  208. QString percentageStr = QString::number((int)percentage);
  209. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  210. }
  211. void Demo001_3::wheelEvent(QWheelEvent *event) {
  212. // 检查鼠标事件是否发生在 Operatewidget 上
  213. if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) {
  214. if (event->angleDelta().y() > 0) {
  215. if (scaleFactor < 3.0) {
  216. scaleFactor *= 1.1;
  217. }
  218. } else {
  219. if (scaleFactor > 0.25) {
  220. scaleFactor *= 0.9;
  221. }
  222. }
  223. int newWidth = currentPixmap.width() * scaleFactor;
  224. int newHeight = currentPixmap.height() * scaleFactor;
  225. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  226. ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
  227. double percentage = scaleFactor * 100;
  228. QString percentageStr = QString::number((int)percentage);
  229. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  230. }
  231. QMainWindow::wheelEvent(event);
  232. }
  233. void Demo001_3::onComboBoxIndexChanged(int index) {
  234. // 根据index获取图片路径
  235. QString imagePath = getImagePathFromIndex(index);
  236. if (!imagePath.isEmpty()) {
  237. QPixmap newPixmap(imagePath);
  238. QPixmap scaledPixmap = newPixmap.scaled(ui->Operatewidget->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  239. ui->Operatewidget->setPixmap(scaledPixmap);
  240. // 更新当前图片的成员变量
  241. currentPixmap = scaledPixmap;
  242. scaleFactor = 1.0;
  243. ui->label_Percentage->setText("100%");
  244. }
  245. }
  246. QString Demo001_3::getImagePathFromIndex(int index) {
  247. QStringList imagePaths = {
  248. ":/images/test_image/image.png",
  249. ":/images/test_image/image_2.png",
  250. };
  251. if (index >= 0 && index < imagePaths.size()) {
  252. return imagePaths[index];
  253. }
  254. return ":/images/test_image/image.png";
  255. }
  256. void Demo001_3::showEvent(QShowEvent *event) {
  257. QMainWindow::showEvent(event);
  258. loadSettings();
  259. }
  260. void Demo001_3::hideEvent(QHideEvent *event) {
  261. QMainWindow::hideEvent(event);
  262. }