#include "opcv/CvGraphicsShowModel.h" CvGraphicsShowModel::CvGraphicsShowModel() { //mCvImageView = new CvImageViewWidget(); //mCvImageView->installEventFilter(this); //mCvImageView->resize(600, 450); //moveToThread(this); mCvGraphicsView = new CvGraphicsViewWidget(); mCvGraphicsView->setObjectName(QStringLiteral("graphicsView")); mCvGraphicsView->setStyleSheet( "QGraphicsView#graphicsView{ " "background-color:transparent; " "border: 1px solid #F0F2F4; " "border-radius: 4px; }"); widget = new QWidget(); widget->resize(600, 450); widget->setObjectName(QStringLiteral("widget")); widget->setStyleSheet("QWidget#widget { background-color:transparent; }"); QGridLayout* gridLayout = new QGridLayout(widget); QVBoxLayout* verticalLayout = new QVBoxLayout(); QHBoxLayout* horizontalLayout = new QHBoxLayout(); horizontalLayout->setDirection(QHBoxLayout::LeftToRight); QLabel* label = new QLabel(); label->setObjectName(QStringLiteral("label")); label->setText(QStringLiteral("图片放缩:")); label->setStyleSheet("QLabel#label { color:#FFFFFF; }"); QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth()); label->setSizePolicy(sizePolicy); label->setMaximumSize(QSize(60, 16777215)); horizontalSlider = new QSlider(); horizontalSlider->setObjectName(QString::fromUtf8("horizontalSlider")); horizontalSlider->setMinimum(-14); horizontalSlider->setMaximum(14); horizontalSlider->setPageStep(2); horizontalSlider->setOrientation(Qt::Horizontal); horizontalSlider->setTickPosition(QSlider::TicksBelow); QObject::connect(horizontalSlider, &QSlider::valueChanged, this, &CvGraphicsShowModel::onQSliderValueChanged); QSpacerItem* horizontalSpacer = new QSpacerItem(15, 20, QSizePolicy::Fixed, QSizePolicy::Minimum); horizontalLayout->addItem(horizontalSpacer); horizontalLayout->addWidget(label); horizontalLayout->addWidget(horizontalSlider); horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); horizontalLayout->addItem(horizontalSpacer); verticalLayout->addLayout(horizontalLayout); verticalLayout->addWidget(mCvGraphicsView); gridLayout->addLayout(verticalLayout, 0, 0, 1, 1); mCvImageData = std::make_shared(); } void CvGraphicsShowModel::onQSliderValueChanged(int val) { qDebug() << "onQSliderValueChanged:" << val; qreal scale = 1.0; if (val > 0) { scale = qPow(1.2, val); qreal temp = scale; scale = scale / mScaledNum; mScaledNum = temp;//更新保存的缩放系数 } else if (val < 0) { scale = qPow(1.2, val); qreal temp = scale; scale = scale / mScaledNum; mScaledNum = temp;//更新保存的缩放系数 } else if (val == 0) { qreal temp = scale; scale = scale / mScaledNum; mScaledNum = temp;//更新保存的缩放系数 } mCvGraphicsView->scale(scale, scale); } void CvGraphicsShowModel::inputConnectionDeleted(QtNodes::Connection const&) { mCvGraphicsView->setScene(new QGraphicsScene()); mCvImageData = std::make_shared(); PortIndex const outPortIndex = 0; modelValidationState = NodeValidationState::Warning; modelValidationError = QStringLiteral("图片输入未连接!"); Q_EMIT dataUpdated(outPortIndex); } unsigned int CvGraphicsShowModel::nPorts(PortType portType) const { unsigned int result = 1; switch (portType) { case PortType::In: result = 1; break; case PortType::Out: result = 1; default: break; } return result; } NodeDataType CvGraphicsShowModel::dataType(PortType portType, PortIndex portIndex) const { switch (portIndex) { case 0: return CvImageData().type(); break; case 1: return CvImageData().type(); break; } return CvImageData().type(); } bool CvGraphicsShowModel::RunTask() { PortIndex const outPortIndex = 0; try { //qDebug() << "11show"; //QThread::sleep(3); //QThread* thread = new QThread(); //qDebug() << "CvGraphicsShowModel::RunTask thread:" << thread->currentThreadId(); mCvGraphicsView->showImage(*mCvImageData->CvImage()); mCvGraphicsView->scale(0.85, 0.85); modelValidationState = NodeValidationState::Valid; modelValidationError = QString(); } catch (...) { modelValidationState = NodeValidationState::Warning; modelValidationError = QStringLiteral("缺失或运行失败!"); } Q_EMIT dataUpdated(outPortIndex); return true; } void CvGraphicsShowModel::setInData(std::shared_ptrdata, int) { if (data == nullptr) return; if (data->type() == mCvImageData->type()) { auto dataPtr = std::dynamic_pointer_cast(data); if (dataPtr->CvImage()->empty()) return; mCvImageData->setCvImage(*dataPtr->CvImage()); } RunTask(); } bool CvGraphicsShowModel::eventFilter(QObject* object, QEvent* event) { if (event->type() == QEvent::Wheel) { } //if (object == mCvGraphicsView->viewport()) //{ // qDebug() << event->type(); // if (event->type() == QEvent::Wheel || event->type() == QEvent::GraphicsSceneWheel) // { // QWheelEvent* wheelEvent = static_cast(event); // qreal qrTmp = 1.0; // if (mScaledFactor < 0.01 || mScaledFactor > 2000) // return false; // if (wheelEvent->delta() > 0) // { // qrTmp = 1.2; // mCvGraphicsView->scale(qrTmp, qrTmp); // } // else // { // qrTmp = 1.0 / 1.2; // mCvGraphicsView->scale(qrTmp, qrTmp); // } // mScaledFactor = mScaledFactor * qrTmp; //保存放大倍数 // qDebug() << "GraphicsSceneWheel"; // return true; // } //} return false; } std::shared_ptr CvGraphicsShowModel::outData(PortIndex index) { switch (index) { case 0: return std::dynamic_pointer_cast(mCvImageData); break; } return std::dynamic_pointer_cast(mCvImageData); }