Skip to content

Commit

Permalink
修复滚动错误
Browse files Browse the repository at this point in the history
  • Loading branch information
tonquer committed Sep 11, 2021
1 parent d6ec51a commit 53a44c7
Show file tree
Hide file tree
Showing 23 changed files with 410 additions and 1,494 deletions.
Binary file modified data/book.db
Binary file not shown.
Binary file removed resources/icon_64x64.png
Binary file not shown.
201 changes: 96 additions & 105 deletions src/qt/com/qt_scroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from enum import Enum
from math import cos, pi

from PySide2.QtCore import QTimer, QDateTime, Qt, QPropertyAnimation, QEasingCurve, QAbstractAnimation
from PySide2.QtCore import QTimer, QDateTime, Qt, QPropertyAnimation, QEasingCurve, QAbstractAnimation, QPoint
from PySide2.QtGui import QWheelEvent
from PySide2.QtWidgets import QApplication, QGraphicsView, QScrollBar

from src.qt.qtmain import QtOwner


class SmoothMode(Enum):
""" 滚动模式 """
Expand All @@ -24,11 +26,12 @@ def __init__(self):
self.animation.setPropertyName(b"value")
self.scrollTime = 500
self.animation.setDuration(self.scrollTime)
self.animation.setEasingCurve(QEasingCurve.Linear)
self.animation.setEasingCurve(QEasingCurve.InOutQuad)
self.animationValue = self.value()
self.backTick = 0
self.laveValue = 0
self.lastV = 0
self.animation.finished.connect(self.Finished)

# def setValue(self, value):
# self.animation.stop()
Expand All @@ -38,59 +41,36 @@ def __init__(self):
# self.animation.start()
# return

def UpdateCurrentValue(self, value):
print(value)
def Finished(self):
print("Finished")
QtOwner().readForm.frame.UpdateScrollBar(self.value())

def StopScroll(self):
self.backTick = 0
self.animation.stop()

def ResetScroll(self):
if self.animation.state() == QAbstractAnimation.State.Running:
startValue = self.animation.startValue()
endValue = self.animation.endValue()
time = self.animation.currentTime()
value = self.animation.currentValue()
laveTime = self.animation.duration() - time
if laveTime > 0:
self.backTick = laveTime
self.laveValue = endValue - value
else:
self.backTick = 0
else:
self.backTick = 0

self.animation.stop()
return

def RestartScroll(self):
if self.backTick == 0 or self.laveValue == 0:
return
print(self.backTick, self.laveValue)
self.animation.stop()
oldValue = self.value()
# print(self.animation.duration())
self.animation.setStartValue(oldValue)
self.animation.setEndValue(oldValue + self.laveValue)
self.animation.setDuration(self.backTick)
self.animation.start()

def Scroll(self, value):
if value * self.lastV < 0:
if self.animation.state() == QAbstractAnimation.State.Running:
self.lastV = value
self.animation.stop()
return
value = min(self.maximum(), value)
value = max(self.minimum(), value)

self.lastV = value
self.animation.stop()
oldValue = self.value()
if oldValue == value:
return

# print(self.animation.duration())
self.animation.setStartValue(oldValue)
self.animation.setDuration(self.scrollTime)
self.animation.setEndValue(oldValue - value)
self.animation.start()


class QtComGraphicsView(QGraphicsView):
def __init__(self, parent):
super(self.__class__, self).__init__(parent)
Expand All @@ -117,8 +97,52 @@ def __init__(self, parent):
self.scrollSize = 500
self.scrollTime = 500

# def wheelEvent(self, e) -> None:
# from src.qt.read.qtreadimg import ReadMode
# if self.parent().qtTool.stripModel not in [ReadMode.UpDown, ReadMode.RightLeftScroll, ReadMode.LeftRightScroll]:
# if e.angleDelta().y() < 0:
# self.parent().qtTool.NextPage()
# else:
# self.parent().qtTool.LastPage()
# return
#
# if self.smoothMode == SmoothMode.NO_SMOOTH:
# super().wheelEvent(e)
# return
# if self.parent().qtTool.stripModel == ReadMode.UpDown:
# scrollBar = self.vScrollBar
# else:
# scrollBar = self.hScrollBar
#
# if e.angleDelta().y() > 0:
# scrollBar.Scroll(self.scrollSize)
# else:
# scrollBar.Scroll(-self.scrollSize)
# # return super().wheelEvent(e)
#
# def SetScrollValue(self, size, time):
# if size == self.scrollSize and time == self.scrollTime:
# return
# self.StopScroll()
# self.scrollSize = size
# self.scrollTime = time
# self.vScrollBar.scrollTime = time
# self.hScrollBar.scrollTime = time
# self.vScrollBar.animation.setDuration(self.scrollTime)
# self.hScrollBar.animation.setDuration(self.scrollTime)
#
# def StopScroll(self):
# self.hScrollBar.StopScroll()
# self.vScrollBar.StopScroll()
#
def Scroll(self, value):
from src.qt.read.qtreadimg import ReadMode
if self.parent().qtTool.stripModel == ReadMode.UpDown:
self.vScrollBar.Scroll(self.vScrollBar.value()+value)
else:
self.hScrollBar.Scroll(self.hScrollBar.value()+value)

def wheelEvent(self, e) -> None:
def wheelEvent(self, e):
from src.qt.read.qtreadimg import ReadMode
if self.parent().qtTool.stripModel not in [ReadMode.UpDown, ReadMode.RightLeftScroll, ReadMode.LeftRightScroll]:
if e.angleDelta().y() < 0:
Expand All @@ -130,71 +154,26 @@ def wheelEvent(self, e) -> None:
if self.smoothMode == SmoothMode.NO_SMOOTH:
super().wheelEvent(e)
return
if self.parent().qtTool.stripModel == ReadMode.UpDown:
scrollBar = self.vScrollBar
else:
scrollBar = self.hScrollBar

if e.angleDelta().y() > 0:
scrollBar.Scroll(self.scrollSize)
else:
scrollBar.Scroll(-self.scrollSize)
# return super().wheelEvent(e)

def SetScrollValue(self, size, time):
if size == self.scrollSize and time == self.scrollTime:
return
self.StopScroll()
self.scrollSize = size
self.scrollTime = time
self.vScrollBar.scrollTime = time
self.hScrollBar.scrollTime = time
self.vScrollBar.animation.setDuration(self.scrollTime)
self.hScrollBar.animation.setDuration(self.scrollTime)

def StopScroll(self):
self.hScrollBar.StopScroll()
self.vScrollBar.StopScroll()

def Scroll(self, value):
from src.qt.read.qtreadimg import ReadMode
if self.parent().qtTool.stripModel == ReadMode.UpDown:
self.vScrollBar.Scroll(value)
else:
self.hScrollBar.Scroll(value)

# def wheelEvent(self, e):
# from src.qt.read.qtreadimg import ReadMode
# if self.parent().qtTool.stripModel not in [ReadMode.UpDown, ReadMode.RightLeftScroll, ReadMode.LeftRightScroll]:
# if e.angleDelta().y() < 0:
# self.parent().qtTool.NextPage()
# else:
# self.parent().qtTool.LastPage()
# return
#
# if self.smoothMode == SmoothMode.NO_SMOOTH:
# super().wheelEvent(e)
# return
#
# # 将当前时间点插入队尾
# now = QDateTime.currentDateTime().toMSecsSinceEpoch()
# self.scrollStamps.append(now)
# while now - self.scrollStamps[0] > 500:
# self.scrollStamps.popleft()
# # 根据未处理完的事件调整移动速率增益
# accerationRatio = min(len(self.scrollStamps) / 15, 1)
# self.qEventParam = (e.pos(), e.globalPos(), e.buttons())
# # 计算步数
# self.stepsTotal = self.fps * self.duration / 1000
# # 计算每一个事件对应的移动距离
# delta = e.angleDelta().y() * self.stepRatio
# if self.acceleration > 0:
# delta += delta * self.acceleration * accerationRatio
# # 将移动距离和步数组成列表,插入队列等待处理
# self.stepsLeftQueue.append([delta, self.stepsTotal])
# # 定时器的溢出时间t=1000ms/帧数
# self.smoothMoveTimer.start(1000 // self.fps)
# # print(e)
# 将当前时间点插入队尾
now = QDateTime.currentDateTime().toMSecsSinceEpoch()
self.scrollStamps.append(now)
while now - self.scrollStamps[0] > 500:
self.scrollStamps.popleft()
# 根据未处理完的事件调整移动速率增益
accerationRatio = min(len(self.scrollStamps) / 15, 1)
self.qEventParam = (e.pos(), e.globalPos(), e.buttons())
# 计算步数
self.stepsTotal = self.fps * self.duration / 1000
# 计算每一个事件对应的移动距离
delta = e.angleDelta().y() * self.stepRatio
if self.acceleration > 0:
delta += delta * self.acceleration * accerationRatio
# 将移动距离和步数组成列表,插入队列等待处理
self.stepsLeftQueue.append([delta, self.stepsTotal])
# 定时器的溢出时间t=1000ms/帧数
self.smoothMoveTimer.start(1000 // self.fps)
# print(e)

def __smoothMove(self):
""" 计时器溢出时进行平滑滚动 """
Expand All @@ -206,18 +185,30 @@ def __smoothMove(self):
# 如果事件已处理完,就将其移出队列
while self.stepsLeftQueue and self.stepsLeftQueue[0][1] == 0:
self.stepsLeftQueue.popleft()
# 构造滚轮事件
e = QWheelEvent(self.qEventParam[0],
self.qEventParam[1],
round(totalDelta),
self.qEventParam[2],
Qt.NoModifier)
# print(e)
# 将构造出来的滚轮事件发送给app处理
from src.qt.read.qtreadimg import ReadMode
if self.parent().qtTool.stripModel in [ReadMode.UpDown]:
# 构造滚轮事件
e = QWheelEvent(self.qEventParam[0],
self.qEventParam[1],
QPoint(),
QPoint(0, totalDelta),
round(totalDelta),
Qt.Vertical,
self.qEventParam[2],
Qt.NoModifier)
QApplication.sendEvent(self.verticalScrollBar(), e)
else:
# 构造滚轮事件
e = QWheelEvent(self.qEventParam[0],
self.qEventParam[1],
QPoint(),
QPoint(0, totalDelta),
round(totalDelta),
Qt.Horizontal,
self.qEventParam[2],
Qt.NoModifier)
QApplication.sendEvent(self.horizontalScrollBar(), e)
# 如果队列已空,停止滚动
if not self.stepsLeftQueue:
Expand Down
Loading

0 comments on commit 53a44c7

Please sign in to comment.