-/***************************************************************************
- * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
- * *
- * This program is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 2 of the License, 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 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 *
- ***************************************************************************/
+/*
+ * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
#include "kitemlistsmoothscroller.h"
#include <QApplication>
-#include <QEvent>
#include <QPropertyAnimation>
#include <QScrollBar>
-#include <QWheelEvent>
#include <QStyle>
+#include <QWheelEvent>
KItemListSmoothScroller::KItemListSmoothScroller(QScrollBar* scrollBar,
QObject* parent) :
m_scrollBarPressed(false),
m_smoothScrolling(true),
m_scrollBar(scrollBar),
- m_animation(0)
+ m_animation(nullptr)
{
m_animation = new QPropertyAnimation(this);
- const int duration = m_scrollBar->style()->styleHint(QStyle::SH_Widget_Animate, nullptr, m_scrollBar) ? 100 : 1;
- m_animation->setDuration(duration);
+ const int animationDuration = m_scrollBar->style()->styleHint(QStyle::SH_Widget_Animation_Duration, nullptr, m_scrollBar);
+ const bool animationEnabled = (animationDuration > 0);
+ m_animation->setDuration(animationEnabled ? animationDuration : 1);
connect(m_animation, &QPropertyAnimation::stateChanged,
this, &KItemListSmoothScroller::slotAnimationStateChanged);
break;
case QEvent::Wheel:
- handleWheelEvent(static_cast<QWheelEvent*>(event));
- return true; // eat event so that QScrollBar does not scroll one step more by itself
+ return false; // we're the ones sending them
default:
break;
void KItemListSmoothScroller::slotAnimationStateChanged(QAbstractAnimation::State newState,
QAbstractAnimation::State oldState)
{
- Q_UNUSED(oldState);
+ Q_UNUSED(oldState)
if (newState == QAbstractAnimation::Stopped && m_smoothScrolling && !m_scrollBarPressed) {
m_smoothScrolling = false;
}
+ if (newState == QAbstractAnimation::Stopped) {
+ Q_EMIT scrollingStopped();
+ }
}
void KItemListSmoothScroller::handleWheelEvent(QWheelEvent* event)
const bool previous = m_smoothScrolling;
m_smoothScrolling = true;
- int numPixels;
- if (!event->pixelDelta().isNull()) {
- numPixels = event->pixelDelta().y();
- } else {
- const int numDegrees = event->angleDelta().y() / 8;
- const int numSteps = qApp->wheelScrollLines() * numDegrees / 15;
- numPixels = numSteps * m_scrollBar->pageStep() / 12;
- }
- int value = m_scrollBar->value();
- if (event->modifiers().testFlag(Qt::ShiftModifier)) {
- const int scrollingDirection = numPixels > 0 ? 1 : -1;
- value -= m_scrollBar->pageStep() * scrollingDirection;
- } else {
- value -= numPixels;
- }
- m_scrollBar->setValue(value);
- m_smoothScrolling = previous;
+ QWheelEvent copy = *event;
+ QApplication::sendEvent(m_scrollBar, ©);
+ event->setAccepted(copy.isAccepted());
- event->accept();
+ m_smoothScrolling = previous;
}