ChartsAndCamerasWnd.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. #include "chartline.h"
  9. #include <QRandomGenerator>
  10. #include <qDebug>
  11. // #include "CInterface.h"
  12. #include "CameraImage/CameraImageHandler.h"
  13. ChartsAndCamerasWnd::ChartsAndCamerasWnd(QWidget *parent) :
  14. QMainWindow(parent),
  15. ui(new Ui::ChartsAndCamerasWnd),
  16. chartLine(new ChartLine(this)),
  17. chartLine1(new ChartLine(this)),
  18. chartLine2(new ChartLine(this))
  19. {
  20. ui->setupUi(this);
  21. // 设置 ChartLine 的位置和大小
  22. chartLine->setParent(ui->Leftwidget);
  23. chartLine->setGeometry(0, 0, 560, 280);
  24. chartLine->setChartLabels("Combined Placement", "Number of Samples", "Device Align");
  25. // 设置 ChartLine1 的位置和大小
  26. chartLine1->setParent(ui->Leftwidget);
  27. chartLine1->setGeometry(0, 245, 560, 280);
  28. chartLine1->setChartLabels("Y placement", "Number of samples", "Y device align");
  29. // 设置 ChartLine2 的位置和大小
  30. chartLine2->setParent(ui->Leftwidget);
  31. chartLine2->setGeometry(0, 490, 560, 280);
  32. chartLine2->setChartLabels("Theta placement", "Number of samples", "Theta device align");
  33. // 初始化系列信息
  34. initializeSeriesInfo();
  35. // 连接所有复选框的 clicked 信号到通用槽函数
  36. QList<QCheckBox*> checkBoxes = {
  37. ui->GreenDiamondcheckBox,
  38. ui->YellowDiamondcheckBox,
  39. ui->BlackDiamondcheckBox,
  40. ui->BlueDiamondcheckBox,
  41. ui->BlueHorizontalLinecheckBox,
  42. ui->BlueVerticalBarcheckBox,
  43. ui->GreenVerticalBarcheckBox,
  44. ui->PurpleVerticalBarcheckBox,
  45. ui->RedDiamondcheckBox,
  46. ui->RedHorizontalLinecheckBox,
  47. ui->WhiteVerticalBarcheckBox
  48. };
  49. for (QCheckBox* cb : checkBoxes) {
  50. cb->setCheckable(true);
  51. connect(cb, &QCheckBox::clicked, this, &ChartsAndCamerasWnd::on_CheckBox_clicked);
  52. }
  53. initFrom();
  54. }
  55. ChartsAndCamerasWnd::~ChartsAndCamerasWnd()
  56. {
  57. delete ui;
  58. }
  59. void ChartsAndCamerasWnd::initializeSeriesInfo()
  60. {
  61. // 初始化系列信息,使用 DataSeries 结构体
  62. m_seriesMap["GreenDiamond"] = DataSeries{
  63. "GreenDiamond",
  64. QColor("#00FF00"), // 绿色
  65. QPixmap(":/images/Participation display/GreenDiamond.png"),
  66. QList<QPointF>() // 初始为空
  67. };
  68. m_seriesMap["YellowDiamond"] = DataSeries{
  69. "YellowDiamond",
  70. QColor("#FFFF00"), // 黄色
  71. QPixmap(":/images/Participation display/YellowDiamond.png"),
  72. QList<QPointF>()
  73. };
  74. m_seriesMap["BlackDiamond"] = DataSeries{
  75. "BlackDiamond",
  76. QColor("#000000"), // 黑色
  77. QPixmap(":/images/Participation display/BlackDiamond.png"),
  78. QList<QPointF>()
  79. };
  80. m_seriesMap["BlueDiamond"] = DataSeries{
  81. "BlueDiamond",
  82. QColor("#0000FF"), // 蓝色
  83. QPixmap(":/images/Participation display/BlueDiamond.png"),
  84. QList<QPointF>()
  85. };
  86. m_seriesMap["BlueHorizontalLine"] = DataSeries{
  87. "BlueHorizontalLine",
  88. QColor("#0000FF"),
  89. QPixmap(":/images/Participation display/Blue_horizontal_line.png"),
  90. QList<QPointF>()
  91. };
  92. m_seriesMap["BlueVerticalBar"] = DataSeries{
  93. "BlueVerticalBar",
  94. QColor("#0000FF"),
  95. QPixmap(":/images/Participation display/Blue_vertical_bar.png"),
  96. QList<QPointF>()
  97. };
  98. m_seriesMap["GreenVerticalBar"] = DataSeries{
  99. "GreenVerticalBar",
  100. QColor("#00FF00"),
  101. QPixmap(":/images/Participation display/Green_vertical_bar.png"),
  102. QList<QPointF>()
  103. };
  104. m_seriesMap["PurpleVerticalBar"] = DataSeries{
  105. "PurpleVerticalBar",
  106. QColor("#800080"),
  107. QPixmap(":/images/Participation display/Purple_vertical_bar.png"),
  108. QList<QPointF>()
  109. };
  110. m_seriesMap["RedDiamond"] = DataSeries{
  111. "RedDiamond",
  112. QColor("#FF0000"), // 红色
  113. QPixmap(":/images/Participation display/RedDiamond.png"),
  114. QList<QPointF>()
  115. };
  116. m_seriesMap["RedHorizontalLine"] = DataSeries{
  117. "RedHorizontalLine",
  118. QColor("#FF0000"),
  119. QPixmap(":/images/Participation display/Red_horizontal_line.png"),
  120. QList<QPointF>()
  121. };
  122. m_seriesMap["WhiteVerticalBar"] = DataSeries{
  123. "WhiteVerticalBar",
  124. QColor("#FFFFFF"), // 白色
  125. QPixmap(":/images/Participation display/White_vertical_bar.png"),
  126. QList<QPointF>()
  127. };
  128. }
  129. void ChartsAndCamerasWnd::on_CheckBox_clicked()
  130. {
  131. // 获取发送信号的复选框
  132. QCheckBox* checkBox = qobject_cast<QCheckBox*>(sender());
  133. if (!checkBox)
  134. return;
  135. QString checkBoxName = checkBox->objectName();
  136. QString seriesName = checkBoxName;
  137. // 移除末尾的 "checkBox"
  138. if (seriesName.endsWith("checkBox")) {
  139. seriesName.chop(QString("checkBox").length());
  140. } else if (seriesName.endsWith("CheckBox")) {
  141. seriesName.chop(QString("CheckBox").length());
  142. }
  143. qDebug() << "Checkbox clicked:" << seriesName << (checkBox->isChecked() ? "Checked" : "Unchecked");
  144. if (!m_seriesMap.contains(seriesName)) {
  145. qDebug() << "No series info for:" << seriesName;
  146. return;
  147. }
  148. DataSeries &seriesInfo = m_seriesMap[seriesName];
  149. if (checkBox->isChecked()) {
  150. // 生成随机数据
  151. QList<QPointF> data;
  152. for (int i = 0; i <= 40; i += 4) {
  153. data.append(QPointF(i, QRandomGenerator::global()->bounded(-30, 80)));
  154. }
  155. seriesInfo.data = data;
  156. // 添加到 ChartLine 实例
  157. chartLine->addDataSeries(seriesInfo);
  158. chartLine1->addDataSeries(seriesInfo);
  159. chartLine2->addDataSeries(seriesInfo);
  160. qDebug() << "Added series:" << seriesName;
  161. }
  162. else {
  163. // 从 ChartLine 实例中移除
  164. chartLine->removeDataSeries(seriesName);
  165. chartLine1->removeDataSeries(seriesName);
  166. chartLine2->removeDataSeries(seriesName);
  167. qDebug() << "Removed series:" << seriesName;
  168. }
  169. }
  170. void ChartsAndCamerasWnd::initFrom() {
  171. QTimer *timer = new QTimer(this);
  172. connect(timer, &QTimer::timeout, this, &ChartsAndCamerasWnd::checkSettings);
  173. timer->start(100);
  174. ui->label_Percentage->setAlignment(Qt::AlignCenter);
  175. initSliders();
  176. initLineEdits();
  177. connectSliderAndLineEdit(ui->verticalSlider, ui->lineEdit);
  178. connectSliderAndLineEdit(ui->verticalSlider_2, ui->lineEdit_2);
  179. connectSliderAndLineEdit(ui->verticalSlider_3, ui->lineEdit_3);
  180. connectSliderAndLineEdit(ui->verticalSlider_4, ui->lineEdit_4);
  181. connectSliderAndLineEdit(ui->verticalSlider_7, ui->lineEdit_7);
  182. connectSliderAndLineEdit(ui->verticalSlider_8, ui->lineEdit_8);
  183. QWidget *viewport = ui->scrollArea->viewport();
  184. QWidget *container = new QWidget(viewport);
  185. QHBoxLayout *layout = new QHBoxLayout(container);
  186. layout->setSpacing(16); // 设置Group之间的间隔距离
  187. layout->setMargin(0);
  188. // CInterface interface;
  189. // ImageInfo image;
  190. // long result = interface.GrabImage(1, image);
  191. // if (result == 0) {
  192. // qDebug() << "Image width: " << image.width;
  193. // qDebug() << "Image height: " << image.height;
  194. // qDebug() << "Image width: " << image.format;
  195. // QImage img;
  196. // // 根据图像格式转换
  197. // switch (image.format) {
  198. // case GRAY8:
  199. // img = QImage(image.data, image.width, image.height, QImage::Format_Grayscale8);
  200. // break;
  201. // case RGB888:
  202. // img = QImage(image.data, image.width, image.height, QImage::Format_RGB888);
  203. // break;
  204. // case ARGB8888:
  205. // img = QImage(image.data, image.width, image.height, QImage::Format_ARGB32);
  206. // break;
  207. // case RGB32:
  208. // img = QImage(image.data, image.width, image.height, QImage::Format_RGB32);
  209. // break;
  210. // case YUV422:
  211. // // YUV422 需要额外的转换逻辑
  212. // // 这里假设你有转换函数 convertYUV422ToRGB
  213. // // img = convertYUV422ToRGB(image.data, image.width, image.height);
  214. // break;
  215. // default:
  216. // qDebug() << "Unsupported image format";
  217. // }
  218. // } else {
  219. // qDebug() << "Failed to grab image, error code: " << result;
  220. // }
  221. QList<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
  222. QList<Group*> widgets;
  223. for (int i = 0; i < numbers.size(); ++i) {
  224. int num = numbers[i];
  225. CameraImageHandler* manager = new CameraImageHandler(num);
  226. Group* widget = manager->getGroup();
  227. if (widget != nullptr) {
  228. layout->addWidget(widget);
  229. widgets.append(widget);
  230. }
  231. delete manager;
  232. }
  233. // 设置控件的最小高度和最大宽度
  234. int minHeight = 162;
  235. int maxWidth = 244;
  236. int num = -16;
  237. for (Group* w : widgets) {
  238. w->setMinimumHeight(minHeight);
  239. w->setMaximumWidth(maxWidth);
  240. num = num + 260;
  241. }
  242. container->setMinimumSize(num, 162);
  243. container->setLayout(layout);
  244. ui->scrollArea->setWidget(container);
  245. ui->scrollArea->resize(508, 177);
  246. ui->Operatewidget->setMouseTracking(true);
  247. }
  248. void ChartsAndCamerasWnd::initSliders() {
  249. QList<QSlider*> sliders = {
  250. ui->verticalSlider, ui->verticalSlider_2, ui->verticalSlider_3,
  251. ui->verticalSlider_4, ui->verticalSlider_7, ui->verticalSlider_8
  252. };
  253. QList<int> initialValues = {11, 22, 9, 11, 0, 0};
  254. for (int i = 0; i < sliders.size(); ++i) {
  255. sliders[i]->setMinimum(0);
  256. sliders[i]->setMaximum(100);
  257. sliders[i]->setValue(initialValues[i]);
  258. }
  259. }
  260. void ChartsAndCamerasWnd::initLineEdits() {
  261. QList<QLineEdit*> lineEdits = {
  262. ui->lineEdit, ui->lineEdit_2, ui->lineEdit_3,
  263. ui->lineEdit_4, ui->lineEdit_7, ui->lineEdit_8
  264. };
  265. for (QLineEdit* lineEdit : lineEdits) {
  266. lineEdit->setAlignment(Qt::AlignCenter);
  267. }
  268. }
  269. void ChartsAndCamerasWnd::connectSliderAndLineEdit(QSlider* slider, QLineEdit* lineEdit)
  270. {
  271. QIntValidator* validator = new QIntValidator(slider->minimum(), slider->maximum(), lineEdit);
  272. lineEdit->setValidator(validator);
  273. connect(slider, &QSlider::valueChanged, [lineEdit](int value) {
  274. lineEdit->setText(QString::number(value));
  275. });
  276. connect(lineEdit, &QLineEdit::textChanged, [slider](const QString &text) {
  277. bool ok;
  278. int value = text.toInt(&ok);
  279. if (ok && value >= slider->minimum() && value <= slider->maximum()) {
  280. slider->setValue(value);
  281. } else if (!text.isEmpty()) {
  282. int closestValue = qMin(qMax(text.toInt(&ok), slider->minimum()), slider->maximum());
  283. slider->setValue(closestValue);
  284. }
  285. });
  286. }
  287. void ChartsAndCamerasWnd::checkSettings() {
  288. QSettings settings("YourCompany", "YourApplication_");
  289. int groupId = settings.value("GroupId", 0).toInt();
  290. int index = settings.value("Index", 0).toInt();
  291. if (groupId != lastGroupId || index != lastIndex) {
  292. lastGroupId = groupId;
  293. lastIndex = index;
  294. loadSettings();
  295. }
  296. }
  297. void ChartsAndCamerasWnd::loadSettings()
  298. {
  299. QSettings settings("YourCompany", "YourApplication_");
  300. int groupId = settings.value("GroupId", 0).toInt();
  301. int index = settings.value("Index", 0).toInt();
  302. loadGroupSettings(groupId, index);
  303. }
  304. void ChartsAndCamerasWnd::loadGroupSettings(int Id, int Index) {
  305. QSettings settings("YourOrganization", "YourApplication");
  306. settings.beginGroup(QString::number(Id));
  307. QString imagePath1 = settings.value("ImagePath1").toString();
  308. QString imagePath2 = settings.value("ImagePath2").toString();
  309. QStringList textList = settings.value("TextList").toStringList();
  310. settings.endGroup();
  311. QSize size = ui->Operatewidget->size();
  312. QPixmap newPixmap;
  313. if (Index == 1) {
  314. newPixmap = QPixmap(imagePath1);
  315. } else if (Index == 2) {
  316. newPixmap = QPixmap(imagePath2);
  317. } else {
  318. newPixmap = QPixmap(":/images/test_image/image.png");
  319. }
  320. QPixmap scaledPixmap = newPixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  321. ui->Operatewidget->setPixmap(scaledPixmap);
  322. // 更新当前图片的成员变量
  323. currentPixmap = scaledPixmap;
  324. scaleFactor = 1.0;
  325. ui->label_Percentage->setText("100%");
  326. ui->DatacomboBox->clear();
  327. ui->DatacomboBox->addItems(textList);
  328. // connect(ui->DatacomboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(Demo001_3::onComboBoxIndexChanged(int)));
  329. }
  330. void ChartsAndCamerasWnd::on_ZoomUpButton_clicked()
  331. {
  332. scaleFactor *= 1.1;
  333. int newWidth = currentPixmap.width() * scaleFactor;
  334. int newHeight = currentPixmap.height() * scaleFactor;
  335. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  336. ui->Operatewidget->setPixmap(scaledImage);
  337. double percentage = scaleFactor * 100;
  338. QString percentageStr = QString::number((int)percentage);
  339. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  340. }
  341. void ChartsAndCamerasWnd::on_ZoomOutButton_clicked()
  342. {
  343. double newScaleFactor = scaleFactor * 0.9;
  344. if (newScaleFactor >= 1.0) {
  345. scaleFactor = newScaleFactor;
  346. } else {
  347. scaleFactor = 1.0;
  348. }
  349. int newWidth = currentPixmap.width() * scaleFactor;
  350. int newHeight = currentPixmap.height() * scaleFactor;
  351. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  352. ui->Operatewidget->setPixmap(scaledImage);
  353. double percentage = scaleFactor * 100;
  354. QString percentageStr = QString::number((int)percentage);
  355. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  356. }
  357. void ChartsAndCamerasWnd::wheelEvent(QWheelEvent *event)
  358. {
  359. if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) {
  360. if (event->angleDelta().y() > 0) {
  361. scaleFactor *= 1.1;
  362. } else {
  363. double newScaleFactor = scaleFactor * 0.9;
  364. if (newScaleFactor >= 1.0) {
  365. scaleFactor = newScaleFactor;
  366. } else {
  367. scaleFactor = 1.0;
  368. }
  369. }
  370. int newWidth = currentPixmap.width() * scaleFactor;
  371. int newHeight = currentPixmap.height() * scaleFactor;
  372. QPixmap scaledImage = currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  373. ui->Operatewidget->setPixmap(scaledImage);
  374. double percentage = scaleFactor * 100;
  375. QString percentageStr = QString::number((int)percentage);
  376. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  377. }
  378. QMainWindow::wheelEvent(event);
  379. }
  380. void ChartsAndCamerasWnd::showEvent(QShowEvent *event) {
  381. QMainWindow::showEvent(event);
  382. loadSettings();
  383. // initFrom();
  384. }
  385. void ChartsAndCamerasWnd::hideEvent(QHideEvent *event) {
  386. QMainWindow::hideEvent(event);
  387. }