SingleCameraOperationWnd.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "SingleCameraOperationWnd.h"
  2. #include "ui_SingleCameraOperationWnd.h"
  3. #include <QSettings>
  4. #include <QCloseEvent>
  5. #include <QVBoxLayout>
  6. #include <QDebug>
  7. #include <QTimer>
  8. #include "CameraMaterialGroupWnd/CameraImage/CameraImageHandler.h"
  9. SingleCameraOperationWnd::SingleCameraOperationWnd(QWidget *parent)
  10. : JOriginalMainWnd(parent)
  11. , ui(new Ui::SingleCameraOperationWnd), scaleFactor(1.0)
  12. {
  13. ui->setupUi(this);
  14. ui->viewwidgetgroup->resizeSingleUI();
  15. isShow = true;
  16. liveClick = true;
  17. initFrom();
  18. }
  19. SingleCameraOperationWnd::~SingleCameraOperationWnd()
  20. {
  21. delete ui;
  22. }
  23. void SingleCameraOperationWnd::initFrom() {
  24. QTimer *timer = new QTimer(this);
  25. connect(timer, &QTimer::timeout, this, &SingleCameraOperationWnd::checkSettings);
  26. timer->start(100);
  27. // 设置右上部分
  28. QWidget *viewport = ui->scrollArea->viewport();
  29. QWidget *container = new QWidget(viewport);
  30. QVBoxLayout *layout = new QVBoxLayout(container);
  31. layout->setSpacing(16); // 设置Group之间的间隔距离
  32. layout->setMargin(0);
  33. QList<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8};
  34. QList<Group*> widgets;
  35. for (int i = 0; i < numbers.size(); ++i) {
  36. int num = numbers[i];
  37. CameraImageHandler* manager = new CameraImageHandler(num);
  38. Group* widget = manager->getGroup();
  39. //connect(widget,&Group::send_button_Signal,this,&SingleCameraOperationWnd::showAndHideButton);
  40. //connect(widget,&Group::send_ComboBox_singal,this,&SingleCameraOperationWnd::handleComBoxChange);
  41. if (widget != nullptr) {
  42. layout->addWidget(widget);
  43. widgets.append(widget);
  44. groupMap[num] = widget;
  45. }
  46. if(manager->getWafer()) {
  47. waferMap.insert(num, manager->getWafer());
  48. }
  49. if(manager->getWafer()) {
  50. waffleMap.insert(num, manager->getWaffle());
  51. }
  52. if(manager->getMaterialBox()){
  53. materialBoxMap.insert(num,manager->getMaterialBox());
  54. }
  55. delete manager;
  56. }
  57. // 设置控件的最小高度和最大宽度
  58. int minHeight = 162;
  59. int maxWidth = 244;
  60. for (Group* w : widgets) {
  61. w->setMinimumHeight(minHeight);
  62. w->setMaximumWidth(maxWidth);
  63. }
  64. container->setLayout(layout);
  65. ui->scrollArea->setWidget(container);
  66. ui->scrollArea->resize(261, 700);
  67. }
  68. void SingleCameraOperationWnd::closeEvent(QCloseEvent *event) {
  69. // 保存滑块状态
  70. // saveSliderStates();
  71. // 确保调用基类的 closeEvent 处理其他关闭逻辑
  72. QMainWindow::closeEvent(event);
  73. }
  74. void SingleCameraOperationWnd::checkSettings() {
  75. QSettings settings("YourCompany", "YourApplication_");
  76. int groupId = settings.value("GroupId", 0).toInt();
  77. int index = settings.value("Index", 0).toInt();
  78. if (groupId != lastGroupId || index != lastIndex) {
  79. lastGroupId = groupId;
  80. lastIndex = index;
  81. loadSettings();
  82. }
  83. }
  84. void SingleCameraOperationWnd::loadSettings()
  85. {
  86. QSettings settings("YourCompany", "YourApplication_");
  87. int groupId = settings.value("GroupId", 0).toInt();
  88. int index = settings.value("Index", 0).toInt();
  89. loadGroupSettings(groupId, index);
  90. }
  91. void SingleCameraOperationWnd::loadGroupSettings(int Id, int Index) {
  92. QSettings settings("YourOrganization", "YourApplication");
  93. settings.beginGroup(QString::number(Id));
  94. QString imagePath = settings.value("ImagePath1").toString();
  95. int materialWndType = settings.value("MaterialWndType").toInt();
  96. QStringList textList = settings.value("TextList").toStringList();
  97. settings.endGroup();
  98. QPixmap newPixmap;
  99. // 判断是实时图片还是物料窗口
  100. if (Index == 1) {
  101. newPixmap = QPixmap(imagePath);
  102. QStringList test = { "1","2" };
  103. ui->viewwidgetgroup->updateOperateWidget(newPixmap, test);
  104. }else if (Index == 2) {
  105. updateMaterialWidget(materialWndType, Id);
  106. }
  107. }
  108. void SingleCameraOperationWnd::updateMaterialWidget( int materialWndType, int groupId){
  109. switch (materialWndType) {
  110. case 1: ui->viewwidgetgroup->setWafer(waferMap.value(lastGroupId)); break;
  111. case 2: ui->viewwidgetgroup->setWaffle(waffleMap.value(lastGroupId)); break;
  112. case 3: ui->viewwidgetgroup->setMaterialBox(materialBoxMap.value(lastGroupId)); break;
  113. }
  114. }
  115. void SingleCameraOperationWnd::InitMainCameraBind(CameraBind* pCameraBind)
  116. {
  117. }
  118. void SingleCameraOperationWnd::UpdateCameraDisplay0(int iCameraId, JVision::ImageInfo imageData)
  119. {
  120. }
  121. void SingleCameraOperationWnd::UpdateCameraDisplay1(int iCameraId, JVision::ImageInfo imageData)
  122. {
  123. }
  124. void SingleCameraOperationWnd::UpdateCameraDisplay2(int iCameraId, JVision::ImageInfo imageData)
  125. {
  126. }
  127. void SingleCameraOperationWnd::UpdateCameraDisplay3(int iCameraId, JVision::ImageInfo imageData)
  128. {
  129. }
  130. void SingleCameraOperationWnd::UpdateCameraDisplay4(int iCameraId, JVision::ImageInfo imageData)
  131. {
  132. }
  133. void SingleCameraOperationWnd::wheelEvent(QWheelEvent *event)
  134. {
  135. mousePos = ui->viewwidgetgroup->getOperatewidget()->mapFromGlobal(event->globalPos());
  136. if (ui->viewwidgetgroup->getOperatewidget()->rect().contains(ui->viewwidgetgroup->getOperatewidget()->mapFromGlobal(event->globalPos()))) {
  137. if (event->angleDelta().y() > 0) {
  138. ui->viewwidgetgroup->updateScale(scaleFactor * 1.1); // 放大
  139. } else {
  140. ui->viewwidgetgroup->updateScale(scaleFactor * 0.9); // 缩小
  141. }
  142. }
  143. if (ui->scrollArea->rect().contains(ui->scrollArea->mapFromGlobal(event->globalPos()))) {
  144. // 获取 QScrollArea 的横向滚动条
  145. QScrollBar *scrollBar = ui->scrollArea->horizontalScrollBar();
  146. // 根据滚轮滚动方向改变滚动条的值
  147. if (event->angleDelta().y() > 0) {
  148. scrollBar->setValue(scrollBar->value() - 50);
  149. } else {
  150. scrollBar->setValue(scrollBar->value() + 50);
  151. }
  152. }
  153. QMainWindow::wheelEvent(event);
  154. }
  155. //void SingleCameraOperationWnd::onComboBoxIndexChanged(int index) {
  156. // // 根据index获取图片路径
  157. // QString imagePath = getImagePathFromIndex(index);
  158. // if (!imagePath.isEmpty()) {
  159. // QPixmap newPixmap(imagePath);
  160. // QPixmap scaledPixmap = newPixmap.scaled(ui->viewwidgetgroup->getOperatewidget()->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  161. // ui->viewwidgetgroup->getOperatewidget()->setPixmap(scaledPixmap);
  162. // // 更新当前图片的成员变量
  163. // currentPixmap = scaledPixmap;
  164. // scaleFactor = 1.0;
  165. // ui->viewwidgetgroup->label_Percentage->setText("100%");
  166. // }
  167. //}
  168. QString SingleCameraOperationWnd::getImagePathFromIndex(int index) {
  169. QStringList imagePaths = {
  170. ":/images/test_image/image.png",
  171. ":/images/test_image/image_2.png",
  172. };
  173. if (index >= 0 && index < imagePaths.size()) {
  174. return imagePaths[index];
  175. }
  176. return ":/images/test_image/image.png";
  177. }
  178. void SingleCameraOperationWnd::showEvent(QShowEvent *event) {
  179. QMainWindow::showEvent(event);
  180. loadSettings();
  181. }
  182. void SingleCameraOperationWnd::hideEvent(QHideEvent *event) {
  183. QMainWindow::hideEvent(event);
  184. }
  185. void SingleCameraOperationWnd::startCamera(int cameraId) {
  186. m_grabber = new ImageGrabber(cameraId);
  187. connect(m_grabber, &ImageGrabber::imageGrabbed,
  188. this, &SingleCameraOperationWnd::updateImage);
  189. m_grabber->start();
  190. }
  191. void SingleCameraOperationWnd::stopCamera(int cameraId){
  192. m_grabber->stop();
  193. }
  194. void SingleCameraOperationWnd::on_DatacomboBox_currentIndexChanged(int index){
  195. QSettings settings("YourCompany", "YourApplication_");
  196. int groupId = settings.value("GroupId", 0).toInt();
  197. if (groupMap.contains(groupId)) {
  198. Group* group = groupMap[groupId];
  199. // 修改属性
  200. group->setDatacomboBox(index);
  201. } else {
  202. qDebug() << "Group with id" << groupId << "not found.";
  203. }
  204. }
  205. //void SingleCameraOperationWnd::handleComBoxChange(int groupId,int index){
  206. // QSettings settings("YourCompany", "YourApplication_");
  207. // int currentGroupId = settings.value("GroupId", 0).toInt();
  208. // if(currentGroupId == groupId){
  209. // ui->viewwidgetgroup->DataSources->setCurrentIndex(index);
  210. // }
  211. //}
  212. void SingleCameraOperationWnd::updateImage(const QImage&image){
  213. QPixmap pixmap = QPixmap::fromImage(image);
  214. QSize size = ui->viewwidgetgroup->getOperatewidget()->size();
  215. // QPixmap scaledPixmap = pixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  216. QPixmap scaledPixmap = pixmap.scaled(size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
  217. ui->viewwidgetgroup->getOperatewidget()->setPixmap(scaledPixmap);
  218. }
  219. void SingleCameraOperationWnd::loadLiveVedio(){
  220. if(liveClick == true){
  221. startCamera(0);
  222. liveClick = false;
  223. }else{
  224. stopCamera(0);
  225. liveClick = true;
  226. }
  227. }