MainAndSecondaryCamerasWnd.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include "MainAndSecondaryCamerasWnd.h"
  2. #include "ui_MainAndSecondaryCamerasWnd.h"
  3. #include <QSettings>
  4. #include <QCloseEvent>
  5. #include "Group.h"
  6. #include <QVBoxLayout>
  7. #include <QTimer>
  8. #include "CameraImage/CameraImageHandler.h"
  9. MainAndSecondaryCamerasWnd::MainAndSecondaryCamerasWnd(QWidget *parent)
  10. : QMainWindow(parent)
  11. , ui(new Ui::MainAndSecondaryCamerasWnd)
  12. {
  13. ui->setupUi(this);
  14. QSettings settings("YourOrganization", "YourApplication");
  15. settings.beginGroup("LastSettings");
  16. lastRightPixmap = settings.value("LastRightPixmap").value<QPixmap>();
  17. lasttextList = settings.value("LasttextList").toStringList();
  18. lastLastRightPixmap = settings.value("LastLastRightPixmap").value<QPixmap>();
  19. lastLasttextList = settings.value("LastLasttextList").toStringList();
  20. settings.endGroup();
  21. initFrom();
  22. }
  23. MainAndSecondaryCamerasWnd::~MainAndSecondaryCamerasWnd()
  24. {
  25. delete ui;
  26. }
  27. void MainAndSecondaryCamerasWnd::initFrom()
  28. {
  29. QTimer *timer = new QTimer(this);
  30. connect(timer, &QTimer::timeout, this, &MainAndSecondaryCamerasWnd::checkSettings);
  31. timer->start(100);
  32. ui->Leftlabel_Percentage->setAlignment(Qt::AlignCenter);
  33. ui->Rightlabel_Percentage->setAlignment(Qt::AlignCenter);
  34. initSliders();
  35. initLineEdits();
  36. connectSliderAndLineEdit(ui->verticalSlider, ui->lineEdit);
  37. connectSliderAndLineEdit(ui->verticalSlider_2, ui->lineEdit_2);
  38. connectSliderAndLineEdit(ui->verticalSlider_3, ui->lineEdit_3);
  39. connectSliderAndLineEdit(ui->verticalSlider_4, ui->lineEdit_4);
  40. connectSliderAndLineEdit(ui->verticalSlider_7, ui->lineEdit_7);
  41. connectSliderAndLineEdit(ui->verticalSlider_8, ui->lineEdit_8);
  42. QWidget *viewport = ui->scrollArea->viewport();
  43. QWidget *container = new QWidget(viewport);
  44. QHBoxLayout *mainLayout = new QHBoxLayout(container);
  45. mainLayout->setSpacing(28);
  46. mainLayout->setMargin(0);
  47. QList<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
  48. QList<Group*> widgets;
  49. for (int i = 0; i < numbers.size(); ++i) {
  50. int num = numbers[i];
  51. CameraImageHandler* manager = new CameraImageHandler(num);
  52. Group* widget = manager->getGroup();
  53. if (widget != nullptr) {
  54. mainLayout->addWidget(widget);
  55. widgets.append(widget);
  56. }
  57. delete manager;
  58. }
  59. // 设置控件的最小高度和最大宽度
  60. int minHeight = 162;
  61. int maxWidth = 244;
  62. int num = -28;
  63. for (Group* w : widgets) {
  64. w->setMinimumHeight(minHeight);
  65. w->setMaximumWidth(maxWidth);
  66. num = num + 272;
  67. }
  68. container->setMinimumSize(num, 162);
  69. container->setLayout(mainLayout);
  70. ui->scrollArea->setWidget(container);
  71. ui->scrollArea->resize(1062, 177);
  72. ui->LeftOperatewidget->setMouseTracking(true);
  73. ui->RightOperatewidget->setMouseTracking(true);
  74. }
  75. void MainAndSecondaryCamerasWnd::initSliders() {
  76. QList<QSlider*> sliders = {
  77. ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
  78. ui->verticalSlider_4, ui->verticalSlider_7, ui->verticalSlider_8
  79. };
  80. QList<int> initialValues = {11, 22, 9, 11, 0, 0};
  81. for (int i = 0; i < sliders.size(); ++i) {
  82. sliders[i]->setMinimum(0);
  83. sliders[i]->setMaximum(100);
  84. sliders[i]->setValue(initialValues[i]);
  85. }
  86. }
  87. void MainAndSecondaryCamerasWnd::initLineEdits() {
  88. QList<QLineEdit*> lineEdits = {
  89. ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
  90. ui->lineEdit_4, ui->lineEdit_7, ui->lineEdit_8
  91. };
  92. for (QLineEdit* lineEdit : lineEdits) {
  93. lineEdit->setAlignment(Qt::AlignCenter);
  94. }
  95. }
  96. void MainAndSecondaryCamerasWnd::connectSliderAndLineEdit(QSlider* slider, QLineEdit* lineEdit)
  97. {
  98. QIntValidator* validator = new QIntValidator(slider->minimum(), slider->maximum(), lineEdit);
  99. lineEdit->setValidator(validator);
  100. connect(slider, &QSlider::valueChanged, [lineEdit](int value) {
  101. lineEdit->setText(QString::number(value));
  102. });
  103. connect(lineEdit, &QLineEdit::textChanged, [slider](const QString &text) {
  104. bool ok;
  105. int value = text.toInt(&ok);
  106. if (ok && value >= slider->minimum() && value <= slider->maximum()) {
  107. slider->setValue(value);
  108. } else if (!text.isEmpty()) {
  109. int closestValue = qMin(qMax(text.toInt(&ok), slider->minimum()), slider->maximum());
  110. slider->setValue(closestValue);
  111. }
  112. });
  113. }
  114. void MainAndSecondaryCamerasWnd::checkSettings() {
  115. QSettings settings("YourCompany", "YourApplication_");
  116. int groupId = settings.value("GroupId", 0).toInt();
  117. int index = settings.value("Index", 0).toInt();
  118. if (groupId != lastGroupId || index != lastIndex) {
  119. lastGroupId = groupId;
  120. lastIndex = index;
  121. loadSettings();
  122. }
  123. }
  124. void MainAndSecondaryCamerasWnd::loadSettings()
  125. {
  126. QSettings settings("YourCompany", "YourApplication_");
  127. int groupId = settings.value("GroupId", 0).toInt();
  128. int index = settings.value("Index", 0).toInt();
  129. loadGroupSettings(groupId, index);
  130. }
  131. void MainAndSecondaryCamerasWnd::loadGroupSettings(int Id, int Index) {
  132. // isUpdatingSettings = true;
  133. QSettings settings("YourOrganization", "YourApplication");
  134. settings.beginGroup(QString::number(Id));
  135. QString imagePath1 = settings.value("ImagePath1").toString();
  136. QString imagePath2 = settings.value("ImagePath2").toString();
  137. QStringList textList = settings.value("TextList").toStringList();
  138. settings.endGroup();
  139. QString lastRightImage = settings.value("LastRightImage").toString();
  140. QSize size_left = ui->LeftOperatewidget->size();
  141. QSize size_right = ui->RightOperatewidget->size();
  142. QPixmap newPixmap;
  143. if (Index == 1) {
  144. newPixmap = QPixmap(imagePath1);
  145. } else if (Index == 2) {
  146. newPixmap = QPixmap(imagePath2);
  147. }
  148. QPixmap scaledPixmap_right = newPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  149. ui->RightOperatewidget->setPixmap(scaledPixmap_right);
  150. QPixmap scaledPixmap_left = lastRightPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  151. ui->LeftOperatewidget->setPixmap(scaledPixmap_left);
  152. // 更新当前图片的成员变量
  153. Left_currentPixmap = scaledPixmap_left;
  154. Left_scaleFactor = 1.0;
  155. Right_currentPixmap = scaledPixmap_right;
  156. Right_scaleFactor = 1.0;
  157. ui->Leftlabel_Percentage->setText("100%");
  158. ui->Rightlabel_Percentage->setText("100%");
  159. ui->LeftDataSources->clear();
  160. ui->LeftDataSources->addItems(lasttextList);
  161. ui->RightDataSources->clear();
  162. ui->RightDataSources->addItems(textList);
  163. // 保存到 QSettings
  164. settings.beginGroup("LastSettings");
  165. settings.setValue("LastRightPixmap", lastRightPixmap);
  166. settings.setValue("LasttextList", lasttextList);
  167. settings.setValue("LastLastRightPixmap", lastLastRightPixmap);
  168. settings.setValue("LastLasttextList", lastLasttextList);
  169. settings.endGroup();
  170. if (isUpdatingSettings) {
  171. lastLastRightPixmap = lastRightPixmap;
  172. lastLasttextList = lasttextList;
  173. lastRightPixmap = scaledPixmap_right;
  174. lasttextList = textList;
  175. }
  176. }
  177. void MainAndSecondaryCamerasWnd::on_LeftZoomUpButton_clicked()
  178. {
  179. Left_scaleFactor *= 1.1;
  180. int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
  181. int newHeight = Left_currentPixmap.height() * Left_scaleFactor;
  182. QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  183. ui->LeftOperatewidget->setPixmap(scaledImage);
  184. double percentage = (Left_scaleFactor * 100);
  185. QString percentageStr = QString::number((int)percentage);
  186. ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  187. }
  188. void MainAndSecondaryCamerasWnd::on_RightZoomUpButton_clicked()
  189. {
  190. Right_scaleFactor *= 1.1;
  191. int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
  192. int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
  193. QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  194. ui->RightOperatewidget->setPixmap(scaledImage);
  195. double percentage = (Right_scaleFactor * 100);
  196. QString percentageStr = QString::number((int)percentage);
  197. ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  198. }
  199. void MainAndSecondaryCamerasWnd::on_LeftZoomOutButton_clicked()
  200. {
  201. double Left_newScaleFactor = Left_scaleFactor * 0.9;
  202. if (Left_newScaleFactor >= 1.0) {
  203. Left_scaleFactor = Left_newScaleFactor;
  204. } else {
  205. Left_scaleFactor = 1.0;
  206. }
  207. int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
  208. int newHeight = Left_currentPixmap.height() * Left_scaleFactor;
  209. QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  210. ui->LeftOperatewidget->setPixmap(scaledImage);
  211. double percentage = (Left_scaleFactor * 100);
  212. QString percentageStr = QString::number((int)percentage);
  213. ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  214. }
  215. void MainAndSecondaryCamerasWnd::on_RightZoomOutButton_clicked()
  216. {
  217. double Right_newScaleFactor = Right_scaleFactor * 0.9;
  218. if (Right_newScaleFactor >= 1.0) {
  219. Right_scaleFactor = Right_newScaleFactor;
  220. } else {
  221. Right_scaleFactor = 1.0;
  222. }
  223. int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
  224. int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
  225. QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  226. ui->RightOperatewidget->setPixmap(scaledImage);
  227. double percentage = (Right_scaleFactor * 100);
  228. QString percentageStr = QString::number((int)percentage);
  229. ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  230. }
  231. void MainAndSecondaryCamerasWnd::wheelEvent(QWheelEvent *event)
  232. {
  233. if (ui->LeftOperatewidget->rect().contains(ui->LeftOperatewidget->mapFromGlobal(event->globalPosition().toPoint()))) {
  234. if (event->angleDelta().y() > 0) {
  235. Left_scaleFactor *= 1.1;
  236. } else {
  237. double Left_newScaleFactor = Left_scaleFactor * 0.9;
  238. if (Left_newScaleFactor >= 1.0) {
  239. Left_scaleFactor = Left_newScaleFactor;
  240. } else {
  241. Left_scaleFactor = 1.0;
  242. }
  243. }
  244. int newWidth_left = Left_currentPixmap.width() * Left_scaleFactor;
  245. int newHeight_left = Left_currentPixmap.height() * Left_scaleFactor;
  246. QPixmap scaledImage_left = Left_currentPixmap.scaled(newWidth_left, newHeight_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  247. ui->LeftOperatewidget->setPixmap(scaledImage_left);
  248. double percentage_left = Left_scaleFactor * 100;
  249. QString percentageStr_left = QString::number((int)percentage_left);
  250. ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr_left));
  251. }
  252. if (ui->RightOperatewidget->rect().contains(ui->RightOperatewidget->mapFromGlobal(event->globalPosition().toPoint()))) {
  253. if (event->angleDelta().y() > 0) {
  254. Right_scaleFactor *= 1.1;
  255. } else {
  256. double Right_newScaleFactor = Right_scaleFactor * 0.9;
  257. if (Right_newScaleFactor >= 1.0) {
  258. Right_scaleFactor = Right_newScaleFactor;
  259. } else {
  260. Right_scaleFactor = 1.0;
  261. }
  262. }
  263. int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
  264. int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
  265. QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  266. ui->RightOperatewidget->setPixmap(scaledImage);
  267. double percentage = Right_scaleFactor * 100;
  268. QString percentageStr = QString::number((int)percentage);
  269. ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  270. }
  271. QMainWindow::wheelEvent(event);
  272. }
  273. void MainAndSecondaryCamerasWnd::showEvent(QShowEvent *event) {
  274. isUpdatingSettings = false;
  275. QMainWindow::showEvent(event);
  276. loadSettings();
  277. isUpdatingSettings = true;
  278. }
  279. void MainAndSecondaryCamerasWnd::hideEvent(QHideEvent *event) {
  280. isUpdatingSettings = false;
  281. QMainWindow::hideEvent(event);
  282. }