From 46bfdbaf82f783b36a0462f7e71e261ccbf60da2 Mon Sep 17 00:00:00 2001 From: Alexandr Kondratev Date: Sat, 23 Jul 2016 18:10:36 +0300 Subject: [PATCH] highgui: window_QT mousecallback code refactored using DRY --- modules/highgui/src/window_QT.cpp | 284 +++++++++++------------------- modules/highgui/src/window_QT.h | 41 +++-- 2 files changed, 122 insertions(+), 203 deletions(-) diff --git a/modules/highgui/src/window_QT.cpp b/modules/highgui/src/window_QT.cpp index 7388a44b8d..e5a25f4717 100644 --- a/modules/highgui/src/window_QT.cpp +++ b/modules/highgui/src/window_QT.cpp @@ -2295,11 +2295,90 @@ void CvWindow::icvSaveTrackbars(QSettings* settings) } +////////////////////////////////////////////////////// +// OCVViewPort + +OCVViewPort::OCVViewPort() +{ + mouseCallback = 0; + mouseData = 0; +} + +void OCVViewPort::setMouseCallBack(CvMouseCallback callback, void* param) +{ + mouseCallback = callback; + mouseData = param; +} + +void OCVViewPort::icvmouseEvent(QMouseEvent* evnt, type_mouse_event category) +{ + int cv_event = -1, flags = 0; + + icvmouseHandler(evnt, category, cv_event, flags); + icvmouseProcessing(QPointF(evnt->pos()), cv_event, flags); +} + +void OCVViewPort::icvmouseHandler(QMouseEvent* evnt, type_mouse_event category, int& cv_event, int& flags) +{ + Qt::KeyboardModifiers modifiers = evnt->modifiers(); + Qt::MouseButtons buttons = evnt->buttons(); + + // This line gives excess flags flushing, with it you cannot predefine flags value. + // icvmouseHandler called with flags == 0 where it really need. + //flags = 0; + if(modifiers & Qt::ShiftModifier) + flags |= CV_EVENT_FLAG_SHIFTKEY; + if(modifiers & Qt::ControlModifier) + flags |= CV_EVENT_FLAG_CTRLKEY; + if(modifiers & Qt::AltModifier) + flags |= CV_EVENT_FLAG_ALTKEY; + + if(buttons & Qt::LeftButton) + flags |= CV_EVENT_FLAG_LBUTTON; + if(buttons & Qt::RightButton) + flags |= CV_EVENT_FLAG_RBUTTON; + if(buttons & Qt::MidButton) + flags |= CV_EVENT_FLAG_MBUTTON; + + if (cv_event == -1) { + if (category == mouse_wheel) { + QWheelEvent *we = (QWheelEvent *) evnt; + cv_event = ((we->orientation() == Qt::Vertical) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL); + flags |= (we->delta() & 0xffff)<<16; + return; + } + switch(evnt->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: + cv_event = CV_EVENT_MOUSEMOVE; + } + } +} + +void OCVViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) +{ + if (mouseCallback) + mouseCallback(cv_event, pt.x(), pt.y(), flags, mouseData); +} + + ////////////////////////////////////////////////////// // DefaultViewPort -DefaultViewPort::DefaultViewPort(CvWindow* arg, int arg2) : QGraphicsView(arg), image2Draw_mat(0) +DefaultViewPort::DefaultViewPort(CvWindow* arg, int arg2) : QGraphicsView(arg), OCVViewPort(), image2Draw_mat(0) { centralWidget = arg; param_keepRatio = arg2; @@ -2315,12 +2394,10 @@ DefaultViewPort::DefaultViewPort(CvWindow* arg, int arg2) : QGraphicsView(arg), connect(timerDisplay, SIGNAL(timeout()), this, SLOT(stopDisplayInfo())); drawInfo = false; + mouseCoordinate = QPoint(-1, -1); positionGrabbing = QPointF(0, 0); - positionCorners = QRect(0, 0, size().width(), size().height()); + positionCorners = QRect(0, 0, size().width(), size().height()); - on_mouse = 0; - on_mouse_param = 0; - mouseCoordinate = QPoint(-1, -1); //no border setStyleSheet( "QGraphicsView { border-style: none; }" ); @@ -2348,13 +2425,6 @@ QWidget* DefaultViewPort::getWidget() } -void DefaultViewPort::setMouseCallBack(CvMouseCallback m, void* param) -{ - on_mouse = m; - - on_mouse_param = param; -} - void DefaultViewPort::writeSettings(QSettings& settings) { settings.setValue("matrix_view.m11", param_matrixWorld.m11()); @@ -2629,13 +2699,10 @@ void DefaultViewPort::resizeEvent(QResizeEvent* evnt) void DefaultViewPort::wheelEvent(QWheelEvent* evnt) { - int delta = evnt->delta(); - int cv_event = ((evnt->orientation() == Qt::Vertical) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL); - int flags = (delta & 0xffff)<<16; - QPoint pt = evnt->pos(); + icvmouseEvent((QMouseEvent *)evnt, mouse_wheel); - icvmouseHandler((QMouseEvent*)evnt, mouse_wheel, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + scaleView(evnt->delta() / 240.0, evnt->pos()); + viewport()->update(); QWidget::wheelEvent(evnt); } @@ -2643,12 +2710,7 @@ void DefaultViewPort::wheelEvent(QWheelEvent* evnt) void DefaultViewPort::mousePressEvent(QMouseEvent* evnt) { - int cv_event = -1, flags = 0; - QPoint pt = evnt->pos(); - - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(evnt, mouse_down, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + icvmouseEvent(evnt, mouse_down); if (param_matrixWorld.m11()>1) { @@ -2662,12 +2724,7 @@ void DefaultViewPort::mousePressEvent(QMouseEvent* evnt) void DefaultViewPort::mouseReleaseEvent(QMouseEvent* evnt) { - int cv_event = -1, flags = 0; - QPoint pt = evnt->pos(); - - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(evnt, mouse_up, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + icvmouseEvent(evnt, mouse_up); if (param_matrixWorld.m11()>1) setCursor(Qt::OpenHandCursor); @@ -2678,30 +2735,20 @@ void DefaultViewPort::mouseReleaseEvent(QMouseEvent* evnt) void DefaultViewPort::mouseDoubleClickEvent(QMouseEvent* evnt) { - int cv_event = -1, flags = 0; - QPoint pt = evnt->pos(); - - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(evnt, mouse_dbclick, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); - + icvmouseEvent(evnt, mouse_dbclick); QWidget::mouseDoubleClickEvent(evnt); } void DefaultViewPort::mouseMoveEvent(QMouseEvent* evnt) { - int cv_event = CV_EVENT_MOUSEMOVE, flags = 0; - QPoint pt = evnt->pos(); - - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(evnt, mouse_move, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); + icvmouseEvent(evnt, mouse_move); if (param_matrixWorld.m11() > 1 && evnt->buttons() == Qt::LeftButton) { + QPoint pt = evnt->pos(); QPointF dxy = (pt - positionGrabbing)/param_matrixWorld.m11(); - positionGrabbing = evnt->pos(); + positionGrabbing = pt; moveView(dxy); } @@ -2848,48 +2895,6 @@ void DefaultViewPort::scaleView(qreal factor,QPointF center) } -//up, down, dclick, move -void DefaultViewPort::icvmouseHandler(QMouseEvent *evnt, type_mouse_event category, int &cv_event, int &flags) -{ - Qt::KeyboardModifiers modifiers = evnt->modifiers(); - Qt::MouseButtons buttons = evnt->buttons(); - - // This line gives excess flags flushing, with it you cannot predefine flags value. - // icvmouseHandler called with flags == 0 where it really need. - //flags = 0; - if(modifiers & Qt::ShiftModifier) - flags |= CV_EVENT_FLAG_SHIFTKEY; - if(modifiers & Qt::ControlModifier) - flags |= CV_EVENT_FLAG_CTRLKEY; - if(modifiers & Qt::AltModifier) - flags |= CV_EVENT_FLAG_ALTKEY; - - if(buttons & Qt::LeftButton) - flags |= CV_EVENT_FLAG_LBUTTON; - if(buttons & Qt::RightButton) - flags |= CV_EVENT_FLAG_RBUTTON; - if(buttons & Qt::MidButton) - flags |= CV_EVENT_FLAG_MBUTTON; - - if (cv_event == -1) - switch(evnt->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: - cv_event = CV_EVENT_MOUSEMOVE; - } -} void DefaultViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) @@ -2901,9 +2906,7 @@ void DefaultViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) mouseCoordinate.rx()=floor(pfx/ratioX); mouseCoordinate.ry()=floor(pfy/ratioY); - if (on_mouse) - on_mouse( cv_event, mouseCoordinate.x(), - mouseCoordinate.y(), flags, on_mouse_param ); + OCVViewPort::icvmouseProcessing(QPointF(mouseCoordinate), cv_event, flags); } @@ -3108,11 +3111,8 @@ void DefaultViewPort::setSize(QSize /*size_*/) #ifdef HAVE_QT_OPENGL -OpenGlViewPort::OpenGlViewPort(QWidget* _parent) : QGLWidget(_parent), size(-1, -1) +OpenGlViewPort::OpenGlViewPort(QWidget* _parent) : QGLWidget(_parent), OCVViewPort(), size(-1, -1) { - mouseCallback = 0; - mouseData = 0; - glDrawCallback = 0; glDrawData = 0; } @@ -3126,11 +3126,6 @@ QWidget* OpenGlViewPort::getWidget() return this; } -void OpenGlViewPort::setMouseCallBack(CvMouseCallback callback, void* param) -{ - mouseCallback = callback; - mouseData = param; -} void OpenGlViewPort::writeSettings(QSettings& /*settings*/) { @@ -3191,116 +3186,37 @@ void OpenGlViewPort::paintGL() glDrawCallback(glDrawData); } + void OpenGlViewPort::wheelEvent(QWheelEvent* evnt) { - int delta = evnt->delta(); - int cv_event = ((evnt->orientation() == Qt::Vertical) ? CV_EVENT_MOUSEWHEEL : CV_EVENT_MOUSEHWHEEL); - int flags = (delta & 0xffff)<<16; - QPoint pt = evnt->pos(); - - icvmouseHandler((QMouseEvent*)evnt, mouse_wheel, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); - - QWidget::wheelEvent(evnt); + icvmouseEvent((QMouseEvent *)evnt, mouse_wheel); + QGLWidget::wheelEvent(evnt); } void OpenGlViewPort::mousePressEvent(QMouseEvent* evnt) { - int cv_event = -1, flags = 0; - QPoint pt = evnt->pos(); - - icvmouseHandler(evnt, mouse_down, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); - + icvmouseEvent(evnt, mouse_down); QGLWidget::mousePressEvent(evnt); } - void OpenGlViewPort::mouseReleaseEvent(QMouseEvent* evnt) { - int cv_event = -1, flags = 0; - QPoint pt = evnt->pos(); - - icvmouseHandler(evnt, mouse_up, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); - + icvmouseEvent(evnt, mouse_up); QGLWidget::mouseReleaseEvent(evnt); } - void OpenGlViewPort::mouseDoubleClickEvent(QMouseEvent* evnt) { - int cv_event = -1, flags = 0; - QPoint pt = evnt->pos(); - - icvmouseHandler(evnt, mouse_dbclick, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); - + icvmouseEvent(evnt, mouse_dbclick); QGLWidget::mouseDoubleClickEvent(evnt); } - void OpenGlViewPort::mouseMoveEvent(QMouseEvent* evnt) { - int cv_event = CV_EVENT_MOUSEMOVE, flags = 0; - QPoint pt = evnt->pos(); - - //icvmouseHandler: pass parameters for cv_event, flags - icvmouseHandler(evnt, mouse_move, cv_event, flags); - icvmouseProcessing(QPointF(pt), cv_event, flags); - + icvmouseEvent(evnt, mouse_move); QGLWidget::mouseMoveEvent(evnt); } -void OpenGlViewPort::icvmouseHandler(QMouseEvent* evnt, type_mouse_event category, int& cv_event, int& flags) -{ - Qt::KeyboardModifiers modifiers = evnt->modifiers(); - Qt::MouseButtons buttons = evnt->buttons(); - - // This line gives excess flags flushing, with it you cannot predefine flags value. - // icvmouseHandler called with flags == 0 where it really need. - //flags = 0; - if(modifiers & Qt::ShiftModifier) - flags |= CV_EVENT_FLAG_SHIFTKEY; - if(modifiers & Qt::ControlModifier) - flags |= CV_EVENT_FLAG_CTRLKEY; - if(modifiers & Qt::AltModifier) - flags |= CV_EVENT_FLAG_ALTKEY; - - if(buttons & Qt::LeftButton) - flags |= CV_EVENT_FLAG_LBUTTON; - if(buttons & Qt::RightButton) - flags |= CV_EVENT_FLAG_RBUTTON; - if(buttons & Qt::MidButton) - flags |= CV_EVENT_FLAG_MBUTTON; - - if (cv_event == -1) - switch(evnt->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: - cv_event = CV_EVENT_MOUSEMOVE; - } -} - - -void OpenGlViewPort::icvmouseProcessing(QPointF pt, int cv_event, int flags) -{ - if (mouseCallback) - mouseCallback(cv_event, pt.x(), pt.y(), flags, mouseData); -} - QSize OpenGlViewPort::sizeHint() const { diff --git a/modules/highgui/src/window_QT.h b/modules/highgui/src/window_QT.h index 16cc9e3853..df80568cf0 100644 --- a/modules/highgui/src/window_QT.h +++ b/modules/highgui/src/window_QT.h @@ -403,10 +403,26 @@ public: }; +class OCVViewPort : public ViewPort +{ +public: + explicit OCVViewPort(); + ~OCVViewPort() {}; + void setMouseCallBack(CvMouseCallback callback, void* param); + +protected: + void icvmouseEvent(QMouseEvent* event, type_mouse_event category); + void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags); + void icvmouseProcessing(QPointF pt, int cv_event, int flags); + + CvMouseCallback mouseCallback; + void* mouseData; +}; + #ifdef HAVE_QT_OPENGL -class OpenGlViewPort : public QGLWidget, public ViewPort +class OpenGlViewPort : public QGLWidget, public OCVViewPort { public: explicit OpenGlViewPort(QWidget* parent); @@ -414,8 +430,6 @@ public: QWidget* getWidget(); - void setMouseCallBack(CvMouseCallback callback, void* param); - void writeSettings(QSettings& settings); void readSettings(QSettings& settings); @@ -448,20 +462,14 @@ protected: private: QSize size; - CvMouseCallback mouseCallback; - void* mouseData; - CvOpenGlDrawCallback glDrawCallback; void* glDrawData; - - void icvmouseHandler(QMouseEvent* event, type_mouse_event category, int& cv_event, int& flags); - void icvmouseProcessing(QPointF pt, int cv_event, int flags); }; #endif // HAVE_QT_OPENGL -class DefaultViewPort : public QGraphicsView, public ViewPort +class DefaultViewPort : public QGraphicsView, public OCVViewPort { Q_OBJECT @@ -471,8 +479,6 @@ public: QWidget* getWidget(); - void setMouseCallBack(CvMouseCallback callback, void* param); - void writeSettings(QSettings& settings); void readSettings(QSettings& settings); @@ -510,6 +516,7 @@ protected: void contextMenuEvent(QContextMenuEvent* event); void resizeEvent(QResizeEvent* event); void paintEvent(QPaintEvent* paintEventInfo); + void wheelEvent(QWheelEvent* event); void mouseMoveEvent(QMouseEvent* event); void mousePressEvent(QMouseEvent* event); @@ -526,17 +533,13 @@ private: QImage image2Draw_qt; int nbChannelOriginImage; - //for mouse callback - CvMouseCallback on_mouse; - void* on_mouse_param; - void scaleView(qreal scaleFactor, QPointF center); void moveView(QPointF delta); - QPoint mouseCoordinate; + QPoint mouseCoordinate; QPointF positionGrabbing; - QRect positionCorners; + QRect positionCorners; QTransform matrixWorld_inv; float ratioX, ratioY; @@ -555,7 +558,7 @@ 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); private slots: