提交 fd1da009 编写于 作者: A Alexander Alekhin

Merge pull request #7129 from paroj:mousecallback-dry-code

......@@ -2300,11 +2300,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;
......@@ -2320,12 +2399,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; }" );
......@@ -2353,13 +2430,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());
......@@ -2634,13 +2704,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);
}
......@@ -2648,12 +2715,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)
{
......@@ -2667,12 +2729,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);
......@@ -2683,30 +2740,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);
}
......@@ -2853,48 +2900,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)
......@@ -2906,9 +2911,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);
}
......@@ -3113,11 +3116,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;
}
......@@ -3131,11 +3131,6 @@ QWidget* OpenGlViewPort::getWidget()
return this;
}
void OpenGlViewPort::setMouseCallBack(CvMouseCallback callback, void* param)
{
mouseCallback = callback;
mouseData = param;
}
void OpenGlViewPort::writeSettings(QSettings& /*settings*/)
{
......@@ -3196,116 +3191,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
{
......
......@@ -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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册