On Tuesday 23 March 2010 13:53:24 Marco Martin wrote:
> in that way the whole webview would flick instead of the webview contents?
>  (webview could also be resized to the whole contents but this would be
>  terribly inefficient right?)

Ah, I see, that's the core of the issue. Yea, the scrolling of contents 
behavior should probably be configurable with properties just for cases like 
this. I don't quite have the time right now to do it in full but the attached 
should be 90% there. Maybe you'll like it better than the other approaches.
It basically checks whether the widget which the ScrollWidget is supposed to 
scroll has defined the following properties:
- contentsSize
- scrollPosition
- scrollPositionX
- scrollPositionY
and if it does it uses those, instead of statically calling widget->size(), 
widget->pos(), widget->x and widget->y. 

It should compile/work, but iirc the position to scrollPosition function needs 
to be negated somewhere (especially visible since the webview scrolling will 
now be inverted and it will probably try to overshoot all the time =) ), I 
don't quite have the time right now to start compiling and testing to see 
where that position needs to be negated but I think this approach would be 
more convenient and cleaner. Especially since it means that the ScrollWidget 
would be able to just handle really anything anyone could possibly throw at it 
and it would just work (tm) in all cases. 

(also possibly viewportGeometry will have to be overwritable for completness 
as well).

z
Index: widgets/scrollwidget.cpp
===================================================================
--- widgets/scrollwidget.cpp	(revision 1106171)
+++ widgets/scrollwidget.cpp	(working copy)
@@ -140,6 +140,11 @@
         hasOvershoot = true;
 
         alignment = Qt::AlignLeft | Qt::AlignTop;
+
+        hasContentsProperty = false;
+        hasOffsetProperty = false;
+        hasXProperty = false;
+        hasYProperty = false;
     }
 
     void adjustScrollbars()
@@ -380,12 +385,12 @@
     }
     void flickX(qreal velocity)
     {
-        flick(flickAnimationX, velocity, widget.data()->x(), minXExtent(), maxXExtent(),
+        flick(flickAnimationX, velocity, widgetX(), minXExtent(), maxXExtent(),
               q->viewportGeometry().width());
     }
     void flickY(qreal velocity)
     {
-        flick(flickAnimationY, velocity, widget.data()->y(),minYExtent(), maxYExtent(),
+        flick(flickAnimationY, velocity, widgetY(),minYExtent(), maxYExtent(),
               q->viewportGeometry().height());
     }
     void fixup(QAnimationGroup *group,
@@ -427,12 +432,12 @@
     void fixupX()
     {
         fixup(fixupAnimation.groupX, fixupAnimation.startX, fixupAnimation.endX,
-              widget.data()->x(), minXExtent(), maxXExtent());
+              widgetX(), minXExtent(), maxXExtent());
     }
     void fixupY()
     {
         fixup(fixupAnimation.groupY, fixupAnimation.startY, fixupAnimation.endY,
-              widget.data()->y(), minYExtent(), maxYExtent());
+              widgetY(), minYExtent(), maxYExtent());
     }
 
     void makeRectVisible()
@@ -496,6 +501,35 @@
         fixupAnimation.groupY->stop();
     }
 
+    void setWidgetX(qreal x)
+    {
+        if (hasXProperty) {
+            widget.data()->setProperty("scrollPositionX", x);
+        } else
+            widget.data()->setX(x);
+    }
+    void setWidgetY(qreal y)
+    {
+        if (hasYProperty) {
+            widget.data()->setProperty("scrollPositionY", y);
+        } else
+            widget.data()->setY(y);
+    }
+    qreal widgetX() const
+    {
+        if (hasXProperty) {
+            return widget.data()->property("scrollPositionX").toReal();
+        } else
+            return widget.data()->x();
+    }
+    qreal widgetY() const
+    {
+        if (hasYProperty) {
+            return widget.data()->property("scrollPositionY").toReal();
+        } else
+            return widget.data()->y();
+    }
+
     void handleMousePressEvent(QGraphicsSceneMouseEvent *event)
     {
         lastPos = QPoint();
@@ -533,7 +567,7 @@
                         rejectY = true;
                 }
                 if (!rejectY && stealEvent) {
-                    widget.data()->setY(qRound(newY));
+                    setWidgetY(qRound(newY));
                     moved = true;
                 }
                 if (qAbs(dy) > QApplication::startDragDistance())
@@ -560,7 +594,7 @@
                         rejectX = true;
                 }
                 if (!rejectX && stealEvent) {
-                    widget.data()->setX(qRound(newX));
+                    setWidgetX(qRound(newX));
                     moved = true;
                 }
 
@@ -741,10 +775,18 @@
     void createFlickAnimations()
     {
         if (widget.data()) {
+            QString xProp = QString::fromLatin1("x");
+            QString yProp = QString::fromLatin1("y");
+
+            if (hasXProperty)
+                xProp = QString::fromLatin1("scrollPositionX");
+            if (hasYProperty)
+                yProp = QString::fromLatin1("scrollPositionY");
+
             flickAnimationX = new QPropertyAnimation(widget.data(),
-                                                     "x", widget.data());
+                                                     xProp.toLatin1(), widget.data());
             flickAnimationY = new QPropertyAnimation(widget.data(),
-                                                     "y", widget.data());
+                                                     yProp.toLatin1(), widget.data());
             QObject::connect(flickAnimationX, SIGNAL(finished()),
                              q, SLOT(fixupX()));
             QObject::connect(flickAnimationY, SIGNAL(finished()),
@@ -768,13 +810,13 @@
             fixupAnimation.groupX = new QSequentialAnimationGroup(widget.data());
             fixupAnimation.groupY = new QSequentialAnimationGroup(widget.data());
             fixupAnimation.startX  = new QPropertyAnimation(widget.data(),
-                                                            "x", widget.data());
+                                                            xProp.toLatin1(), widget.data());
             fixupAnimation.startY  = new QPropertyAnimation(widget.data(),
-                                                            "y", widget.data());
+                                                            yProp.toLatin1(), widget.data());
             fixupAnimation.endX = new QPropertyAnimation(widget.data(),
-                                                         "x", widget.data());
+                                                         xProp.toLatin1(), widget.data());
             fixupAnimation.endY = new QPropertyAnimation(widget.data(),
-                                                         "y", widget.data());
+                                                         yProp.toLatin1(), widget.data());
             fixupAnimation.groupX->addAnimation(
                 fixupAnimation.startX);
             fixupAnimation.groupY->addAnimation(
@@ -800,9 +842,9 @@
                              q, SIGNAL(scrollStateChanged(QAbstractAnimation::State,
                                                           QAbstractAnimation::State)));
 
-            directMoveAnimation = new QPropertyAnimation(widget.data(),
-                                                         "pos",
-                                                         widget.data());
+            directMoveAnimation = new QPropertyAnimation(q,
+                                                         "scrollPosition",
+                                                         q);
             QObject::connect(directMoveAnimation, SIGNAL(finished()),
                              q, SLOT(fixupX()));
             QObject::connect(directMoveAnimation, SIGNAL(finished()),
@@ -887,6 +929,11 @@
     bool hasOvershoot;
 
     Qt::Alignment alignment;
+
+    bool hasContentsProperty;
+    bool hasOffsetProperty;
+    bool hasXProperty;
+    bool hasYProperty;
 };
 
 
@@ -918,9 +965,14 @@
     }
 
     d->widget = widget;
-    d->createFlickAnimations();
     //it's not good it's setting a size policy here, but it's done to be retrocompatible with older applications
     if (widget) {
+        d->hasContentsProperty = widget->property("contentsSize").isValid();
+        d->hasOffsetProperty = widget->property("scrollPosition").isValid();
+        d->hasXProperty = widget->property("scrollPositionX").isValid();
+        d->hasYProperty = widget->property("scrollPositionY").isValid();
+        d->createFlickAnimations();
+
         connect(widget, SIGNAL(xChanged()), this, SLOT(setScrollX()));
         connect(widget, SIGNAL(yChanged()), this, SLOT(setScrollY()));
         widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
@@ -998,11 +1050,13 @@
 
 void ScrollWidget::registerAsDragHandle(QGraphicsWidget *item)
 {
+    Q_UNUSED(item);
     return;
 }
 
 void ScrollWidget::unregisterAsDragHandle(QGraphicsWidget *item)
 {
+    Q_UNUSED(item);
     return;
 }
 
@@ -1018,19 +1072,36 @@
 
 QSizeF ScrollWidget::contentsSize() const
 {
-    return d->widget ? d->widget.data()->size() : QSizeF();
+    if (d->widget) {
+        if (d->hasContentsProperty) {
+            QVariant var = d->widget.data()->property("contentsSize");
+            return var.toSizeF();
+        } else
+            return d->widget.data()->size();
+    }
+    return QSizeF();
 }
 
 void ScrollWidget::setScrollPosition(const QPointF &position)
 {
     if (d->widget) {
-        d->widget.data()->setPos(-position.toPoint());
+        if (d->hasOffsetProperty)
+            d->widget.data()->setProperty("scrollPosition", -position);
+        else
+            d->widget.data()->setPos(-position.toPoint());
     }
 }
 
 QPointF ScrollWidget::scrollPosition() const
 {
-    return d->widget ? -d->widget.data()->pos() : QPointF();
+    if (d->widget) {
+        if (d->hasOffsetProperty) {
+            QVariant var = d->widget.data()->property("scrollPosition");
+            return -var.toPointF();
+        } else
+            -d->widget.data()->pos();
+    }
+    return QPointF();
 }
 
 void ScrollWidget::setStyleSheet(const QString &styleSheet)
@@ -1163,7 +1234,7 @@
 {
     //only the scrolling widget and its children
     if (!d->widget.data() ||
-        (!d->widget.data()->isAncestorOf(i) && i != d->scrollingWidget))
+        (!d->scrollingWidget->isAncestorOf(i) && i != d->scrollingWidget))
         return false;
 
     bool stealThisEvent = d->stealEvent;
Index: widgets/webview.cpp
===================================================================
--- widgets/webview.cpp	(revision 1106171)
+++ widgets/webview.cpp	(working copy)
@@ -35,6 +35,7 @@
 #include "animator.h"
 #include "plasma.h"
 #include "widgets/webview.h"
+#include "widgets/scrollwidget.h"
 #include "private/animablegraphicswebview_p.h"
 
 namespace Plasma
@@ -53,6 +54,7 @@
 
     WebView *q;
     AnimableGraphicsWebView *webView;
+    ScrollWidget *scrollWidget;
     bool loaded;
 };
 
@@ -66,14 +68,16 @@
     setAcceptsHoverEvents(true);
     setFlags(QGraphicsItem::ItemIsFocusable);
 
-    d->webView = new AnimableGraphicsWebView(this);
-    d->webView->setDragToScroll(false);
+    d->scrollWidget = new Plasma::ScrollWidget(this);
+    d->webView = new AnimableGraphicsWebView(d->scrollWidget);
+    d->scrollWidget->setWidget(d->webView);
+    d->scrollWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    d->scrollWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
+    d->webView->setDragToScroll(true);
     QPalette palette = qApp->palette();
     palette.setBrush(QPalette::Base, Qt::transparent);
     d->webView->page()->setPalette(palette);
 
-    Plasma::Animator::self()->registerScrollingManager(d->webView);
-
     connect(d->webView, SIGNAL(loadProgress(int)),
             this, SIGNAL(loadProgress(int)));
     connect(d->webView, SIGNAL(loadFinished(bool)),
@@ -271,7 +275,8 @@
 void WebView::setGeometry(const QRectF &geometry)
 {
     QGraphicsWidget::setGeometry(geometry);
-    d->webView->setGeometry(QRectF(0, 0, geometry.width(), geometry.height()));
+    d->scrollWidget->setGeometry(QRectF(0, 0, geometry.width(), geometry.height()));
+    d->webView->setGeometry(d->scrollWidget->viewportGeometry());
 }
 
 QSizeF WebView::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
Index: private/animablegraphicswebview_p.h
===================================================================
--- private/animablegraphicswebview_p.h	(revision 1106171)
+++ private/animablegraphicswebview_p.h	(working copy)
@@ -38,6 +38,8 @@
 
     Q_PROPERTY(qreal zoomFactor READ zoom WRITE setZoom)
     Q_PROPERTY(QPointF scrollPosition READ scrollPosition WRITE setScrollPosition)
+    Q_PROPERTY(qreal scrollPositionX READ scrollPositionX WRITE setScrollPositionX)
+    Q_PROPERTY(qreal scrollPositionY READ scrollPositionY WRITE setScrollPositionY)
     Q_PROPERTY(QSizeF contentsSize READ contentsSize)
     Q_PROPERTY(QRectF viewportGeometry READ viewportGeometry)
 
@@ -46,6 +48,10 @@
 
     QPointF scrollPosition() const;
     void setScrollPosition(const QPointF &position);
+    qreal scrollPositionX() const;
+    void setScrollPositionX(qreal position);
+    qreal scrollPositionY() const;
+    void setScrollPositionY(qreal position);
     QSizeF contentsSize() const;
     QRectF viewportGeometry() const;
     void setDragToScroll(bool enable);
Index: private/animablegraphicswebview.cpp
===================================================================
--- private/animablegraphicswebview.cpp	(revision 1106171)
+++ private/animablegraphicswebview.cpp	(working copy)
@@ -165,5 +165,28 @@
     event->setAccepted(!m_dragToScroll);
 }
 
+qreal AnimableGraphicsWebView::scrollPositionX() const
+{
+    return page()->mainFrame()->scrollPosition().x();
+}
+
+void AnimableGraphicsWebView::setScrollPositionX(qreal position)
+{
+    QPointF pt(position, scrollPositionY());
+    setScrollPosition(pt);
+}
+
+qreal AnimableGraphicsWebView::scrollPositionY() const
+{
+    return page()->mainFrame()->scrollPosition().y();
+}
+
+void AnimableGraphicsWebView::setScrollPositionY(qreal position)
+{
+    QPointF pt(scrollPositionX(), position);
+    setScrollPosition(pt);
+}
+
+
 #include "animablegraphicswebview_p.moc"
 
_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel

Reply via email to