struct scale {
float from;
float to;
float step;
};
class zoom_filter
: public QObject { Q_OBJECT
scale *x_scale, *y_scale;
public:
float *step, new_factor{1.0};
zoom_filter
(QObject *p, scale
*x_scale, scale
*y_scale
) : QObject(p
), x_scale
(x_scale
), y_scale
(y_scale
) { p->installEventFilter(this);
}
// qDebug() << "Event called!";
if (ev
->type
() == QEvent::Gesture) { QGestureEvent *e = static_cast<QGestureEvent *>(ev);
if (QGesture *p = e->gesture(Qt::PinchGesture)) {
qDebug() << "Pinch gesture";
QPinchGesture *pinch = static_cast<QPinchGesture *>(p);
if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) {
qDebug() << "Pinch zoom factor" << pinch->totalScaleFactor()
<< " " << pinch->scaleFactor();
rescale(pinch->scaleFactor(), pinch->centerPoint(),
new_factor = pinch->scaleFactor();
}
} else if (QGesture *p = e->gesture(Qt::SwipeGesture)) {
qDebug() << "Swipe gesture";
}
} else if (ev
->type
() == QEvent::Resize) { qDebug() << "Rsize event";
}
return QObject::eventFilter(o, ev
);
}
QWidget *w
= qobject_cast<QWidget
*>
(parent
());
if (w)
w = w->parentWidget();
return qobject_cast<QwtPlot *>(w);
}
virtual void rescale
(double factor,
QPointF pos,
QSize screen_size
) { if (plt == NULL)
return;
factor = qAbs(factor);
if (factor == 1.0 || factor == 0.0)
return;
for (int axisId
= 0; axisId <
QwtPlot::axisCnt; axisId
++) {
double v1 = scaleMap.s1();
double v2 = scaleMap.s2();
if (scaleMap.transformation()) {
// the coordinate system of the paint device is always linear
v1 = scaleMap.transform(v1); // scaleMap.p1()
v2 = scaleMap.transform(v2); // scaleMap.p2()
}
double center = 0.5 * (v1 + v2);
const double width_2 = 0.5 * (v2 - v1) * factor;
center = pos.y() / (float)screen_size.height() *
v2; // 0.5 * ( v1 + v2 );
else if (axisId
== QwtPlot::xBottom) center = pos.x() / (float)screen_size.width() * v2; // v2;
else
continue;
v1 = center - width_2;
v2 = center + width_2;
if (scaleMap.transformation()) {
v1 = scaleMap.invTransform(v1);
v2 = scaleMap.invTransform(v2);
}
float sstep;
// x_scale and y_scale got the initial scales and get updated accordingly
x_scale->step = v2 * x_scale->step / x_scale->to;
sstep = x_scale->step;
x_scale->to = v2;
x_scale->from = v1;
} else if (axisId
== QwtPlot::yLeft) { float sscale = v2 / x_scale->to;
qDebug() << "X sc " << factor << sscale << v2
<< y_scale->step << y_scale->to;
y_scale->step = v2 * y_scale->step / y_scale->to;
sstep = y_scale->step;
y_scale->to = v2;
y_scale->from = v1;
}
// The step is not working as intended
plt->setAxisScale(axisId, v1, v2); //, sstep);
// doReplot = true;
}
}
}
};
struct scale {
float from;
float to;
float step;
};
class zoom_filter : public QObject {
Q_OBJECT
scale *x_scale, *y_scale;
public:
float *step, new_factor{1.0};
zoom_filter(QObject *p, scale *x_scale, scale *y_scale)
: QObject(p), x_scale(x_scale), y_scale(y_scale) {
p->installEventFilter(this);
}
virtual bool eventFilter(QObject *o, QEvent *ev) {
// qDebug() << "Event called!";
if (ev->type() == QEvent::Gesture) {
QGestureEvent *e = static_cast<QGestureEvent *>(ev);
if (QGesture *p = e->gesture(Qt::PinchGesture)) {
qDebug() << "Pinch gesture";
QPinchGesture *pinch = static_cast<QPinchGesture *>(p);
if (pinch->changeFlags() & QPinchGesture::ScaleFactorChanged) {
qDebug() << "Pinch zoom factor" << pinch->totalScaleFactor()
<< " " << pinch->scaleFactor();
rescale(pinch->scaleFactor(), pinch->centerPoint(),
((QWidget *)parent())->size());
new_factor = pinch->scaleFactor();
}
} else if (QGesture *p = e->gesture(Qt::SwipeGesture)) {
qDebug() << "Swipe gesture";
}
} else if (ev->type() == QEvent::Resize) {
qDebug() << "Rsize event";
}
return QObject::eventFilter(o, ev);
}
QwtPlot *plot() {
QWidget *w = qobject_cast<QWidget *>(parent());
if (w)
w = w->parentWidget();
return qobject_cast<QwtPlot *>(w);
}
virtual void rescale(double factor, QPointF pos, QSize screen_size) {
QwtPlot *plt = plot();
if (plt == NULL)
return;
factor = qAbs(factor);
if (factor == 1.0 || factor == 0.0)
return;
for (int axisId = 0; axisId < QwtPlot::axisCnt; axisId++) {
if (axisId == QwtPlot::xBottom || axisId == QwtPlot::yLeft) {
const QwtScaleMap scaleMap = plt->canvasMap(axisId);
double v1 = scaleMap.s1();
double v2 = scaleMap.s2();
if (scaleMap.transformation()) {
// the coordinate system of the paint device is always linear
v1 = scaleMap.transform(v1); // scaleMap.p1()
v2 = scaleMap.transform(v2); // scaleMap.p2()
}
double center = 0.5 * (v1 + v2);
const double width_2 = 0.5 * (v2 - v1) * factor;
if (axisId == QwtPlot::yLeft)
center = pos.y() / (float)screen_size.height() *
v2; // 0.5 * ( v1 + v2 );
else if (axisId == QwtPlot::xBottom)
center = pos.x() / (float)screen_size.width() * v2; // v2;
else
continue;
v1 = center - width_2;
v2 = center + width_2;
if (scaleMap.transformation()) {
v1 = scaleMap.invTransform(v1);
v2 = scaleMap.invTransform(v2);
}
float sstep;
// x_scale and y_scale got the initial scales and get updated accordingly
if (axisId == QwtPlot::xBottom) {
x_scale->step = v2 * x_scale->step / x_scale->to;
sstep = x_scale->step;
x_scale->to = v2;
x_scale->from = v1;
} else if (axisId == QwtPlot::yLeft) {
float sscale = v2 / x_scale->to;
qDebug() << "X sc " << factor << sscale << v2
<< y_scale->step << y_scale->to;
y_scale->step = v2 * y_scale->step / y_scale->to;
sstep = y_scale->step;
y_scale->to = v2;
y_scale->from = v1;
}
// The step is not working as intended
plt->setAxisScale(axisId, v1, v2); //, sstep);
// doReplot = true;
}
}
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks