Debian Codegraph @

tests/manual/nodetypes_ng/nodetypes_ng.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the demonstration applications of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50
51 #include <QGuiApplication>
52 #include <QThread>
53 #include <QQuickView>
54 #include <QQmlEngine>
55 #include <QQmlContext>
56 #include <QQuickPaintedItem>
57 #include <QPainter>
58 #include <QTimer>
59 #include <QQuickItemGrabResult>
60 #include <QSGRectangleNode>
61
62 class Helper : public QObject
63 {
64 Q_OBJECT
65
66 public:
67 Helper(QQuickWindow *w) : m_window(w) { }
68
69 Q_INVOKABLE void sleep(int ms) {
70 QThread::msleep(ms);
71 }
72
73 Q_INVOKABLE void testGrabWindow() {
74 QImage img = m_window->grabWindow();
75 qDebug() << "Saving image to grab_window_result.png" << img;
76 img.save("grab_window_result.png");
77 }
78
79 Q_INVOKABLE void testGrabItem(QQuickItem *item) {
80 qDebug() << item;
81 if (!item)
82 return;
83
84 QSharedPointer<QQuickItemGrabResult> result = item->grabToImage();
85 if (!result)
86 return;
87
88 auto f = [](const QImage &image) {
89 qDebug() << "Saving image to grab_item_result.png" << image;
90 image.save("grab_item_result.png");
91 };
92 if (result->image().isNull()) {
93 connect(result.data(), &QQuickItemGrabResult::ready, [f, result] {
94 f(result->image());
95 });
96 } else {
97 f(result->image());
98 }
99 }
100
101 QQuickWindow *m_window;
102 };
103
104 class TextBalloon : public QQuickPaintedItem
105 {
106 Q_OBJECT
107 Q_PROPERTY(bool rightAligned READ isRightAligned WRITE setRightAligned NOTIFY rightAlignedChanged)
108 Q_PROPERTY(bool innerAnim READ innerAnimEnabled WRITE setInnerAnimEnabled NOTIFY innerAnimChanged)
109
110 public:
111 TextBalloon(QQuickItem *parent = nullptr) : QQuickPaintedItem(parent) {
112 connect(&m_timer, &QTimer::timeout, this, &TextBalloon::onAnim);
113 m_timer.setInterval(500);
114 }
115 void paint(QPainter *painter);
116
117 bool isRightAligned() { return m_rightAligned; }
118 void setRightAligned(bool rightAligned);
119
120 bool innerAnimEnabled() const { return m_innerAnim; }
121 void setInnerAnimEnabled(bool b);
122
123 signals:
124 void rightAlignedChanged();
125 void innerAnimChanged();
126
127 private slots:
128 void onAnim();
129
130 private:
131 bool m_rightAligned = false;
132 bool m_innerAnim = false;
133 QTimer m_timer;
134 QRect m_animRect = QRect(10, 10, 50, 20);
135 int m_anim = 0;
136 };
137
138 void TextBalloon::paint(QPainter *painter)
139 {
140 QBrush brush(QColor("#007430"));
141
142 painter->setBrush(brush);
143 painter->setPen(Qt::NoPen);
144 painter->setRenderHint(QPainter::Antialiasing);
145
146 painter->drawRoundedRect(0, 0, boundingRect().width(), boundingRect().height() - 10, 10, 10);
147
148 if (m_rightAligned) {
149 const QPointF points[3] = {
150 QPointF(boundingRect().width() - 10.0, boundingRect().height() - 10.0),
151 QPointF(boundingRect().width() - 20.0, boundingRect().height()),
152 QPointF(boundingRect().width() - 30.0, boundingRect().height() - 10.0),
153 };
154 painter->drawConvexPolygon(points, 3);
155 } else {
156 const QPointF points[3] = {
157 QPointF(10.0, boundingRect().height() - 10.0),
158 QPointF(20.0, boundingRect().height()),
159 QPointF(30.0, boundingRect().height() - 10.0),
160 };
161 painter->drawConvexPolygon(points, 3);
162 }
163
164 if (m_innerAnim) {
165 painter->fillRect(m_animRect, Qt::lightGray);
166 const int x = m_animRect.x() + m_anim;
167 const int y = m_animRect.y() + m_animRect.height() / 2;
168 painter->setPen(QPen(QBrush(Qt::SolidLine), 4));
169 painter->drawLine(x + 4, y, x + 10, y);
170 m_anim += 10;
171 if (m_anim > m_animRect.width())
172 m_anim = 0;
173 }
174 }
175
176 void TextBalloon::setRightAligned(bool rightAligned)
177 {
178 if (m_rightAligned == rightAligned)
179 return;
180
181 m_rightAligned = rightAligned;
182 emit rightAlignedChanged();
183 }
184
185 void TextBalloon::setInnerAnimEnabled(bool b)
186 {
187 if (m_innerAnim == b)
188 return;
189
190 m_innerAnim = b;
191 if (!b)
192 m_timer.stop();
193 else
194 m_timer.start();
195 emit innerAnimChanged();
196 }
197
198 void TextBalloon::onAnim()
199 {
200 update(m_animRect);
201 }
202
203 class PerPixelRect : public QQuickItem
204 {
205 Q_OBJECT
206 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
207
208 public:
209 PerPixelRect();
210 void setColor(const QColor &c);
211 QColor color() const { return m_color; }
212 QSGNode *updatePaintNode(QSGNode *old, UpdatePaintNodeData *);
213
214 signals:
215 void colorChanged(const QColor &c);
216
217 private:
218 QColor m_color;
219 };
220
221 PerPixelRect::PerPixelRect()
222 {
223 setFlag(ItemHasContents);
224 }
225
226 void PerPixelRect::setColor(const QColor &c)
227 {
228 if (c == m_color)
229 return;
230 m_color = c;
231 emit colorChanged(c);
232 }
233
234 QSGNode *PerPixelRect::updatePaintNode(QSGNode *node, UpdatePaintNodeData *)
235 {
236 delete node;
237 node = new QSGNode;
238
239 const int w = width();
240 const int h = height();
241 QQuickWindow *win = window();
242 for (int y = 0; y < h; ++y) {
243 for (int x = 0; x < w; ++x) {
244 QSGRectangleNode *rn = win->createRectangleNode();
245 rn->setRect(x, y, 1, 1);
246 rn->setColor(m_color);
247 node->appendChildNode(rn);
248 }
249 }
250
251 return node;
252 }
253
254 int main(int argc, char **argv)
255 {
256 QGuiApplication app(argc, argv);
257
258 qDebug("Available tests:");
259 qDebug(" [R] - Simple rectangle (vertexcolor material)");
260 qDebug(" [3] - Rectangles (incl. smoothcolor material, scissor)");
261 qDebug(" [4] - A lot of rectangles (incl. stencil and scissor)");
262 qDebug(" [5] - Rectangles with multiple clip list entries");
263 qDebug(" [I] - Images");
264 qDebug(" [A] - Atlased images");
265 qDebug(" [P] - QQuickPaintedItem");
266 qDebug(" [C] - Compressed textures");
267 qDebug(" [T] - Text (native)");
268 qDebug(" [D] - Text (distance field)");
269 qDebug(" [L] - Layers");
270 qDebug(" [6] - ShaderEffectSource without ShaderEffect");
271 qDebug(" [E] - ShaderEffect (and GraphicsInfo)");
272 qDebug(" [Z] - ShaderEffect without animated properties");
273 qDebug(" [G] - Grab current window");
274 qDebug(" [F] - Grab item");
275 qDebug(" [W] - Multiple windows");
276 qDebug(" [N] - Lots of rectangle nodes");
277 qDebug("\nPress S to stop the currently running test\n");
278
279 QQuickView view;
280 Helper helper(&view);
281
282 const bool usingRhi = qEnvironmentVariableIntValue("QSG_RHI") != 0;
283 const QString rhiBackend = QString::fromLatin1(qgetenv("QSG_RHI_BACKEND"));
284 if (usingRhi)
285 view.setTitle(QLatin1String("RHI: ") + (rhiBackend.isEmpty() ? QLatin1String("default") : rhiBackend));
286 else
287 view.setTitle(QLatin1String("legacy OpenGL"));
288
289 if (app.arguments().contains(QLatin1String("--multisample"))) {
290 qDebug("Requesting sample count 4");
291 QSurfaceFormat fmt = view.format();
292 fmt.setSamples(4);
293 fmt.setDepthBufferSize(24);
294 fmt.setStencilBufferSize(8);
295 view.setFormat(fmt);
296 }
297 if (app.arguments().contains(QLatin1String("--coreprofile"))) {
298 qDebug("Requesting core profile (applicable only with OpenGL)");
299 QSurfaceFormat fmt = view.format();
300 fmt.setVersion(3, 2);
301 fmt.setProfile(QSurfaceFormat::CoreProfile);
302 view.setFormat(fmt);
303 }
304 if (app.arguments().contains(QLatin1String("--transparent"))) {
305 qDebug("Requesting alpha channel for the window and using Qt::transparent as background");
306 QSurfaceFormat fmt = view.format();
307 fmt.setAlphaBufferSize(8);
308 view.setFormat(fmt);
309 view.setColor(Qt::transparent);
310 }
311
312 view.engine()->rootContext()->setContextProperty(QLatin1String("helper"), &helper);
313
314 qmlRegisterType<TextBalloon>("Stuff", 1, 0, "TextBalloon");
315 qmlRegisterType<PerPixelRect>("Stuff", 1, 0, "PerPixelRect");
316
317 view.setResizeMode(QQuickView::SizeRootObjectToView);
318 view.resize(1024, 768);
319 view.setSource(QUrl("qrc:/main.qml"));
320 view.show();
321
322 return app.exec();
323 }
324
325 #include "nodetypes_ng.moc"