summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaszlo Agocs <laszlo.agocs@digia.com>2014-07-29 16:55:32 +0200
committerLaszlo Agocs <laszlo.agocs@digia.com>2014-08-05 16:47:42 +0200
commit34fbc61f22f185bc4ef542132111d045956f5011 (patch)
treeb327dd7db83c92ac9e4da57201f79c6dddcd0f32
parente48737ae778b8ef5e8e905825a2787b97deea23d (diff)
Introduce QRasterWindow
A simple convenience class providing a QWindow that has a paintEvent and supports opening a painter on itself. It behaves exactly like QOpenGLWindow in this respect, which is not surprising since they share the same base class (QPaintDeviceWindow). QRasterWindow does not however have any OpenGL dependencies and will be present in -no-opengl builds too. [ChangeLog] Added QRasterWindow, a thin convenience wrapper for a QWindow on which a QPainter can be opened. Done-with: Jorgen Lind <jorgen.lind@digia.com> Change-Id: I37e82720492945d7b85d5f713eea8d5f7556e511 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@digia.com>
-rw-r--r--examples/qpa/qpa.pro3
-rw-r--r--examples/qpa/qrasterwindow/main.cpp127
-rw-r--r--examples/qpa/qrasterwindow/qrasterwindow.pro4
-rw-r--r--src/gui/kernel/kernel.pri6
-rw-r--r--src/gui/kernel/qrasterwindow.cpp136
-rw-r--r--src/gui/kernel/qrasterwindow.h69
-rw-r--r--tests/auto/gui/kernel/kernel.pro3
-rw-r--r--tests/auto/gui/kernel/qrasterwindow/qrasterwindow.pro6
-rw-r--r--tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp101
9 files changed, 451 insertions, 4 deletions
diff --git a/examples/qpa/qpa.pro b/examples/qpa/qpa.pro
index 27293482ef..85b51dc5f4 100644
--- a/examples/qpa/qpa.pro
+++ b/examples/qpa/qpa.pro
@@ -1,4 +1,5 @@
requires(qtHaveModule(gui))
TEMPLATE = subdirs
-SUBDIRS = windows
+SUBDIRS = windows \
+ qrasterwindow
diff --git a/examples/qpa/qrasterwindow/main.cpp b/examples/qpa/qrasterwindow/main.cpp
new file mode 100644
index 0000000000..0e87d41682
--- /dev/null
+++ b/examples/qpa/qrasterwindow/main.cpp
@@ -0,0 +1,127 @@
+/****************************************************************************
+ **
+ ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+ ** Contact: http://www.qt-project.org/legal
+ **
+ ** This file is part of the examples of the Qt Toolkit.
+ **
+ ** $QT_BEGIN_LICENSE:BSD$
+ ** You may use this file under the terms of the BSD license as follows:
+ **
+ ** "Redistribution and use in source and binary forms, with or without
+ ** modification, are permitted provided that the following conditions are
+ ** met:
+ ** * Redistributions of source code must retain the above copyright
+ ** notice, this list of conditions and the following disclaimer.
+ ** * Redistributions in binary form must reproduce the above copyright
+ ** notice, this list of conditions and the following disclaimer in
+ ** the documentation and/or other materials provided with the
+ ** distribution.
+ ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
+ ** of its contributors may be used to endorse or promote products derived
+ ** from this software without specific prior written permission.
+ **
+ **
+ ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+ **
+ ** $QT_END_LICENSE$
+ **
+ ****************************************************************************/
+
+#include <QRasterWindow>
+#include <QPainter>
+#include <QGuiApplication>
+#include <QMatrix4x4>
+#include <QTimer>
+
+static QPainterPath painterPathForTriangle()
+{
+ static const QPointF bottomLeft(-1.0, -1.0);
+ static const QPointF top(0.0, 1.0);
+ static const QPointF bottomRight(1.0, -1.0);
+
+ QPainterPath path(bottomLeft);
+ path.lineTo(top);
+ path.lineTo(bottomRight);
+ path.closeSubpath();
+ return path;
+}
+
+class PaintedWindow : public QRasterWindow
+{
+ Q_OBJECT
+
+public:
+ PaintedWindow()
+ {
+ m_view.lookAt(QVector3D(3,1,1),
+ QVector3D(0,0,0),
+ QVector3D(0,1,0));
+ m_timer.setInterval(16);
+ connect(&m_timer, SIGNAL(timeout()), this, SLOT(update()));
+ m_timer.start();
+ }
+
+protected:
+ void paintEvent(QPaintEvent *)
+ {
+ QPainter p(this);
+ p.fillRect(QRect(0,0,width(),height()),Qt::gray);
+
+ p.setWorldTransform(m_window_matrix.toTransform());
+
+ QMatrix4x4 mvp = m_projection * m_view * m_model;
+ p.setTransform(mvp.toTransform(), true);
+
+ p.fillPath(painterPathForTriangle(), m_brush);
+
+ m_model.rotate(1, 0, 1, 0);
+ }
+
+ void resizeEvent(QResizeEvent *)
+ {
+ m_window_matrix = QTransform();
+ m_window_matrix.translate(width() / 2.0, height() / 2.0);
+ m_window_matrix.scale(width() / 2.0, -height() / 2.0);
+
+ m_projection.setToIdentity();
+ m_projection.perspective(45.f, qreal(width()) / qreal(height()), 0.1f, 100.f);
+
+ QLinearGradient gradient(QPointF(-1,-1), QPointF(1,1));
+ gradient.setColorAt(0, Qt::red);
+ gradient.setColorAt(1, Qt::green);
+
+ m_brush = QBrush(gradient);
+ }
+
+private:
+ QMatrix4x4 m_window_matrix;
+ QMatrix4x4 m_projection;
+ QMatrix4x4 m_view;
+ QMatrix4x4 m_model;
+ QBrush m_brush;
+ QTimer m_timer;
+};
+
+int main (int argc, char **argv)
+{
+ QGuiApplication app(argc, argv);
+
+ PaintedWindow window;
+ window.create();
+ window.show();
+
+ return app.exec();
+}
+
+#include "main.moc"
diff --git a/examples/qpa/qrasterwindow/qrasterwindow.pro b/examples/qpa/qrasterwindow/qrasterwindow.pro
new file mode 100644
index 0000000000..e5bf34f25c
--- /dev/null
+++ b/examples/qpa/qrasterwindow/qrasterwindow.pro
@@ -0,0 +1,4 @@
+SOURCES += main.cpp
+
+target.path = $$[QT_INSTALL_EXAMPLES]/qpa/qrasterwindow
+INSTALLS += target
diff --git a/src/gui/kernel/kernel.pri b/src/gui/kernel/kernel.pri
index bc167cea57..c2422ec98b 100644
--- a/src/gui/kernel/kernel.pri
+++ b/src/gui/kernel/kernel.pri
@@ -71,7 +71,8 @@ HEADERS += \
kernel/qplatformsessionmanager.h \
kernel/qpixelformat.h \
kernel/qpaintdevicewindow.h \
- kernel/qpaintdevicewindow_p.h
+ kernel/qpaintdevicewindow_p.h \
+ kernel/qrasterwindow.h
SOURCES += \
kernel/qgenericpluginfactory.cpp \
@@ -124,7 +125,8 @@ SOURCES += \
kernel/qplatformsessionmanager.cpp \
kernel/qplatformmenu.cpp \
kernel/qpixelformat.cpp \
- kernel/qpaintdevicewindow.cpp
+ kernel/qpaintdevicewindow.cpp \
+ kernel/qrasterwindow.cpp
contains(QT_CONFIG, opengl)|contains(QT_CONFIG, opengles2) {
HEADERS += \
diff --git a/src/gui/kernel/qrasterwindow.cpp b/src/gui/kernel/qrasterwindow.cpp
new file mode 100644
index 0000000000..5f4d707f86
--- /dev/null
+++ b/src/gui/kernel/qrasterwindow.cpp
@@ -0,0 +1,136 @@
+/****************************************************************************
+ **
+ ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+ ** Contact: http://www.qt-project.org/legal
+ **
+ ** This file is part of the QtGui module of the Qt Toolkit.
+ **
+ ** $QT_BEGIN_LICENSE:LGPL$
+ ** Commercial License Usage
+ ** Licensees holding valid commercial Qt licenses may use this file in
+ ** accordance with the commercial license agreement provided with the
+ ** Software or, alternatively, in accordance with the terms contained in
+ ** a written agreement between you and Digia. For licensing terms and
+ ** conditions see http://qt.digia.com/licensing. For further information
+ ** use the contact form at http://qt.digia.com/contact-us.
+ **
+ ** GNU Lesser General Public License Usage
+ ** Alternatively, 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, Digia gives you certain additional
+ ** rights. These rights are described in the Digia 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.
+ **
+ **
+ ** $QT_END_LICENSE$
+ **
+ ****************************************************************************/
+
+#include "qrasterwindow.h"
+
+#include <QtGui/private/qpaintdevicewindow_p.h>
+
+#include <QtGui/QBackingStore>
+#include <QtGui/QPainter>
+
+QT_BEGIN_NAMESPACE
+
+/*!
+ \class QRasterWindow
+ \inmodule QtGui
+ \since 5.4
+ \brief QRasterWindow is a convenience class for using QPainter on a QWindow
+
+ QRasterWindow is a QWindow with a raster-based, non-OpenGL surface. On top of
+ the functionality offered by QWindow, QRasterWindow adds a virtual
+ paintEvent() function and the possibility to open a QPainter on itself. The
+ underlying paint engine will be the raster one, meaning that all drawing will
+ happen on the CPU. For performing accelerated, OpenGL-based drawing, use
+ QOpenGLWindow instead.
+
+ Internally the class is thin wrapper for QWindow and QBackingStore
+ and is very similar to the \l{Raster Window Example}{Raster Window
+ Example} that uses these classes directly.
+
+ \sa QPaintDeviceWindow::paintEvent(), QPaintDeviceWindow::update()
+*/
+
+class QRasterWindowPrivate : public QPaintDeviceWindowPrivate
+{
+ Q_DECLARE_PUBLIC(QRasterWindow)
+public:
+ void beginPaint(const QRegion &region) Q_DECL_OVERRIDE
+ {
+ Q_Q(QRasterWindow);
+ if (backingstore->size() != q->size()) {
+ backingstore->resize(q->size());
+ markWindowAsDirty();
+ }
+ backingstore->beginPaint(region);
+ }
+
+ void endPaint() Q_DECL_OVERRIDE
+ {
+ backingstore->endPaint();
+ }
+
+ void flush(const QRegion &region) Q_DECL_OVERRIDE
+ {
+ Q_Q(QRasterWindow);
+ backingstore->flush(region, q);
+ }
+
+ QScopedPointer<QBackingStore> backingstore;
+};
+
+/*!
+ Constructs a new QRasterWindow with \a parent.
+*/
+QRasterWindow::QRasterWindow(QWindow *parent)
+ : QPaintDeviceWindow(*(new QRasterWindowPrivate), parent)
+{
+ setSurfaceType(QSurface::RasterSurface);
+ d_func()->backingstore.reset(new QBackingStore(this));
+}
+
+/*!
+ \internal
+*/
+int QRasterWindow::metric(PaintDeviceMetric metric) const
+{
+ Q_D(const QRasterWindow);
+
+ switch (metric) {
+ case PdmDepth:
+ return d->backingstore->paintDevice()->depth();
+ case PdmDevicePixelRatio:
+ return d->backingstore->paintDevice()->devicePixelRatio();
+ default:
+ break;
+ }
+ return QPaintDeviceWindow::metric(metric);
+}
+
+/*!
+ \internal
+*/
+QPaintDevice *QRasterWindow::redirected(QPoint *) const
+{
+ Q_D(const QRasterWindow);
+ return d->backingstore->paintDevice();
+}
+
+QT_END_NAMESPACE
diff --git a/src/gui/kernel/qrasterwindow.h b/src/gui/kernel/qrasterwindow.h
new file mode 100644
index 0000000000..93e8421c6b
--- /dev/null
+++ b/src/gui/kernel/qrasterwindow.h
@@ -0,0 +1,69 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtGui module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, 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, Digia gives you certain additional
+** rights. These rights are described in the Digia 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.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QRASTERWINDOW_H
+#define QRASTERWINDOW_H
+
+#include <QtGui/QPaintDeviceWindow>
+
+QT_BEGIN_NAMESPACE
+
+class QRasterWindowPrivate;
+
+class Q_GUI_EXPORT QRasterWindow : public QPaintDeviceWindow
+{
+ Q_OBJECT
+ Q_DECLARE_PRIVATE(QRasterWindow)
+
+public:
+ explicit QRasterWindow(QWindow *parent = 0);
+
+protected:
+ int metric(PaintDeviceMetric metric) const Q_DECL_OVERRIDE;
+ QPaintDevice *redirected(QPoint *) const Q_DECL_OVERRIDE;
+
+private:
+ Q_DISABLE_COPY(QRasterWindow)
+};
+
+QT_END_NAMESPACE
+
+#endif
diff --git a/tests/auto/gui/kernel/kernel.pro b/tests/auto/gui/kernel/kernel.pro
index 7d8af15b25..7d47a4167d 100644
--- a/tests/auto/gui/kernel/kernel.pro
+++ b/tests/auto/gui/kernel/kernel.pro
@@ -21,7 +21,8 @@ SUBDIRS=\
qwindow \
qguiapplication \
qpixelformat \
- qopenglwindow
+ qopenglwindow \
+ qrasterwindow
!qtHaveModule(widgets): SUBDIRS -= \
qmouseevent_modal \
diff --git a/tests/auto/gui/kernel/qrasterwindow/qrasterwindow.pro b/tests/auto/gui/kernel/qrasterwindow/qrasterwindow.pro
new file mode 100644
index 0000000000..9acd7e510a
--- /dev/null
+++ b/tests/auto/gui/kernel/qrasterwindow/qrasterwindow.pro
@@ -0,0 +1,6 @@
+CONFIG += testcase
+TARGET = tst_qrasterwindow
+
+QT += core-private gui-private testlib
+
+SOURCES += tst_qrasterwindow.cpp
diff --git a/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp b/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp
new file mode 100644
index 0000000000..78d0d0a879
--- /dev/null
+++ b/tests/auto/gui/kernel/qrasterwindow/tst_qrasterwindow.cpp
@@ -0,0 +1,101 @@
+/****************************************************************************
+**
+** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, 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, Digia gives you certain additional
+** rights. These rights are described in the Digia 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.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtGui/QRasterWindow>
+#include <QtTest/QtTest>
+#include <QtGui/QPainter>
+
+class tst_QRasterWindow : public QObject
+{
+ Q_OBJECT
+
+private slots:
+ void create();
+ void basic();
+};
+
+void tst_QRasterWindow::create()
+{
+ QRasterWindow w;
+
+ w.resize(640, 480);
+ w.show();
+
+ QTest::qWaitForWindowExposed(&w);
+}
+
+class PainterWindow : public QRasterWindow
+{
+public:
+ void reset() { paintCount = 0; }
+
+ void paintEvent(QPaintEvent*) Q_DECL_OVERRIDE {
+ ++paintCount;
+ QPainter p(this);
+ p.fillRect(QRect(0, 0, 100, 100), Qt::blue);
+ p.end();
+ }
+
+ int paintCount;
+};
+
+void tst_QRasterWindow::basic()
+{
+ PainterWindow w;
+ w.reset();
+ w.resize(400, 400);
+ w.show();
+ QTest::qWaitForWindowExposed(&w);
+
+ QVERIFY(w.paintCount >= 1);
+
+ w.reset();
+ w.update();
+ int maxWait = 1000;
+ while (w.paintCount == 0 && --maxWait > 0)
+ QTest::qWait(10);
+
+ QVERIFY(w.paintCount >= 1);
+}
+
+#include <tst_qrasterwindow.moc>
+
+QTEST_MAIN(tst_QRasterWindow)