From ce213312003fdf9f67e2f5b72f2f6166dfb3d530 Mon Sep 17 00:00:00 2001 From: kriegerj Date: Mon, 12 Oct 2015 17:05:15 +0200 Subject: [PATCH] tried to add a special pop-menu, when left ouse-double-lcicked (optional) --- jkqtplotter.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++----- jkqtplotter.h | 23 ++++++++++++++ 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/jkqtplotter.cpp b/jkqtplotter.cpp index 272c63e854..f8c9ad2dba 100644 --- a/jkqtplotter.cpp +++ b/jkqtplotter.cpp @@ -60,6 +60,8 @@ JKQtPlotter::JKQtPlotter(QWidget *parent): void JKQtPlotter::init(bool datastore_internal, QWidget* parent, JKQTPdatastore* datast) { + leftDoubleClickAction=LeftDoubleClickDefault; + menuSpecialContextMenu=NULL; mouseContextX=0; mouseContextY=0; setParent(parent); @@ -273,6 +275,8 @@ void JKQtPlotter::mouseMoveEvent ( QMouseEvent * event ) { void JKQtPlotter::mousePressEvent ( QMouseEvent * event ){ if (event->button()==Qt::LeftButton) { + mouseLastClickX=event->x(); + mouseLastClickY=event->y(); if ( (mouseActionMode!=JKQtPlotter::NoMouseAction)) { mouseDragRectXStart=plotter->p2x(event->x()/magnification); @@ -284,6 +288,8 @@ void JKQtPlotter::mousePressEvent ( QMouseEvent * event ){ if (mouseActionMode==JKQtPlotter::ClickEvents) emit userClickFinished(mouseDragRectXStart, mouseDragRectYStart, event->modifiers()); } } else if (event->button()==Qt::RightButton) { + mouseLastClickX=event->x(); + mouseLastClickY=event->y(); if (rightMouseButtonAction==JKQtPlotter::RightMouseButtonZoom) { double xmin=plotter->p2x((long)round((double)plotter->get_iplotBorderLeft()-(double)plotter->get_plotWidth()/2.0)); double xmax=plotter->p2x((long)round((double)plotter->get_iplotBorderLeft()+1.5*(double)plotter->get_plotWidth())); @@ -305,12 +311,8 @@ void JKQtPlotter::mousePressEvent ( QMouseEvent * event ){ //update(); event->accept(); } else if (rightMouseButtonAction==JKQtPlotter::RightMouseButtonContextMenu) { - mouseContextX=plotter->p2x(event->x()/magnification); - mouseContextY=plotter->p2y((event->y()-getPlotYOffset())/magnification); - contextMenu->close(); - initContextMenu(); - contextMenu->popup(event->globalPos()); - emit contextMenuOpened(mouseContextX, mouseContextY, contextMenu); + openContextMenu(event->x(), event->y()); + event->accept(); } } @@ -324,6 +326,9 @@ void JKQtPlotter::mousePressEvent ( QMouseEvent * event ){ } void JKQtPlotter::mouseReleaseEvent ( QMouseEvent * event ){ + if ((event->flags()&Qt::MouseEventCreatedDoubleClick)==Qt::MouseEventCreatedDoubleClick) { + return; + } if (mouseDragingRectangle && event->button()==Qt::LeftButton) { mouseDragRectXEnd=plotter->p2x(event->x()/magnification); mouseDragRectYEnd=plotter->p2y((event->y()-getPlotYOffset())/magnification); @@ -370,7 +375,19 @@ void JKQtPlotter::mouseDoubleClickEvent ( QMouseEvent * event ){ if ( (event->x()/magnification>=plotter->get_iplotBorderLeft()) && (event->x()/magnification<=plotter->get_plotWidth()+plotter->get_iplotBorderLeft()) && ((event->y()-getPlotYOffset())/magnification>=plotter->get_iplotBorderTop()) && ((event->y()-getPlotYOffset())/magnification<=plotter->get_plotHeight()+plotter->get_iplotBorderTop()) ) { - if (rightMouseButtonAction==JKQtPlotter::RightMouseButtonZoom) { + mouseLastClickX=event->x(); + mouseLastClickY=event->y(); + if (event->button()==Qt::LeftButton) { + if (leftDoubleClickAction==LeftDoubleClickContextMenu) { + openContextMenu(event->x(), event->y()); + event->accept(); + } else if (leftDoubleClickAction==LeftDoubleClickSpecialContextMenu) { + openSpecialContextMenu(event->x(), event->y()); + event->accept(); + } + } + + if (rightMouseButtonAction==JKQtPlotter::RightMouseButtonZoom && event->button()==Qt::RightButton) { double factor=4.0; if (event->button()==Qt::RightButton) factor=1; double xmin=plotter->p2x((long)round((double)event->x()/magnification-(double)plotter->get_plotWidth()/factor)); @@ -770,6 +787,15 @@ void JKQtPlotter::set_zoomByMouseRectangle(bool zomByrectangle) { else mouseActionMode=JKQtPlotter::NoMouseAction; } +void JKQtPlotter::set_menuSpecialContextMenu(QMenu *menu) +{ + menuSpecialContextMenu=menu; + if (menuSpecialContextMenu) { + menuSpecialContextMenu->setParent(this); + menuSpecialContextMenu->close(); + } +} + void JKQtPlotter::setMagnification(double m) { magnification=m; @@ -813,6 +839,47 @@ void JKQtPlotter::setMousePositionLabel(const QString &text) if (displayCustomMousePosition) update(); } +void JKQtPlotter::openContextMenu() +{ + openContextMenu(mouseLastClickX, mouseLastClickY); +} + +void JKQtPlotter::openContextMenu(int x, int y) +{ + //qDebug()<<"openContextMenu("<p2x(x/magnification); + mouseContextY=plotter->p2y((y-getPlotYOffset())/magnification); + contextMenu->close(); + initContextMenu(); + contextMenu->popup(mapToGlobal(QPoint(x,y))); + qDebug()<<" -> "<size()<pos()<parent(); + emit contextMenuOpened(mouseContextX, mouseContextY, contextMenu); + //qDebug()<<"openContextMenu("<actions().size(); i++) { + qDebug()<<" - "<actions().at(i)->text(); + } + mouseContextX=plotter->p2x(x/magnification); + mouseContextY=plotter->p2y((y-getPlotYOffset())/magnification); + menuSpecialContextMenu->close(); + menuSpecialContextMenu->popup(mapToGlobal(QPoint(x,y))); + menuSpecialContextMenu->resize(menuSpecialContextMenu->sizeHint()); + qDebug()<<" -> "<size()<pos()<parent(); + emit contextMenuOpened(mouseContextX, mouseContextY, menuSpecialContextMenu); + qDebug()<<"openSpecialContextMenu("<getXAxis(); } inline JKQTPverticalAxis* getYAxis() { return plotter->getYAxis(); } @@ -400,6 +413,10 @@ class LIB_EXPORT JKQtPlotter: public QWidget { void setMousePositionLabel(const QString& text); + void openContextMenu(); + void openContextMenu(int x, int y); + void openSpecialContextMenu(); + void openSpecialContextMenu(int x, int y); signals: /** \brief signal: emitted whenever the mouse moved over the plot */ void plotMouseMove(double x, double y); @@ -498,6 +515,10 @@ class LIB_EXPORT JKQtPlotter: public QWidget { /** \brief indicates whether zooming using the mouse-wheel is activated */ bool zoomByMouseWheel; + /** \brief indicates the action to perform on a left mouse-button double-click */ + LeftDoubleClickAction leftDoubleClickAction; + QMenu* menuSpecialContextMenu; + /** \brief toolbar class used for user input */ @@ -597,6 +618,8 @@ class LIB_EXPORT JKQtPlotter: public QWidget { QMenu* contextMenu; double mouseContextX; double mouseContextY; + int mouseLastClickX; + int mouseLastClickY; QList contextSubMenus; void initContextMenu();