bool scroll_widget::event(QEvent *a_event_ptr) { if (!o_scroll_widget->m_viewport) { return QGraphicsObject::event(a_event_ptr); } switch (a_event_ptr->type()) { case QScrollPrepareEvent::ScrollPrepare: { qDebug() << Q_FUNC_INFO << "Prepare :"; QScrollPrepareEvent *se = static_cast<QScrollPrepareEvent *>(a_event_ptr); se->setViewportSize(o_scroll_widget->m_size); QRectF br = o_scroll_widget->m_viewport->boundingRect(); se->setContentPosRange(QRectF( 0, 0, qMax(qreal(0), br.width() - o_scroll_widget->m_size.width()), qMax(qreal(0), br.height() - o_scroll_widget->m_size.height()))); se->setContentPos(-o_scroll_widget->m_viewport->pos()); se->accept(); return QGraphicsObject::event(a_event_ptr); } case QScrollEvent::Scroll: { qDebug() << Q_FUNC_INFO << "Scroll"; QScrollEvent *se = static_cast<QScrollEvent *>(a_event_ptr); o_scroll_widget->m_viewport->setPos(-se->contentPos() - se->overshootDistance()); return true; } default: break; } return QGraphicsObject::event(a_event_ptr); }
bool QtViewportInteractionEngine::event(QEvent* event) { switch (event->type()) { case QEvent::ScrollPrepare: { QScrollPrepareEvent* prepareEvent = static_cast<QScrollPrepareEvent*>(event); const QRectF viewportRect = m_viewport->boundingRect(); const QRectF contentRect = m_viewport->mapRectFromItem(m_content, m_content->boundingRect()); const QRectF posRange = computePosRangeForItemAtScale(m_content->contentsScale()); prepareEvent->setContentPosRange(posRange); prepareEvent->setViewportSize(viewportRect.size()); // As we want to push the contents and not actually scroll it, we need to invert the positions here. prepareEvent->setContentPos(-contentRect.topLeft()); prepareEvent->accept(); return true; } case QEvent::Scroll: { QScrollEvent* scrollEvent = static_cast<QScrollEvent*>(event); QPointF newPos = -scrollEvent->contentPos() - scrollEvent->overshootDistance(); if (m_content->pos() != newPos) { QPointF currentPosInCSSCoordinates = m_viewport->mapToWebContent(m_content->pos()); QPointF newPosInCSSCoordinates = m_viewport->mapToWebContent(newPos); // This must be emitted before viewportUpdateRequested so that the web process knows where to look for tiles. emit viewportTrajectoryVectorChanged(currentPosInCSSCoordinates - newPosInCSSCoordinates); m_content->setPos(newPos); } return true; } default: break; } return QObject::event(event); }
bool PlotWidget::eventFilter(QObject *obj, QEvent *ev) { if (ev->type() == QEvent::Scroll) { QScrollEvent *se = static_cast<QScrollEvent *>(ev); QScroller *scroller = QScroller::scroller(m_widget); QPointF v = scroller->velocity(); //v.rx() *= scroller->pixelPerMeter().x(); //v.ry() *= scroller->pixelPerMeter().y(); PlotItem pi = { v, se->contentPos(), se->overshootDistance() }; addPlotItem(pi); } return QWidget::eventFilter(obj, ev); }