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"
|
2009-09-17 18:24:19 +02:00
|
|
|
#include "private/kineticscroll_p.h"
|
2009-03-03 18:31:54 +01:00
|
|
|
//Qt
|
|
|
|
#include <QGraphicsSceneResizeEvent>
|
|
|
|
#include <QGraphicsGridLayout>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QWidget>
|
|
|
|
|
|
|
|
//KDE
|
|
|
|
#include <kmimetype.h>
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
|
|
|
//Plasma
|
|
|
|
#include <plasma/widgets/scrollbar.h>
|
2009-08-03 22:41:42 +02:00
|
|
|
#include <plasma/widgets/svgwidget.h>
|
|
|
|
#include <plasma/svg.h>
|
2009-03-03 18:31:54 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2009-09-17 18:24:19 +02:00
|
|
|
class ScrollWidgetPrivate: public KineticScrolling
|
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-03-03 18:31:54 +01:00
|
|
|
dragging(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ScrollWidgetPrivate()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void adjustScrollbars()
|
|
|
|
{
|
|
|
|
verticalScrollBar->nativeWidget()->setMaximum(qMax(0, int((widget->size().height() - scrollingWidget->size().height())/10)));
|
|
|
|
|
|
|
|
if (verticalScrollBarPolicy == Qt::ScrollBarAlwaysOff ||
|
|
|
|
verticalScrollBar->nativeWidget()->maximum() == 0) {
|
|
|
|
if (layout->itemAt(2) == verticalScrollBar) {
|
|
|
|
layout->removeAt(2);
|
|
|
|
} else if (layout->itemAt(1) == verticalScrollBar) {
|
|
|
|
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) {
|
|
|
|
if (layout->itemAt(2) == horizontalScrollBar) {
|
|
|
|
layout->removeAt(2);
|
|
|
|
} else if (layout->itemAt(1) == horizontalScrollBar) {
|
|
|
|
layout->removeAt(1);
|
|
|
|
}
|
|
|
|
horizontalScrollBar->hide();
|
|
|
|
} else if (!horizontalScrollBar->isVisible()) {
|
|
|
|
layout->addItem(horizontalScrollBar, 1, 0);
|
|
|
|
horizontalScrollBar->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (widget && widget->size().height() <= q->size().height()) {
|
2009-08-03 22:41:42 +02:00
|
|
|
topBorder->deleteLater();
|
|
|
|
bottomBorder->deleteLater();
|
|
|
|
topBorder = 0;
|
|
|
|
bottomBorder = 0;
|
2009-09-19 17:35:16 +02:00
|
|
|
} else if (!topBorder) {
|
2009-08-03 22:41:42 +02:00
|
|
|
topBorder = new Plasma::SvgWidget(q);
|
|
|
|
topBorder->setSvg(borderSvg);
|
|
|
|
topBorder->setElementID("border-top");
|
|
|
|
topBorder->setZValue(900);
|
|
|
|
topBorder->resize(topBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
topBorder->show();
|
|
|
|
bottomBorder = new Plasma::SvgWidget(q);
|
|
|
|
bottomBorder->setSvg(borderSvg);
|
|
|
|
bottomBorder->setElementID("border-bottom");
|
|
|
|
bottomBorder->setZValue(900);
|
|
|
|
bottomBorder->resize(bottomBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
bottomBorder->show();
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-19 18:06:41 +02:00
|
|
|
if (widget && widget->size().width() <= q->size().width()) {
|
2009-08-03 22:41:42 +02:00
|
|
|
leftBorder->deleteLater();
|
|
|
|
rightBorder->deleteLater();
|
|
|
|
leftBorder = 0;
|
|
|
|
rightBorder = 0;
|
2009-09-19 17:35:16 +02:00
|
|
|
} else if (!leftBorder) {
|
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);
|
|
|
|
leftBorder->resize(leftBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
leftBorder->show();
|
|
|
|
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);
|
|
|
|
rightBorder->resize(rightBorder->effectiveSizeHint(Qt::PreferredSize));
|
|
|
|
rightBorder->show();
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
2009-07-25 15:14:19 +02:00
|
|
|
|
|
|
|
layout->activate();
|
2009-08-03 22:41:42 +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-07-25 15:14:19 +02:00
|
|
|
widget->resize(scrollingWidget->size().width(), widget->size().height());
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void verticalScroll(int value)
|
|
|
|
{
|
|
|
|
if (!dragging) {
|
|
|
|
widget->setPos(QPoint(0, -value*10));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void horizontalScroll(int value)
|
|
|
|
{
|
|
|
|
if (!dragging) {
|
|
|
|
widget->setPos(QPoint(-value*10, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void adjustClipping()
|
|
|
|
{
|
|
|
|
const bool clip = widget->size().width() > scrollingWidget->size().width() ||
|
|
|
|
widget->size().height() > scrollingWidget->size().height();
|
|
|
|
|
|
|
|
scrollingWidget->setFlag(QGraphicsItem::ItemClipsChildrenToShape, clip);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
bool dragging;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ScrollWidget::ScrollWidget(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new ScrollWidgetPrivate(this))
|
|
|
|
{
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
d->layout = new QGraphicsGridLayout(this);
|
2009-08-03 22:41:42 +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);
|
|
|
|
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);
|
|
|
|
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-09-17 18:24:19 +02:00
|
|
|
d->setWidgets(widget, d->scrollingWidget);
|
2009-03-03 18:31:54 +01:00
|
|
|
widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
|
|
|
widget->setParentItem(d->scrollingWidget);
|
|
|
|
widget->setPos(QPoint(0,0));
|
|
|
|
widget->installEventFilter(this);
|
|
|
|
d->adjustScrollbars();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ScrollWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
|
|
|
|
{
|
|
|
|
if (!d->widget) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->adjustScrollbars();
|
|
|
|
|
|
|
|
d->adjustClipping();
|
|
|
|
|
2009-08-03 22:41:42 +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-17 18:24:19 +02:00
|
|
|
d->mouseMoveEvent(event);
|
2009-03-03 18:31:54 +01:00
|
|
|
QGraphicsWidget::mouseMoveEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
event->accept();
|
2009-09-17 18:24:19 +02:00
|
|
|
d->mousePressEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
|
|
|
d->mouseReleaseEvent(event);
|
2009-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScrollWidget::wheelEvent(QGraphicsSceneWheelEvent *event)
|
|
|
|
{
|
2009-09-17 18:24:19 +02:00
|
|
|
event->accept();
|
|
|
|
d->wheelReleaseEvent( event );
|
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-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-03-03 18:31:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include <scrollwidget.moc>
|
|
|
|
|