#include "ControlOperationPage.h" #include "ui_ControlOperationPage.h" #include "JoystickPage.h" ControlOperationPage::ControlOperationPage(QWidget* parent) : QWidget(parent) , ui(new Ui::ControlOperationPage) { ui->setupUi(this); m_isEnable = false; m_bRuler = false; updateOperateWidget(QPixmap()); InitWnd(); initForm(); } ControlOperationPage::~ControlOperationPage() { delete ui; } void ControlOperationPage::updateOperateWidget(const QPixmap& pixmap) { QSize size = ui->Operatewidget->size(); QPixmap scaledPixmap = pixmap.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); clearLayout(); ImageView* customView = new ImageView(ui->Operatewidget); customView->setPixmap(scaledPixmap); QVBoxLayout* layout = new QVBoxLayout(ui->Operatewidget); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(customView); ui->Operatewidget->setLayout(layout); m_currentPixmap = scaledPixmap; m_scaleFactor = 1.0; m_previousScaleFactor = 1.0; ui->label_Percentage->setText("100%"); m_currentMode = ModeImage; m_currentImageView = customView; applyScale(); } void ControlOperationPage::initImage() { clearLayout(); ImageView* customView = new ImageView(ui->Operatewidget); QVBoxLayout* layout = new QVBoxLayout(ui->Operatewidget); layout->setContentsMargins(0, 0, 0, 0); layout->addWidget(customView); ui->Operatewidget->setLayout(layout); m_scaleFactor = 1.0; m_previousScaleFactor = 1.0; ui->label_Percentage->setText("100%"); m_currentMode = ModeImage; m_currentImageView = customView; applyScale(); } void ControlOperationPage::setDataSources(const QStringList& textList) { ui->DataSources->clear(); ui->DataSources->addItems(textList); } // 清除大窗口上当前的布局 void ControlOperationPage::clearLayout() { // 获取当前布局 QLayout* layout = ui->Operatewidget->layout(); if (layout) { QLayoutItem* child; while ((child = layout->takeAt(0)) != nullptr) { if (child->widget() != nullptr) { delete child->widget(); // 删除控件 } delete child; // 删除布局项 } delete layout; // 删除布局本身 } } void ControlOperationPage::on_ZoomUpButton_clicked() { m_mousePos = ui->Operatewidget->geometry().center(); updateScale(m_scaleFactor * 1.1); } void ControlOperationPage::on_ZoomOutButton_clicked() { m_mousePos = ui->Operatewidget->geometry().center(); updateScale(m_scaleFactor * 0.9); } // 更新缩放比例 void ControlOperationPage::updateScale(double newScaleFactor) { if (newScaleFactor >= 1.0) { m_scaleFactor = newScaleFactor; } else { m_scaleFactor = 1.0; // 最小缩放比例为 1.0 } applyScale(); // 应用缩放 } // 应用缩放 void ControlOperationPage::applyScale() { if (m_currentMode == ModeImage && m_currentImageView) { // 图片模式:缩放图片 QTransform transform; transform.scale(m_scaleFactor, m_scaleFactor); m_currentImageView->setTransform(transform); } else if (m_currentMode == ModeView && m_currentView) { // View 模式:缩放 view QTransform transform; transform.scale(m_scaleFactor, m_scaleFactor); m_currentView->setTransform(transform); } // 更新百分比显示 double percentage = m_scaleFactor * 100; QString percentageStr = QString::number((int)percentage); ui->label_Percentage->setText(QString("%1%").arg(percentageStr)); } void ControlOperationPage::handleDoubleClick() { if (m_currentMode == ModeImage && m_currentImageView) { QTransform transform; transform.scale(1, 1); m_currentImageView->setTransform(transform); } else if (m_currentMode == ModeView && m_currentView) { QTransform transform; transform.scale(1, 1); m_currentView->setTransform(transform); } m_scaleFactor = 1.0; m_previousScaleFactor = 1.0; ui->label_Percentage->setText("100%"); } void ControlOperationPage::updateMaterialWidget(kinds materialWndType) { clearLayout(); switch (materialWndType) { case wafer_kind: KindsofWidget(wafer_kind); break; case waffle_kind: KindsofWidget(waffle_kind); break; case materialbox_kind: KindsofWidget(materialbox_kind); break; case bond_kind:KindsofWidget(bond_kind); break; } } void ControlOperationPage::KindsofWidget(kinds kind) { bool isRun = false; ui->Operatewidget->clearPixmap(); QVBoxLayout* layout = new QVBoxLayout(ui->Operatewidget); layout->setContentsMargins(0, 0, 0, 0); m_currentMode = ModeView; if (kind == wafer_kind) { if (m_wafer) { isRun = true; m_wafer->initFrom(ui->Operatewidget); layout->addWidget(m_wafer->view); m_currentView = m_wafer->view; } } else if (kind == waffle_kind) { if (m_waffle) { isRun = true; m_waffle->initFrom(ui->Operatewidget); layout->addWidget(m_waffle->view); m_currentView = m_waffle->view; } } else if (kind == materialbox_kind) { if (m_materialbox) { isRun = true; m_materialbox->initFrom(ui->Operatewidget); layout->addWidget(m_materialbox->view); m_currentView = m_materialbox->view; } } else if (kind == bond_kind) { if (m_bond) { isRun = true; m_bond->initFrom(ui->Operatewidget); layout->addWidget(m_bond->m_pView); m_currentView = m_bond->m_pView; } } if (isRun) { ui->Operatewidget->setLayout(layout); m_currentMode = ModeView; m_scaleFactor = 1.0; applyScale(); } } void ControlOperationPage::setWafer(Wafer* wafer) { m_wafer = wafer; updateMaterialWidget(wafer_kind); } void ControlOperationPage::setWaffle(Waffle* waffle) { m_waffle = waffle; updateMaterialWidget(waffle_kind); } void ControlOperationPage::setMaterialBox(MaterialBox* materialbox) { m_materialbox = materialbox; updateMaterialWidget(materialbox_kind); } void ControlOperationPage::setBond(Bond* bond) { m_bond = bond; updateMaterialWidget(bond_kind); } void ControlOperationPage::initForm() { connect(ui->Operatewidget, &ImageWidget::sendDoubleClicksignal, this, &ControlOperationPage::handleDoubleClick); ui->Operatewidget->setMouseTracking(true); connect(ui->DataSources, QOverload::of(&QComboBox::currentIndexChanged), this, &ControlOperationPage::on_DataSources_currentIndexChanged); } void ControlOperationPage::on_DataSources_currentIndexChanged(int comboxIndex) { send_ComboBox_singal(comboxIndex); } ImageWidget* ControlOperationPage::getOperatewidget() { return ui->Operatewidget; } void ControlOperationPage::resizeSingleUI(bool bMax /*= false*/) { ui->DataSources->setGeometry(QRect(20, 20, 400, 32)); ui->Operatewidget->setGeometry(QRect(20, 72, 786, 786)); //ui->line_2->setGeometry(QRect(826, 20, 1, 953)); ui->LiveButton->setGeometry(QRect(436, 20, 60, 32)); ui->horizontalLayout_2->setGeometry(QRect(12, 882, 786, 32)); ui->layoutWidget->setGeometry(QRect(10, 882, 790, 31)); ui->ZoomUpButton->setGeometry(QRect(25, 882, 120, 32)); ui->ZoomOutButton->setGeometry(QRect(155, 882, 100, 32)); ui->label_Percentage->setGeometry(QRect(265, 882, 120, 32)); ui->RulerButton->setGeometry(QRect(400, 882, 110, 32)); ui->PenButton->setGeometry(QRect(530, 882, 100, 32)); // ui->switchJoystickBut ui->horizontalLayout->setGeometry(QRect(12, 882, 786, 32)); ui->BackGround->setGeometry(QRect(16, 68, 794, 794)); if (bMax) { ui->Operatewidget->m_nSingleCameraOperationWnd = true; } } void ControlOperationPage::resizeChartsAndCamerasUI() { } qreal ControlOperationPage::getScaleFactorValue() { return m_scaleFactor; } void ControlOperationPage::setScaleFactorSize(QPixmap scaledImage) { //int newWidth = scaledImage.width() * m_scaleFactor; //int newHeight = scaledImage.height() * m_scaleFactor; //QPixmap curr_scaledImage = scaledImage.scaled(newWidth, newHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation); // ui->Operatewidget->setPixmapAndPoint(curr_scaledImage, m_previousScaleFactor, m_scaleFactor, m_mousePos); // ui->Operatewidget->setPixmap(curr_scaledImage); if (m_currentImageView == nullptr || m_currentMode == ModeView) { updateOperateWidget(scaledImage); } else { QSize size = ui->Operatewidget->size(); QPixmap scaledPixmap = scaledImage.scaled(size, Qt::KeepAspectRatio, Qt::SmoothTransformation); m_currentImageView->setCurPixmap(scaledPixmap); } } void ControlOperationPage::UpDateCameraBind(CameraBind* pCameraBind) { m_pCameraBindCopy = pCameraBind; m_isAdd = true; // 有指针了在去刷新 if (m_pCameraBindCopy != nullptr) { DeduplicationBox(ui->moduleTypeComboBox, m_pCameraBindCopy->m_vecCAxis, 0); DeduplicationBox(ui->axisTypeComboBox, m_pCameraBindCopy->m_vecCAxis, 1); } m_isAdd = false; setSwitchJoystickButEnable(true); } void ControlOperationPage::setSwitchJoystickButEnable(bool isEnable) { ui->switchJoystickBut->setEnabled(isEnable); if (isEnable == false) { ResetIdleTimer(false); m_isUserOnclick = false; } } void ControlOperationPage::on_switchJoystickBut_clicked() { m_isUserOnclick = true; ResetIdleTimer(true); UpdataModuleType("aaa", 3); ui->switchJoystickBut->setEnabled(false); } void ControlOperationPage::MouseMovedSlots(const QPoint& delta) { //qDebug() << "MouseMovedSlots:" << delta; if (m_pCameraBindCopy) { m_pCameraBindCopy->JCameraMove(0, delta.x(), delta.y()); } } void ControlOperationPage::RequestCursorMoveSlots(const QPoint& pos) { QCursor::setPos(pos); } void ControlOperationPage::on_moduleTypeComboBox_currentIndexChanged(int index) { m_isAdd = true; QString strMod = ui->moduleTypeComboBox->itemText(index); ui->axisTypeComboBox->clear(); for (auto a : m_pCameraBindCopy->m_vecCAxis) { // 如果2个相等说明当前模组,与轴匹配 if (strMod == a->GetModuleType().c_str()) { ui->axisTypeComboBox->addItem(a->GetStringAxisType().c_str()); } } m_isAdd = false; UpdataModuleType(strMod, 1); } void ControlOperationPage::on_axisTypeComboBox_currentIndexChanged(int index) { if (!m_isAdd) { UpdataModuleType(ui->axisTypeComboBox->itemText(index), 2); } } void ControlOperationPage::timerEvent(QTimerEvent* event) { int nID = event->timerId(); if (nID == m_idleTimer) { ResetIdleTimer(false); m_isUserOnclick = false; ui->switchJoystickBut->setEnabled(true); } } //void ControlOperationPage::mousePressEvent(QMouseEvent* event) //{ // if (event->button() == Qt::LeftButton) // { // LockMouse(true); // } // else if (event->button() == Qt::RightButton) // { // LockMouse(false); // } //} // //void ControlOperationPage::mouseMoveEvent(QMouseEvent* event) //{ // int a = 10; //} // //bool ControlOperationPage::eventFilter(QObject* obj, QEvent* event) //{ // switch (event->type()) // { // case QEvent::KeyPress: // case QEvent::MouseMove: // case QEvent::MouseButtonPress: // case QEvent::MouseButtonDblClick: // case QEvent::Wheel: // { // if (m_isUserOnclick) // { // ResetIdleTimer(false); // ResetIdleTimer(true); // } // } // break; // default: // break; // } // // return QWidget::eventFilter(obj, event); //} void ControlOperationPage::wheelEvent(QWheelEvent* event) { if (ui->Operatewidget->rect().contains(ui->Operatewidget->mapFromGlobal(event->globalPos()))) { m_mousePos = ui->Operatewidget->mapFromGlobal(event->globalPos()); if (event->angleDelta().y() > 0) { updateScale(m_scaleFactor * 1.1); // 放大 } else { updateScale(m_scaleFactor * 0.9); // 缩小 } } } void ControlOperationPage::HideLayout(bool bShow) { for (int i = 0; i < ui->horizontalLayout->count(); ++i) { QWidget* widget = ui->horizontalLayout->itemAt(i)->widget(); if (widget) { if (bShow) { widget->show(); } else { widget->hide(); } } } } void ControlOperationPage::ResetIdleTimer(bool bStart /*= false*/) { if (bStart) { if (isActiveWindow()) { m_idleTimer = startTimer(g_unnSuspensionWaitingTime); } } else { killTimer(m_idleTimer); m_idleTimer = -1; } HideLayout(bStart); } void ControlOperationPage::InitWnd() { HideLayout(false); /* connect(ui->widgetPage, &JoystickPage::PositionChangedSignals, [&](qreal x, qreal y) { ui->showLabel->setText(QString("摇杆位置: (%1, %2)").arg(x, 0, 'f', 2).arg(y, 0, 'f', 2)); });*/ this->setMouseTracking(true); this->installEventFilter(this); ui->RulerButton->setCheckable(true); ui->PenButton->setCheckable(true); ui->RulerButton->setChecked(false); ui->RulerButton->setChecked(false); } void ControlOperationPage::CreateMouseMonitor(bool isStart) { if (isStart) { m_pMousethread = new JMouseMonitorThread(this); connect(m_pMousethread, &JMouseMonitorThread::MouseMovedSlg, this, &ControlOperationPage::MouseMovedSlots); connect(m_pMousethread, &JMouseMonitorThread::RequestCursorMoveSlg, this, &ControlOperationPage::RequestCursorMoveSlots); m_pMousethread->start(); } else { m_pMousethread->stop(); m_pMousethread->wait(); delete m_pMousethread; m_pMousethread = nullptr; } } void ControlOperationPage::LockMouse(bool islockMouse) { if (m_pCameraBindCopy) { if (islockMouse) { if (!m_bMouseRun) { CreateMouseMonitor(true); QPoint center = rect().center(); QPoint globalCenter = mapToGlobal(center); m_pMousethread->setLockCenter(globalCenter); QCursor::setPos(globalCenter); setCursor(Qt::BlankCursor); grabMouse(); m_bMouseRun = true; } } else { if (m_bMouseRun) { if (m_pMousethread) { m_pMousethread->unlock(); releaseMouse(); unsetCursor(); QCursor::setPos(mapToGlobal(rect().center())); CreateMouseMonitor(false); } m_bMouseRun = false; } } } } void ControlOperationPage::UpdataModuleType(const QString& strMode, int nIndex) { //if (nIndex == 1) //{ // m_currentSelectRunAxis.ModuleType = strMode.toStdString(); //} //else if (nIndex == 2) //{ // m_currentSelectRunAxis.AxisType = strMode.toStdString(); //} m_currentSelectRunAxis.ModuleType = ui->moduleTypeComboBox->currentText().toStdString(); m_currentSelectRunAxis.AxisType = ui->axisTypeComboBox->currentText().toStdString(); if (m_isAdd == false) { m_currentSelectRunAxis.isSwitch = true; emit SendModuleTypeSignals(m_currentSelectRunAxis); m_currentSelectRunAxis.isSwitch = false; } } template void ControlOperationPage::DeduplicationBox(QComboBox* pCom, const T& veTemp, int nIndex) { for (auto& a : veTemp) { QString strName; if (nIndex == 0) { strName = a->GetModuleType().c_str(); } else if (nIndex == 1) { strName = a->GetStringAxisType().c_str(); } QStringList items; for (int i = 0; i < pCom->count(); ++i) { items << pCom->itemText(i); } bool bMa = false; // 是否匹配 for (auto b : items) { if (b == strName) { bMa = true; break; } } if (!bMa) { pCom->addItem(strName); } } } void ControlOperationPage::setEnableControls(bool enable) { ui->LiveButton->setEnabled(enable); ui->DataSources->setEnabled(enable); ui->RulerButton->setEnabled(enable); ui->PenButton->setEnabled(enable); if (enable == true) { ui->LiveButton->setStyleSheet( "QPushButton:hover {" " background-color: #45a049;" "}" "QPushButton:pressed {" " background-color: #3e8e41;" "}" ); ui->RulerButton->setStyleSheet("background-color: #CBD0FF;"); ui->PenButton->setStyleSheet("background-color: #CBD0FF;"); } else { ui->LiveButton->setStyleSheet("background-color: lightgray;"); ui->RulerButton->setStyleSheet("background-color: lightgray;"); ui->PenButton->setStyleSheet("background-color: lightgray;"); } } void ControlOperationPage::setBlueBord() { ui->BackGround->setStyleSheet(QString::fromUtf8("border: 4px solid blue;")); } void ControlOperationPage::on_RulerButton_clicked() { if (nullptr == m_currentImageView) return; m_bRuler = !m_bRuler; if (m_bRuler == true) { } else { } if (ui->RulerButton->isChecked()) { m_currentImageView->addRuler(); if (ui->PenButton->isChecked()) { ui->PenButton->setChecked(false); m_currentImageView->setIsDrawing(false); //ui->PenButton->setStyleSheet("QPushButton { background-color: none; }"); ui->PenButton->setStyleSheet("QPushButton { background-color: #CBD0FF; }"); } ui->RulerButton->setStyleSheet("QPushButton { background-color: #808FFF; }"); } else { m_currentImageView->cancelRuler(); ui->RulerButton->setStyleSheet("QPushButton { background-color: #CBD0FF; }"); } } void ControlOperationPage::on_PenButton_clicked() { if (nullptr == m_currentImageView) return; m_isEnable = !m_isEnable; if (m_isEnable == true) { } else { } if (ui->PenButton->isChecked()) { ui->PenButton->setStyleSheet("QPushButton { background-color: #808FFF; }"); //ui->Operatewidget->setIsDrawing(true); m_currentImageView->setIsDrawing(true); if (ui->RulerButton->isChecked()) { ui->RulerButton->setChecked(false); m_currentImageView->cancelRuler(); ui->RulerButton->setStyleSheet("QPushButton { background-color: #CBD0FF; }"); } } else { //ui->Operatewidget->setIsDrawing(false); m_currentImageView->setIsDrawing(false); ui->PenButton->setStyleSheet("QPushButton { background-color: #CBD0FF; }"); } } void ControlOperationPage::SaveNewImage() { //// 捕获 ui->Operatewidget 的当前显示内容 //QPixmap pixmap = ui->Operatewidget->grab(); //// 使用当前时间生成默认文件名 //QString defaultFileName = QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm-ss") + ".png"; //// 弹出文件对话框,让用户选择保存路径和文件名 //QString filePath = QFileDialog::getSaveFileName( // this, // tr("保存图片"), // QDir::homePath() + "/" + defaultFileName, // 默认路径为用户目录,文件名为当前时间 // tr("PNG 文件 (*.png);;JPEG 文件 (*.jpg);;所有文件 (*)") //); //// 如果用户取消保存,则退出函数 //if (filePath.isEmpty()) { // return; //} //if (!pixmap.save(filePath)) { // QMessageBox::warning(this, tr("保存失败"), tr("无法保存图片到指定路径。")); //} //else { // QMessageBox::information(this, tr("保存成功"), tr("图片已成功保存到:") + filePath); //} } void ControlOperationPage::setComboBox(const QList> fileList, const int index) { ui->DataSources->clear(); // 使用QList批量添加到QComboBox for (const auto& pair : fileList) { ui->DataSources->addItem(pair.first, pair.second); } ui->DataSources->setCurrentIndex(index); } QPixmap ControlOperationPage::getCurrentComboBoxPixmap(const int index) { if (index < 0) return NULL; // 从用户数据中获取完整路径并输出 QString imagePath = ui->DataSources->itemData(index).toString(); QPixmap pixmap(imagePath); return pixmap; }