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>
This commit is contained in:
Laszlo Agocs 2014-07-29 16:55:32 +02:00
parent e48737ae77
commit 34fbc61f22
9 changed files with 451 additions and 4 deletions

View File

@ -1,4 +1,5 @@
requires(qtHaveModule(gui))
TEMPLATE = subdirs
SUBDIRS = windows
SUBDIRS = windows \
qrasterwindow

View File

@ -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"

View File

@ -0,0 +1,4 @@
SOURCES += main.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/qpa/qrasterwindow
INSTALLS += target

View File

@ -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 += \

View File

@ -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

View File

@ -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

View File

@ -21,7 +21,8 @@ SUBDIRS=\
qwindow \
qguiapplication \
qpixelformat \
qopenglwindow
qopenglwindow \
qrasterwindow
!qtHaveModule(widgets): SUBDIRS -= \
qmouseevent_modal \

View File

@ -0,0 +1,6 @@
CONFIG += testcase
TARGET = tst_qrasterwindow
QT += core-private gui-private testlib
SOURCES += tst_qrasterwindow.cpp

View File

@ -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)