ChartsAndCamerasWnd.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #include "ChartsAndCamerasWnd.h"
  2. #include "ui_ChartsAndCamerasWnd.h"
  3. #include <QSettings>
  4. #include <QCloseEvent>
  5. #include "Group.h"
  6. #include <QVBoxLayout>
  7. #include <QTimer>
  8. Demo001_1::Demo001_1(QWidget *parent)
  9. : QMainWindow(parent)
  10. , ui(new Ui::Demo001_1)
  11. {
  12. ui->setupUi(this);
  13. initFrom();
  14. }
  15. Demo001_1::~Demo001_1()
  16. {
  17. delete ui;
  18. }
  19. void Demo001_1::initFrom() {
  20. QTimer *timer = new QTimer(this);
  21. connect(timer, &QTimer::timeout, this, &Demo001_1::checkSettings);
  22. timer->start(100);
  23. ui->label_Percentage->setAlignment(Qt::AlignCenter);
  24. // 设置右下部分
  25. // loadSliderStates();
  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 *leftLayout = new QVBoxLayout();
  40. QVBoxLayout *rightLayout = new QVBoxLayout();
  41. leftLayout->setSpacing(16); // 设置Group之间的间隔距离
  42. leftLayout->setMargin(0);
  43. rightLayout->setSpacing(16); // 设置Group之间的间隔距离
  44. rightLayout->setMargin(0);
  45. // 添加控件到左侧布局
  46. QStringList dispmodel;
  47. dispmodel << "P die align" << "D device align";
  48. Group *widget = new Group(6, ":/images/test_image/image_1.png", ":/images/test_image/image_2.png", dispmodel, this);
  49. leftLayout->addWidget(widget);
  50. QStringList dispmodel1;
  51. dispmodel1 << "D device align" << "P die align";
  52. Group *widget_1 = new Group(7, ":/images/test_image/image_3.png", ":/images/test_image/image_4.png", dispmodel1, this);
  53. leftLayout->addWidget(widget_1);
  54. // 添加控件到右侧布局
  55. QStringList dispmodel2;
  56. dispmodel2 << "B device align" << "P die align";
  57. Group *widget_2 = new Group(8, ":/images/test_image/image_5.png", ":/images/test_image/image_6.png", dispmodel2, this);
  58. rightLayout->addWidget(widget_2);
  59. QStringList dispmodel3;
  60. dispmodel3 << "U die align" << "D device align";
  61. Group *widget_3 = new Group(9, ":/images/test_image/image_7.png", ":/images/test_image/image_8.png", dispmodel3, this);
  62. rightLayout->addWidget(widget_3);
  63. // 添加最后一个控件到左侧布局
  64. // Group *widget_4 = new Group(5, ":/images/test_image/image.png", ":/images/test_image/image_2.png", dispmodel, this);
  65. // leftLayout->addWidget(widget_4);
  66. // 设置控件的最小高度和最大宽度
  67. int minHeight = 162;
  68. int maxWidth = 244;
  69. QWidgetList widgets = {widget, widget_1, widget_2, widget_3, /*widget_4*/};
  70. for (QWidget *w : widgets) {
  71. w->setMinimumHeight(minHeight);
  72. w->setMaximumWidth(maxWidth);
  73. }
  74. mainLayout->addLayout(leftLayout);
  75. mainLayout->addSpacing(8);
  76. mainLayout->addLayout(rightLayout);
  77. mainLayout->setContentsMargins(0, 0, 0, 0);
  78. container->setLayout(mainLayout);
  79. ui->scrollArea->setWidget(container);
  80. ui->scrollArea->resize(521, 162);
  81. ui->Operatewidget->setMouseTracking(true);
  82. }
  83. void Demo001_1::initSliders() {
  84. QList<QSlider*> sliders = {
  85. ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
  86. ui->verticalSlider_4, ui->verticalSlider_7, ui->verticalSlider_8
  87. };
  88. QList<int> initialValues = {11, 22, 9, 11, 0, 0};
  89. for (int i = 0; i < sliders.size(); ++i) {
  90. sliders[i]->setMinimum(0);
  91. sliders[i]->setMaximum(100);
  92. sliders[i]->setValue(initialValues[i]);
  93. }
  94. }
  95. void Demo001_1::initLineEdits() {
  96. QList<QLineEdit*> lineEdits = {
  97. ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
  98. ui->lineEdit_4, ui->lineEdit_7, ui->lineEdit_8
  99. };
  100. for (QLineEdit* lineEdit : lineEdits) {
  101. lineEdit->setAlignment(Qt::AlignCenter);
  102. }
  103. }
  104. void Demo001_1::connectSliderAndLineEdit(QSlider* slider, QLineEdit* lineEdit)
  105. {
  106. QIntValidator* validator = new QIntValidator(slider->minimum(), slider->maximum(), lineEdit);
  107. lineEdit->setValidator(validator);
  108. connect(slider, &QSlider::valueChanged, [lineEdit](int value) {
  109. lineEdit->setText(QString::number(value));
  110. });
  111. connect(lineEdit, &QLineEdit::textChanged, [slider](const QString &text) {
  112. bool ok;
  113. int value = text.toInt(&ok);
  114. if (ok && value >= slider->minimum() && value <= slider->maximum()) {
  115. slider->setValue(value);
  116. } else if (!text.isEmpty()) {
  117. int closestValue = qMin(qMax(text.toInt(&ok), slider->minimum()), slider->maximum());
  118. slider->setValue(closestValue);
  119. }
  120. });
  121. }
  122. void Demo001_1::checkSettings() {
  123. QSettings settings("YourCompany", "YourApplication_");
  124. int groupId = settings.value("GroupId", 0).toInt();
  125. int index = settings.value("Index", 0).toInt();
  126. if (groupId != lastGroupId || index != lastIndex) {
  127. lastGroupId = groupId;
  128. lastIndex = index;
  129. loadSettings();
  130. }
  131. }
  132. void Demo001_1::loadSettings()
  133. {
  134. QSettings settings("YourCompany", "YourApplication_");
  135. int groupId = settings.value("GroupId", 0).toInt();
  136. int index = settings.value("Index", 0).toInt();
  137. loadGroupSettings(groupId, index);
  138. }
  139. void Demo001_1::loadGroupSettings(int Id, int Index) {
  140. QSettings settings("YourOrganization", "YourApplication");
  141. settings.beginGroup(QString::number(Id));
  142. QString imagePath1 = settings.value("ImagePath1").toString();
  143. QString imagePath2 = settings.value("ImagePath2").toString();
  144. QStringList textList = settings.value("TextList").toStringList();
  145. settings.endGroup();
  146. QSize size = ui->Operatewidget->size();
  147. QPixmap newPixmap;
  148. if (Index == 1) {
  149. newPixmap = QPixmap(imagePath1);
  150. } else if (Index == 2) {
  151. newPixmap = QPixmap(imagePath2);
  152. } else {
  153. newPixmap = QPixmap(":/images/test_image/image.png");
  154. }
  155. QPixmap scaledPixmap = newPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  156. ui->Operatewidget->setPixmap(scaledPixmap);
  157. // 更新当前图片的成员变量
  158. currentPixmap = scaledPixmap;
  159. scaleFactor = 1.0;
  160. ui->label_Percentage->setText("100%");
  161. ui->DatacomboBox->clear();
  162. ui->DatacomboBox->addItems(textList);
  163. // connect(ui->DatacomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Demo001_3::onComboBoxIndexChanged(int)));
  164. }
  165. void Demo001_1::on_ZoomUpButton_clicked()
  166. {
  167. if (scaleFactor < 3.0) {
  168. scaleFactor *= 1.1;
  169. }
  170. int newWidth = currentPixmap.width() * scaleFactor;
  171. int newHeight = currentPixmap.height() * scaleFactor;
  172. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  173. ui->Operatewidget->setPixmap(scaledImage);
  174. double percentage = scaleFactor * 100;
  175. QString percentageStr = QString::number((int)percentage);
  176. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  177. }
  178. void Demo001_1::on_ZoomOutButton_clicked()
  179. {
  180. if (scaleFactor > 0.25) {
  181. scaleFactor *= 0.9;
  182. }
  183. int newWidth = currentPixmap.width() * scaleFactor;
  184. int newHeight = currentPixmap.height() * scaleFactor;
  185. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  186. ui->Operatewidget->setPixmap(scaledImage);
  187. double percentage = scaleFactor * 100;
  188. QString percentageStr = QString::number((int)percentage);
  189. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  190. }
  191. void Demo001_1::wheelEvent(QWheelEvent *event)
  192. {
  193. if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) {
  194. if (event->angleDelta().y() > 0) {
  195. if (scaleFactor < 3.0) {
  196. scaleFactor *= 1.1;
  197. }
  198. } else {
  199. if (scaleFactor > 0.25) {
  200. scaleFactor *= 0.9;
  201. }
  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. QMainWindow::wheelEvent(event);
  212. }
  213. void Demo001_1::showEvent(QShowEvent *event) {
  214. QMainWindow::showEvent(event);
  215. loadSettings();
  216. // initFrom();
  217. }
  218. void Demo001_1::hideEvent(QHideEvent *event) {
  219. QMainWindow::hideEvent(event);
  220. }