aboutsummaryrefslogtreecommitdiffstats
path: root/examples/hyperui/hyperuilib/scrollarea.py
blob: 4d851995b22d932d8b2a59d95b10bf836a7a625d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
"""
/*
 * This file is part of PySide: Python for Qt
 *
 * Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Contact: PySide team <contact@pyside.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */
"""

from PySide2 import QtGui, QtCore
from kineticscroll import KineticScroll

#Ok I know, this class is not necessary in python
#but this will change the logic and this will bring more work

class ScrollAreaPrivate:
    def __init__(self, parent):
        self._offset = 0
        self._maximumOffset = 0
        self._widget = None
        self._kinetic = None
        self._isDragging = False
        self._mouseDownPos = -1
        self._moveConstant = 15
        self._clickConstant = 25
        self._scrollArea = parent
        self._ignoreList = []


    def smoothPos(self, y):
        if abs(self._mouseDownPos - y) <= self._moveConstant:
            return y
        elif self._mouseDownPos - y < 0:
            return y - self._moveConstant
        else:
            return y + self._moveConstant


    def isClickPossible(self, y):
        if self._isDragging or self._mouseDownPos < 0:
            return False
        else:
            return abs(y - self._mouseDownPos) <= self._clickConstant

    def updateMaximumOffset(self):
        value = 0
        if self._widget:
            value = max(0, self._widget.size().height() - self._scrollArea.size().height())

        if value != self._maximumOffset:
            self._maximumOffset = value
            self._scrollArea.emit(QtCore.SIGNAL("maximumOffsetChanged()"))

    def reconfigure(self):
        if self._widget:
            self._widget.resize(self._scrollArea.size().width(), self._widget.size().height())
            self.updateMaximumOffset()
            self._scrollArea.setOffset(self._offset)

    def sendClick(self, x, y):
        if self._scrollArea.scene() == None:
            return

        event = QtGui.QGraphicsSceneMouseEvent(QtCore.QEvent.GraphicsSceneMousePress)
        event.setButton(QtCore.Qt.LeftButton)
        pos = QtCore.QPointF(x, y)
        event.setScenePos(pos)
        self._ignoreList.append(pos)
        QtCore.QCoreApplication.postEvent(self._scrollArea.scene(), event)

        event = QtGui.QGraphicsSceneMouseEvent(QtCore.QEvent.GraphicsSceneMouseRelease)
        event.setButton(QtCore.Qt.LeftButton)

        pos = QtCore.QPointF(x, y)
        event.setScenePos(pos)
        self._ignoreList.append(pos)
        QtCore.QCoreApplication.postEvent(self._scrollArea.scene(), event)


class ScrollArea(QtGui.QGraphicsWidget):
    def __init__(self, parent = None):
        QtGui.QGraphicsWidget.__init__(self, parent)

        self.setFlags(QtGui.QGraphicsItem.ItemHasNoContents)
        self.setFlags(QtGui.QGraphicsItem.ItemClipsChildrenToShape)

        self._d = ScrollAreaPrivate(self)
        self._d._kinetic = KineticScroll(self)
        self.connect(self._d._kinetic, QtCore.SIGNAL("signalMoveOffset(int)"), QtCore.SLOT("kineticMove(int)"))


    def widget(self):
        return self._d._widget

    def setWidget(self, widget):
        if self._d._widget:
            self._d._widget.setParentItem(0)
            self._d._widget.removeEventFilter(self)
            self._d._widget = None

        if widget:
            self._d._widget = widget
            self._d._widget.setParentItem(self)
            self._d._widget.installEventFilter(self)
            self._d._widget.setPos(0, 0)
            self._d._widget.setFlag(QtGui.QGraphicsItem.ItemStacksBehindParent)
            self._d.reconfigure()


    def offset(self):
        return self._d._offset

    def setOffset(self, offset):
        if self._d._widget:
            value = max(min(offset, self._d._maximumOffset), 0)

            if value != self._d._offset:
                self._d._offset = value
                self._d._widget.setY(-value)
                self.emit(QtCore.SIGNAL("offsetChanged()"))

    def maximumOffset(self):
        return self._d._maximumOffset

    def resizeEvent(self, event):
        QtGui.QGraphicsWidget.resizeEvent(self, event)
        self._d.reconfigure()

    def eventFilter(self, object, event):
        if object == self._d._widget and event.type() == QtCore.QEvent.GraphicsSceneResize:
            self._d.reconfigure()

        return False

    def mousePressEvent(self, event):
        pos = event.scenePos()
        if pos in self._d._ignoreList:
            self._d._ignoreList.remove(pos)
            event.ignore()
            return

        y = event.pos().y()
        self._d._mouseDownPos = y
        self._d._isDragging = not self._d._kinetic.mouseDown(y)

    def stopKinetic(self):
        self._d._kinetic.kineticStop()

    def mouseReleaseEvent(self, event):
        pos = event.scenePos()
        if pos in self._d._ignoreList:
            self._d._ignoreList.remove(pos)
            event.ignore()
            return

        if self._d._mouseDownPos >= 0:
            y = event.pos().y()
            if self._d.isClickPossible(y):
                self._d.sendClick(event.scenePos().x(), event.scenePos().y())
                self._d._kinetic.mouseCancel()
            else:
                self._d._kinetic.mouseUp(self._d.smoothPos(y))

        self._d._mouseDownPos = -1

    def mouseMoveEvent(self, event):
        if self._d._mouseDownPos >= 0:
            y = event.pos().y()

            if not self._d.isClickPossible(y):
                self._d._isDragging = True

            if abs(self._d._mouseDownPos - y) > self._d._moveConstant:
                self._d._kinetic.mouseMove(self._d.smoothPos(y))

    def kineticMove(self, value):
        finalOffset = self.offset() - value
        self.setOffset(finalOffset)

        if value == 0 or finalOffset != self.offset():
            self._d._kinetic.kineticStop()
            return False

        return True