MainAndSecondaryCamerasWnd.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. Demo001_2::Demo001_2(QWidget *parent)
  9. : QMainWindow(parent)
  10. , ui(new Ui::Demo001_2)
  11. {
  12. ui->setupUi(this);
  13. initFrom();
  14. }
  15. Demo001_2::~Demo001_2()
  16. {
  17. delete ui;
  18. }
  19. void Demo001_2::initFrom()
  20. {
  21. QTimer *timer = new QTimer(this);
  22. connect(timer, &QTimer::timeout, this, &Demo001_2::checkSettings);
  23. timer->start(100);
  24. ui->Leftlabel_Percentage->setAlignment(Qt::AlignCenter);
  25. ui->Rightlabel_Percentage->setAlignment(Qt::AlignCenter);
  26. initSliders();
  27. initLineEdits();
  28. connectSliderAndLineEdit(ui->verticalSlider, ui->lineEdit);
  29. connectSliderAndLineEdit(ui->verticalSlider_2, ui->lineEdit_2);
  30. connectSliderAndLineEdit(ui->verticalSlider_3, ui->lineEdit_3);
  31. connectSliderAndLineEdit(ui->verticalSlider_4, ui->lineEdit_4);
  32. connectSliderAndLineEdit(ui->verticalSlider_7, ui->lineEdit_7);
  33. connectSliderAndLineEdit(ui->verticalSlider_8, ui->lineEdit_8);
  34. QWidget *viewport = ui->scrollArea->viewport();
  35. QWidget *container = new QWidget(viewport);
  36. // 创建水平布局
  37. QHBoxLayout *mainLayout = new QHBoxLayout(container);
  38. // 创建四个垂直布局,分别用于四个区域
  39. QVBoxLayout *vLayout1 = new QVBoxLayout();
  40. QVBoxLayout *vLayout2 = new QVBoxLayout();
  41. QVBoxLayout *vLayout3 = new QVBoxLayout();
  42. QVBoxLayout *vLayout4 = new QVBoxLayout();
  43. vLayout1->setSpacing(16);
  44. vLayout1->setMargin(0);
  45. vLayout2->setSpacing(16);
  46. vLayout2->setMargin(0);
  47. vLayout3->setSpacing(16);
  48. vLayout3->setMargin(0);
  49. vLayout4->setSpacing(16);
  50. vLayout4->setMargin(0);
  51. QStringList dispmodel1;
  52. dispmodel1 << "P die align" << "D device align";
  53. Group *widget1 = new Group(16, ":/images/test_image/image_1.png", ":/images/test_image/image_2.png", dispmodel1, this);
  54. QStringList dispmodel2;
  55. dispmodel2 << "D device align" << "P die align";
  56. Group *widget2 = new Group(17, ":/images/test_image/image_3.png", ":/images/test_image/image_4.png", dispmodel2, this);
  57. QStringList dispmodel3;
  58. dispmodel3 << "B device align" << "P die align";
  59. Group *widget3 = new Group(18, ":/images/test_image/image_5.png", ":/images/test_image/image_6.png", dispmodel3, this);
  60. QStringList dispmodel4;
  61. dispmodel4 << "U die align" << "D device align";
  62. Group *widget4 = new Group(19, ":/images/test_image/image_7.png", ":/images/test_image/image_8.png", dispmodel4, this);
  63. QStringList dispmodel5;
  64. dispmodel5 << "P die align" << "D device align";
  65. Group *widget5 = new Group(20, ":/images/test_image/image_1.png", ":/images/test_image/image_2.png", dispmodel1, this);
  66. QStringList dispmodel6;
  67. dispmodel6 << "D device align" << "P die align";
  68. Group *widget6 = new Group(21, ":/images/test_image/image_3.png", ":/images/test_image/image_4.png", dispmodel2, this);
  69. QStringList dispmodel7;
  70. dispmodel7 << "B device align" << "P die align";
  71. Group *widget7 = new Group(22, ":/images/test_image/image_5.png", ":/images/test_image/image_6.png", dispmodel3, this);
  72. QStringList dispmodel8;
  73. dispmodel8 << "U die align" << "D device align";
  74. Group *widget8 = new Group(23, ":/images/test_image/image_7.png", ":/images/test_image/image_8.png", dispmodel4, this);
  75. vLayout1->addWidget(widget1);
  76. vLayout1->addWidget(widget8);
  77. vLayout2->addWidget(widget7);
  78. vLayout2->addWidget(widget2);
  79. vLayout3->addWidget(widget3);
  80. vLayout3->addWidget(widget6);
  81. vLayout4->addWidget(widget5);
  82. vLayout4->addWidget(widget4);
  83. int minHeight = 162;
  84. int maxWidth = 244;
  85. QWidgetList widgets = {widget1, widget2, widget3, widget4, widget5, widget6, widget7, widget8};
  86. for (QWidget *w : widgets) {
  87. w->setMinimumHeight(minHeight);
  88. w->setMaximumWidth(maxWidth);
  89. }
  90. mainLayout->addLayout(vLayout1);
  91. mainLayout->addSpacing(8); // 第一个和第二个之间的间距
  92. mainLayout->addLayout(vLayout2);
  93. mainLayout->addSpacing(52); // 第二个和第三个之间的间距
  94. mainLayout->addLayout(vLayout3);
  95. mainLayout->addSpacing(8); // 第三个和第四个之间的间距
  96. mainLayout->addLayout(vLayout4);
  97. mainLayout->setContentsMargins(0, 0, 8, 0);
  98. container->setLayout(mainLayout);
  99. ui->scrollArea->setWidget(container);
  100. ui->scrollArea->resize(1083, 162);
  101. ui->LeftOperatewidget->setMouseTracking(true);
  102. ui->RightOperatewidget->setMouseTracking(true);
  103. }
  104. void Demo001_2::initSliders() {
  105. QList<QSlider*> sliders = {
  106. ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
  107. ui->verticalSlider_4, ui->verticalSlider_7, ui->verticalSlider_8
  108. };
  109. QList<int> initialValues = {11, 22, 9, 11, 0, 0};
  110. for (int i = 0; i < sliders.size(); ++i) {
  111. sliders[i]->setMinimum(0);
  112. sliders[i]->setMaximum(100);
  113. sliders[i]->setValue(initialValues[i]);
  114. }
  115. }
  116. void Demo001_2::initLineEdits() {
  117. QList<QLineEdit*> lineEdits = {
  118. ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
  119. ui->lineEdit_4, ui->lineEdit_7, ui->lineEdit_8
  120. };
  121. for (QLineEdit* lineEdit : lineEdits) {
  122. lineEdit->setAlignment(Qt::AlignCenter);
  123. }
  124. }
  125. void Demo001_2::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_2::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_2::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_2::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_left = ui->LeftOperatewidget->size();
  168. QSize size_right = ui->RightOperatewidget->size();
  169. QPixmap LeftPixmap = QPixmap(imagePath2);
  170. QPixmap RightPixmap = QPixmap(imagePath1);
  171. QPixmap scaledPixmap_left = LeftPixmap.scaled(size_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  172. ui->LeftOperatewidget->setPixmap(scaledPixmap_left);
  173. QPixmap scaledPixmap_right = RightPixmap.scaled(size_right, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  174. ui->RightOperatewidget->setPixmap(scaledPixmap_right);
  175. // 更新当前图片的成员变量
  176. Left_currentPixmap = scaledPixmap_left;
  177. Left_scaleFactor = 1.0;
  178. Right_currentPixmap = scaledPixmap_right;
  179. Right_scaleFactor = 1.0;
  180. ui->Leftlabel_Percentage->setText("100%");
  181. ui->Rightlabel_Percentage->setText("100%");
  182. ui->LeftDataSources->clear();
  183. ui->LeftDataSources->addItems(textList);
  184. ui->RightDataSources->clear();
  185. ui->RightDataSources->addItems(textList);
  186. // connect(ui->DatacomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Demo001_3::onComboBoxIndexChanged(int)));
  187. }
  188. void Demo001_2::on_LeftZoomUpButton_clicked()
  189. {
  190. if (Left_scaleFactor < 3.0) {
  191. Left_scaleFactor *= 1.1;
  192. }
  193. int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
  194. int newHeight = Left_currentPixmap.height() * Left_scaleFactor;
  195. QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  196. ui->LeftOperatewidget->setPixmap(scaledImage);
  197. double percentage = (Left_scaleFactor * 100);
  198. QString percentageStr = QString::number((int)percentage);
  199. ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  200. }
  201. void Demo001_2::on_RightZoomUpButton_clicked()
  202. {
  203. if (Right_scaleFactor < 3.0) {
  204. Right_scaleFactor *= 1.1;
  205. }
  206. int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
  207. int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
  208. QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  209. ui->RightOperatewidget->setPixmap(scaledImage);
  210. double percentage = (Right_scaleFactor * 100);
  211. QString percentageStr = QString::number((int)percentage);
  212. ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  213. }
  214. void Demo001_2::on_LeftZoomOutButton_clicked()
  215. {
  216. if (Left_scaleFactor > 0.25) {
  217. Left_scaleFactor *= 0.9;
  218. }
  219. int newWidth = Left_currentPixmap.width() * Left_scaleFactor;
  220. int newHeight = Left_currentPixmap.height() * Left_scaleFactor;
  221. QPixmap scaledImage = Left_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  222. ui->LeftOperatewidget->setPixmap(scaledImage);
  223. double percentage = (Left_scaleFactor * 100);
  224. QString percentageStr = QString::number((int)percentage);
  225. ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  226. }
  227. void Demo001_2::on_RightZoomOutButton_clicked()
  228. {
  229. if (Right_scaleFactor > 0.25) {
  230. Right_scaleFactor *= 0.9;
  231. }
  232. int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
  233. int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
  234. QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  235. ui->RightOperatewidget->setPixmap(scaledImage);
  236. double percentage = (Right_scaleFactor * 100);
  237. QString percentageStr = QString::number((int)percentage);
  238. ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  239. }
  240. void Demo001_2::wheelEvent(QWheelEvent *event)
  241. {
  242. if (ui->LeftOperatewidget->rect().contains(ui->LeftOperatewidget->mapFromGlobal(event->globalPosition().toPoint()))) {
  243. if (event->angleDelta().y() > 0) {
  244. if (Left_scaleFactor < 3.0) {
  245. Left_scaleFactor *= 1.1;
  246. }
  247. } else {
  248. if (Left_scaleFactor > 0.25) {
  249. Left_scaleFactor *= 0.9;
  250. }
  251. }
  252. int newWidth_left = Left_currentPixmap.width() * Left_scaleFactor;
  253. int newHeight_left = Left_currentPixmap.height() * Left_scaleFactor;
  254. QPixmap scaledImage_left = Left_currentPixmap.scaled(newWidth_left, newHeight_left, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  255. ui->LeftOperatewidget->setPixmap(scaledImage_left);
  256. double percentage_left = Left_scaleFactor * 100;
  257. QString percentageStr_left = QString::number((int)percentage_left);
  258. ui->Leftlabel_Percentage->setText(QString("%1%").arg(percentageStr_left));
  259. }
  260. if (ui->RightOperatewidget->rect().contains(ui->RightOperatewidget->mapFromGlobal(event->globalPosition().toPoint()))) {
  261. if (event->angleDelta().y() > 0) {
  262. if (Right_scaleFactor < 3.0) {
  263. Right_scaleFactor *= 1.1;
  264. }
  265. } else {
  266. if (Right_scaleFactor > 0.25) {
  267. Right_scaleFactor *= 0.9;
  268. }
  269. }
  270. int newWidth = Right_currentPixmap.width() * Right_scaleFactor;
  271. int newHeight = Right_currentPixmap.height() * Right_scaleFactor;
  272. QPixmap scaledImage = Right_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  273. ui->RightOperatewidget->setPixmap(scaledImage);
  274. double percentage = Right_scaleFactor * 100;
  275. QString percentageStr = QString::number((int)percentage);
  276. ui->Rightlabel_Percentage->setText(QString("%1%").arg(percentageStr));
  277. }
  278. QMainWindow::wheelEvent(event);
  279. }
  280. void Demo001_2::showEvent(QShowEvent *event) {
  281. QMainWindow::showEvent(event);
  282. loadSettings();
  283. }
  284. void Demo001_2::hideEvent(QHideEvent *event) {
  285. QMainWindow::hideEvent(event);
  286. }