New functions with QT GUI:
authorYannick Verdie <no@email>
Fri, 25 Jun 2010 17:19:11 +0000 (17:19 +0000)
committerYannick Verdie <no@email>
Fri, 25 Jun 2010 17:19:11 +0000 (17:19 +0000)
- fix zoom offset of 0.5
- fix CMake problem with QT detection
- Code optimisation

CMakeLists.txt
modules/highgui/src/window_QT.cpp
modules/highgui/src/window_QT.h

index cd3cb6b..97cf0c5 100644 (file)
@@ -498,9 +498,19 @@ set(HAVE_QT 0)
 set(HAVE_QT_OPENGL 0)\r
 set(AUTOSWITCH_QT 0)#uses to disable QT\r
 \r
+#set the booleans\r
 find_package(Qt4)# QUIET)\r
 if (QT4_FOUND AND AUTOSWITCH_QT)\r
     set(WITH_QT ON CACHE BOOL "Build with QT Backend support")\r
+    find_package (OPENGL QUIET)\r
+    if (QT_QTOPENGL_FOUND AND OPENGL_FOUND)\r
+    set(WITH_QT_OPENGL ON CACHE BOOL "Add OpenGL extention to QT")\r
+    endif()\r
+endif()\r
+    \r
+    \r
+#test if QT Bool is true\r
+if (WITH_QT)\r
     set(HAVE_QT 1)\r
     add_definitions(-DHAVE_QT=1)\r
 \r
@@ -515,11 +525,11 @@ if (QT4_FOUND AND AUTOSWITCH_QT)
                endif()\r
     endif()\r
     \r
-    #if (QT_QTOPENGL_FOUND)\r
-    #    set(WITH_QT_OPENGL ON CACHE BOOL "add OpenGL extention to QT")\r
-    #    set(HAVE_QT_OPENGL 1)\r
-    #    add_definitions(-DOPENCV_GL=1)\r
-    #endif()\r
+    if (WITH_QT_OPENGL)\r
+    message(STATUS "OpenGL for QT enable")\r
+        set(HAVE_QT_OPENGL 1)\r
+        add_definitions(-DOPENCV_GL=1)\r
+    endif()\r
 endif()\r
     \r
 ############################### TBB ################################\r
index 4fcb779..56f2d41 100755 (executable)
@@ -436,7 +436,6 @@ CV_IMPL void cvShowImage( const char* name, const CvArr* arr )
 
 
 
-
 //----------OBJECT----------------
 
 GuiReceiver::GuiReceiver() : _bTimeOut(false)
@@ -797,11 +796,11 @@ CvWindow::CvWindow(QString arg, int arg2)
     layout->setSpacing(0);
     layout->setMargin(0);
     layout->addWidget(myview,Qt::AlignCenter);
+    //layout->addStretch(0);
 
     if (flags == CV_WINDOW_AUTOSIZE)
         layout->setSizeConstraint(QLayout::SetFixedSize);
 
-
     //now status bar
     myBar = new QStatusBar;
     myBar->setSizeGripEnabled(false);
@@ -933,6 +932,8 @@ ViewPort::ViewPort(CvWindow* arg, int arg2, bool arg3)
 
     setupViewport(centralWidget);
     setContentsMargins(0,0,0,0);
+    //this->setFrameStyle(0);
+    //this->setFrameShape(QFrame::Box);
 
     setObjectName(QString::fromUtf8("graphicsView"));
     timerDisplay = new QTimer(this);
@@ -1048,7 +1049,10 @@ void ViewPort::updateImage(void* arr)
 
         ratioX=float(image2Draw_ipl->width)/float(width());
         ratioY=float(image2Draw_ipl->height)/float(height());
-        //centralWidget->myBar_msg->setMaximumWidth(width());
+
+        if (keepRatio)
+            deltaOffset = computeOffset();
+
         updateGeometry();
     }
 
@@ -1063,6 +1067,16 @@ void ViewPort::setMouseCallBack(CvMouseCallback m, void* 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()
 {
     qreal left, top, right, bottom;
@@ -1095,22 +1109,6 @@ void ViewPort::controlImagePosition()
         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
     positionCorners.setTopLeft(QPoint(left,top));
     positionCorners.setBottomRight(QPoint(right,bottom));
@@ -1164,53 +1162,11 @@ void ViewPort::wheelEvent(QWheelEvent *event)
 void ViewPort::mousePressEvent(QMouseEvent *event)
 {
     int cv_event = -1, flags = 0;
-    QPoint pt = event->pos()+deltaOffset;
-
-
-    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_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;
+    QPoint pt = event->pos();
 
-    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_down, cv_event, flags);
+    icvmouseProcessing(QPointF(pt), cv_event, flags);
 
     if (matrixWorld.m11()>1)
     {
@@ -1225,53 +1181,11 @@ void ViewPort::mouseReleaseEvent(QMouseEvent *event)
 {
 
     int cv_event = -1, flags = 0;
-    QPoint pt = event->pos()+deltaOffset;
-
-
-    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 );
+    QPoint pt = event->pos();
 
+    //icvmouseHandler: pass parameters for cv_event, flags
+    icvmouseHandler(event, mouse_up, cv_event, flags);
+    icvmouseProcessing(QPointF(pt), cv_event, flags);
 
     if (matrixWorld.m11()>1)
         setCursor(Qt::OpenHandCursor);
@@ -1282,125 +1196,98 @@ void ViewPort::mouseReleaseEvent(QMouseEvent *event)
 void ViewPort::mouseDoubleClickEvent(QMouseEvent *event)
 {
     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:;
-    }
+    //icvmouseHandler: pass parameters for cv_event, flags
+    icvmouseHandler(event, mouse_dbclick, cv_event, flags);
+    icvmouseProcessing(QPointF(pt), cv_event, flags);
 
-    switch(event->button())
+    QWidget::mouseDoubleClickEvent(event);
+}
+
+void ViewPort::mouseMoveEvent(QMouseEvent *event)
+{
+    int cv_event = -1, flags = 0;
+    QPoint pt = event->pos();
+
+    //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)
     {
-    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:;
-    }
+        QPointF dxy = (pt - positionGrabbing)/matrixWorld.m11();
 
-    //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 );
+        positionGrabbing = event->pos();
 
+        moveView(dxy);
+    }
 
-    QWidget::mouseDoubleClickEvent(event);
+    //I update the statusbar here because if the user does a cvWaitkey(0) (like with inpaint.cpp)
+    //the status bar will only be repaint when a click occurs.
+    viewport()->update();
+
+    QWidget::mouseMoveEvent(event);
 }
 
-void ViewPort::mouseMoveEvent(QMouseEvent *event)
+//up, down, dclick, move
+void ViewPort::icvmouseHandler(QMouseEvent *event, type_mouse_event category, int &cv_event, int &flags)
 {
-    int cv_event = -1, flags = 0;
-    QPoint pt = event->pos()-deltaOffset;
 
     switch(event->modifiers())
     {
     case Qt::ShiftModifier:
-        flags = CV_EVENT_FLAG_SHIFTKEY;
-        break;
+       flags = CV_EVENT_FLAG_SHIFTKEY;
+       break;
     case Qt::ControlModifier:
-        flags = CV_EVENT_FLAG_CTRLKEY;
-        break;
+       flags = CV_EVENT_FLAG_CTRLKEY;
+       break;
     case Qt::AltModifier:
-        flags = CV_EVENT_FLAG_ALTKEY;
-        break;
+       flags = CV_EVENT_FLAG_ALTKEY;
+       break;
     case Qt::NoModifier        :
-        break;
+       break;
     case Qt::MetaModifier:
-        break;
+       break;
     case Qt::KeypadModifier:
-        break;
+       break;
     default:;
     }
 
-    cv_event = CV_EVENT_MOUSEMOVE;
-    switch(event->buttons())
+    switch(event->button())
     {
     case Qt::LeftButton:
-        flags |= CV_EVENT_FLAG_LBUTTON;
-        break;
+       cv_event = tableMouseButtons[category][0];
+       flags |= CV_EVENT_FLAG_LBUTTON;
+       break;
     case Qt::RightButton:
-        flags |= CV_EVENT_FLAG_RBUTTON;
-        break;
+       cv_event = tableMouseButtons[category][1];
+       flags |= CV_EVENT_FLAG_RBUTTON;
+       break;
     case Qt::MidButton:
-        flags |= CV_EVENT_FLAG_MBUTTON;
-        break;
+       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
-    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 && event->buttons() == Qt::LeftButton)
-    {
-        QPointF dxy = (pt - positionGrabbing)/matrixWorld.m11();
+    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);
 
-        positionGrabbing = event->pos();
-
-        moveView(dxy);
-    }
-    
-    //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.
-    viewport()->update();
+    if (on_mouse)
+       on_mouse( cv_event, mouseCoordinate.x(),mouseCoordinate.y(), flags, on_mouse_param );
 
-    QWidget::mouseMoveEvent(event);
 }
 
 QSize ViewPort::sizeHint() const
 {
-    //return QSize(width(),width()/2);
     if(image2Draw_ipl)
     {
         return QSize(image2Draw_ipl->width,image2Draw_ipl->height);
@@ -1424,12 +1311,13 @@ void ViewPort::resizeEvent ( QResizeEvent *event)
     ratioX=float(image2Draw_ipl->width)/float(width());
     ratioY=float(image2Draw_ipl->height)/float(height());
 
+    if(keepRatio)
+        deltaOffset = computeOffset();
+
     return QGraphicsView::resizeEvent(event);
 }
 
 
-
-
 void ViewPort::paintEvent(QPaintEvent* event)
 {
     QPainter myPainter(viewport());
@@ -1479,11 +1367,12 @@ void ViewPort::draw2D(QPainter *painter)
 
 void ViewPort::drawStatusBar()
 {
+    //centralWidget->myBar_msg->setMaximumWidth(width());
     //CvScalar value = cvGet2D(image2Draw_ipl,mouseCoordinate.y(),mouseCoordinate.x());
     QRgb rgbValue = image2Draw_qt.pixel(mouseCoordinate);
     centralWidget->myBar_msg->setText(tr("<font color='black'>Coordinate: %1x%2 ~ </font>")
-                                      .arg(mouseCoordinate.x())
-                                      .arg(mouseCoordinate.y())+
+                                     .arg(mouseCoordinate.x())
+                                     .arg(mouseCoordinate.y())+
                                       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='blue'>B:%5</font>").arg(qBlue(rgbValue))//.arg(value.val[2])
index 94fa314..b8e8a2d 100644 (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
 {
     Q_OBJECT
@@ -207,6 +216,8 @@ public slots:
     void siftWindowOnUp() ;
     void siftWindowOnDown();
     void resizeEvent ( QResizeEvent * );
+    int heightForWidth(int w) const;
+    bool hasHeightForWidth() const;
 
 private:
     Qt::AspectRatioMode modeRatio;
@@ -244,6 +255,8 @@ private:
     void draw2D(QPainter *painter);
     void drawStatusBar();
     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)
     void draw3D();
@@ -257,6 +270,7 @@ private slots:
 };
 
 
+
 //here css for trackbar
 /* from http://thesmithfam.org/blog/2010/03/10/fancy-qslider-stylesheet */
 static const QString str_Trackbar_css = QString("")