viewwidgetgroup.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #include "viewwidgetgroup.h"
  2. #include "ui_viewwidgetgroup.h"
  3. ViewWidgetGroup::ViewWidgetGroup(QWidget *parent)
  4. : QWidget(parent)
  5. , ui(new Ui::ViewWidgetGroup)
  6. {
  7. ui->setupUi(this);
  8. }
  9. ViewWidgetGroup::~ViewWidgetGroup()
  10. {
  11. delete ui;
  12. }
  13. void ViewWidgetGroup::updateOperateWidget(const QPixmap& pixmap, const QStringList& textList){
  14. QSize size = ui->Operatewidget->size();
  15. QPixmap scaledPixmap = pixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  16. clearLayout();
  17. ui->Operatewidget->setPixmap(scaledPixmap);
  18. m_currentPixmap = scaledPixmap;
  19. m_scaleFactor = 1.0;
  20. m_previousScaleFactor = 1.0;
  21. ui->label_Percentage->setText("100%");
  22. ui->DataSources->clear();
  23. ui->DataSources->addItems(textList);
  24. m_currentMode = ModeImage;
  25. }
  26. // 清除大窗口上当前的布局
  27. void ViewWidgetGroup::clearLayout() {
  28. // 获取当前布局
  29. QLayout* layout = ui->Operatewidget->layout();
  30. if (layout) {
  31. QLayoutItem *child;
  32. while ((child = layout->takeAt(0)) != nullptr) {
  33. if (child->widget() != nullptr) {
  34. delete child->widget(); // 删除控件
  35. }
  36. delete child; // 删除布局项
  37. }
  38. delete layout; // 删除布局本身
  39. }
  40. }
  41. void ViewWidgetGroup::on_ZoomUpButton_clicked() {
  42. m_mousePos = ui->Operatewidget->geometry().center();
  43. updateScale(m_scaleFactor * 1.1);
  44. }
  45. void ViewWidgetGroup::on_ZoomOutButton_clicked() {
  46. m_mousePos = ui->Operatewidget->geometry().center();
  47. updateScale(m_scaleFactor * 0.9);
  48. }
  49. // 更新缩放比例
  50. void ViewWidgetGroup::updateScale(double newScaleFactor) {
  51. if (newScaleFactor >= 1.0) {
  52. m_scaleFactor = newScaleFactor;
  53. } else {
  54. m_scaleFactor = 1.0; // 最小缩放比例为 1.0
  55. }
  56. applyScale(); // 应用缩放
  57. }
  58. // 应用缩放
  59. void ViewWidgetGroup::applyScale() {
  60. if (m_currentMode == ModeImage) {
  61. // 图片模式:缩放图片
  62. int newWidth = m_currentPixmap.width() * m_scaleFactor;
  63. int newHeight = m_currentPixmap.height() * m_scaleFactor;
  64. QPixmap scaledImage = m_currentPixmap.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  65. ui->Operatewidget->setPixmapAndPoint(scaledImage, m_previousScaleFactor, m_scaleFactor, m_mousePos);
  66. m_previousScaleFactor = m_scaleFactor;
  67. } else if (m_currentMode == ModeView && m_currentView) {
  68. // View 模式:缩放 view
  69. QTransform transform;
  70. transform.scale(m_scaleFactor, m_scaleFactor);
  71. m_currentView->setTransform(transform);
  72. }
  73. // 更新百分比显示
  74. double percentage = m_scaleFactor * 100;
  75. QString percentageStr = QString::number((int)percentage);
  76. ui->label_Percentage->setText(QString("%1%").arg(percentageStr));
  77. }
  78. void ViewWidgetGroup::handleDoubleClick(){
  79. if (m_currentMode == ModeImage) {
  80. QPixmap scaledImage = m_currentPixmap.scaled(m_currentPixmap.width(), m_currentPixmap.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
  81. ui->Operatewidget->setPixmap(scaledImage); // 这里传递缩放后的图片
  82. } else if (m_currentMode == ModeView && m_currentView) {
  83. QTransform transform;
  84. transform.scale(1, 1);
  85. m_currentView->setTransform(transform);
  86. }
  87. m_scaleFactor = 1.0;
  88. m_previousScaleFactor = 1.0;
  89. ui->label_Percentage->setText("100%");
  90. }
  91. void ViewWidgetGroup::updateMaterialWidget( kinds materialWndType) {
  92. clearLayout();
  93. switch (materialWndType) {
  94. case wafer_kind: KindsofWidget(wafer_kind); break;
  95. case waffle_kind: KindsofWidget(waffle_kind); break;
  96. case materialbox_kind: KindsofWidget(materialbox_kind); break;
  97. case bond_kind:KindsofWidget(bond_kind);break;
  98. }
  99. }
  100. void ViewWidgetGroup::KindsofWidget(kinds kind) {
  101. ui->Operatewidget->clearPixmap();
  102. QVBoxLayout *layout = new QVBoxLayout(ui->Operatewidget);
  103. layout->setContentsMargins(0, 0, 0, 0);
  104. m_currentMode = ModeView;
  105. if(kind == wafer_kind){
  106. m_wafer->initFrom(ui->Operatewidget);
  107. layout->addWidget(m_wafer->view);
  108. m_currentView = m_wafer->view;
  109. }else if(kind == waffle_kind){
  110. m_waffle->initFrom(ui->Operatewidget);
  111. layout->addWidget(m_waffle->view);
  112. m_currentView = m_waffle->view;
  113. }else if(kind == materialbox_kind){
  114. m_materialbox->initFrom(ui->Operatewidget);
  115. layout->addWidget(m_materialbox->view);
  116. m_currentView = m_materialbox->view;
  117. }else{
  118. }
  119. ui->Operatewidget->setLayout(layout);
  120. m_currentMode = ModeView;
  121. m_scaleFactor = 1.0;
  122. applyScale();
  123. }
  124. void ViewWidgetGroup::setWafer(Wafer *wafer){
  125. m_wafer = wafer;
  126. updateMaterialWidget(wafer_kind);
  127. }
  128. void ViewWidgetGroup::setWaffle(Waffle *waffle){
  129. m_waffle = waffle;
  130. updateMaterialWidget(waffle_kind);
  131. }
  132. void ViewWidgetGroup::setMaterialBox(MaterialBox *materialbox){
  133. m_materialbox = materialbox;
  134. updateMaterialWidget(materialbox_kind);
  135. }
  136. //void ViewWidgetGroup::setBond(Bond *bond){
  137. // m_bond = bond;
  138. //}
  139. void ViewWidgetGroup::initForm() {
  140. connect(ui->Operatewidget, &ImageWidget::sendDoubleClicksignal, this, &ViewWidgetGroup::handleDoubleClick);
  141. ui->Operatewidget->setMouseTracking(true);
  142. }
  143. ImageWidget* ViewWidgetGroup::getOperatewidget() {
  144. return ui->Operatewidget;
  145. }