2009-03-03 18:31:54 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Marco Martin <notmart@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "scrollwidget.h"
|
|
|
|
//Qt
|
|
|
|
#include <QGraphicsSceneResizeEvent>
|
|
|
|
#include <QGraphicsGridLayout>
|
2009-11-06 19:57:35 +01:00
|
|
|
#include <QGraphicsScene>
|
2009-03-03 18:31:54 +01:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QWidget>
|
2009-10-04 22:08:37 +02:00
|
|
|
#include <QTimer>
|
2009-03-03 18:31:54 +01:00
|
|
|
|
|
|
|
//KDE
|
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <kdebug.h>
|
2009-11-13 17:03:02 +01:00
|
|
|
#include <kglobalsettings.h>
|
2009-03-03 18:31:54 +01:00
|
|
|
|
|
|
|
//Plasma
|
|
|
|
#include <plasma/widgets/scrollbar.h>
|
2009-08-03 22:41:42 +02:00
|
|
|
#include <plasma/widgets/svgwidget.h>
|
2009-09-24 22:40:16 +02:00
|
|
|
#include <plasma/animator.h>
|
2009-08-03 22:41:42 +02:00
|
|
|
#include <plasma/svg.h>
|
2009-03-03 18:31:54 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-10-20 00:10:25 +02:00
|
|
|
class ScrollWidgetPrivate
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ScrollWidgetPrivate(ScrollWidget *parent)
|
|
|
|
: q(parent),
|
|
|
|
widget(0),
|
2009-08-03 22:41:42 +02:00
|
|
|
topBorder(0),
|
|
|
|
bottomBorder(0),
|
|
|
|
leftBorder(0),
|
|
|
|
rightBorder(0),
|
2009-09-24 22:40:16 +02:00
|
|
|
dragging(false),
|
|
|
|
animId(0)
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScrollWidgetPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void adjustScrollbars()
|
|
|
|
{
|
|
|
|
verticalScrollBar->nativeWidget()->setMaximum(qMax(0, int((widget->size().height() - scrollingWidget->size().height())/10)));
|
|
|
|
|
|
|
|
if (verticalScrollBarPolicy == Qt::ScrollBarAlwaysOff ||
|
|
|
|
verticalScrollBar->nativeWidget()->maximum() == 0) {
|
2009-10-07 21:27:01 +02:00
|
|
|
if (layout->count() > 2 && layout->itemAt(2) == verticalScrollBar) {
|
2009-03-03 18:31:54 +01:00
|
|
|
layout->removeAt(2);
|
2009-10-07 21:27:01 +02:00
|
|
|
} else if (layout->count() > 1 && layout->itemAt(1) == verticalScrollBar) {
|
2009-03-03 18:31:54 +01:00
|
|
|
layout->removeAt(1);
|
|
|
|
}
|
|
|
|
verticalScrollBar->hide();
|
2009-09-19 17:35:16 +02:00
|
|
|
} else if (!verticalScrollBar->isVisible()) {
|
|
|
|
layout->addItem(verticalScrollBar, 0, 1);
|
|
|
|
verticalScrollBar->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
horizontalScrollBar->nativeWidget()->setMaximum(qMax(0, int((widget->size().width() - scrollingWidget->size().width())/10)));
|
|
|
|
|
|
|
|
if (horizontalScrollBarPolicy == Qt::ScrollBarAlwaysOff ||
|
|
|
|
horizontalScrollBar->nativeWidget()->maximum() == 0) {
|
2009-10-07 21:27:01 +02:00
|
|
|
if (layout->count() > 2 && layout->itemAt(2) == horizontalScrollBar) {
|
2009-09-19 17:35:16 +02:00
|
|
|
layout->removeAt(2);
|
2009-10-07 21:27:01 +02:00
|
|
|
} else if (layout->count() > 1 && layout->itemAt(1) == horizontalScrollBar) {
|
2009-09-19 17:35:16 +02:00
|
|
|
layout->removeAt(1);
|
|
|
|
}
|
|
|
|
horizontalScrollBar->hide();
|
|
|
|
} else if (!horizontalScrollBar->isVisible()) {
|
|
|
|
layout->addItem(horizontalScrollBar, 1, 0);
|
|
|
|
horizontalScrollBar->show();
|
|
|
|
}
|
|
|
|
|
2009-09-30 19:05:40 +02:00
|
|
|
if (widget && !topBorder && widget->size().height() > q->size().height()) {
|
2009-08-03 22:41:42 +02:00
|
|
|
topBorder = new Plasma::SvgWidget(q);
|
|
|
|
topBorder->setSvg(borderSvg);
|
|
|
|
topBorder->setElementID("border-top");
|
|
|
|
topBorder->setZValue(900);
|
2009-10-21 21:47:20 +02:00
|
|
|
topBorder->resize(topBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
topBorder->show();
|
2009-08-03 22:41:42 +02:00
|
|
|
bottomBorder = new Plasma::SvgWidget(q);
|
|
|
|
bottomBorder->setSvg(borderSvg);
|
|
|
|
bottomBorder->setElementID("border-bottom");
|
|
|
|
bottomBorder->setZValue(900);
|
2009-10-21 21:47:20 +02:00
|
|
|
bottomBorder->resize(bottomBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
bottomBorder->show();
|
2009-10-02 22:17:45 +02:00
|
|
|
} else if (topBorder && widget && widget->size().height() <= q->size().height()) {
|
2009-09-30 19:05:40 +02:00
|
|
|
//FIXME: in some cases topBorder->deleteLater() is deleteNever(), why?
|
|
|
|
topBorder->hide();
|
|
|
|
bottomBorder->hide();
|
|
|
|
topBorder->deleteLater();
|
|
|
|
bottomBorder->deleteLater();
|
|
|
|
topBorder = 0;
|
|
|
|
bottomBorder = 0;
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-30 19:05:40 +02:00
|
|
|
if (widget && !leftBorder && widget->size().width() > q->size().width()) {
|
2009-08-03 22:41:42 +02:00
|
|
|
leftBorder = new Plasma::SvgWidget(q);
|
|
|
|
leftBorder->setSvg(borderSvg);
|
2009-08-14 19:26:15 +02:00
|
|
|
leftBorder->setElementID("border-left");
|
2009-08-03 22:41:42 +02:00
|
|
|
leftBorder->setZValue(900);
|
2009-10-21 21:47:20 +02:00
|
|
|
leftBorder->resize(leftBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
leftBorder->show();
|
2009-08-03 22:41:42 +02:00
|
|
|
rightBorder = new Plasma::SvgWidget(q);
|
|
|
|
rightBorder->setSvg(borderSvg);
|
2009-08-14 19:26:15 +02:00
|
|
|
rightBorder->setElementID("border-right");
|
2009-08-03 22:41:42 +02:00
|
|
|
rightBorder->setZValue(900);
|
2009-10-21 21:47:20 +02:00
|
|
|
rightBorder->resize(rightBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
rightBorder->show();
|
2009-10-02 22:17:45 +02:00
|
|
|
} else if (leftBorder && widget && widget->size().width() <= q->size().width()) {
|
2009-09-30 19:05:40 +02:00
|
|
|
leftBorder->hide();
|
|
|
|
rightBorder->hide();
|
|
|
|
leftBorder->deleteLater();
|
|
|
|
rightBorder->deleteLater();
|
|
|
|
leftBorder = 0;
|
|
|
|
rightBorder = 0;
|
2009-10-01 00:10:35 +02:00
|
|
|
}
|
2009-07-25 15:14:19 +02:00
|
|
|
|
|
|
|
layout->activate();
|
2009-08-03 22:41:42 +02:00
|
|
|
|
2009-10-21 21:47:20 +02:00
|
|
|
if (topBorder) {
|
|
|
|
topBorder->resize(q->size().width(), topBorder->size().height());
|
|
|
|
bottomBorder->resize(q->size().width(), bottomBorder->size().height());
|
|
|
|
bottomBorder->setPos(0, q->size().height() - topBorder->size().height());
|
|
|
|
}
|
|
|
|
if (leftBorder) {
|
|
|
|
leftBorder->resize(leftBorder->size().width(), q->size().height());
|
|
|
|
rightBorder->resize(rightBorder->size().width(), q->size().height());
|
|
|
|
rightBorder->setPos(q->size().width() - rightBorder->size().width(), 0);
|
|
|
|
}
|
|
|
|
|
2009-10-21 16:32:24 +02:00
|
|
|
QSizeF widgetSize = widget->size();
|
|
|
|
if (widget->sizePolicy().expandingDirections() & Qt::Horizontal) {
|
2009-10-21 16:48:14 +02:00
|
|
|
//keep a 1 pixel border
|
2009-11-05 20:21:25 +01:00
|
|
|
widgetSize.setWidth(scrollingWidget->size().width()-borderSize);
|
2009-10-21 16:32:24 +02:00
|
|
|
}
|
|
|
|
if (widget->sizePolicy().expandingDirections() & Qt::Vertical) {
|
2009-11-05 20:21:25 +01:00
|
|
|
widgetSize.setHeight(scrollingWidget->size().height()-borderSize);
|
2009-10-21 16:32:24 +02:00
|
|
|
}
|
|
|
|
widget->resize(widgetSize);
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void verticalScroll(int value)
|
|
|
|
{
|
|
|
|
if (!dragging) {
|
2009-10-13 12:10:08 +02:00
|
|
|
widget->setPos(QPoint(widget->pos().x(), -value*10));
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void horizontalScroll(int value)
|
|
|
|
{
|
|
|
|
if (!dragging) {
|
2009-10-13 12:10:08 +02:00
|
|
|
widget->setPos(QPoint(-value*10, widget->pos().y()));
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void adjustClipping()
|
|
|
|
{
|
2009-10-21 17:01:06 +02:00
|
|
|
const bool clip = widget->size().width() > scrollingWidget->size().width() || widget->size().height() > scrollingWidget->size().height();
|
|
|
|
|
|
|
|
scrollingWidget->setFlag(QGraphicsItem::ItemClipsChildrenToShape, clip);
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
2009-10-04 22:08:37 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2009-12-01 21:49:34 +01:00
|
|
|
void makeItemVisible()
|
|
|
|
{
|
|
|
|
QRectF rect(widget->mapFromScene(itemToBeVisible->scenePos()), itemToBeVisible->boundingRect().size());
|
|
|
|
rectToBeVisible = rect;
|
|
|
|
|
|
|
|
makeRectVisible();
|
|
|
|
}
|
|
|
|
|
2009-11-06 21:20:35 +01:00
|
|
|
void cleanupDragHandles(QObject *destroyed)
|
|
|
|
{
|
|
|
|
dragHandles.remove(static_cast<QGraphicsWidget *>(destroyed));
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:31:54 +01:00
|
|
|
ScrollWidget *q;
|
|
|
|
QGraphicsWidget *scrollingWidget;
|
|
|
|
QGraphicsWidget *widget;
|
2009-08-03 22:41:42 +02:00
|
|
|
Plasma::Svg *borderSvg;
|
|
|
|
Plasma::SvgWidget *topBorder;
|
|
|
|
Plasma::SvgWidget *bottomBorder;
|
|
|
|
Plasma::SvgWidget *leftBorder;
|
|
|
|
Plasma::SvgWidget *rightBorder;
|
2009-03-03 18:31:54 +01:00
|
|
|
QGraphicsGridLayout *layout;
|
|
|
|
ScrollBar *verticalScrollBar;
|
|
|
|
Qt::ScrollBarPolicy verticalScrollBarPolicy;
|
|
|
|
ScrollBar *horizontalScrollBar;
|
|
|
|
Qt::ScrollBarPolicy horizontalScrollBarPolicy;
|
|
|
|
QString styleSheet;
|
2009-12-01 21:49:34 +01:00
|
|
|
QGraphicsItem *itemToBeVisible;
|
2009-10-04 22:08:37 +02:00
|
|
|
QRectF rectToBeVisible;
|
2009-11-13 17:03:02 +01:00
|
|
|
QPointF dragHandleClicked;
|
2009-11-06 21:20:35 +01:00
|
|
|
QSet<QGraphicsWidget *>dragHandles;
|
2009-03-03 18:31:54 +01:00
|
|
|
bool dragging;
|
2009-09-24 22:40:16 +02:00
|
|
|
int animId;
|
2009-11-05 20:21:25 +01:00
|
|
|
static const int borderSize = 4;
|
2009-03-03 18:31:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ScrollWidget::ScrollWidget(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new ScrollWidgetPrivate(this))
|
|
|
|
{
|
2009-11-12 21:44:02 +01:00
|
|
|
setFocusPolicy(Qt::StrongFocus);
|
2009-03-03 18:31:54 +01:00
|
|
|
d->layout = new QGraphicsGridLayout(this);
|
2009-10-16 13:14:46 +02:00
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
2009-10-21 21:47:20 +02:00
|
|
|
d->layout->setContentsMargins(1, 1, 1, 1);
|
2009-03-03 18:31:54 +01:00
|
|
|
d->scrollingWidget = new QGraphicsWidget(this);
|
|
|
|
d->layout->addItem(d->scrollingWidget, 0, 0);
|
2009-08-03 22:41:42 +02:00
|
|
|
d->borderSvg = new Plasma::Svg(this);
|
|
|
|
d->borderSvg->setImagePath("widgets/scrollwidget");
|
2009-03-03 18:31:54 +01:00
|
|
|
|
|
|
|
d->verticalScrollBarPolicy = Qt::ScrollBarAsNeeded;
|
|
|
|
d->verticalScrollBar = new Plasma::ScrollBar(this);
|
2009-11-12 21:44:02 +01:00
|
|
|
d->verticalScrollBar->setFocusPolicy(Qt::NoFocus);
|
2009-03-03 18:31:54 +01:00
|
|
|
d->layout->addItem(d->verticalScrollBar, 0, 1);
|
|
|
|
d->verticalScrollBar->nativeWidget()->setMinimum(0);
|
|
|
|
d->verticalScrollBar->nativeWidget()->setMaximum(100);
|
|
|
|
connect(d->verticalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(verticalScroll(int)));
|
|
|
|
|
|
|
|
d->horizontalScrollBarPolicy = Qt::ScrollBarAsNeeded;
|
|
|
|
d->horizontalScrollBar = new Plasma::ScrollBar(this);
|
2009-11-12 21:44:02 +01:00
|
|
|
d->verticalScrollBar->setFocusPolicy(Qt::NoFocus);
|
2009-03-03 18:31:54 +01:00
|
|
|
d->horizontalScrollBar->setOrientation(Qt::Horizontal);
|
|
|
|
d->layout->addItem(d->horizontalScrollBar, 1, 0);
|
|
|
|
d->horizontalScrollBar->nativeWidget()->setMinimum(0);
|
|
|
|
d->horizontalScrollBar->nativeWidget()->setMaximum(100);
|
|
|
|
connect(d->horizontalScrollBar, SIGNAL(valueChanged(int)), this, SLOT(horizontalScroll(int)));
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollWidget::~ScrollWidget()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::setWidget(QGraphicsWidget *widget)
|
|
|
|
{
|
|
|
|
if (d->widget && d->widget != widget) {
|
|
|
|
d->widget->removeEventFilter(this);
|
|
|
|
delete d->widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->widget = widget;
|
2009-10-20 00:10:25 +02:00
|
|
|
Plasma::Animator::self()->registerScrollingManager(this);
|
2009-10-21 16:32:24 +02:00
|
|
|
//it's not good it's setting a size policy here, but it's done to be retrocompatible with older applications
|
2009-11-14 05:31:06 +01:00
|
|
|
|
|
|
|
if (widget) {
|
|
|
|
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
widget->setParentItem(d->scrollingWidget);
|
|
|
|
widget->setPos(QPoint(0,0));
|
|
|
|
widget->installEventFilter(this);
|
|
|
|
d->adjustScrollbars();
|
|
|
|
}
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsWidget *ScrollWidget::widget() const
|
|
|
|
{
|
|
|
|
return d->widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 21:21:48 +01:00
|
|
|
void ScrollWidget::setHorizontalScrollBarPolicy(const Qt::ScrollBarPolicy policy)
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
|
|
|
d->horizontalScrollBarPolicy = policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 21:21:48 +01:00
|
|
|
Qt::ScrollBarPolicy ScrollWidget::horizontalScrollBarPolicy() const
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
|
|
|
return d->horizontalScrollBarPolicy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-26 21:21:48 +01:00
|
|
|
void ScrollWidget::setVerticalScrollBarPolicy(const Qt::ScrollBarPolicy policy)
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
|
|
|
d->verticalScrollBarPolicy = policy;
|
|
|
|
}
|
|
|
|
|
2009-03-26 21:21:48 +01:00
|
|
|
Qt::ScrollBarPolicy ScrollWidget::verticalScrollBarPolicy() const
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
|
|
|
return d->verticalScrollBarPolicy;
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:40:16 +02:00
|
|
|
void ScrollWidget::ensureRectVisible(const QRectF &rect)
|
|
|
|
{
|
2009-09-24 23:27:40 +02:00
|
|
|
if (!d->widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-04 22:08:37 +02:00
|
|
|
d->rectToBeVisible = rect;
|
|
|
|
d->makeRectVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::ensureItemVisible(QGraphicsItem *item)
|
|
|
|
{
|
|
|
|
if (!d->widget || !item) {
|
2009-09-24 22:40:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-10-04 22:08:37 +02:00
|
|
|
QGraphicsItem *parentOfItem = item->parentItem();
|
|
|
|
while (parentOfItem != d->widget) {
|
|
|
|
if (!parentOfItem) {
|
|
|
|
return;
|
|
|
|
}
|
2009-09-24 22:40:16 +02:00
|
|
|
|
2009-10-04 22:08:37 +02:00
|
|
|
parentOfItem = parentOfItem->parentItem();
|
2009-09-24 22:40:16 +02:00
|
|
|
}
|
|
|
|
|
2009-12-01 21:49:34 +01:00
|
|
|
d->itemToBeVisible = item;
|
2009-09-24 22:40:16 +02:00
|
|
|
|
2009-10-04 22:08:37 +02:00
|
|
|
// We need to wait for the parent item to resize...
|
2009-12-01 21:49:34 +01:00
|
|
|
QTimer::singleShot(0, this, SLOT(makeItemVisible()));
|
2009-09-24 22:40:16 +02:00
|
|
|
}
|
2009-03-03 18:31:54 +01:00
|
|
|
|
2009-11-06 21:20:35 +01:00
|
|
|
void ScrollWidget::registerAsDragHandle(QGraphicsWidget *item)
|
2009-11-06 19:57:35 +01:00
|
|
|
{
|
2009-11-06 21:20:35 +01:00
|
|
|
if (!d->widget || !item || d->dragHandles.contains(item)) {
|
2009-11-06 19:57:35 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGraphicsItem *parentOfItem = item->parentItem();
|
|
|
|
while (parentOfItem != d->widget) {
|
|
|
|
if (!parentOfItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentOfItem = parentOfItem->parentItem();
|
|
|
|
}
|
|
|
|
|
2009-11-06 21:20:35 +01:00
|
|
|
connect(item, SIGNAL(destroyed(QObject *)), this, SLOT(cleanupDragHandles(QObject *)));
|
|
|
|
item->installEventFilter(this);
|
2009-11-06 19:57:35 +01:00
|
|
|
d->dragHandles.insert(item);
|
|
|
|
}
|
|
|
|
|
2009-11-06 21:20:35 +01:00
|
|
|
void ScrollWidget::unregisterAsDragHandle(QGraphicsWidget *item)
|
2009-11-06 19:57:35 +01:00
|
|
|
{
|
|
|
|
if (item) {
|
2009-11-06 21:20:35 +01:00
|
|
|
item->removeEventFilter(this);
|
2009-11-06 19:57:35 +01:00
|
|
|
d->dragHandles.remove(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
QRectF ScrollWidget::viewportGeometry() const
|
2009-10-01 18:47:19 +02:00
|
|
|
{
|
2009-10-08 18:04:56 +02:00
|
|
|
QRectF result;
|
2009-10-01 18:47:19 +02:00
|
|
|
if (!d->widget) {
|
2009-10-08 18:04:56 +02:00
|
|
|
return result;
|
2009-10-01 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
return d->scrollingWidget->boundingRect();
|
2009-10-01 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
QSizeF ScrollWidget::contentsSize() const
|
2009-10-01 18:47:19 +02:00
|
|
|
{
|
2009-11-14 05:31:06 +01:00
|
|
|
return d->widget ? d->widget->size() : QSizeF();
|
2009-10-01 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
void ScrollWidget::setScrollPosition(const QPointF &position)
|
2009-10-01 18:47:19 +02:00
|
|
|
{
|
2009-11-14 05:31:06 +01:00
|
|
|
if (d->widget) {
|
2009-11-19 20:40:07 +01:00
|
|
|
d->widget->setPos(-position.toPoint());
|
2009-11-14 05:31:06 +01:00
|
|
|
}
|
2009-10-01 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2009-10-08 18:04:56 +02:00
|
|
|
QPointF ScrollWidget::scrollPosition() const
|
2009-10-01 18:47:19 +02:00
|
|
|
{
|
2009-11-14 05:31:06 +01:00
|
|
|
return d->widget ? -d->widget->pos() : QPointF();
|
2009-10-01 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:31:54 +01:00
|
|
|
void ScrollWidget::setStyleSheet(const QString &styleSheet)
|
|
|
|
{
|
|
|
|
d->styleSheet = styleSheet;
|
|
|
|
d->verticalScrollBar->setStyleSheet(styleSheet);
|
|
|
|
d->horizontalScrollBar->setStyleSheet(styleSheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ScrollWidget::styleSheet() const
|
|
|
|
{
|
|
|
|
return d->styleSheet;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget *ScrollWidget::nativeWidget() const
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-10-02 18:07:06 +02:00
|
|
|
void ScrollWidget::focusInEvent(QFocusEvent *event)
|
|
|
|
{
|
|
|
|
Q_UNUSED(event)
|
|
|
|
|
|
|
|
if (d->widget) {
|
|
|
|
d->widget->setFocus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:31:54 +01:00
|
|
|
|
|
|
|
void ScrollWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->widget) {
|
2009-12-09 19:50:59 +01:00
|
|
|
QGraphicsWidget::resizeEvent(event);
|
2009-03-03 18:31:54 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->adjustScrollbars();
|
|
|
|
|
|
|
|
d->adjustClipping();
|
|
|
|
|
2009-10-21 21:47:20 +02:00
|
|
|
//if topBorder exists bottomBorder too
|
|
|
|
if (d->topBorder) {
|
|
|
|
d->topBorder->resize(event->newSize().width(), d->topBorder->size().height());
|
|
|
|
d->bottomBorder->resize(event->newSize().width(), d->bottomBorder->size().height());
|
|
|
|
d->bottomBorder->setPos(0, event->newSize().height() - d->bottomBorder->size().height());
|
|
|
|
}
|
|
|
|
if (d->leftBorder) {
|
|
|
|
d->leftBorder->resize(d->leftBorder->size().width(), event->newSize().height());
|
|
|
|
d->rightBorder->resize(d->rightBorder->size().width(), event->newSize().height());
|
|
|
|
d->rightBorder->setPos(event->newSize().width() - d->rightBorder->size().width(), 0);
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:31:54 +01:00
|
|
|
QGraphicsWidget::resizeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:40:16 +02:00
|
|
|
if (d->animId) {
|
|
|
|
Animator::self()->stopItemMovement(d->animId);
|
|
|
|
}
|
|
|
|
|
2009-10-15 22:12:01 +02:00
|
|
|
event->ignore();
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2009-11-16 23:05:35 +01:00
|
|
|
if (!(event->buttons() & Qt::LeftButton)) {
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
|
2009-09-24 22:40:16 +02:00
|
|
|
if (d->animId) {
|
|
|
|
Animator::self()->stopItemMovement(d->animId);
|
|
|
|
}
|
2009-09-17 18:24:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2009-10-15 22:12:01 +02:00
|
|
|
event->ignore();
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
2009-12-04 14:01:33 +01:00
|
|
|
void ScrollWidget::wheelEvent(QGraphicsSceneWheelEvent *)
|
2009-03-03 18:31:54 +01:00
|
|
|
{
|
2009-09-24 22:40:16 +02:00
|
|
|
if (d->animId) {
|
|
|
|
Animator::self()->stopItemMovement(d->animId);
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScrollWidget::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->widget) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (watched == d->widget && event->type() == QEvent::GraphicsSceneResize) {
|
|
|
|
d->adjustScrollbars();
|
|
|
|
d->adjustClipping();
|
2009-10-16 16:53:11 +02:00
|
|
|
//force to refresh the size hint
|
|
|
|
layout()->invalidate();
|
2009-06-08 22:33:56 +02:00
|
|
|
} else if (watched == d->widget && event->type() == QEvent::GraphicsSceneMove) {
|
2009-06-08 23:06:23 +02:00
|
|
|
d->horizontalScrollBar->blockSignals(true);
|
|
|
|
d->verticalScrollBar->blockSignals(true);
|
2009-06-08 22:33:56 +02:00
|
|
|
d->horizontalScrollBar->setValue(-d->widget->pos().x()/10);
|
|
|
|
d->verticalScrollBar->setValue(-d->widget->pos().y()/10);
|
2009-06-08 23:06:23 +02:00
|
|
|
d->horizontalScrollBar->blockSignals(false);
|
|
|
|
d->verticalScrollBar->blockSignals(false);
|
2009-11-06 21:20:35 +01:00
|
|
|
} else if (d->dragHandles.contains(static_cast<QGraphicsWidget *>(watched))) {
|
|
|
|
if (event->type() == QEvent::GraphicsSceneMousePress ||
|
|
|
|
event->type() == QEvent::GraphicsSceneMouseMove ||
|
|
|
|
event->type() == QEvent::GraphicsSceneMouseRelease) {
|
|
|
|
if (scene()) {
|
|
|
|
scene()->sendEvent(this, event);
|
|
|
|
}
|
2009-11-13 17:03:02 +01:00
|
|
|
|
|
|
|
QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(event);
|
|
|
|
if (event->type() == QEvent::GraphicsSceneMousePress) {
|
|
|
|
d->dragHandleClicked = me->scenePos();
|
|
|
|
} else if (event->type() == QEvent::GraphicsSceneMouseRelease &&
|
|
|
|
(d->dragHandleClicked.toPoint() - me->scenePos().toPoint()).manhattanLength() > KGlobalSettings::dndEventDelay()) {
|
|
|
|
return true;
|
|
|
|
}
|
2009-11-06 21:20:35 +01:00
|
|
|
}
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-10-16 16:10:55 +02:00
|
|
|
QSizeF ScrollWidget::sizeHint(Qt::SizeHint which, const QSizeF & constraint) const
|
|
|
|
{
|
|
|
|
QSizeF hint = QGraphicsWidget::sizeHint(which, constraint);
|
|
|
|
|
|
|
|
if (which == Qt::PreferredSize && d->widget) {
|
2009-11-05 20:21:25 +01:00
|
|
|
return (d->widget->size()+QSize(d->borderSize, d->borderSize)).expandedTo(d->widget->effectiveSizeHint(Qt::PreferredSize));
|
2009-10-16 16:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:31:54 +01:00
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <scrollwidget.moc>
|
|
|
|
|