Example #1
0
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 AbstractWheelWidget::event(QEvent *e)
{
    switch (e->type()) {
// ![1]
    case QEvent::ScrollPrepare:
    {
        // We set the snap positions as late as possible so that we are sure
        // we get the correct itemHeight
        QScroller *scroller = QScroller::scroller(this);
        scroller->setSnapPositionsY( WHEEL_SCROLL_OFFSET, itemHeight() );

        QScrollPrepareEvent *se = static_cast<QScrollPrepareEvent *>(e);
        se->setViewportSize(QSizeF(size()));
        // we claim a huge scrolling area and a huge content position and
        // hope that the user doesn't notice that the scroll area is restricted
        se->setContentPosRange(QRectF(0.0, 0.0, 0.0, WHEEL_SCROLL_OFFSET * 2));
        se->setContentPos(QPointF(0.0, WHEEL_SCROLL_OFFSET + m_currentItem * itemHeight() + m_itemOffset));
        se->accept();
        return true;
    }
// ![1]
// ![2]
    case QEvent::Scroll:
    {
        QScrollEvent *se = static_cast<QScrollEvent *>(e);

        qreal y = se->contentPos().y();
        int iy = y - WHEEL_SCROLL_OFFSET;
        int ih = itemHeight();

// ![2]
// ![3]
        // -- calculate the current item position and offset and redraw the widget
        int ic = itemCount();
        if (ic>0) {
            m_currentItem = iy / ih % ic;
            m_itemOffset = iy % ih;

            // take care when scrolling backwards. Modulo returns negative numbers
            if (m_itemOffset < 0) {
                m_itemOffset += ih;
                m_currentItem--;
            }

            if (m_currentItem < 0)
                m_currentItem += ic;
        }
        // -- repaint
        update();

        se->accept();
        return true;
// ![3]
    }
    default:
        return QWidget::event(e);
    }
    return true;
}
Example #4
0
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);
}
Example #5
0
bool SwipeCover::event(QEvent *e)
{
    switch (e->type()) {
    case QEvent::ScrollPrepare:
    {
        for(int i=0; i<delta; i++)
            posLst.append(curPos);
        QScrollPrepareEvent *se = static_cast<QScrollPrepareEvent *>(e);
        se->setViewportSize(QSizeF(parentSize));
        se->setContentPosRange(QRectF(0.0, 0.0, 0.0,range));
        se->setContentPos(QPointF(0.0, curPos));
        se->accept();
        isScrolled=false;
        qDebug()<<"ScrollPrepare";
        return true;
    }
    case QEvent::Scroll:
    {
        QScrollEvent *se = static_cast<QScrollEvent *>(e);
        isScrolled = true;
        curPos  =  se->contentPos().y();
        scrollBar->setValue((int)(curPos/k));
        qDebug() <<"Scroll: " << curPos;
        int ic = 20;
        if (ic>0) {
        }
        se->accept();
        return true;
    }
    case QEvent::MouseButtonPress:
        isScrolled = false;
        return true;
    case QEvent::MouseButtonRelease:
        if(!isScrolled)
        {
            qDebug()<<"MouseButtonClick!!! "<<((QMouseEvent*)e)->pos();
            emit onClick(((QMouseEvent*)e)->pos()+QPoint(0,scrollBar->value()));
        }
        isScrolled = false;
        return true;

    default:
        return QWidget::event(e);
    }
    return true;

}