ginebra2/ScrollableViewBase.cpp
author hgs
Fri, 15 Oct 2010 17:30:59 -0400
changeset 16 3c88a81ff781
parent 3 0954f5dd2cd0
permissions -rw-r--r--
201041
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
*
hgs
parents:
diff changeset
     5
* This program is free software: you can redistribute it and/or modify
hgs
parents:
diff changeset
     6
* it under the terms of the GNU Lesser General Public License as published by
hgs
parents:
diff changeset
     7
* the Free Software Foundation, version 2.1 of the License.
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* This program is distributed in the hope that it will be useful,
hgs
parents:
diff changeset
    10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
hgs
parents:
diff changeset
    11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
hgs
parents:
diff changeset
    12
* GNU Lesser General Public License for more details.
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* You should have received a copy of the GNU Lesser General Public License
hgs
parents:
diff changeset
    15
* along with this program.  If not,
hgs
parents:
diff changeset
    16
* see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
hgs
parents:
diff changeset
    17
*
hgs
parents:
diff changeset
    18
* Description:
hgs
parents:
diff changeset
    19
*
hgs
parents:
diff changeset
    20
*/
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
#include "ScrollableViewBase.h"
hgs
parents:
diff changeset
    23
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
#include "Kinetics/KineticScroller.h"
hgs
parents:
diff changeset
    26
hgs
parents:
diff changeset
    27
#include <QGraphicsScene>
hgs
parents:
diff changeset
    28
#include <QGraphicsSceneMouseEvent>
hgs
parents:
diff changeset
    29
#include <QPointF>
16
hgs
parents: 3
diff changeset
    30
#include <QApplication>
3
hgs
parents:
diff changeset
    31
namespace GVA {
hgs
parents:
diff changeset
    32
hgs
parents:
diff changeset
    33
ScrollableViewBase::ScrollableViewBase(QGraphicsItem* parent, Qt::WindowFlags wFlags)
hgs
parents:
diff changeset
    34
    : QGraphicsWidget(parent, wFlags)
hgs
parents:
diff changeset
    35
    , m_scrollWidget(0)
hgs
parents:
diff changeset
    36
{
hgs
parents:
diff changeset
    37
    //To get only the mouse events within the ScrollableViewBase.
hgs
parents:
diff changeset
    38
    setFlag(QGraphicsItem::ItemClipsToShape, true);
hgs
parents:
diff changeset
    39
hgs
parents:
diff changeset
    40
    //To disable childrens painting outside the ViewportItem's rect.
hgs
parents:
diff changeset
    41
    setFlag(QGraphicsItem::ItemClipsChildrenToShape, true);
hgs
parents:
diff changeset
    42
hgs
parents:
diff changeset
    43
    //To speed up painting.
16
hgs
parents: 3
diff changeset
    44
    //setFlag(QGraphicsItem::ItemHasNoContents, true);
3
hgs
parents:
diff changeset
    45
    setAttribute(Qt::WA_OpaquePaintEvent, true);
hgs
parents:
diff changeset
    46
hgs
parents:
diff changeset
    47
    m_kineticScroller = new KineticScroller(this);
hgs
parents:
diff changeset
    48
hgs
parents:
diff changeset
    49
    //Event filter on all children of the viewport, so that all mouse
hgs
parents:
diff changeset
    50
    //events can be intercepted and used for kinetic scrolling.
hgs
parents:
diff changeset
    51
    setFiltersChildEvents(true);
hgs
parents:
diff changeset
    52
}
hgs
parents:
diff changeset
    53
hgs
parents:
diff changeset
    54
ScrollableViewBase::~ScrollableViewBase()
hgs
parents:
diff changeset
    55
{}
hgs
parents:
diff changeset
    56
hgs
parents:
diff changeset
    57
void ScrollableViewBase::setWidget(QGraphicsWidget* view)
hgs
parents:
diff changeset
    58
{
hgs
parents:
diff changeset
    59
    if (view == m_scrollWidget)
hgs
parents:
diff changeset
    60
        return;
hgs
parents:
diff changeset
    61
hgs
parents:
diff changeset
    62
    if (m_scrollWidget) {
hgs
parents:
diff changeset
    63
        m_scrollWidget->setParentItem(0);
hgs
parents:
diff changeset
    64
        delete m_scrollWidget;
hgs
parents:
diff changeset
    65
    }
hgs
parents:
diff changeset
    66
hgs
parents:
diff changeset
    67
    m_scrollWidget = view;
hgs
parents:
diff changeset
    68
    m_scrollWidget->setAttribute(Qt::WA_OpaquePaintEvent, true);
hgs
parents:
diff changeset
    69
    m_scrollWidget->setParentItem(this);
hgs
parents:
diff changeset
    70
}
hgs
parents:
diff changeset
    71
hgs
parents:
diff changeset
    72
QSize ScrollableViewBase::viewportSize() const
hgs
parents:
diff changeset
    73
{
hgs
parents:
diff changeset
    74
    return size().toSize();
hgs
parents:
diff changeset
    75
}
hgs
parents:
diff changeset
    76
hgs
parents:
diff changeset
    77
QPoint ScrollableViewBase::maximumScrollPosition() const
hgs
parents:
diff changeset
    78
{
16
hgs
parents: 3
diff changeset
    79
    QSizeF contentsSize = m_scrollWidget->size() * m_scrollWidget->scale();;
3
hgs
parents:
diff changeset
    80
    QSizeF viewportSize = size();
hgs
parents:
diff changeset
    81
    QSize maxScrollSize = (contentsSize - viewportSize).toSize();
hgs
parents:
diff changeset
    82
hgs
parents:
diff changeset
    83
    return QPoint(qMax(0, maxScrollSize.width()), qMax(0, maxScrollSize.height()));
hgs
parents:
diff changeset
    84
}
hgs
parents:
diff changeset
    85
hgs
parents:
diff changeset
    86
QPoint ScrollableViewBase::scrollPosition() const
hgs
parents:
diff changeset
    87
{
hgs
parents:
diff changeset
    88
    return (-scrollWidgetPos()).toPoint();
hgs
parents:
diff changeset
    89
}
hgs
parents:
diff changeset
    90
hgs
parents:
diff changeset
    91
void ScrollableViewBase::setScrollPosition(const QPoint& pos, const QPoint& overShoot)
hgs
parents:
diff changeset
    92
{
16
hgs
parents: 3
diff changeset
    93
    QPoint p = scrollPosition();
hgs
parents: 3
diff changeset
    94
    QPoint d = pos - p;
hgs
parents: 3
diff changeset
    95
3
hgs
parents:
diff changeset
    96
    m_overShoot = overShoot;
16
hgs
parents: 3
diff changeset
    97
    setScrollWidgetPos(-pos + m_overShoot);
hgs
parents: 3
diff changeset
    98
    //QApplication::sendPostedEvents();
hgs
parents: 3
diff changeset
    99
    
hgs
parents: 3
diff changeset
   100
    emit viewScrolled(p, d);
3
hgs
parents:
diff changeset
   101
}
hgs
parents:
diff changeset
   102
hgs
parents:
diff changeset
   103
void ScrollableViewBase::stateChanged(KineticScrollable::State oldState
hgs
parents:
diff changeset
   104
                                      , KineticScrollable::State newState)
hgs
parents:
diff changeset
   105
{
hgs
parents:
diff changeset
   106
    Q_UNUSED(oldState);
hgs
parents:
diff changeset
   107
    Q_UNUSED(newState);
hgs
parents:
diff changeset
   108
}
16
hgs
parents: 3
diff changeset
   109
/*
3
hgs
parents:
diff changeset
   110
void ScrollableViewBase::setScrollWidgetGeometry(const QRectF& geom)
hgs
parents:
diff changeset
   111
{
hgs
parents:
diff changeset
   112
    scrollWidget()->setGeometry(adjustScrollWidgetRect(geom));
hgs
parents:
diff changeset
   113
}
hgs
parents:
diff changeset
   114
hgs
parents:
diff changeset
   115
QRectF ScrollableViewBase::adjustScrollWidgetRect(const QRectF& rect)
hgs
parents:
diff changeset
   116
{
hgs
parents:
diff changeset
   117
    //FIX ME : Stop animation of scroll widget before adjusting it here??
hgs
parents:
diff changeset
   118
    QRectF newRect(rect);
hgs
parents:
diff changeset
   119
hgs
parents:
diff changeset
   120
    QSizeF widgetSize = rect.size();
hgs
parents:
diff changeset
   121
    QSizeF viewportSize = size();
hgs
parents:
diff changeset
   122
hgs
parents:
diff changeset
   123
    qreal w = viewportSize.width() - widgetSize.width();
hgs
parents:
diff changeset
   124
    qreal h = viewportSize.height() - widgetSize.height();
hgs
parents:
diff changeset
   125
hgs
parents:
diff changeset
   126
    if ( w > 0 ) {
hgs
parents:
diff changeset
   127
        newRect.moveLeft(0);
hgs
parents:
diff changeset
   128
        m_extraPos.setX(w/2);
hgs
parents:
diff changeset
   129
      }
hgs
parents:
diff changeset
   130
    else {
hgs
parents:
diff changeset
   131
    	m_extraPos.setX(0);
hgs
parents:
diff changeset
   132
        if (newRect.x() < w)
hgs
parents:
diff changeset
   133
            newRect.moveLeft(w);
hgs
parents:
diff changeset
   134
        if (newRect.x() > 0)
hgs
parents:
diff changeset
   135
            newRect.moveLeft(0);
hgs
parents:
diff changeset
   136
    }
hgs
parents:
diff changeset
   137
hgs
parents:
diff changeset
   138
    if ( h > 0 ) {
hgs
parents:
diff changeset
   139
        m_extraPos.setY(h/2);
hgs
parents:
diff changeset
   140
        newRect.moveTop(0);
hgs
parents:
diff changeset
   141
      }
hgs
parents:
diff changeset
   142
    else {
hgs
parents:
diff changeset
   143
    	 m_extraPos.setY(0);
hgs
parents:
diff changeset
   144
        if (newRect.y() < h)
hgs
parents:
diff changeset
   145
            newRect.moveTop(h);
hgs
parents:
diff changeset
   146
        if (newRect.y() > 0)
hgs
parents:
diff changeset
   147
            newRect.moveTop(0);
hgs
parents:
diff changeset
   148
    }
hgs
parents:
diff changeset
   149
    //newRect.translate(m_extraPos);
hgs
parents:
diff changeset
   150
    newRect.translate(m_overShoot);
hgs
parents:
diff changeset
   151
    return newRect;
hgs
parents:
diff changeset
   152
}
16
hgs
parents: 3
diff changeset
   153
*/
3
hgs
parents:
diff changeset
   154
void ScrollableViewBase::setScrollWidgetPos(const QPointF& pos)
hgs
parents:
diff changeset
   155
{
16
hgs
parents: 3
diff changeset
   156
    scrollWidget()->setPos(pos);
3
hgs
parents:
diff changeset
   157
}
hgs
parents:
diff changeset
   158
hgs
parents:
diff changeset
   159
QPointF ScrollableViewBase::scrollWidgetPos() const
hgs
parents:
diff changeset
   160
{
hgs
parents:
diff changeset
   161
    return scrollWidget()->pos()  /*- m_extraPos*/ - m_overShoot;
hgs
parents:
diff changeset
   162
}
hgs
parents:
diff changeset
   163
hgs
parents:
diff changeset
   164
} //namespace GVA