SingleCameraOperationWnd.cpp 11 KB

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