You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

customplotZoom.cpp 1.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "customplotZoom.h"
  2. CustomPlotZoom::CustomPlotZoom(QWidget* parent)
  3. : QCustomPlot(parent)
  4. , mZoomMode(false)
  5. , mRubberBand(new QRubberBand(QRubberBand::Rectangle, this))
  6. {}
  7. CustomPlotZoom::~CustomPlotZoom()
  8. {}
  9. void CustomPlotZoom::setZoomMode(bool mode)
  10. {
  11. mZoomMode = mode;
  12. }
  13. void CustomPlotZoom::mousePressEvent(QMouseEvent* event)
  14. {
  15. if (mZoomMode) {
  16. if (event->button() == Qt::RightButton) {
  17. mOrigin = event->pos();
  18. mRubberBand->setGeometry(QRect(mOrigin, QSize()));
  19. mRubberBand->show();
  20. }
  21. }
  22. QCustomPlot::mousePressEvent(event);
  23. }
  24. void CustomPlotZoom::mouseMoveEvent(QMouseEvent* event)
  25. {
  26. if (mRubberBand->isVisible()) {
  27. mRubberBand->setGeometry(QRect(mOrigin, event->pos()).normalized());
  28. }
  29. QCustomPlot::mouseMoveEvent(event);
  30. }
  31. void CustomPlotZoom::mouseReleaseEvent(QMouseEvent* event)
  32. {
  33. if (mRubberBand->isVisible()) {
  34. const QRect zoomRect = mRubberBand->geometry();
  35. int xp1, yp1, xp2, yp2;
  36. zoomRect.getCoords(&xp1, &yp1, &xp2, &yp2);
  37. double x1 = xAxis->pixelToCoord(xp1);
  38. double y1 = xAxis->pixelToCoord(yp1);
  39. double x2 = xAxis->pixelToCoord(xp2);
  40. double y2 = xAxis->pixelToCoord(yp2);
  41. xAxis->setRange(x1, x2);
  42. yAxis->setRange(y1, y2);
  43. mRubberBand->hide();
  44. replot();
  45. }
  46. QCustomPlot::mouseReleaseEvent(event);
  47. }