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