Debian Codegraph @

tests/manual/examples/qpa/qrasterwindow/main.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include <QRasterWindow>
5 #include <QPainter>
6 #include <QPainterPath>
7 #include <QGuiApplication>
8 #include <QMatrix4x4>
9 #include <QTimer>
10
11 static QPainterPath painterPathForTriangle()
12 {
13 static const QPointF bottomLeft(-1.0, -1.0);
14 static const QPointF top(0.0, 1.0);
15 static const QPointF bottomRight(1.0, -1.0);
16
17 QPainterPath path(bottomLeft);
18 path.lineTo(top);
19 path.lineTo(bottomRight);
20 path.closeSubpath();
21 return path;
22 }
23
24 class PaintedWindow : public QRasterWindow
25 {
26 Q_OBJECT
27
28 public:
29 PaintedWindow()
30 {
31 m_view.lookAt(QVector3D(3,1,1),
32 QVector3D(0,0,0),
33 QVector3D(0,1,0));
34 m_timer.setInterval(16);
35 connect(&m_timer, &QTimer::timeout, this, qOverload<>(&PaintedWindow::update));
36 m_timer.start();
37 }
38
39 protected:
40 void paintEvent(QPaintEvent *)
41 {
42 QPainter p(this);
43 p.fillRect(QRect(0,0,width(),height()),Qt::gray);
44
45 p.setWorldTransform(m_window_matrix.toTransform());
46
47 QMatrix4x4 mvp = m_projection * m_view * m_model;
48 p.setTransform(mvp.toTransform(), true);
49
50 p.fillPath(painterPathForTriangle(), m_brush);
51
52 m_model.rotate(1, 0, 1, 0);
53 }
54
55 void resizeEvent(QResizeEvent *)
56 {
57 m_window_matrix = QTransform();
58 m_window_matrix.translate(width() / 2.0, height() / 2.0);
59 m_window_matrix.scale(width() / 2.0, -height() / 2.0);
60
61 m_projection.setToIdentity();
62 m_projection.perspective(45.f, qreal(width()) / qreal(height()), 0.1f, 100.f);
63
64 QLinearGradient gradient(QPointF(-1,-1), QPointF(1,1));
65 gradient.setColorAt(0, Qt::red);
66 gradient.setColorAt(1, Qt::green);
67
68 m_brush = QBrush(gradient);
69 }
70
71 private:
72 QMatrix4x4 m_window_matrix;
73 QMatrix4x4 m_projection;
74 QMatrix4x4 m_view;
75 QMatrix4x4 m_model;
76 QBrush m_brush;
77 QTimer m_timer;
78 };
79
80 int main (int argc, char **argv)
81 {
82 QGuiApplication app(argc, argv);
83
84 PaintedWindow window;
85 window.create();
86 window.show();
87
88 return app.exec();
89 }
90
91 #include "main.moc"