SingleCameraOperationWnd.cpp 11 KB

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