From 5a2efb490bc3549ef42420a0dafcf22072785e0d Mon Sep 17 00:00:00 2001 From: Morten Johan Sorvig Date: Mon, 9 Jan 2012 11:25:40 +0100 Subject: QWheelEvent high-resolution delta support. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Support pixel-based deltas as well as sending dx and dy values in the same event. Keep source and behavior compatibility with Qt 4. New API: QPoint pixelDelta() const QPoint angleDelta() const Deprecate delta() and orientation(). Both pixel-based deltas and combined updates are necessary for smooth trackpad-based scrolling on OS X. Qt 4 compatible behavior is achieved by sending an extra wheel event in cases where the initial event has a combined dx and dy update. This extra event sends dx in delta() and orientation(), with pixelDelta() and angleDelta() set to null. Modify the Cocoa implementation to provide pixel deltas. It is expected that not all platforms can provide these. Angle deltas will always be available. Change-Id: I20c10f0df338ddcd6a3f7a4d40949ed5ae3b4795 Reviewed-by: Morten Johan Sørvig --- tests/manual/cocoa/wheelevent/main.cpp | 59 +++++++ tests/manual/cocoa/wheelevent/wheelevent.pro | 6 + tests/manual/cocoa/wheelevent/window.cpp | 240 +++++++++++++++++++++++++++ tests/manual/cocoa/wheelevent/window.h | 76 +++++++++ 4 files changed, 381 insertions(+) create mode 100644 tests/manual/cocoa/wheelevent/main.cpp create mode 100644 tests/manual/cocoa/wheelevent/wheelevent.pro create mode 100644 tests/manual/cocoa/wheelevent/window.cpp create mode 100644 tests/manual/cocoa/wheelevent/window.h (limited to 'tests') diff --git a/tests/manual/cocoa/wheelevent/main.cpp b/tests/manual/cocoa/wheelevent/main.cpp new file mode 100644 index 0000000000..979498991d --- /dev/null +++ b/tests/manual/cocoa/wheelevent/main.cpp @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +#include "window.h" + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + Window window; + window.show(); + + + return app.exec(); +} + + + diff --git a/tests/manual/cocoa/wheelevent/wheelevent.pro b/tests/manual/cocoa/wheelevent/wheelevent.pro new file mode 100644 index 0000000000..6eca6d48cf --- /dev/null +++ b/tests/manual/cocoa/wheelevent/wheelevent.pro @@ -0,0 +1,6 @@ +TEMPLATE = app + +HEADERS += window.h +SOURCES += window.cpp main.cpp + +QT += core gui gui-private core-private diff --git a/tests/manual/cocoa/wheelevent/window.cpp b/tests/manual/cocoa/wheelevent/window.cpp new file mode 100644 index 0000000000..b305b057bf --- /dev/null +++ b/tests/manual/cocoa/wheelevent/window.cpp @@ -0,0 +1,240 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "window.h" + +#include + +#include +#include +#include + +static int colorIndexId = 0; + +QColor colorTable[] = +{ + QColor("#f09f8f"), + QColor("#a2bff2"), + QColor("#c0ef8f") +}; + +Window::Window(QScreen *screen) + : QWindow(screen) + , m_backgroundColorIndex(colorIndexId++) +{ + initialize(); +} + +Window::Window(QWindow *parent) + : QWindow(parent) + , m_backgroundColorIndex(colorIndexId++) +{ + initialize(); +} + +void Window::initialize() +{ + if (parent()) + setGeometry(QRect(160, 120, 320, 240)); + else { + setGeometry(QRect(10, 10, 640, 480)); + + setSizeIncrement(QSize(10, 10)); + setBaseSize(QSize(640, 480)); + setMinimumSize(QSize(240, 160)); + setMaximumSize(QSize(800, 600)); + } + + create(); + m_backingStore = new QBackingStore(this); + + m_image = QImage(geometry().size(), QImage::Format_RGB32); + m_image.fill(colorTable[m_backgroundColorIndex % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba()); + + m_lastPos = QPoint(-1, -1); + m_renderTimer = 0; +} + +void Window::mousePressEvent(QMouseEvent *event) +{ + m_lastPos = event->pos(); +} + +void Window::mouseMoveEvent(QMouseEvent *event) +{ + if (m_lastPos != QPoint(-1, -1)) { + QPainter p(&m_image); + p.setRenderHint(QPainter::Antialiasing); + p.drawLine(m_lastPos, event->pos()); + m_lastPos = event->pos(); + } + + scheduleRender(); +} + +void Window::wheelEvent(QWheelEvent *event) +{ + qDebug() << "wheelEvent delta" << event->delta() << "orientation" << event->orientation(); + qDebug() << "wheelEvent pixelDelta" << event->pixelDelta(); + qDebug() << "wheelEvent angleDelta" << event->angleDelta(); + + const bool useQt4API = false; + + if (useQt4API) { + if (event->orientation() == Qt::Horizontal) + scrollOffset.setX(scrollOffset.x() + event->delta()); + else + scrollOffset.setY(scrollOffset.y() + event->delta()); + scheduleRender(); + } else { + if (!event->pixelDelta().isNull()) { + scrollOffset += event->pixelDelta(); + scheduleRender(); + } + } +} + +void Window::mouseReleaseEvent(QMouseEvent *event) +{ + if (m_lastPos != QPoint(-1, -1)) { + QPainter p(&m_image); + p.setRenderHint(QPainter::Antialiasing); + p.drawLine(m_lastPos, event->pos()); + m_lastPos = QPoint(-1, -1); + } + + scheduleRender(); +} + +void Window::exposeEvent(QExposeEvent *) +{ + scheduleRender(); +} + +void Window::resizeEvent(QResizeEvent *) +{ + QImage old = m_image; + + //qDebug() << "Window::resizeEvent" << width << height; + + int width = qMax(geometry().width(), old.width()); + int height = qMax(geometry().height(), old.height()); + + if (width > old.width() || height > old.height()) { + m_image = QImage(width, height, QImage::Format_RGB32); + m_image.fill(colorTable[(m_backgroundColorIndex) % (sizeof(colorTable) / sizeof(colorTable[0]))].rgba()); + + QPainter p(&m_image); + p.drawImage(0, 0, old); + } + + render(); +} + +void Window::keyPressEvent(QKeyEvent *event) +{ + switch (event->key()) { + case Qt::Key_Backspace: + m_text.chop(1); + break; + case Qt::Key_Enter: + case Qt::Key_Return: + m_text.append('\n'); + break; + default: + m_text.append(event->text()); + break; + } + scheduleRender(); +} + +void Window::scheduleRender() +{ + if (!m_renderTimer) + m_renderTimer = startTimer(1); +} + +void Window::timerEvent(QTimerEvent *) +{ + render(); + killTimer(m_renderTimer); + m_renderTimer = 0; +} + +void Window::render() +{ + QRect rect(QPoint(), geometry().size()); + + m_backingStore->resize(rect.size()); + + m_backingStore->beginPaint(rect); + + QPaintDevice *device = m_backingStore->paintDevice(); + + QPainter p(device); + p.drawImage(0, 0, m_image); + + QFont font; + font.setPixelSize(32); + + p.setFont(font); + p.drawText(rect, 0, m_text); + + // draw grid: + int gridSpace = 80; + + for (int y = 0; y < geometry().height() + gridSpace; y+= gridSpace) { + int offset = scrollOffset.y() % gridSpace; + //int color = ((y + offset) %255);// + scrollOffset.y()) % 255); + p.drawLine(0, y + offset, geometry().width(), y + offset); + //p.setBrush(QColor(color,0, 0)); + //p.fillRect(0, y + offset, geometry().width(), gridSpace,QColor(color,0, 0)); + } + + for (int x = 0; x < geometry().width() + gridSpace; x+= gridSpace) { + p.drawLine(x + scrollOffset.x() % gridSpace, 0, x + scrollOffset.x() % gridSpace, geometry().height()); + } + + m_backingStore->endPaint(); + m_backingStore->flush(rect); +} + + diff --git a/tests/manual/cocoa/wheelevent/window.h b/tests/manual/cocoa/wheelevent/window.h new file mode 100644 index 0000000000..523c9f3dc9 --- /dev/null +++ b/tests/manual/cocoa/wheelevent/window.h @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +class Window : public QWindow +{ +public: + Window(QWindow *parent = 0); + Window(QScreen *screen); + +protected: + void mousePressEvent(QMouseEvent *); + void mouseMoveEvent(QMouseEvent *); + void mouseReleaseEvent(QMouseEvent *); + void wheelEvent(QWheelEvent *event); + + void keyPressEvent(QKeyEvent *); + + void exposeEvent(QExposeEvent *); + void resizeEvent(QResizeEvent *); + + void timerEvent(QTimerEvent *); + +private: + void render(); + void scheduleRender(); + void initialize(); + + QString m_text; + QImage m_image; + QPoint m_lastPos; + int m_backgroundColorIndex; + QBackingStore *m_backingStore; + int m_renderTimer; + QPoint scrollOffset; +}; -- cgit v1.2.3