void QtViewportInteractionEngine::wheelEvent(QWheelEvent* ev) { if (scrollAnimationActive() || scaleAnimationActive() || pinchGestureActive()) return; // Ignore. // A normal scroll-tick should have a delta of 120 (1/8) degrees. Convert this to // local standard scroll step of 3 lines of 20 pixels. static const int cDefaultQtScrollStep = 20; static const int wheelScrollLines = 3; const int wheelTick = wheelScrollLines * cDefaultQtScrollStep; int pixelDelta = ev->delta() * (wheelTick / 120.f); QPointF newPosition = m_viewport->contentPos(); if (ev->orientation() == Qt::Horizontal) newPosition.rx() -= pixelDelta; else newPosition.ry() -= pixelDelta; QRectF endPosRange = computePosRangeForItemAtScale(m_content->contentsScale()); QPointF currentPosition = m_viewport->contentPos(); newPosition = boundPosition(endPosRange.topLeft(), newPosition, endPosRange.bottomRight()); m_viewport->setContentPos(newPosition); emit contentViewportChanged(currentPosition - newPosition); }
void QtViewportInteractionEngine::focusEditableArea(const QRectF& caretArea, const QRectF& targetArea) { QRectF endArea = itemRectFromCSS(targetArea); qreal endItemScale = itemScaleFromCSS(innerBoundedCSSScale(2.0)); const QRectF viewportRect = m_viewport->boundingRect(); qreal x; const qreal borderOffset = 10; if ((endArea.width() + borderOffset) * endItemScale <= viewportRect.width()) { // Center the input field in the middle of the view, if it is smaller than // the view at the scale target. x = viewportRect.center().x() - endArea.width() * endItemScale / 2.0; } else { // Ensure that the caret always has borderOffset contents pixels to the right // of it, and secondarily (if possible), that the area has borderOffset // contents pixels to the left of it. qreal caretOffset = itemCoordFromCSS(caretArea.x()) - endArea.x(); x = qMin(viewportRect.width() - (caretOffset + borderOffset) * endItemScale, borderOffset * endItemScale); } const QPointF hotspot = QPointF(endArea.x(), endArea.center().y()); const QPointF viewportHotspot = QPointF(x, /* FIXME: visibleCenter */ viewportRect.center().y()); QPointF endPosition = hotspot * endItemScale - viewportHotspot; QRectF endPosRange = computePosRangeForItemAtScale(endItemScale); endPosition = boundPosition(endPosRange.topLeft(), endPosition, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, viewportRect.size() / endItemScale); animateItemRectVisible(endVisibleContentRect); }
bool QtViewportInteractionEngine::ensureContentWithinViewportBoundary(bool immediate) { ASSERT(m_suspendCount); if (scrollAnimationActive() || scaleAnimationActive()) return false; qreal endItemScale = itemScaleFromCSS(innerBoundedCSSScale(currentCSSScale())); const QRectF viewportRect = m_viewport->boundingRect(); QPointF viewportHotspot = viewportRect.center(); QPointF endPosition = m_viewport->mapToWebContent(viewportHotspot) * endItemScale - viewportHotspot; QRectF endPosRange = computePosRangeForItemAtScale(endItemScale); endPosition = boundPosition(endPosRange.topLeft(), endPosition, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, viewportRect.size() / endItemScale); if (immediate) { setItemRectVisible(endVisibleContentRect); return true; } return !animateItemRectVisible(endVisibleContentRect); }
void QtViewportInteractionEngine::zoomToAreaGestureEnded(const QPointF& touchPoint, const QRectF& targetArea) { if (!targetArea.isValid()) return; if (scrollAnimationActive() || scaleAnimationActive()) return; const int margin = 10; // We want at least a little bit or margin. QRectF endArea = itemRectFromCSS(targetArea.adjusted(-margin, -margin, margin, margin)); const QRectF viewportRect = m_viewport->boundingRect(); qreal targetCSSScale = cssScaleFromItem(viewportRect.size().width() / endArea.size().width()); qreal endItemScale = itemScaleFromCSS(innerBoundedCSSScale(qMin(targetCSSScale, qreal(2.5)))); // We want to end up with the target area filling the whole width of the viewport (if possible), // and centralized vertically where the user requested zoom. Thus our hotspot is the center of // the targetArea x-wise and the requested zoom position, y-wise. const QPointF hotspot = QPointF(endArea.center().x(), touchPoint.y() * m_constraints.devicePixelRatio); const QPointF viewportHotspot = viewportRect.center(); QPointF endPosition = hotspot * endItemScale - viewportHotspot; QRectF endPosRange = computePosRangeForItemAtScale(endItemScale); endPosition = boundPosition(endPosRange.topLeft(), endPosition, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, viewportRect.size() / endItemScale); animateItemRectVisible(endVisibleContentRect); }
void QtViewportInteractionEngine::wheelEvent(QWheelEvent* ev) { if (scrollAnimationActive() || scaleAnimationActive() || pinchGestureActive()) return; // Ignore. int delta = ev->delta(); // A delta that is not mod 120 indicates a device that is sending // fine-resolution scroll events, so use the delta as number of wheel ticks // and number of pixels to scroll. See also webkit.org/b/29601 bool fullTick = !(delta % 120); static const int cDefaultQtScrollStep = 20; static const int wheelScrollLines = 3; int scrollLines = (fullTick) ? wheelScrollLines * cDefaultQtScrollStep : 1; delta = (fullTick) ? delta / 120.0f : delta; delta *= scrollLines; QPointF newPosition = m_viewport->contentPos(); if (ev->orientation() == Qt::Horizontal) newPosition.rx() += delta; else newPosition.ry() += delta; QRectF endPosRange = computePosRangeForItemAtScale(m_content->contentsScale()); QPointF currentPosition = m_viewport->contentPos(); newPosition = boundPosition(endPosRange.topLeft(), newPosition, endPosRange.bottomRight()); m_viewport->setContentPos(newPosition); emit contentViewportChanged(currentPosition - newPosition); }
void BoidFlock::update() { for (unsigned i = 0; i < boidCount; i++) { Boid *b = boids[i]; vec3 v1 = rule1(b); vec3 v2 = rule2(b); vec3 v3 = rule3(b); vec3 pos = boundPosition(b); b->incVelocity((v1 + v2 + v3 + pos) / 100.0f); limitSpeed(b); b->update(); } }
void QtViewportInteractionEngine::pagePositionRequest(const QPoint& pagePosition) { // Ignore the request if suspended. Can only happen due to delay in event delivery. if (m_suspendCount) return; qreal endItemScale = m_content->contentsScale(); // Stay at same scale. QRectF endPosRange = computePosRangeForItemAtScale(endItemScale); QPointF endPosition = boundPosition(endPosRange.topLeft(), pagePosition * endItemScale, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, m_viewport->boundingRect().size() / endItemScale); setItemRectVisible(endVisibleContentRect); }
QRectF QtViewportHandler::nearestValidBounds() const { qreal endItemScale = itemScaleFromCSS(innerBoundedCSSScale(currentCSSScale())); const QRectF viewportRect = m_viewportItem->boundingRect(); QPointF viewportHotspot = viewportRect.center(); QPointF endPosition = m_viewportItem->mapToWebContent(viewportHotspot) * endItemScale - viewportHotspot; QRectF endPosRange = computePosRangeForPageItemAtScale(endItemScale); endPosition = boundPosition(endPosRange.topLeft(), endPosition, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, viewportRect.size() / endItemScale); return endVisibleContentRect; }
void QtViewportHandler::pageContentPositionRequested(const QPoint& cssPosition) { // Ignore the request if suspended. Can only happen due to delay in event delivery. if (m_suspendCount) return; qreal endItemScale = m_pageItem->contentsScale(); // Stay at same scale. QRectF endPosRange = computePosRangeForPageItemAtScale(endItemScale); QPointF endPosition = boundPosition(endPosRange.topLeft(), cssPosition * endItemScale, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, m_viewportItem->boundingRect().size() / endItemScale); ViewportUpdateDeferrer guard(this); setPageItemRectVisible(endVisibleContentRect); }
void QtViewportHandler::zoomToAreaGestureEnded(const QPointF& touchPoint, const QRectF& targetArea) { // This can only happen as a result of a user interaction. ASSERT(m_hadUserInteraction); if (!targetArea.isValid()) return; if (m_suspendCount) return; const int margin = 10; // We want at least a little bit of margin. QRectF endArea = itemRectFromCSS(targetArea.adjusted(-margin, -margin, margin, margin)); const QRectF viewportRect = m_viewportItem->boundingRect(); qreal targetCSSScale = viewportRect.size().width() / endArea.size().width(); qreal endCSSScale = innerBoundedCSSScale(qMin(targetCSSScale, qreal(2.5))); qreal endItemScale = itemScaleFromCSS(endCSSScale); qreal currentScale = m_pageItem->contentsScale(); // We want to end up with the target area filling the whole width of the viewport (if possible), // and centralized vertically where the user requested zoom. Thus our hotspot is the center of // the targetArea x-wise and the requested zoom position, y-wise. const QPointF hotspot = QPointF(endArea.center().x(), itemCoordFromCSS(touchPoint.y())); const QPointF viewportHotspot = viewportRect.center(); QPointF endPosition = hotspot * endCSSScale - viewportHotspot; QRectF endPosRange = computePosRangeForPageItemAtScale(endItemScale); endPosition = boundPosition(endPosRange.topLeft(), endPosition, endPosRange.bottomRight()); QRectF endVisibleContentRect(endPosition / endItemScale, viewportRect.size() / endItemScale); enum { ZoomIn, ZoomBack, ZoomOut, NoZoom } zoomAction = ZoomIn; if (!m_scaleStack.isEmpty()) { // Zoom back out if attempting to scale to the same current scale, or // attempting to continue scaling out from the inner most level. // Use fuzzy compare with a fixed error to be able to deal with largish differences due to pixel rounding. if (fuzzyCompare(endItemScale, currentScale, 0.01)) { // If moving the viewport would expose more of the targetRect and move at least 40 pixels, update position but do not scale out. QRectF currentContentRect(m_viewportItem->contentPos() / currentScale, viewportRect.size() / currentScale); QRectF targetIntersection = endVisibleContentRect.intersected(targetArea); if (!currentContentRect.contains(targetIntersection) && (qAbs(endVisibleContentRect.top() - currentContentRect.top()) >= 40 || qAbs(endVisibleContentRect.left() - currentContentRect.left()) >= 40)) zoomAction = NoZoom; else zoomAction = ZoomBack; } else if (fuzzyCompare(endItemScale, m_zoomOutScale, 0.01)) zoomAction = ZoomBack; else if (endItemScale < currentScale) zoomAction = ZoomOut; } switch (zoomAction) { case ZoomIn: m_scaleStack.append(ScaleStackItem(currentScale, m_viewportItem->contentPos().x())); m_zoomOutScale = endItemScale; break; case ZoomBack: { ScaleStackItem lastScale = m_scaleStack.takeLast(); endItemScale = lastScale.scale; endCSSScale = cssScaleFromItem(lastScale.scale); // Recalculate endPosition and bound it according to new scale. endPosition.setY(hotspot.y() * endCSSScale - viewportHotspot.y()); endPosition.setX(lastScale.xPosition); endPosRange = computePosRangeForPageItemAtScale(endItemScale); endPosition = boundPosition(endPosRange.topLeft(), endPosition, endPosRange.bottomRight()); endVisibleContentRect = QRectF(endPosition / endItemScale, viewportRect.size() / endItemScale); break; } case ZoomOut: // Unstack all scale-levels deeper than the new level, so a zoom-back won't end up zooming in. while (!m_scaleStack.isEmpty() && m_scaleStack.last().scale >= endItemScale) m_scaleStack.removeLast(); m_zoomOutScale = endItemScale; break; case NoZoom: break; } animatePageItemRectVisible(endVisibleContentRect); }