Commit 57b3f0ce authored by DreamSourceLab's avatar DreamSourceLab

Add zoom&gesture support in macOS

parent 21704f55
......@@ -264,7 +264,27 @@ void Header::wheelEvent(QWheelEvent *event)
const vector< boost::shared_ptr<Trace> > traces(
_view.get_traces(ALL_VIEW));
// Vertical scrolling
double shift = event->delta() / 80.0;
double shift = 0;
#ifdef Q_OS_DARWIN
static bool active = true;
static int64_t last_time;
if (event->source() == Qt::MouseEventSynthesizedBySystem) {
if (active) {
last_time = QDateTime::currentMSecsSinceEpoch();
shift = event->delta() > 1.5 ? -1 :
event->delta() < -1.5 ? 1 : 0;
}
int64_t cur_time = QDateTime::currentMSecsSinceEpoch();
if (cur_time - last_time > 100)
active = true;
else
active = false;
} else {
shift = -event->delta() / 80.0;
}
#else
shift = event->delta() / 80.0;
#endif
BOOST_FOREACH(const boost::shared_ptr<Trace> t, traces)
if (t->mouse_wheel(width(), event->pos(), shift))
break;
......
......@@ -293,8 +293,9 @@ void View::update_hori_res()
}
}
void View::zoom(double steps, int offset)
bool View::zoom(double steps, int offset)
{
bool ret = true;
_preScale = _scale;
_preOffset = _offset;
......@@ -304,7 +305,7 @@ void View::zoom(double steps, int offset)
} else {
if (_session.get_capture_state() == SigSession::Running &&
_session.get_instant())
return;
return ret;
double hori_res = -1;
if(steps > 0.5)
......@@ -315,6 +316,8 @@ void View::zoom(double steps, int offset)
if (hori_res > 0) {
const double scale = _session.cur_view_time() / get_view_width();
_scale = max(min(scale, _maxscale), _minscale);
} else {
ret = false;
}
}
......@@ -327,6 +330,8 @@ void View::zoom(double steps, int offset)
viewport_update();
update_scroll();
}
return ret;
}
void View::timebase_changed()
......@@ -816,6 +821,7 @@ bool View::viewportEvent(QEvent *e)
case QEvent::MouseButtonDblClick:
case QEvent::MouseMove:
case QEvent::Wheel:
case QEvent::Gesture:
return false;
default:
......
......@@ -130,7 +130,7 @@ public:
void capture_init();
void zoom(double steps);
void zoom(double steps, int offset);
bool zoom(double steps, int offset);
/**
* Sets the scale and offset.
......
......@@ -130,6 +130,13 @@ QPoint Viewport::get_mouse_point() const
return _mouse_point;
}
bool Viewport::event(QEvent *event)
{
if (event->type() == QEvent::NativeGesture)
return gestureEvent(static_cast<QNativeGestureEvent*>(event));
return QWidget::event(event);
}
void Viewport::paintEvent(QPaintEvent *event)
{
(void)event;
......@@ -1007,11 +1014,32 @@ void Viewport::wheelEvent(QWheelEvent *event)
if (event->orientation() == Qt::Vertical) {
// Vertical scrolling is interpreted as zooming in/out
const int offset = event->x();
#ifdef Q_OS_DARWIN
static bool active = true;
static int64_t last_time;
if (event->source() == Qt::MouseEventSynthesizedBySystem) {
if (active && (event->modifiers() & Qt::ShiftModifier)) {
last_time = QDateTime::currentMSecsSinceEpoch();
const double scale = event->delta() > 1.5 ? 1 :
event->delta() < -1.5 ? -1 : 0;
_view.zoom(scale, offset);
}
int64_t cur_time = QDateTime::currentMSecsSinceEpoch();
if (cur_time - last_time > 50)
active = true;
else
active = false;
} else {
_view.zoom(-event->delta() / 80, offset);
}
#else
_view.zoom(event->delta() / 80, offset);
#endif
} else if (event->orientation() == Qt::Horizontal) {
// Horizontal scrolling is interpreted as moving left/right
_view.set_scale_offset(_view.scale(),
event->delta() + _view.offset());
if (!(event->modifiers() & Qt::ShiftModifier))
_view.set_scale_offset(_view.scale(),
_view.offset() - event->delta());
}
}
......@@ -1027,6 +1055,32 @@ void Viewport::wheelEvent(QWheelEvent *event)
measure();
}
bool Viewport::gestureEvent(QNativeGestureEvent *event)
{
static double total_scale = 0;
switch(event->gestureType()) {
case Qt::BeginNativeGesture:
break;
case Qt::EndNativeGesture:
total_scale = 0;
break;
case Qt::ZoomNativeGesture: {
total_scale += event->value() * 2;
if (_view.zoom(total_scale, _view.hover_point().x()))
total_scale = 0;
}
break;
case Qt::SmartZoomNativeGesture:
_view.zoom(-1, _view.hover_point().x());
break;
default:
return QWidget::event(event);
}
measure();
return true;
}
void Viewport::leaveEvent(QEvent *)
{
_mouse_point = QPoint(-1, -1);
......
......@@ -32,6 +32,7 @@
#include <QTime>
#include <QTimer>
#include <QWidget>
#include <QNativeGestureEvent>
#include "../view/view.h"
#include "../../extdef.h"
......@@ -107,16 +108,18 @@ public:
bool get_dso_trig_moved() const;
protected:
void paintEvent(QPaintEvent *event);
bool event(QEvent *event) override;
void paintEvent(QPaintEvent *event) override;
private:
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseDoubleClickEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event);
void leaveEvent(QEvent *);
void resizeEvent(QResizeEvent *e);
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseDoubleClickEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
void leaveEvent(QEvent *) override;
void resizeEvent(QResizeEvent *e) override;
bool gestureEvent(QNativeGestureEvent *event);
void paintSignals(QPainter& p, QColor fore, QColor back);
void paintProgress(QPainter& p, QColor fore, QColor back);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment