From 81b39250cb9466bc00ed41efaabd66afeaa3c6d2 Mon Sep 17 00:00:00 2001 From: Giulio Camuffo Date: Sun, 4 Oct 2009 20:08:37 +0000 Subject: [PATCH] added ensureItemVisible(QGraphicsItem*) svn path=/trunk/KDE/kdelibs/; revision=1031380 --- widgets/scrollwidget.cpp | 70 +++++++++++++++++++++++++++++----------- widgets/scrollwidget.h | 10 +++++- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/widgets/scrollwidget.cpp b/widgets/scrollwidget.cpp index f7899a4c6..0540c1b4d 100644 --- a/widgets/scrollwidget.cpp +++ b/widgets/scrollwidget.cpp @@ -24,6 +24,7 @@ #include #include #include +#include //KDE #include @@ -182,6 +183,38 @@ public: scrollingWidget->setFlag(QGraphicsItem::ItemClipsChildrenToShape, true); } + void makeRectVisible() + { + QRectF viewRect = scrollingWidget->boundingRect(); + //ensure the rect is not outside the widget bounding rect + QRectF mappedRect = QRectF(QPointF(qBound((qreal)0.0, rectToBeVisible.x(), widget->size().width() - rectToBeVisible.width()), + qBound((qreal)0.0, rectToBeVisible.y(), widget->size().height() - rectToBeVisible.height())), + rectToBeVisible.size()); + mappedRect = widget->mapToItem(scrollingWidget, mappedRect).boundingRect(); + + if (viewRect.contains(mappedRect)) { + return; + } + + QPointF delta(0, 0); + + if (mappedRect.top() < 0) { + delta.setY(-mappedRect.top()); + } else if (mappedRect.bottom() > viewRect.bottom()) { + delta.setY(viewRect.bottom() - mappedRect.bottom()); + } + + if (mappedRect.left() < 0) { + delta.setX(-mappedRect.left()); + } else if (mappedRect.right() > viewRect.right()) { + delta.setX(viewRect.right() - mappedRect.right()); + } + + animId = Animator::self()->moveItem( + widget, Plasma::Animator::SlideOutMovement, + (widget->pos() + delta).toPoint()); + } + ScrollWidget *q; QGraphicsWidget *scrollingWidget; QGraphicsWidget *widget; @@ -196,6 +229,7 @@ public: ScrollBar *horizontalScrollBar; Qt::ScrollBarPolicy horizontalScrollBarPolicy; QString styleSheet; + QRectF rectToBeVisible; bool dragging; int animId; }; @@ -284,32 +318,30 @@ void ScrollWidget::ensureRectVisible(const QRectF &rect) return; } - QRectF viewRect = d->scrollingWidget->boundingRect(); - //ensure the rect is not outside the widget bounding rect - QRectF mappedRect = QRectF(QPointF(qBound((qreal)0.0, rect.x(), d->widget->size().width()-rect.width()), - qBound((qreal)0.0, rect.y(), d->widget->size().height()-rect.height())), rect.size()); - mappedRect = d->widget->mapToItem(d->scrollingWidget, mappedRect).boundingRect(); - if (viewRect.contains(mappedRect)) { + d->rectToBeVisible = rect; + d->makeRectVisible(); +} + +void ScrollWidget::ensureItemVisible(QGraphicsItem *item) +{ + if (!d->widget || !item) { return; } - QPointF delta(0, 0); + QGraphicsItem *parentOfItem = item->parentItem(); + while (parentOfItem != d->widget) { + if (!parentOfItem) { + return; + } - if (mappedRect.top() < 0) { - delta.setY(-mappedRect.top()); - } else if (mappedRect.bottom() > viewRect.bottom()) { - delta.setY(viewRect.bottom() - mappedRect.bottom()); + parentOfItem = parentOfItem->parentItem(); } - if (mappedRect.left() < 0) { - delta.setX(-mappedRect.left()); - } else if (mappedRect.right() > viewRect.right()) { - delta.setX(viewRect.right() - mappedRect.right()); - } + QRectF rect(d->widget->mapFromScene(item->scenePos()), item->boundingRect().size()); + d->rectToBeVisible = rect; - d->animId = Animator::self()->moveItem( - d->widget, Plasma::Animator::SlideOutMovement, - (d->widget->pos() + delta).toPoint()); + // We need to wait for the parent item to resize... + QTimer::singleShot(0, this, SLOT(makeRectVisible())); } qreal ScrollWidget::horizontalScrollValue() const diff --git a/widgets/scrollwidget.h b/widgets/scrollwidget.h index 983284408..4d90c855a 100644 --- a/widgets/scrollwidget.h +++ b/widgets/scrollwidget.h @@ -106,6 +106,14 @@ public: */ void ensureRectVisible(const QRectF &rect); + /** + * Scroll the view until the given item is visible + * + * @param item item we want visible + * @since 4.4 + */ + void ensureItemVisible(QGraphicsItem *item); + /** * The horizontal scroll value, between 0 and 100 * @since 4.4 @@ -169,7 +177,7 @@ private: Q_PRIVATE_SLOT(d, void verticalScroll(int value)) Q_PRIVATE_SLOT(d, void horizontalScroll(int value)) - + Q_PRIVATE_SLOT(d, void makeRectVisible()) }; } // namespace Plasma