New functions with QT GUI:

- fix zoom offset of 0.5
- fix CMake problem with QT detection
- Code optimisation
This commit is contained in:
Yannick Verdie 2010-06-25 17:19:11 +00:00
parent 2dcad1eb81
commit 6314cd28cf
3 changed files with 125 additions and 212 deletions

View File

@ -498,9 +498,19 @@ set(HAVE_QT 0)
set(HAVE_QT_OPENGL 0) set(HAVE_QT_OPENGL 0)
set(AUTOSWITCH_QT 0)#uses to disable QT set(AUTOSWITCH_QT 0)#uses to disable QT
#set the booleans
find_package(Qt4)# QUIET) find_package(Qt4)# QUIET)
if (QT4_FOUND AND AUTOSWITCH_QT) if (QT4_FOUND AND AUTOSWITCH_QT)
set(WITH_QT ON CACHE BOOL "Build with QT Backend support") set(WITH_QT ON CACHE BOOL "Build with QT Backend support")
find_package (OPENGL QUIET)
if (QT_QTOPENGL_FOUND AND OPENGL_FOUND)
set(WITH_QT_OPENGL ON CACHE BOOL "Add OpenGL extention to QT")
endif()
endif()
#test if QT Bool is true
if (WITH_QT)
set(HAVE_QT 1) set(HAVE_QT 1)
add_definitions(-DHAVE_QT=1) add_definitions(-DHAVE_QT=1)
@ -515,11 +525,11 @@ if (QT4_FOUND AND AUTOSWITCH_QT)
endif() endif()
endif() endif()
#if (QT_QTOPENGL_FOUND) if (WITH_QT_OPENGL)
# set(WITH_QT_OPENGL ON CACHE BOOL "add OpenGL extention to QT") message(STATUS "OpenGL for QT enable")
# set(HAVE_QT_OPENGL 1) set(HAVE_QT_OPENGL 1)
# add_definitions(-DOPENCV_GL=1) add_definitions(-DOPENCV_GL=1)
#endif() endif()
endif() endif()
############################### TBB ################################ ############################### TBB ################################

View File

@ -436,7 +436,6 @@ CV_IMPL void cvShowImage( const char* name, const CvArr* arr )
//----------OBJECT---------------- //----------OBJECT----------------
GuiReceiver::GuiReceiver() : _bTimeOut(false) GuiReceiver::GuiReceiver() : _bTimeOut(false)
@ -797,11 +796,11 @@ CvWindow::CvWindow(QString arg, int arg2)
layout->setSpacing(0); layout->setSpacing(0);
layout->setMargin(0); layout->setMargin(0);
layout->addWidget(myview,Qt::AlignCenter); layout->addWidget(myview,Qt::AlignCenter);
//layout->addStretch(0);
if (flags == CV_WINDOW_AUTOSIZE) if (flags == CV_WINDOW_AUTOSIZE)
layout->setSizeConstraint(QLayout::SetFixedSize); layout->setSizeConstraint(QLayout::SetFixedSize);
//now status bar //now status bar
myBar = new QStatusBar; myBar = new QStatusBar;
myBar->setSizeGripEnabled(false); myBar->setSizeGripEnabled(false);
@ -933,6 +932,8 @@ ViewPort::ViewPort(CvWindow* arg, int arg2, bool arg3)
setupViewport(centralWidget); setupViewport(centralWidget);
setContentsMargins(0,0,0,0); setContentsMargins(0,0,0,0);
//this->setFrameStyle(0);
//this->setFrameShape(QFrame::Box);
setObjectName(QString::fromUtf8("graphicsView")); setObjectName(QString::fromUtf8("graphicsView"));
timerDisplay = new QTimer(this); timerDisplay = new QTimer(this);
@ -1048,7 +1049,10 @@ void ViewPort::updateImage(void* arr)
ratioX=float(image2Draw_ipl->width)/float(width()); ratioX=float(image2Draw_ipl->width)/float(width());
ratioY=float(image2Draw_ipl->height)/float(height()); ratioY=float(image2Draw_ipl->height)/float(height());
//centralWidget->myBar_msg->setMaximumWidth(width());
if (keepRatio)
deltaOffset = computeOffset();
updateGeometry(); updateGeometry();
} }
@ -1063,6 +1067,16 @@ void ViewPort::setMouseCallBack(CvMouseCallback m, void* param)
on_mouse_param = param; on_mouse_param = param;
} }
int ViewPort::heightForWidth(int w) const
{
return w*float(image2Draw_ipl->height)/float(image2Draw_ipl->width);
}
bool ViewPort::hasHeightForWidth() const
{
return true;
}
void ViewPort::controlImagePosition() void ViewPort::controlImagePosition()
{ {
qreal left, top, right, bottom; qreal left, top, right, bottom;
@ -1095,22 +1109,6 @@ void ViewPort::controlImagePosition()
bottom = sizeImage.height(); bottom = sizeImage.height();
} }
/*
if (keepRatio)
{
cout<<"here"<<endl;
QSize t1(image2Draw_ipl->width, image2Draw_ipl->height);
QSize delta(width(),height());
t1.scale(delta.width(), delta.height(), Qt::KeepAspectRatio);
delta = (delta - t1)/2;
//left += delta.width();
//right += delta.width();
//top += delta.height();
//bottom += delta.height();
matrixWorld.translate(delta.width(),delta.height());
}
*/
//save corner position //save corner position
positionCorners.setTopLeft(QPoint(left,top)); positionCorners.setTopLeft(QPoint(left,top));
positionCorners.setBottomRight(QPoint(right,bottom)); positionCorners.setBottomRight(QPoint(right,bottom));
@ -1164,53 +1162,11 @@ void ViewPort::wheelEvent(QWheelEvent *event)
void ViewPort::mousePressEvent(QMouseEvent *event) void ViewPort::mousePressEvent(QMouseEvent *event)
{ {
int cv_event = -1, flags = 0; int cv_event = -1, flags = 0;
QPoint pt = event->pos()+deltaOffset; QPoint pt = event->pos();
//icvmouseHandler: pass parameters for cv_event, flags
switch(event->modifiers()) icvmouseHandler(event, mouse_down, cv_event, flags);
{ icvmouseProcessing(QPointF(pt), cv_event, flags);
case Qt::ShiftModifier:
flags = CV_EVENT_FLAG_SHIFTKEY;
break;
case Qt::ControlModifier:
flags = CV_EVENT_FLAG_CTRLKEY;
break;
case Qt::AltModifier:
flags = CV_EVENT_FLAG_ALTKEY;
break;
case Qt::NoModifier :
break;
case Qt::MetaModifier:
break;
case Qt::KeypadModifier:
break;
default:;
}
switch(event->button())
{
case Qt::LeftButton:
cv_event = CV_EVENT_LBUTTONDOWN;
flags |= CV_EVENT_FLAG_LBUTTON;
break;
case Qt::RightButton:
cv_event = CV_EVENT_RBUTTONDOWN;
flags |= CV_EVENT_FLAG_RBUTTON;
break;
case Qt::MidButton:
cv_event = CV_EVENT_MBUTTONDOWN;
flags |= CV_EVENT_FLAG_MBUTTON;
break;
default:;
}
//to convert mouse coordinate
matrixWorld_inv.map(pt.x(),pt.y(),&mouseCoordinate.rx(),&mouseCoordinate.ry());
mouseCoordinate.rx()*=ratioX;
mouseCoordinate.ry()*=ratioY;
if (on_mouse)
on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param );
if (matrixWorld.m11()>1) if (matrixWorld.m11()>1)
{ {
@ -1225,53 +1181,11 @@ void ViewPort::mouseReleaseEvent(QMouseEvent *event)
{ {
int cv_event = -1, flags = 0; int cv_event = -1, flags = 0;
QPoint pt = event->pos()+deltaOffset; QPoint pt = event->pos();
switch(event->modifiers())
{
case Qt::ShiftModifier:
flags = CV_EVENT_FLAG_SHIFTKEY;
break;
case Qt::ControlModifier:
flags = CV_EVENT_FLAG_CTRLKEY;
break;
case Qt::AltModifier:
flags = CV_EVENT_FLAG_ALTKEY;
break;
case Qt::NoModifier :
break;
case Qt::MetaModifier:
break;
case Qt::KeypadModifier:
break;
default:;
}
switch(event->button())
{
case Qt::LeftButton:
cv_event = CV_EVENT_LBUTTONUP;
flags |= CV_EVENT_FLAG_LBUTTON;
break;
case Qt::RightButton:
cv_event = CV_EVENT_RBUTTONUP;
flags |= CV_EVENT_FLAG_RBUTTON;
break;
case Qt::MidButton:
cv_event = CV_EVENT_MBUTTONUP;
flags |= CV_EVENT_FLAG_MBUTTON;
break;
default:;
}
//to convert mouse coordinate
matrixWorld_inv.map(pt.x(),pt.y(),&mouseCoordinate.rx(),&mouseCoordinate.ry());
mouseCoordinate.rx()*=ratioX;
mouseCoordinate.ry()*=ratioY;
if (on_mouse)
on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param );
//icvmouseHandler: pass parameters for cv_event, flags
icvmouseHandler(event, mouse_up, cv_event, flags);
icvmouseProcessing(QPointF(pt), cv_event, flags);
if (matrixWorld.m11()>1) if (matrixWorld.m11()>1)
setCursor(Qt::OpenHandCursor); setCursor(Qt::OpenHandCursor);
@ -1282,52 +1196,11 @@ void ViewPort::mouseReleaseEvent(QMouseEvent *event)
void ViewPort::mouseDoubleClickEvent(QMouseEvent *event) void ViewPort::mouseDoubleClickEvent(QMouseEvent *event)
{ {
int cv_event = -1, flags = 0; int cv_event = -1, flags = 0;
QPoint pt = event->pos()+deltaOffset; QPoint pt = event->pos();
switch(event->modifiers())
{
case Qt::ShiftModifier:
flags = CV_EVENT_FLAG_SHIFTKEY;
break;
case Qt::ControlModifier:
flags = CV_EVENT_FLAG_CTRLKEY;
break;
case Qt::AltModifier:
flags = CV_EVENT_FLAG_ALTKEY;
break;
case Qt::NoModifier :
break;
case Qt::MetaModifier:
break;
case Qt::KeypadModifier:
break;
default:;
}
switch(event->button())
{
case Qt::LeftButton:
cv_event = CV_EVENT_LBUTTONDBLCLK;
flags |= CV_EVENT_FLAG_LBUTTON;
break;
case Qt::RightButton:
cv_event = CV_EVENT_RBUTTONDBLCLK;
flags |= CV_EVENT_FLAG_RBUTTON;
break;
case Qt::MidButton:
cv_event = CV_EVENT_MBUTTONDBLCLK;
flags |= CV_EVENT_FLAG_MBUTTON;
break;
default:;
}
//to convert mouse coordinate
matrixWorld_inv.map(pt.x(),pt.y(),&mouseCoordinate.rx(),&mouseCoordinate.ry());
mouseCoordinate.rx()*=ratioX;
mouseCoordinate.ry()*=ratioY;
if (on_mouse)
on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param );
//icvmouseHandler: pass parameters for cv_event, flags
icvmouseHandler(event, mouse_dbclick, cv_event, flags);
icvmouseProcessing(QPointF(pt), cv_event, flags);
QWidget::mouseDoubleClickEvent(event); QWidget::mouseDoubleClickEvent(event);
} }
@ -1335,51 +1208,11 @@ void ViewPort::mouseDoubleClickEvent(QMouseEvent *event)
void ViewPort::mouseMoveEvent(QMouseEvent *event) void ViewPort::mouseMoveEvent(QMouseEvent *event)
{ {
int cv_event = -1, flags = 0; int cv_event = -1, flags = 0;
QPoint pt = event->pos()-deltaOffset; QPoint pt = event->pos();
switch(event->modifiers())
{
case Qt::ShiftModifier:
flags = CV_EVENT_FLAG_SHIFTKEY;
break;
case Qt::ControlModifier:
flags = CV_EVENT_FLAG_CTRLKEY;
break;
case Qt::AltModifier:
flags = CV_EVENT_FLAG_ALTKEY;
break;
case Qt::NoModifier :
break;
case Qt::MetaModifier:
break;
case Qt::KeypadModifier:
break;
default:;
}
cv_event = CV_EVENT_MOUSEMOVE;
switch(event->buttons())
{
case Qt::LeftButton:
flags |= CV_EVENT_FLAG_LBUTTON;
break;
case Qt::RightButton:
flags |= CV_EVENT_FLAG_RBUTTON;
break;
case Qt::MidButton:
flags |= CV_EVENT_FLAG_MBUTTON;
break;
default:;
}
//to convert mouse coordinate
matrixWorld_inv.map(pt.x(),pt.y(),&mouseCoordinate.rx(),&mouseCoordinate.ry());
mouseCoordinate.rx()*=ratioX;
mouseCoordinate.ry()*=ratioY;
if (on_mouse)
on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param );
//icvmouseHandler: pass parameters for cv_event, flags
icvmouseHandler(event, mouse_move, cv_event, flags);
icvmouseProcessing(QPointF(pt), cv_event, flags);
if (matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton) if (matrixWorld.m11()>1 && event->buttons() == Qt::LeftButton)
@ -1390,17 +1223,71 @@ void ViewPort::mouseMoveEvent(QMouseEvent *event)
moveView(dxy); moveView(dxy);
} }
//I update the statusbar here because if the user does a cvWaitkey(0) (like with inpaint.cpp) //I update the statusbar here because if the user does a cvWaitkey(0) (like with inpaint.cpp)
//the status bar will be repaint only when a click occurs. //the status bar will only be repaint when a click occurs.
viewport()->update(); viewport()->update();
QWidget::mouseMoveEvent(event); QWidget::mouseMoveEvent(event);
} }
//up, down, dclick, move
void ViewPort::icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags)
{
switch(event->modifiers())
{
case Qt::ShiftModifier:
flags = CV_EVENT_FLAG_SHIFTKEY;
break;
case Qt::ControlModifier:
flags = CV_EVENT_FLAG_CTRLKEY;
break;
case Qt::AltModifier:
flags = CV_EVENT_FLAG_ALTKEY;
break;
case Qt::NoModifier :
break;
case Qt::MetaModifier:
break;
case Qt::KeypadModifier:
break;
default:;
}
switch(event->button())
{
case Qt::LeftButton:
cv_event = tableMouseButtons[category][0];
flags |= CV_EVENT_FLAG_LBUTTON;
break;
case Qt::RightButton:
cv_event = tableMouseButtons[category][1];
flags |= CV_EVENT_FLAG_RBUTTON;
break;
case Qt::MidButton:
cv_event = tableMouseButtons[category][2];
flags |= CV_EVENT_FLAG_MBUTTON;
break;
default:;
}
}
void ViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags)
{
//to convert mouse coordinate
qreal pfx, pfy;
matrixWorld_inv.map(pt.x(),pt.y(),&pfx,&pfy);
mouseCoordinate.rx()=floor((pfx-deltaOffset.x())*ratioX);
mouseCoordinate.ry()=floor((pfy-deltaOffset.y())*ratioY);
if (on_mouse)
on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param );
}
QSize ViewPort::sizeHint() const QSize ViewPort::sizeHint() const
{ {
//return QSize(width(),width()/2);
if(image2Draw_ipl) if(image2Draw_ipl)
{ {
return QSize(image2Draw_ipl->width,image2Draw_ipl->height); return QSize(image2Draw_ipl->width,image2Draw_ipl->height);
@ -1424,12 +1311,13 @@ void ViewPort::resizeEvent ( QResizeEvent *event)
ratioX=float(image2Draw_ipl->width)/float(width()); ratioX=float(image2Draw_ipl->width)/float(width());
ratioY=float(image2Draw_ipl->height)/float(height()); ratioY=float(image2Draw_ipl->height)/float(height());
if(keepRatio)
deltaOffset = computeOffset();
return QGraphicsView::resizeEvent(event); return QGraphicsView::resizeEvent(event);
} }
void ViewPort::paintEvent(QPaintEvent* event) void ViewPort::paintEvent(QPaintEvent* event)
{ {
QPainter myPainter(viewport()); QPainter myPainter(viewport());
@ -1479,11 +1367,12 @@ void ViewPort::draw2D(QPainter *painter)
void ViewPort::drawStatusBar() void ViewPort::drawStatusBar()
{ {
//centralWidget->myBar_msg->setMaximumWidth(width());
//CvScalar value = cvGet2D(image2Draw_ipl,mouseCoordinate.y(),mouseCoordinate.x()); //CvScalar value = cvGet2D(image2Draw_ipl,mouseCoordinate.y(),mouseCoordinate.x());
QRgb rgbValue = image2Draw_qt.pixel(mouseCoordinate); QRgb rgbValue = image2Draw_qt.pixel(mouseCoordinate);
centralWidget->myBar_msg->setText(tr("<font color='black'>Coordinate: %1x%2 ~ </font>") centralWidget->myBar_msg->setText(tr("<font color='black'>Coordinate: %1x%2 ~ </font>")
.arg(mouseCoordinate.x()) .arg(mouseCoordinate.x())
.arg(mouseCoordinate.y())+ .arg(mouseCoordinate.y())+
tr("<font color='red'>R:%3 </font>").arg(qRed(rgbValue))+//.arg(value.val[0])+ tr("<font color='red'>R:%3 </font>").arg(qRed(rgbValue))+//.arg(value.val[0])+
tr("<font color='green'>G:%4 </font>").arg(qGreen(rgbValue))+//.arg(value.val[1])+ tr("<font color='green'>G:%4 </font>").arg(qGreen(rgbValue))+//.arg(value.val[1])+
tr("<font color='blue'>B:%5</font>").arg(qBlue(rgbValue))//.arg(value.val[2]) tr("<font color='blue'>B:%5</font>").arg(qBlue(rgbValue))//.arg(value.val[2])

View File

@ -180,6 +180,15 @@ private:
enum type_mouse_event {mouse_up = 0, mouse_down = 1, mouse_dbclick = 2, mouse_move = 3};
static const int tableMouseButtons[][3]={
{CV_EVENT_LBUTTONUP,CV_EVENT_RBUTTONUP,CV_EVENT_MBUTTONUP}, //mouse_up
{CV_EVENT_LBUTTONDOWN,CV_EVENT_RBUTTONDOWN,CV_EVENT_MBUTTONDOWN}, //mouse_down
{CV_EVENT_LBUTTONDBLCLK,CV_EVENT_RBUTTONDBLCLK,CV_EVENT_MBUTTONDBLCLK}, //mouse_dbclick
{CV_EVENT_MOUSEMOVE,CV_EVENT_MOUSEMOVE,CV_EVENT_MOUSEMOVE} //mouse_move
};
class ViewPort : public QGraphicsView class ViewPort : public QGraphicsView
{ {
Q_OBJECT Q_OBJECT
@ -207,6 +216,8 @@ public slots:
void siftWindowOnUp() ; void siftWindowOnUp() ;
void siftWindowOnDown(); void siftWindowOnDown();
void resizeEvent ( QResizeEvent * ); void resizeEvent ( QResizeEvent * );
int heightForWidth(int w) const;
bool hasHeightForWidth() const;
private: private:
Qt::AspectRatioMode modeRatio; Qt::AspectRatioMode modeRatio;
@ -244,6 +255,8 @@ private:
void draw2D(QPainter *painter); void draw2D(QPainter *painter);
void drawStatusBar(); void drawStatusBar();
void controlImagePosition(); void controlImagePosition();
void icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags);
void icvmouseProcessing(QPointF pt, int cv_event, int flags);
#if defined(OPENCV_GL) #if defined(OPENCV_GL)
void draw3D(); void draw3D();
@ -257,6 +270,7 @@ private slots:
}; };
//here css for trackbar //here css for trackbar
/* from http://thesmithfam.org/blog/2010/03/10/fancy-qslider-stylesheet */ /* from http://thesmithfam.org/blog/2010/03/10/fancy-qslider-stylesheet */
static const QString str_Trackbar_css = QString("") static const QString str_Trackbar_css = QString("")