New functions with QT GUI
use inv matrix for Zoom in/out for better result Fix some bugs with zoom/panning
This commit is contained in:
parent
78a659287f
commit
386496863c
@ -825,10 +825,8 @@ ViewPort::ViewPort(QWidget* arg, int arg2)
|
|||||||
timerDisplay->setSingleShot(true);
|
timerDisplay->setSingleShot(true);
|
||||||
connect(timerDisplay, SIGNAL(timeout()), this, SLOT(stopDisplayInfo()));
|
connect(timerDisplay, SIGNAL(timeout()), this, SLOT(stopDisplayInfo()));
|
||||||
drawInfo = false;
|
drawInfo = false;
|
||||||
previousFactor = 1;
|
|
||||||
previousCenter = QPointF(0,0);
|
|
||||||
previousDelta = QPointF(0,0);
|
|
||||||
positionGrabbing = QPointF(0,0);
|
positionGrabbing = QPointF(0,0);
|
||||||
|
positionCorners = QRect(0,0,size().width(),size().height());
|
||||||
|
|
||||||
if (mode == CV_MODE_OPENGL)
|
if (mode == CV_MODE_OPENGL)
|
||||||
{
|
{
|
||||||
@ -905,23 +903,58 @@ void ViewPort::setMouseCallBack(CvMouseCallback m, void* param)
|
|||||||
void ViewPort::controlImagePosition()
|
void ViewPort::controlImagePosition()
|
||||||
{
|
{
|
||||||
qreal left, top, right, bottom;
|
qreal left, top, right, bottom;
|
||||||
matrixWorld.map(0,0,&left,&top);
|
|
||||||
if (left < 0)left = 0;
|
|
||||||
if (top < 0)top = 0;
|
|
||||||
if (left>0 || top > 0)
|
|
||||||
matrixWorld.translate(-left,-top);
|
|
||||||
|
|
||||||
|
|
||||||
|
matrixWorld.map(0,0,&left,&top);
|
||||||
|
if (left > 0)
|
||||||
|
{
|
||||||
|
matrixWorld.translate(-left,0);
|
||||||
|
left = 0;
|
||||||
|
}
|
||||||
|
if (top > 0)
|
||||||
|
{
|
||||||
|
matrixWorld.translate(0,-top);
|
||||||
|
top = 0;
|
||||||
|
}
|
||||||
|
positionCorners.setTopLeft(QPoint(left,top));
|
||||||
|
|
||||||
QSize sizeImage = size();
|
QSize sizeImage = size();
|
||||||
matrixWorld.map(sizeImage.width(),sizeImage.height(),&right,&bottom);
|
matrixWorld.map(sizeImage.width(),sizeImage.height(),&right,&bottom);
|
||||||
if (right > sizeImage.width()) right = sizeImage.width();
|
if (right < sizeImage.width())
|
||||||
if (bottom > sizeImage.height()) bottom = sizeImage.height();
|
{
|
||||||
if (right < sizeImage.width() || bottom < sizeImage.height())
|
matrixWorld.translate(sizeImage.width()-right,0);
|
||||||
matrixWorld.translate(sizeImage.width()-right,sizeImage.height()-bottom);
|
right = sizeImage.width();
|
||||||
|
}
|
||||||
|
if (bottom < sizeImage.height())
|
||||||
|
{
|
||||||
|
matrixWorld.translate(0,sizeImage.height()-bottom);
|
||||||
|
bottom = sizeImage.height();
|
||||||
|
}
|
||||||
|
positionCorners.setBottomRight(QPoint(right,bottom));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ViewPort::scaleView(qreal factor,QPointF center)
|
void ViewPort::scaleView(qreal factor,QPointF center)
|
||||||
{
|
{
|
||||||
|
factor/=5;//-0.1 <-> 0.1
|
||||||
|
factor+=1;//0.9 <-> 1.1
|
||||||
|
|
||||||
|
if (matrixWorld.m11()==1 && factor < 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (matrixWorld.m11()*factor<1)
|
||||||
|
factor = 1/matrixWorld.m11();
|
||||||
|
|
||||||
|
//inverse the transform
|
||||||
|
int a, b;
|
||||||
|
matrixWorld.inverted().map(center.x(),center.y(),&a,&b);;
|
||||||
|
|
||||||
|
matrixWorld.translate(a-factor*a,b-factor*b);
|
||||||
|
matrixWorld.scale(factor,factor);
|
||||||
|
|
||||||
|
controlImagePosition();
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
factor += previousFactor;
|
factor += previousFactor;
|
||||||
if (factor < 1 || factor > 100)
|
if (factor < 1 || factor > 100)
|
||||||
return;
|
return;
|
||||||
@ -929,7 +962,7 @@ void ViewPort::scaleView(qreal factor,QPointF center)
|
|||||||
center= (center-previousCenter)/previousFactor + previousCenter;//move to global coordinate
|
center= (center-previousCenter)/previousFactor + previousCenter;//move to global coordinate
|
||||||
QPointF delta = QPointF(center-center*factor);
|
QPointF delta = QPointF(center-center*factor);
|
||||||
|
|
||||||
matrixWorld.reset ();
|
//matrixWorld.reset ();
|
||||||
matrixWorld.translate(delta.x(),delta.y());//newCenter.x(),newCenter.y());
|
matrixWorld.translate(delta.x(),delta.y());//newCenter.x(),newCenter.y());
|
||||||
matrixWorld.scale(factor,factor);
|
matrixWorld.scale(factor,factor);
|
||||||
|
|
||||||
@ -939,7 +972,9 @@ void ViewPort::scaleView(qreal factor,QPointF center)
|
|||||||
//previousDelta = delta;
|
//previousDelta = delta;
|
||||||
previousFactor = factor;
|
previousFactor = factor;
|
||||||
|
|
||||||
if (previousFactor>1)
|
*/
|
||||||
|
|
||||||
|
if (matrixWorld.m11()>1)
|
||||||
setCursor(Qt::OpenHandCursor);
|
setCursor(Qt::OpenHandCursor);
|
||||||
else
|
else
|
||||||
unsetCursor();
|
unsetCursor();
|
||||||
@ -999,7 +1034,7 @@ void ViewPort::mousePressEvent(QMouseEvent *event)
|
|||||||
on_mouse( cv_event, pt.x(), pt.y(), flags, on_mouse_param );
|
on_mouse( cv_event, pt.x(), pt.y(), flags, on_mouse_param );
|
||||||
|
|
||||||
|
|
||||||
if (previousFactor>1)
|
if (matrixWorld.m11()>1)
|
||||||
{
|
{
|
||||||
setCursor(Qt::ClosedHandCursor);
|
setCursor(Qt::ClosedHandCursor);
|
||||||
positionGrabbing = event->pos();
|
positionGrabbing = event->pos();
|
||||||
@ -1055,7 +1090,7 @@ void ViewPort::mouseReleaseEvent(QMouseEvent *event)
|
|||||||
if (on_mouse)
|
if (on_mouse)
|
||||||
on_mouse( cv_event, pt.x(), pt.y(), flags, on_mouse_param );
|
on_mouse( cv_event, pt.x(), pt.y(), flags, on_mouse_param );
|
||||||
|
|
||||||
if (previousFactor>1)
|
if (matrixWorld.m11()>1)
|
||||||
setCursor(Qt::OpenHandCursor);
|
setCursor(Qt::OpenHandCursor);
|
||||||
|
|
||||||
QWidget::mouseReleaseEvent(event);
|
QWidget::mouseReleaseEvent(event);
|
||||||
@ -1140,15 +1175,15 @@ void ViewPort::mouseMoveEvent(QMouseEvent *event)
|
|||||||
on_mouse( cv_event, pt.x(), pt.y(), flags, on_mouse_param );
|
on_mouse( cv_event, pt.x(), pt.y(), flags, on_mouse_param );
|
||||||
|
|
||||||
|
|
||||||
if (previousFactor>1 && event->buttons() == Qt::LeftButton)
|
if (matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton)
|
||||||
{
|
{
|
||||||
QPointF dxy = (pt - positionGrabbing)/previousFactor;
|
QPointF dxy = (pt - positionGrabbing)/matrixWorld.m11();
|
||||||
|
|
||||||
positionGrabbing = event->pos();
|
positionGrabbing = event->pos();
|
||||||
|
|
||||||
matrixWorld.translate(dxy.x(),dxy.y());
|
matrixWorld.translate(dxy.x(),dxy.y());
|
||||||
controlImagePosition();
|
controlImagePosition();
|
||||||
}
|
}
|
||||||
|
|
||||||
QWidget::mouseMoveEvent(event);
|
QWidget::mouseMoveEvent(event);
|
||||||
}
|
}
|
||||||
@ -1293,4 +1328,3 @@ void ViewPort::draw3D()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -168,20 +168,17 @@ public:
|
|||||||
void startDisplayInfo(QString text, int delayms);
|
void startDisplayInfo(QString text, int delayms);
|
||||||
void setMouseCallBack(CvMouseCallback m, void* param);
|
void setMouseCallBack(CvMouseCallback m, void* param);
|
||||||
|
|
||||||
QTransform matrixWorld;
|
|
||||||
qreal scaleFactor;//for zoom int/out
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
//reference:
|
//reference:
|
||||||
//http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
|
//http://www.qtcentre.org/wiki/index.php?title=QGraphicsView:_Smooth_Panning_and_Zooming
|
||||||
//http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html
|
//http://doc.qt.nokia.com/4.6/gestures-imagegestures-imagewidget-cpp.html
|
||||||
void scaleView(qreal scaleFactor, QPointF center);
|
void scaleView(qreal scaleFactor, QPointF center);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QPointF previousCenter ;
|
|
||||||
qreal previousFactor;//for zoom int/out
|
|
||||||
QPointF previousDelta;
|
|
||||||
QPointF positionGrabbing;
|
QPointF positionGrabbing;
|
||||||
|
QRect positionCorners;
|
||||||
|
QTransform matrixWorld;
|
||||||
|
|
||||||
CvMouseCallback on_mouse;
|
CvMouseCallback on_mouse;
|
||||||
void* on_mouse_param;
|
void* on_mouse_param;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user