Results 1 to 3 of 3

Thread: Modifying QwtPlotMagnifier so it can handle touch events.

  1. #1
    Join Date
    Feb 2018
    Posts
    7
    Thanked 1 Time in 1 Post

    Default Modifying QwtPlotMagnifier so it can handle touch events.

    Hello, I'm back with a small problem trying to use the QwtPlotMagnifier (its rescale function), I have modified it so it can handle touch events (pinch zooms) but I have a small problem I'm not understanding how to fix. I want to also zoom the axis step respectively. But the example uses just the scale intervals, and when I zoom the step gets set to a default value (I guess this is given by QwtScaleEngine?), I've tried to use a dumb rule of three to get a new scale but with no avail. (new_step = old_step / old_to_scale * new_to_scale).

    Qt Code:
    1. struct scale {
    2. float from;
    3. float to;
    4. float step;
    5. };
    6.  
    7. class zoom_filter : public QObject {
    8. Q_OBJECT
    9.  
    10. scale *x_scale, *y_scale;
    11.  
    12. public:
    13. float *step, new_factor{1.0};
    14. zoom_filter(QObject *p, scale *x_scale, scale *y_scale)
    15. : QObject(p), x_scale(x_scale), y_scale(y_scale) {
    16. p->installEventFilter(this);
    17. }
    18.  
    19. virtual bool eventFilter(QObject *o, QEvent *ev) {
    20. // qDebug() << "Event called!";
    21.  
    22. if (ev->type() == QEvent::Gesture) {
    23. QGestureEvent *e = static_cast<QGestureEvent *>(ev);
    24. if (QGesture *p = e->gesture(Qt::PinchGesture)) {
    25. qDebug() << "Pinch gesture";
    26. QPinchGesture *pinch = static_cast<QPinchGesture *>(p);
    27. if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) {
    28. qDebug() << "Pinch zoom factor" << pinch->totalScaleFactor()
    29. << " " << pinch->scaleFactor();
    30. rescale(pinch->scaleFactor(), pinch->centerPoint(),
    31. ((QWidget *)parent())->size());
    32. new_factor = pinch->scaleFactor();
    33. }
    34. } else if (QGesture *p = e->gesture(Qt::SwipeGesture)) {
    35. qDebug() << "Swipe gesture";
    36. }
    37. } else if (ev->type() == QEvent::Resize) {
    38. qDebug() << "Rsize event";
    39. }
    40.  
    41. return QObject::eventFilter(o, ev);
    42. }
    43.  
    44. QwtPlot *plot() {
    45. QWidget *w = qobject_cast<QWidget *>(parent());
    46.  
    47. if (w)
    48. w = w->parentWidget();
    49.  
    50. return qobject_cast<QwtPlot *>(w);
    51. }
    52.  
    53. virtual void rescale(double factor, QPointF pos, QSize screen_size) {
    54. QwtPlot *plt = plot();
    55. if (plt == NULL)
    56. return;
    57.  
    58. factor = qAbs(factor);
    59. if (factor == 1.0 || factor == 0.0)
    60. return;
    61.  
    62. for (int axisId = 0; axisId < QwtPlot::axisCnt; axisId++) {
    63.  
    64. if (axisId == QwtPlot::xBottom || axisId == QwtPlot::yLeft) {
    65. const QwtScaleMap scaleMap = plt->canvasMap(axisId);
    66.  
    67. double v1 = scaleMap.s1();
    68. double v2 = scaleMap.s2();
    69.  
    70. if (scaleMap.transformation()) {
    71. // the coordinate system of the paint device is always linear
    72. v1 = scaleMap.transform(v1); // scaleMap.p1()
    73. v2 = scaleMap.transform(v2); // scaleMap.p2()
    74. }
    75.  
    76. double center = 0.5 * (v1 + v2);
    77. const double width_2 = 0.5 * (v2 - v1) * factor;
    78.  
    79. if (axisId == QwtPlot::yLeft)
    80. center = pos.y() / (float)screen_size.height() *
    81. v2; // 0.5 * ( v1 + v2 );
    82. else if (axisId == QwtPlot::xBottom)
    83. center = pos.x() / (float)screen_size.width() * v2; // v2;
    84. else
    85. continue;
    86.  
    87. v1 = center - width_2;
    88. v2 = center + width_2;
    89.  
    90. if (scaleMap.transformation()) {
    91. v1 = scaleMap.invTransform(v1);
    92. v2 = scaleMap.invTransform(v2);
    93. }
    94.  
    95. float sstep;
    96. // x_scale and y_scale got the initial scales and get updated accordingly
    97. if (axisId == QwtPlot::xBottom) {
    98. x_scale->step = v2 * x_scale->step / x_scale->to;
    99. sstep = x_scale->step;
    100. x_scale->to = v2;
    101. x_scale->from = v1;
    102. } else if (axisId == QwtPlot::yLeft) {
    103. float sscale = v2 / x_scale->to;
    104. qDebug() << "X sc " << factor << sscale << v2
    105. << y_scale->step << y_scale->to;
    106.  
    107. y_scale->step = v2 * y_scale->step / y_scale->to;
    108. sstep = y_scale->step;
    109. y_scale->to = v2;
    110. y_scale->from = v1;
    111. }
    112.  
    113. // The step is not working as intended
    114. plt->setAxisScale(axisId, v1, v2); //, sstep);
    115. // doReplot = true;
    116. }
    117. }
    118. }
    119. };
    To copy to clipboard, switch view to plain text mode 

  2. The following user says thank you to ifhmcos for this useful post:

    embeddedmz (29th August 2019)

  3. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Modifying QwtPlotMagnifier so it can handle touch events.

    You can use QwtPlot::setAxisScaleDiv instead of QwtPlot::setAxisScale if you want to have control over the position of the ticks.

    Uwe

  4. #3
    Join Date
    Feb 2018
    Posts
    7
    Thanked 1 Time in 1 Post

    Default Re: Modifying QwtPlotMagnifier so it can handle touch events.

    Hi thanks for all your help. I've looked a bit more into setAxisScaleDiv but It takes a QwtScaleDiv object and I don't want that of a finer control over ticks since they're all evenly spaced, I just want to set a step for major ticks and another for medium ticks (I need to show a grid of squares of 1x1mm contained in bigger squares of 5x5mm). If there is no other option I'll calculate all the ticks by hand.

Similar Threads

  1. Touch Events Are ignored?
    By kanika gupta in forum Newbie
    Replies: 1
    Last Post: 5th April 2016, 10:26
  2. Touch events not recognized for trackpad
    By mentalmushroom in forum Qt Programming
    Replies: 7
    Last Post: 13th February 2015, 17:10
  3. Replies: 3
    Last Post: 18th May 2011, 17:56
  4. QGraphicsProxyWidget and touch events
    By johnsoga in forum Qt Programming
    Replies: 0
    Last Post: 21st February 2010, 07:53
  5. How to Implement the Touch Screen Events?
    By rchaitanya in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 25th December 2008, 10:44

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.