You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

963 lines
24 KiB
C++

5 years ago
#include "RoiScene.h"
#include<QGraphicsSceneMouseEvent>
#include<QGraphicsRectItem>
#include "RoiItem.h"
#include "QGraphicsSceneEvent"
#include "colossusbase.h"
#pragma execution_character_set("utf-8")
RoiScene::RoiScene(QObject *parent)
:QGraphicsScene(parent)
{
m_bDisableMenu = true;
m_pImageItem = NULL;
mpShowImage = NULL;
m_tCurrentPos = QPoint(0, 0);
flag = 0;
}
RoiScene::~RoiScene()
{
m_graphItemMapStruct.clear();
if (m_pImageItem)
{
delete m_pImageItem;
m_pImageItem = nullptr;
}
if (mpShowImage)
{
delete mpShowImage;
mpShowImage = NULL;
}
if(pLineItem)
delete pLineItem;
if(pTenLineITemH)
delete pTenLineITemH;
if (pTenLineITemV)
delete pTenLineITemV;
}
void RoiScene::mouseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
switch (mouseEvent->type()){
case QEvent::GraphicsSceneMousePress:
QGraphicsScene::mousePressEvent(mouseEvent);
break;
case QEvent::GraphicsSceneMouseMove:
QGraphicsScene::mouseMoveEvent(mouseEvent);
break;
case QEvent::GraphicsSceneMouseRelease:
QGraphicsScene::mouseReleaseEvent(mouseEvent);
break;
}
}
bool RoiScene::Initialize(LP_DETECTOR_ROI_DATA roiData)
{
if (pLineItem)
{
removeItem(pLineItem);
pLineItem = NULL;
}
if (pTenLineITemH){
removeItem(pTenLineITemH);
pTenLineITemH = NULL;
}
if (pTenLineITemV){
removeItem(pTenLineITemV);
pTenLineITemV = NULL;
}
this->clear();
if (mpShowImage)
{
delete mpShowImage;
mpShowImage = NULL;
}
InitShowImage(roiData.img);
updateInfoLable();
m_graphItemMapStruct.clear();
int nRecordCount = roiData.records.size();
if (nRecordCount > 0)
{
for (int i = 0; i < nRecordCount; i++)
{
Item_List record = roiData.records.at(i);
if (!roiData.img.empty())
{
int maxwidth = roiData.img.cols;
int maxheigh = roiData.img.rows;
CheckRecord(record,maxwidth,maxheigh);
}
if (!InitRecord(record, i))
return false;
}
}
if (m_graphItemMapStruct.size() == 2)
{
GraphicsItem *pFirst =m_graphItemMapStruct.firstKey();
GraphicsItem *pSecond = m_graphItemMapStruct.lastKey();
int firstVal = pFirst->boundingRect().width()*pFirst->boundingRect().height();
int secondVal = pSecond->boundingRect().width()*pSecond->boundingRect().height();
if (firstVal > secondVal)
{
centerPoint = pFirst->scenePos();
startPoint = pSecond->scenePos();
m_graphItemMapStruct[pFirst]._model = ENUM_LOCKROI;
m_graphItemMapStruct[pSecond]._model = ENUM_UNBINDROI;
}
else
{
centerPoint = pSecond->scenePos();
startPoint = pFirst->scenePos();
m_graphItemMapStruct[pSecond]._model = ENUM_LOCKROI;
m_graphItemMapStruct[pFirst]._model = ENUM_UNBINDROI;
}
}
else
{
flag = 0;
centerPoint = startPoint = QPointF();
}
if (bUseTenFlag == true)
{
updateTenItem(true);
}
return true;
}
bool RoiScene::CheckRecord(Item_List &record, int maxwidth,int maxheight)
{
Item_List newRecord;
int nCount = record.size();
if (nCount > 1)
nCount = 1;
if (nCount > 0)
{
for (int i = 0; i < nCount; i++)
{
Feature_List item = record.at(i);
if (item.first == RECTANGLE)
{
if (item.second.at(2)>maxwidth)
{
item.second.replace(2, maxwidth / 2);
}
if (item.second.at(3) > maxheight)
{
item.second.replace(3, maxheight / 2);
}
}
else if (item.first == ELLIPSE)
{
if (item.second.at(2) > maxwidth)
{
item.second.replace(2, maxwidth / 2);
}
if (item.second.at(3) > maxheight)
{
item.second.replace(3, maxheight / 2);
}
}
else if (item.first == CIRCLE)
{
if (maxheight > maxwidth)
{
if (item.second.at(2) > maxwidth)
{
item.second.replace(2, maxwidth / 2);
}
}
else
{
if (item.second.at(2) > maxheight)
{
item.second.replace(2, maxheight / 2);
}
}
}
newRecord.push_back(item);
}
record = newRecord;
}
return true;
}
bool RoiScene::InitRecord(Item_List record, int nIndex)
{
int nCount = record.size();
if (nCount > 1)
nCount = 1;
if (nCount > 0)
{
QList<GraphicsItem *> graphicItems;
for (int i = 0; i < nCount; i++)
{
Feature_List item = record.at(i);
GraphicsItem* graphicItem = NULL;
if (item.first == RECTANGLE)
{
QRect mRect;
graphicItem = new GraphicsRectItem(QRect(0, 0, abs(item.second.at(2)), abs(item.second.at(3))), NULL);
graphicItem->SetType(GraphicsRect);
}
else if (item.first == ELLIPSE)
{
graphicItem = new GraphicsEllipseItem(QRect(0, 0, abs(item.second.at(2)) * 2, abs(item.second.at(3)) * 2), NULL);
graphicItem->SetType(GraphicsEllipse);
}
else if (item.first == CIRCLE)
{
graphicItem = new GraphicsEllipseItem(QRect(0, 0, abs(item.second.at(2)) * 2, abs(item.second.at(2)) * 2), NULL);
graphicItem->SetType(GraphicsCircle);
}
if (graphicItem)
{
graphicItem->SetLock(true);
graphicItem->SetColor(SystemStateInfo::rgb);
graphicItem->SetCheckState(true);
graphicItem->setPen(Qt::NoPen);
graphicItem->setZValue(1);
graphicItems.push_back(graphicItem);
addItem(graphicItem);
QPointF itemPoint;
itemPoint.setX(item.second.at(0));
itemPoint.setY(item.second.at(1));
graphicItem->setPos(itemPoint-mImgPosOffSet);
//m_graphItemMap[graphicItem]= maptoImgNewPos(graphicItem);
tagROIParas tagItem;
tagItem._model = ENUM_LOCKROI;
tagItem.rectf = maptoImgNewPos(graphicItem);
m_graphItemMapStruct[graphicItem] = tagItem;
}
}
}
return true;
}
void RoiScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (DrawTool::c_drawShape == selection)
{
QList<QGraphicsItem *> items = this->selectedItems();
GraphicsItem *item = 0;
QPointF mousePoint = maptoImgPos(mouseEvent->scenePos());
QString strPoint = QString("%1,%2").arg(mousePoint.x()).arg(mousePoint.y());
if (items.count() == 1)
item = qgraphicsitem_cast<GraphicsItem*>(items.first());
if (item != 0 && item->GetLock())
{
//return QGraphicsScene::mousePressEvent(mouseEvent);
}
if (item == NULL)
return QGraphicsScene::mousePressEvent(mouseEvent);
DrawTool * tool = DrawTool::findTool(DrawTool::c_drawShape);
if (tool)
tool->mousePressEvent(mouseEvent, this);
if (item != 0 && !item->GetCheckState())
{
//m_pRoiDlg->ResetItem(maptoImgPos(item->scenePos()), item->boundingRect().toRect());
emit sgResetItem(maptoImgPos(item->scenePos()), item->boundingRect().toRect());
m_graphItemMapStruct[item].rectf = maptoImgNewPos(item);
}
}
else
{
flag = 0;
DrawTool * tool = DrawTool::findTool(DrawTool::c_drawShape);
if (tool)
tool->mousePressEvent(mouseEvent, this);
}
}
void RoiScene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (DrawTool::c_drawShape == selection)
5 years ago
{
QList<QGraphicsItem *> items = this->selectedItems();
GraphicsItem *item = 0;
if (items.count() == 1)
item = qgraphicsitem_cast<GraphicsItem*>(items.first());
if (item == 0)
return;
if(item->GetLock())
return;
5 years ago
DrawTool * tool = DrawTool::findTool(DrawTool::c_drawShape);
m_tCurrentPos = maptoImgPos(mouseEvent->scenePos());
updateInfoLable();
if (tool)
tool->mouseMoveEvent(mouseEvent, this);
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
else
5 years ago
{
DrawTool * tool = DrawTool::findTool(DrawTool::c_drawShape);
m_tCurrentPos = maptoImgPos(mouseEvent->scenePos());
updateInfoLable();
5 years ago
if (tool)
tool->mouseMoveEvent(mouseEvent, this);
QGraphicsScene::mouseMoveEvent(mouseEvent);
}
}
5 years ago
void RoiScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
DrawTool * tool = DrawTool::findTool(DrawTool::c_drawShape);
if (tool)
tool->mouseReleaseEvent(mouseEvent, this);
5 years ago
QList<QGraphicsItem *> items = this->selectedItems();
GraphicsItem *item = 0;
5 years ago
if (items.count() == 1)
item = qgraphicsitem_cast<GraphicsItem*>(items.first());
5 years ago
if (item != 0)
{
//m_pRoiDlg->ResetItem(maptoImgPos(mouseEvent->scenePos()), item->boundingRect().toRect());
emit sgResetItem(maptoImgPos(mouseEvent->scenePos()), item->boundingRect().toRect());
m_graphItemMapStruct[item].rectf = maptoImgNewPos(item);
DrawTool::c_drawShape = selection;
QGraphicsScene::mouseReleaseEvent(mouseEvent);
5 years ago
}
5 years ago
}
void RoiScene::MoveSelectedItem(DirType type)
{
QList<QGraphicsItem *> items = this->selectedItems();
GraphicsItem *item = 0;
if (items.count() == 1)
item = qgraphicsitem_cast<GraphicsItem*>(items.first());
if (item != 0 && !item->GetCheckState())
{
switch (type)
{
case LEFT:
{
item->moveBy(-1, 0);
break;
}
case RIGHT:
{
item->moveBy(1, 0);
break;
}
case UP:
{
item->moveBy(0, -1);
break;
}
case DOWN:
{
item->moveBy(0, 1);
break;
}
}
emit sgResetItem(maptoImgPos(item->scenePos()),item->boundingRect().toRect());
//m_pRoiDlg->ResetItem(maptoImgPos(item->scenePos()),item->boundingRect().toRect());
m_graphItemMapStruct[item].rectf = maptoImgNewPos(item);
}
}
void RoiScene::SetSelectedItem(QPoint pos, QRect size)
{
QList<QGraphicsItem *> items = this->selectedItems();
GraphicsRectItem *item = 0;
if (items.count() == 1)
item = qgraphicsitem_cast<GraphicsRectItem*>(items.first());
if (item != 0 && !item->GetCheckState())
{
item->resetItem(pos, size);
}
}
void RoiScene::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Left:
{
MoveSelectedItem(LEFT);
break;
}
case Qt::Key_Right:
{
MoveSelectedItem(RIGHT);
break;
}
case Qt::Key_Up:
{
MoveSelectedItem(UP);
break;
}
case Qt::Key_Down:
{
MoveSelectedItem(DOWN);
break;
}
}
return QGraphicsScene::keyPressEvent(event);;
}
void RoiScene::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
QPointF p =event->pos();
QString s = QString("%1,%2").arg(p.x(), p.y());
}
void RoiScene::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
{
QPointF p = event->pos();
QString s = QString("%1,%2").arg(p.x(), p.y());
}
void RoiScene::hoverLeaveEvent(QGraphicsSceneHoverEvent * event)
{
QPointF p = event->pos();
QString s = QString("%1,%2").arg(p.x(), p.y());
}
void RoiScene::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
if (m_bDisableMenu)
return;
QList<QGraphicsItem *> items = this->selectedItems();
QPointF pPointF = maptoImgPos(event->scenePos());
bool bInItem = false;
GraphicsItem *ptmpItem = nullptr;
for (QMap<GraphicsItem*, tagROIParas>::iterator its = m_graphItemMapStruct.begin(); its != m_graphItemMapStruct.end(); ++its){
bInItem = its->rectf.contains(pPointF);
if (bInItem == true)
{
ptmpItem = its.key();
break;
}
}
if (bInItem == false)
return;
if (!ptmpItem)
return;
QMenu menu((QWidget*)(this->parent()));
QAction *deleteAction = NULL;
QAction *checkAction = NULL;
QAction *unCheckAction = NULL;
QAction *radiusFixAction = NULL;
QAction *radiusUnfixAction = NULL;
enumMemuModel tmpModel = m_graphItemMapStruct[ptmpItem]._model;
int itemCount = m_graphItemMapStruct.size();
if (itemCount == 1)
if (tmpModel == ENUM_UNBINDROI)
{
tmpModel = ENUM_LOCKROI;
m_graphItemMapStruct[ptmpItem]._model = tmpModel;
flag = 0;
}
if (tmpModel == ENUM_LOCKROI )//绑定
{
unCheckAction = menu.addAction(QObject::tr("解绑"));
unCheckAction->setObjectName("unCheckAction");
}
else if (tmpModel == ENUM_BINDROI)
{
if (!ptmpItem->GetLock())
{
checkAction = menu.addAction(QObject::tr("绑定"));
checkAction->setObjectName("checkAction");
}
unCheckAction = menu.addAction(QObject::tr("解除径向绑定"));
unCheckAction->setObjectName("unCheckAction");
}
else if (tmpModel == ENUM_UNBINDROI)
{
radiusFixAction = menu.addAction(QObject::tr("径向解锁"));
radiusFixAction->setObjectName("radiusUnfixAction");
}
else
{
if (items.size() > 0){
deleteAction = menu.addAction(QObject::tr("删除"));
deleteAction->setObjectName("deleteAction");
}
checkAction = menu.addAction(QObject::tr("绑定"));
checkAction->setObjectName("checkAction");
if (m_graphItemMapStruct.size() == 2){
if (tmpModel != ENUM_UNLOCKROI){
unCheckAction = menu.addAction(QObject::tr("解绑"));
unCheckAction->setObjectName("unCheckAction");
}
GraphicsItem *pFirst = m_graphItemMapStruct.firstKey();
GraphicsItem *pSecond = m_graphItemMapStruct.lastKey();
int firstVal = pFirst->boundingRect().width()*pFirst->boundingRect().height();
int secondVal = pSecond->boundingRect().width()*pSecond->boundingRect().height();
GraphicsItem *pSmallItem = NULL;
if (firstVal > secondVal)
{
pSmallItem = pSecond;
}
else
pSmallItem = pFirst;
if (pSmallItem == ptmpItem){
radiusFixAction = menu.addAction(QObject::tr("径向绑定"));
radiusFixAction->setObjectName("radiusFixAction");
}
}
}
QAction *selectedAction = menu.exec(event->screenPos());
if (selectedAction == NULL)
{
menu.clear();
return;
}
QString strObj = selectedAction->objectName();
if (strObj == "deleteAction")//删除
{
flag = 0;
QList<QGraphicsItem *> items = this->selectedItems();
GraphicsItem *item = 0;
if (items.count() == 1)
item = qgraphicsitem_cast<GraphicsItem*>(items.first());
if (item != 0 && !item->GetLock()){
if (m_graphItemMapStruct.contains(item))
m_graphItemMapStruct.remove(item);
removeItem(item);
delete item;
}
if (m_graphItemMapStruct.size() <= 1)
{
if (m_graphItemMapStruct.size() > 0){
if (m_graphItemMapStruct.first()._model == ENUM_UNBINDROI)
m_graphItemMapStruct.first()._model = ENUM_LOCKROI;
}
flag = 0;
}
updateLineItem();
}
else if (strObj == "checkAction")//绑定
{
flag = 0;
{
GraphicsItem *item = ptmpItem;//
if (item && !item->GetLock())
{
item->SetCheckState(true);
item->setState(SelectionHandleOff);
item->setFlag(QGraphicsItem::ItemIsMovable, false);
item->setFlag(QGraphicsItem::ItemIsSelectable, false);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
item->SetLock(true);
m_graphItemMapStruct[ptmpItem]._model = ENUM_LOCKROI;
}
}
updateLineItem();
}
else if (strObj == "radiusFixAction")//径向绑定
{
QPointF valveP = ptmpItem->scenePos();
if (m_graphItemMapStruct.size()==2)
{
for (QMap<GraphicsItem*, tagROIParas>::iterator its = m_graphItemMapStruct.begin(); its != m_graphItemMapStruct.end(); ++its)
{
GraphicsItem *item = its.key();
if (item)
{
if ((item->scenePos()) != valveP)
{
item->SetLock(true);
item->setState(SelectionHandleOff);
item->SetCheckState(true);
item->setFlag(QGraphicsItem::ItemIsMovable, false);
item->setFlag(QGraphicsItem::ItemIsSelectable, false);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
centerPoint =(item->scenePos());
m_graphItemMapStruct[item]._model = ENUM_LOCKROI;
}
else
{
item->SetCheckState(false);
item->SetLock(false);
item->setFlag(QGraphicsItem::ItemIsMovable, true);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
startPoint = valveP;
flag = 1;
m_graphItemMapStruct[item]._model = ENUM_BINDROI;
}
updateLineItem();
}
}
}
}
else if (strObj == "radiusUnfixAction")//径向解绑
{
flag = 1;
GraphicsItem *item = 0;
item = ptmpItem;//
if (item->GetCheckState()||item->GetLock())
{
startPoint = item->scenePos();
{
if (m_graphItemMapStruct.contains(item))
{
//item->setState(SelectionHandleOff);
item->setState(SelectionHandleActive);
item->setFlag(QGraphicsItem::ItemIsMovable, true);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
item->SetCheckState(false);
item->SetLock(false);
m_graphItemMapStruct[item]._model = ENUM_BINDROI;
updateLineItem();
}
}
}
}
else if (strObj == "unCheckAction")//解绑
{
flag = 0;
GraphicsItem *item = 0;
item = ptmpItem;
//if (item->GetLock()||item->GetCheckState())
{
if (ENUM_BINDROI == m_graphItemMapStruct[item]._model || ENUM_UNBINDROI == m_graphItemMapStruct[item]._model)
{
for (QMap<GraphicsItem *, tagROIParas>::iterator its = m_graphItemMapStruct.begin(); its != m_graphItemMapStruct.end(); ++its)
{
GraphicsItem *pItem = (its.key());
//pItem->setState(SelectionHandleOff);
pItem->setState(SelectionHandleActive);
pItem->setFlag(QGraphicsItem::ItemIsMovable, true);
pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);
pItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
pItem->SetCheckState(false);
pItem->SetLock(false);
its->_model = ENUM_UNLOCKROI;
}
}
else
{
if (m_graphItemMapStruct.contains(item))
{
//item->setState(SelectionHandleOff);
bool bulockflag = false;
if (m_graphItemMapStruct.size() == 2){
if (m_graphItemMapStruct[item]._model == ENUM_LOCKROI)
{
for (QMap<GraphicsItem *, tagROIParas>::iterator its = m_graphItemMapStruct.begin(); its != m_graphItemMapStruct.end(); ++its)
{
if ((*its)._model == ENUM_UNBINDROI)
{
bulockflag = true;
break;
}
}
}
}
item->setState(SelectionHandleActive);
item->setFlag(QGraphicsItem::ItemIsMovable, true);
item->setFlag(QGraphicsItem::ItemIsSelectable, true);
item->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
item->SetCheckState(false);
item->SetLock(false);
m_graphItemMapStruct[item]._model = ENUM_UNLOCKROI;
if (bulockflag == true)
{
for (QMap<GraphicsItem *, tagROIParas>::iterator its = m_graphItemMapStruct.begin(); its != m_graphItemMapStruct.end(); ++its)
{
GraphicsItem *pItem = (its.key());
//pItem->setState(SelectionHandleOff);
pItem->setState(SelectionHandleActive);
pItem->setFlag(QGraphicsItem::ItemIsMovable, true);
pItem->setFlag(QGraphicsItem::ItemIsSelectable, true);
pItem->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
pItem->SetCheckState(false);
pItem->SetLock(false);
its->_model = ENUM_UNLOCKROI;
}
}
}
}
}
updateLineItem();
}
QGraphicsScene::contextMenuEvent(event);
menu.clear();
update();
}
void RoiScene::scaleImg(qreal scale)
{
QTransform t;
t.scale(scale, scale);
mpGraphicsView->setTransform(t);
}
void RoiScene::updateInfoLable()
{
if (!mpShowImage)
{
return;
}
QRgb pixel = mpShowImage->pixel(m_tCurrentPos);
QColor rgb(pixel);
QColor hsv = rgb.toHsv();
QString infoStr("Position:[%1, %2] Color(RGB):[%3, %4, %5] (HSV):[%6, %7, %8]");
infoStr = infoStr.arg(m_tCurrentPos.x()).arg(m_tCurrentPos.y())
.arg(rgb.red()).arg(rgb.green()).arg(rgb.blue())
.arg(hsv.hue()).arg(hsv.saturation()).arg(hsv.value());
mpInfoLabel->setText(infoStr);
}
QRectF RoiScene::maptoImgNewPos(GraphicsItem *pItem)
{
if (pItem){//相对图像坐标转换
QPointF m_startPoint = maptoImgPos(QPointF(pItem->scenePos().x() - pItem->boundingRect().width()/2.0, \
pItem->scenePos().y() - pItem->boundingRect().height()/2.0));
QPointF endPoint = maptoImgPos(QPointF(pItem->scenePos().x() + pItem->boundingRect().width()/2.0, \
pItem->scenePos().y() + pItem->boundingRect().height()/2.0));
QRectF rectF(m_startPoint, endPoint);
return rectF;
}
return QRectF();
}
QPoint RoiScene::maptoImgPos(QPointF& iscenePos)
{
if (!m_pImageItem)
{
return QPoint();
}
return (m_pImageItem->mapFromScene(iscenePos) +
mImgPosOffSet + QPointF(-0.5, -0.5)).toPoint();
}
void RoiScene::InitShowImage(cv::Mat img)
{
if (!mpGraphicsView)
return;
5 years ago
if (img.empty())
return;
mpShowImage = ColossusBase::convMat2QImage(img);
if (!mpShowImage)
return;
clear();
m_pImageItem = addPixmap(QPixmap::fromImage(*mpShowImage));
5 years ago
mImgPosOffSet = QPoint(mpShowImage->width() / 2, mpShowImage->height() / 2);
m_pImageItem->setOffset(-mpShowImage->width() / 2.0, -mpShowImage->height() / 2.0);
m_pImageItem->setPos(0, 0);
mImgOriSize = mpShowImage->size();
qreal scale = std::min((qreal)mpGraphicsView->width() / mImgOriSize.width(), (qreal)mpGraphicsView->height() / mImgOriSize.height());
scaleImg(scale);
setSceneRect(-mImgOriSize.width() / 2, -mImgOriSize.height()/2, mImgOriSize.width(), mImgOriSize.height());
return;
}
void RoiScene::wheelEvent(QGraphicsSceneWheelEvent * wheelEvent)
{
if (!m_pImageItem)
{
return;
}
qreal numDegrees = wheelEvent->delta() / 120;
qreal newScale = mpGraphicsView->transform().m11();
newScale *= std::pow(1.1, numDegrees);
m_pImageItem->setTransformOriginPoint(0, 0);
scaleImg(newScale);
}
void RoiScene::zoomImage(bool bZoomIn)
{
if (!mpGraphicsView) {
return;
}
qreal degr = bZoomIn == false ? 1.2 : -1.2;
qreal scale = mpGraphicsView->transform().m11();
scale *= std::pow(1.1, degr);
QPointF center = mpGraphicsView->mapToScene(mpGraphicsView->width() / 2, mpGraphicsView->height() / 2);
QTransform t;
t.scale(scale, scale);
mpGraphicsView->resetTransform();
mpGraphicsView->setTransform(t);
mpGraphicsView->centerOn(center);
}
void RoiScene::scaleImageWithFixedViewSize()
{
if (!mpShowImage)
{
return;
}
QImage scaledImage;
//if (!imgScaleFunc())
{
scaledImage = mpShowImage->scaled(mpShowImage->width()*mFixedSizeScale, mpShowImage->height()*mFixedSizeScale,
Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
m_pImageItem->setPixmap(QPixmap::fromImage(scaledImage.scaled(mpShowImage->width(),
mpShowImage->height()), Qt::NoFormatConversion));
}
// else
// {
// scaledImage = mImgScaleFunc(*mpImg, mFixedSizeScale, Qt::SmoothTransformation);
// mpImgItem->setPixmap(QPixmap::fromImage(mImgScaleFunc(scaledImage,
// 1.0 / mFixedSizeScale, Qt::FastTransformation), Qt::NoFormatConversion));
// }
//emit imgScaledWithFixedSize(fixedSizeScale());
}
//用于判断Item是否超出screen边界
bool RoiScene::InImgRectangel(GraphicsItem *pItem)
{
if (!pItem)
return false;
QPointF m_startPoint = maptoImgPos(QPointF(pItem->scenePos().x() - pItem->boundingRect().width() / 2.0, \
pItem->scenePos().y() - pItem->boundingRect().height() / 2.0));
QPointF endPoint = maptoImgPos(QPointF(pItem->scenePos().x() + pItem->boundingRect().width() / 2.0, \
pItem->scenePos().y() + pItem->boundingRect().height() / 2.0));
if (m_startPoint.x() < 0 || m_startPoint.y() < 0)
return false;
if (endPoint.x() > mImgOriSize.width() || endPoint.y() > mImgOriSize.height())
return false;
mImgOriSize;
return true;
}
void RoiScene::updateLineItem()
{
if (flag == 1){
float alxs = 0.0;
QPoint mCenPoint = centerPoint.toPoint();
QPoint mValPoint = startPoint.toPoint();
alxs = (mCenPoint.y() - startPoint.y())*1.0 / (mCenPoint.x() - startPoint.x());
float bweight = mCenPoint.y() - alxs*mCenPoint.x();
QRectF rect = sceneRect();
QPoint mStart = QPoint(rect.x(), rect.y());
QPoint mEnd = QPoint(rect.x()+rect.width(), rect.y()+rect.height());
QPoint newPoint1, newPoint2;
float y1 = alxs*mStart.x() + bweight;
float x1 = (mStart.y() - bweight)*1.0 / alxs;
if (y1 > mEnd.y() || y1 < mStart.y())
{
float newY = alxs*x1 + bweight;
newPoint1 = QPoint(x1, newY);
}
else
{
float newX = mStart.x();
newPoint1 = QPoint(newX, y1);
}
float y2 = alxs*mEnd.x() + bweight;
float x2 = (mEnd.y() - bweight)*1.0 / alxs;
if (y2 > mEnd.y() || y2 < mStart.y())
{
float newY = alxs*x2 + bweight;
newPoint2 = QPoint(x2, newY);
}
else
{
float newX = mEnd.x();
newPoint2 = QPoint(newX, y2);
}
if (newPoint2.x() < mStart.x())
{
newPoint2.setY(mStart.y());
newPoint2.setX((mStart.y() - bweight) *1.0/ alxs);
}
QLine line(newPoint1, newPoint2);
QPen pen(Qt::blue, 2);
if (pLineItem)
{
removeItem(pLineItem);
pLineItem = NULL;
}
pLineItem = addLine(line,pen);
}
else
{
if (pLineItem){
removeItem(pLineItem);
pLineItem = NULL;
}
}
}
void RoiScene::updateTenItem(bool bFlag)
{
bUseTenFlag = bFlag;
if (bUseTenFlag){
QLine lineV(QPoint(0, -mImgOriSize.height() / 2), QPoint(0, mImgOriSize.height() / 2));
QLine lineH(QPoint(-mImgOriSize.width() / 2, 0), QPoint(mImgOriSize.width() / 2, 0));
QPen pen(Qt::green, 5);
if (pTenLineITemH)
{
removeItem(pTenLineITemH);
pTenLineITemH = NULL;
}
if (pTenLineITemV)
{
removeItem(pTenLineITemV);
pTenLineITemV = NULL;
}
pTenLineITemH = addLine(lineH, pen);
pTenLineITemV = addLine(lineV, pen);
}
else
{
if (pTenLineITemH)
{
removeItem(pTenLineITemH);
pTenLineITemH = NULL;
}
if (pTenLineITemV)
{
removeItem(pTenLineITemV);
pTenLineITemV = NULL;
}
}
update();
}