Debian Codegraph @

examples/widgets/painting/composition/composition.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 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 "composition.h"
52 #include <QBoxLayout>
53 #include <QRadioButton>
54 #include <QTimer>
55 #include <QDateTime>
56 #include <QSlider>
57 #include <QMouseEvent>
58 #include <qmath.h>
59
60 #if QT_CONFIG(opengl)
61 #include <QOpenGLFunctions>
62 #include <QOpenGLWindow>
63 #endif
64
65 const int animationInterval = 15; // update every 16 ms = ~60FPS
66
67 CompositionWidget::CompositionWidget(QWidget *parent)
68 : QWidget(parent)
69 {
70 CompositionRenderer *view = new CompositionRenderer(this);
71
72 QGroupBox *mainGroup = new QGroupBox(parent);
73 mainGroup->setTitle(tr("Composition Modes"));
74
75 QGroupBox *modesGroup = new QGroupBox(mainGroup);
76 modesGroup->setTitle(tr("Mode"));
77
78 rbClear = new QRadioButton(tr("Clear"), modesGroup);
79 connect(rbClear, &QAbstractButton::clicked, view, &CompositionRenderer::setClearMode);
80 rbSource = new QRadioButton(tr("Source"), modesGroup);
81 connect(rbSource, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceMode);
82 rbDest = new QRadioButton(tr("Destination"), modesGroup);
83 connect(rbDest, &QAbstractButton::clicked, view, &CompositionRenderer::setDestMode);
84 rbSourceOver = new QRadioButton(tr("Source Over"), modesGroup);
85 connect(rbSourceOver, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceOverMode);
86 rbDestOver = new QRadioButton(tr("Destination Over"), modesGroup);
87 connect(rbDestOver, &QAbstractButton::clicked, view, &CompositionRenderer::setDestOverMode);
88 rbSourceIn = new QRadioButton(tr("Source In"), modesGroup);
89 connect(rbSourceIn, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceInMode);
90 rbDestIn = new QRadioButton(tr("Dest In"), modesGroup);
91 connect(rbDestIn, &QAbstractButton::clicked, view, &CompositionRenderer::setDestInMode);
92 rbSourceOut = new QRadioButton(tr("Source Out"), modesGroup);
93 connect(rbSourceOut, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceOutMode);
94 rbDestOut = new QRadioButton(tr("Dest Out"), modesGroup);
95 connect(rbDestOut, &QAbstractButton::clicked, view, &CompositionRenderer::setDestOutMode);
96 rbSourceAtop = new QRadioButton(tr("Source Atop"), modesGroup);
97 connect(rbSourceAtop, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceAtopMode);
98 rbDestAtop = new QRadioButton(tr("Dest Atop"), modesGroup);
99 connect(rbDestAtop, &QAbstractButton::clicked, view, &CompositionRenderer::setDestAtopMode);
100 rbXor = new QRadioButton(tr("Xor"), modesGroup);
101 connect(rbXor, &QAbstractButton::clicked, view, &CompositionRenderer::setXorMode);
102
103 rbPlus = new QRadioButton(tr("Plus"), modesGroup);
104 connect(rbPlus, &QAbstractButton::clicked, view, &CompositionRenderer::setPlusMode);
105 rbMultiply = new QRadioButton(tr("Multiply"), modesGroup);
106 connect(rbMultiply, &QAbstractButton::clicked, view, &CompositionRenderer::setMultiplyMode);
107 rbScreen = new QRadioButton(tr("Screen"), modesGroup);
108 connect(rbScreen, &QAbstractButton::clicked, view, &CompositionRenderer::setScreenMode);
109 rbOverlay = new QRadioButton(tr("Overlay"), modesGroup);
110 connect(rbOverlay, &QAbstractButton::clicked, view, &CompositionRenderer::setOverlayMode);
111 rbDarken = new QRadioButton(tr("Darken"), modesGroup);
112 connect(rbDarken, &QAbstractButton::clicked, view, &CompositionRenderer::setDarkenMode);
113 rbLighten = new QRadioButton(tr("Lighten"), modesGroup);
114 connect(rbLighten, &QAbstractButton::clicked, view, &CompositionRenderer::setLightenMode);
115 rbColorDodge = new QRadioButton(tr("Color Dodge"), modesGroup);
116 connect(rbColorDodge, &QAbstractButton::clicked, view, &CompositionRenderer::setColorDodgeMode);
117 rbColorBurn = new QRadioButton(tr("Color Burn"), modesGroup);
118 connect(rbColorBurn, &QAbstractButton::clicked, view, &CompositionRenderer::setColorBurnMode);
119 rbHardLight = new QRadioButton(tr("Hard Light"), modesGroup);
120 connect(rbHardLight, &QAbstractButton::clicked, view, &CompositionRenderer::setHardLightMode);
121 rbSoftLight = new QRadioButton(tr("Soft Light"), modesGroup);
122 connect(rbSoftLight, &QAbstractButton::clicked, view, &CompositionRenderer::setSoftLightMode);
123 rbDifference = new QRadioButton(tr("Difference"), modesGroup);
124 connect(rbDifference, &QAbstractButton::clicked, view, &CompositionRenderer::setDifferenceMode);
125 rbExclusion = new QRadioButton(tr("Exclusion"), modesGroup);
126 connect(rbExclusion, &QAbstractButton::clicked, view, &CompositionRenderer::setExclusionMode);
127
128 QGroupBox *circleColorGroup = new QGroupBox(mainGroup);
129 circleColorGroup->setTitle(tr("Circle color"));
130 QSlider *circleColorSlider = new QSlider(Qt::Horizontal, circleColorGroup);
131 circleColorSlider->setRange(0, 359);
132 circleColorSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
133 connect(circleColorSlider, &QAbstractSlider::valueChanged, view, &CompositionRenderer::setCircleColor);
134
135 QGroupBox *circleAlphaGroup = new QGroupBox(mainGroup);
136 circleAlphaGroup->setTitle(tr("Circle alpha"));
137 QSlider *circleAlphaSlider = new QSlider(Qt::Horizontal, circleAlphaGroup);
138 circleAlphaSlider->setRange(0, 255);
139 circleAlphaSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
140 connect(circleAlphaSlider, &QAbstractSlider::valueChanged, view, &CompositionRenderer::setCircleAlpha);
141
142 QPushButton *showSourceButton = new QPushButton(mainGroup);
143 showSourceButton->setText(tr("Show Source"));
144 #if QT_CONFIG(opengl)
145 QPushButton *enableOpenGLButton = new QPushButton(mainGroup);
146 enableOpenGLButton->setText(tr("Use OpenGL"));
147 enableOpenGLButton->setCheckable(true);
148 enableOpenGLButton->setChecked(view->usesOpenGL());
149 #endif
150 QPushButton *whatsThisButton = new QPushButton(mainGroup);
151 whatsThisButton->setText(tr("What's This?"));
152 whatsThisButton->setCheckable(true);
153
154 QPushButton *animateButton = new QPushButton(mainGroup);
155 animateButton->setText(tr("Animated"));
156 animateButton->setCheckable(true);
157 animateButton->setChecked(true);
158
159 QHBoxLayout *viewLayout = new QHBoxLayout(this);
160 viewLayout->addWidget(view);
161 viewLayout->addWidget(mainGroup);
162
163 QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
164 mainGroupLayout->addWidget(circleColorGroup);
165 mainGroupLayout->addWidget(circleAlphaGroup);
166 mainGroupLayout->addWidget(modesGroup);
167 mainGroupLayout->addStretch();
168 mainGroupLayout->addWidget(animateButton);
169 mainGroupLayout->addWidget(whatsThisButton);
170 mainGroupLayout->addWidget(showSourceButton);
171 #if QT_CONFIG(opengl)
172 mainGroupLayout->addWidget(enableOpenGLButton);
173 #endif
174
175 QGridLayout *modesLayout = new QGridLayout(modesGroup);
176 modesLayout->addWidget(rbClear, 0, 0);
177 modesLayout->addWidget(rbSource, 1, 0);
178 modesLayout->addWidget(rbDest, 2, 0);
179 modesLayout->addWidget(rbSourceOver, 3, 0);
180 modesLayout->addWidget(rbDestOver, 4, 0);
181 modesLayout->addWidget(rbSourceIn, 5, 0);
182 modesLayout->addWidget(rbDestIn, 6, 0);
183 modesLayout->addWidget(rbSourceOut, 7, 0);
184 modesLayout->addWidget(rbDestOut, 8, 0);
185 modesLayout->addWidget(rbSourceAtop, 9, 0);
186 modesLayout->addWidget(rbDestAtop, 10, 0);
187 modesLayout->addWidget(rbXor, 11, 0);
188
189 modesLayout->addWidget(rbPlus, 0, 1);
190 modesLayout->addWidget(rbMultiply, 1, 1);
191 modesLayout->addWidget(rbScreen, 2, 1);
192 modesLayout->addWidget(rbOverlay, 3, 1);
193 modesLayout->addWidget(rbDarken, 4, 1);
194 modesLayout->addWidget(rbLighten, 5, 1);
195 modesLayout->addWidget(rbColorDodge, 6, 1);
196 modesLayout->addWidget(rbColorBurn, 7, 1);
197 modesLayout->addWidget(rbHardLight, 8, 1);
198 modesLayout->addWidget(rbSoftLight, 9, 1);
199 modesLayout->addWidget(rbDifference, 10, 1);
200 modesLayout->addWidget(rbExclusion, 11, 1);
201
202
203 QVBoxLayout *circleColorLayout = new QVBoxLayout(circleColorGroup);
204 circleColorLayout->addWidget(circleColorSlider);
205
206 QVBoxLayout *circleAlphaLayout = new QVBoxLayout(circleAlphaGroup);
207 circleAlphaLayout->addWidget(circleAlphaSlider);
208
209 view->loadDescription(":res/composition/composition.html");
210 view->loadSourceFile(":res/composition/composition.cpp");
211
212 connect(whatsThisButton, &QAbstractButton::clicked, view, &ArthurFrame::setDescriptionEnabled);
213 connect(view, &ArthurFrame::descriptionEnabledChanged, whatsThisButton, &QAbstractButton::setChecked);
214 connect(showSourceButton, &QAbstractButton::clicked, view, &ArthurFrame::showSource);
215 #if QT_CONFIG(opengl)
216 connect(enableOpenGLButton, &QAbstractButton::clicked, view, &ArthurFrame::enableOpenGL);
217 #endif
218 connect(animateButton, &QAbstractButton::toggled, view, &CompositionRenderer::setAnimationEnabled);
219
220 circleColorSlider->setValue(270);
221 circleAlphaSlider->setValue(200);
222 rbSourceOut->animateClick();
223
224 setWindowTitle(tr("Composition Modes"));
225 }
226
227 CompositionWidget::~CompositionWidget()
228 {
229 }
230
231
232 void CompositionWidget::nextMode()
233 {
234 /*
235 if (!m_animation_enabled)
236 return;
237 if (rbClear->isChecked()) rbSource->animateClick();
238 if (rbSource->isChecked()) rbDest->animateClick();
239 if (rbDest->isChecked()) rbSourceOver->animateClick();
240 if (rbSourceOver->isChecked()) rbDestOver->animateClick();
241 if (rbDestOver->isChecked()) rbSourceIn->animateClick();
242 if (rbSourceIn->isChecked()) rbDestIn->animateClick();
243 if (rbDestIn->isChecked()) rbSourceOut->animateClick();
244 if (rbSourceOut->isChecked()) rbDestOut->animateClick();
245 if (rbDestOut->isChecked()) rbSourceAtop->animateClick();
246 if (rbSourceAtop->isChecked()) rbDestAtop->animateClick();
247 if (rbDestAtop->isChecked()) rbXor->animateClick();
248 if (rbXor->isChecked()) rbClear->animateClick();
249 */
250 }
251
252 CompositionRenderer::CompositionRenderer(QWidget *parent)
253 : ArthurFrame(parent)
254 {
255 m_animation_enabled = true;
256 m_animationTimer = startTimer(animationInterval);
257 m_image = QImage(":res/composition/flower.jpg");
258 m_image.setAlphaChannel(QImage(":res/composition/flower_alpha.jpg"));
259 m_circle_alpha = 127;
260 m_circle_hue = 255;
261 m_current_object = NoObject;
262 m_composition_mode = QPainter::CompositionMode_SourceOut;
263
264 m_circle_pos = QPoint(200, 100);
265
266 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
267 #if QT_CONFIG(opengl)
268 m_pbuffer_size = 1024;
269 #endif
270 }
271
272 CompositionRenderer::~CompositionRenderer()
273 {
274 }
275
276 QRectF rectangle_around(const QPointF &p, const QSizeF &size = QSize(250, 200))
277 {
278 QRectF rect(p, size);
279 rect.translate(-size.width()/2, -size.height()/2);
280 return rect;
281 }
282
283 void CompositionRenderer::setAnimationEnabled(bool enabled)
284 {
285 if (m_animation_enabled == enabled)
286 return;
287 m_animation_enabled = enabled;
288 if (enabled) {
289 Q_ASSERT(!m_animationTimer);
290 m_animationTimer = startTimer(animationInterval);
291 } else {
292 killTimer(m_animationTimer);
293 m_animationTimer = 0;
294 }
295 }
296
297 void CompositionRenderer::updateCirclePos()
298 {
299 if (m_current_object != NoObject)
300 return;
301 QDateTime dt = QDateTime::currentDateTime();
302 qreal t = dt.toMSecsSinceEpoch() / 1000.0;
303
304 qreal x = width() / qreal(2) + (qCos(t*8/11) + qSin(-t)) * width() / qreal(4);
305 qreal y = height() / qreal(2) + (qSin(t*6/7) + qCos(t * qreal(1.5))) * height() / qreal(4);
306
307 setCirclePos(QLineF(m_circle_pos, QPointF(x, y)).pointAt(0.02));
308 }
309
310 void CompositionRenderer::drawBase(QPainter &p)
311 {
312 p.setPen(Qt::NoPen);
313
314 QLinearGradient rect_gradient(0, 0, 0, height());
315 rect_gradient.setColorAt(0, Qt::red);
316 rect_gradient.setColorAt(.17, Qt::yellow);
317 rect_gradient.setColorAt(.33, Qt::green);
318 rect_gradient.setColorAt(.50, Qt::cyan);
319 rect_gradient.setColorAt(.66, Qt::blue);
320 rect_gradient.setColorAt(.81, Qt::magenta);
321 rect_gradient.setColorAt(1, Qt::red);
322 p.setBrush(rect_gradient);
323 p.drawRect(width() / 2, 0, width() / 2, height());
324
325 QLinearGradient alpha_gradient(0, 0, width(), 0);
326 alpha_gradient.setColorAt(0, Qt::white);
327 alpha_gradient.setColorAt(0.2, Qt::white);
328 alpha_gradient.setColorAt(0.5, Qt::transparent);
329 alpha_gradient.setColorAt(0.8, Qt::white);
330 alpha_gradient.setColorAt(1, Qt::white);
331
332 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
333 p.setBrush(alpha_gradient);
334 p.drawRect(0, 0, width(), height());
335
336 p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
337
338 p.setPen(Qt::NoPen);
339 p.setRenderHint(QPainter::SmoothPixmapTransform);
340 p.drawImage(rect(), m_image);
341 }
342
343 void CompositionRenderer::drawSource(QPainter &p)
344 {
345 p.setPen(Qt::NoPen);
346 p.setRenderHint(QPainter::Antialiasing);
347 p.setCompositionMode(m_composition_mode);
348
349 QRectF circle_rect = rectangle_around(m_circle_pos);
350 QColor color = QColor::fromHsvF(m_circle_hue / 360.0, 1, 1, m_circle_alpha / 255.0);
351 QLinearGradient circle_gradient(circle_rect.topLeft(), circle_rect.bottomRight());
352 circle_gradient.setColorAt(0, color.lighter());
353 circle_gradient.setColorAt(0.5, color);
354 circle_gradient.setColorAt(1, color.darker());
355 p.setBrush(circle_gradient);
356
357 p.drawEllipse(circle_rect);
358 }
359
360 void CompositionRenderer::paint(QPainter *painter)
361 {
362 #if QT_CONFIG(opengl)
363 if (usesOpenGL() && glWindow()->isValid()) {
364
365 if (!m_blitter.isCreated())
366 m_blitter.create();
367
368 int new_pbuf_size = m_pbuffer_size;
369 while (size().width() > new_pbuf_size || size().height() > new_pbuf_size)
370 new_pbuf_size *= 2;
371
372 while (size().width() < new_pbuf_size/2 && size().height() < new_pbuf_size/2)
373 new_pbuf_size /= 2;
374
375 if (!m_fbo || new_pbuf_size != m_pbuffer_size) {
376 m_fbo.reset(new QFboPaintDevice(QSize(new_pbuf_size, new_pbuf_size), false, false));
377 m_pbuffer_size = new_pbuf_size;
378 }
379
380 if (size() != m_previous_size) {
381 m_previous_size = size();
382 QPainter p(m_fbo.get());
383 p.setCompositionMode(QPainter::CompositionMode_Source);
384 p.fillRect(QRect(QPoint(0, 0), size()), Qt::transparent);
385 p.setCompositionMode(QPainter::CompositionMode_SourceOver);
386 drawBase(p);
387 p.end();
388 m_base_tex = m_fbo->takeTexture();
389 }
390
391 painter->beginNativePainting();
392 {
393 QPainter p(m_fbo.get());
394 p.beginNativePainting();
395 m_blitter.bind();
396 const QRect targetRect(QPoint(0, 0), m_fbo->size());
397 const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), m_fbo->size()));
398 m_blitter.blit(m_base_tex, target, QOpenGLTextureBlitter::OriginBottomLeft);
399 m_blitter.release();
400 p.endNativePainting();
401 drawSource(p);
402 p.end();
403 m_compositing_tex = m_fbo->takeTexture();
404 }
405 painter->endNativePainting();
406
407 painter->beginNativePainting();
408 auto *funcs = QOpenGLContext::currentContext()->functions();
409 funcs->glEnable(GL_BLEND);
410 funcs->glBlendEquation(GL_FUNC_ADD);
411 funcs->glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
412 m_blitter.bind();
413 const QRect targetRect(QPoint(0, 0), m_fbo->size());
414 const QMatrix4x4 target = QOpenGLTextureBlitter::targetTransform(targetRect, QRect(QPoint(0, 0), size()));
415 m_blitter.blit(m_compositing_tex, target, QOpenGLTextureBlitter::OriginBottomLeft);
416 m_blitter.release();
417 painter->endNativePainting();
418 } else
419 #endif
420 {
421 // using a QImage
422 if (m_buffer.size() != size()) {
423 m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
424 m_base_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
425
426 m_base_buffer.fill(0);
427
428 QPainter p(&m_base_buffer);
429
430 drawBase(p);
431 }
432
433 memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.sizeInBytes());
434
435 {
436 QPainter p(&m_buffer);
437 drawSource(p);
438 }
439
440 painter->drawImage(0, 0, m_buffer);
441 }
442 }
443
444 void CompositionRenderer::mousePressEvent(QMouseEvent *e)
445 {
446 setDescriptionEnabled(false);
447
448 QRectF circle = rectangle_around(m_circle_pos);
449
450 if (circle.contains(e->pos())) {
451 m_current_object = Circle;
452 m_offset = circle.center() - e->pos();
453 } else {
454 m_current_object = NoObject;
455 }
456 if (m_animation_enabled) {
457 killTimer(m_animationTimer);
458 m_animationTimer = 0;
459 }
460 }
461
462 void CompositionRenderer::mouseMoveEvent(QMouseEvent *e)
463 {
464 if (m_current_object == Circle)
465 setCirclePos(e->pos() + m_offset);
466 }
467
468 void CompositionRenderer::mouseReleaseEvent(QMouseEvent *)
469 {
470 m_current_object = NoObject;
471
472 if (m_animation_enabled) {
473 Q_ASSERT(!m_animationTimer);
474 m_animationTimer = startTimer(animationInterval);
475 }
476 }
477
478 void CompositionRenderer::timerEvent(QTimerEvent *event)
479 {
480 if (event->timerId() == m_animationTimer)
481 updateCirclePos();
482 }
483
484 void CompositionRenderer::setCirclePos(const QPointF &pos)
485 {
486 const QRect oldRect = rectangle_around(m_circle_pos).toAlignedRect();
487 m_circle_pos = pos;
488 const QRect newRect = rectangle_around(m_circle_pos).toAlignedRect();
489 #if QT_CONFIG(opengl)
490 if (usesOpenGL()) {
491 update();
492 return;
493 }
494 #endif
495 update(oldRect | newRect);
496 }
497