Debian Codegraph @

examples/widgets/painting/composition/composition.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "composition.h"
5 #include <QBoxLayout>
6 #include <QRadioButton>
7 #include <QTimer>
8 #include <QDateTime>
9 #include <QSlider>
10 #include <QMouseEvent>
11 #include <qmath.h>
12
13 using namespace std::chrono_literals;
14
15 const auto animationInterval = 15ms; // update every 16 ms = ~60FPS
16
17 CompositionWidget::CompositionWidget(QWidget *parent)
18 : QWidget(parent)
19 {
20 CompositionRenderer *view = new CompositionRenderer(this);
21
22 QGroupBox *mainGroup = new QGroupBox(parent);
23 mainGroup->setTitle(tr("Composition Modes"));
24
25 QGroupBox *modesGroup = new QGroupBox(mainGroup);
26 modesGroup->setTitle(tr("Mode"));
27
28 rbClear = new QRadioButton(tr("Clear"), modesGroup);
29 connect(rbClear, &QAbstractButton::clicked, view, &CompositionRenderer::setClearMode);
30 rbSource = new QRadioButton(tr("Source"), modesGroup);
31 connect(rbSource, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceMode);
32 rbDest = new QRadioButton(tr("Destination"), modesGroup);
33 connect(rbDest, &QAbstractButton::clicked, view, &CompositionRenderer::setDestMode);
34 rbSourceOver = new QRadioButton(tr("Source Over"), modesGroup);
35 connect(rbSourceOver, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceOverMode);
36 rbDestOver = new QRadioButton(tr("Destination Over"), modesGroup);
37 connect(rbDestOver, &QAbstractButton::clicked, view, &CompositionRenderer::setDestOverMode);
38 rbSourceIn = new QRadioButton(tr("Source In"), modesGroup);
39 connect(rbSourceIn, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceInMode);
40 rbDestIn = new QRadioButton(tr("Dest In"), modesGroup);
41 connect(rbDestIn, &QAbstractButton::clicked, view, &CompositionRenderer::setDestInMode);
42 rbSourceOut = new QRadioButton(tr("Source Out"), modesGroup);
43 connect(rbSourceOut, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceOutMode);
44 rbDestOut = new QRadioButton(tr("Dest Out"), modesGroup);
45 connect(rbDestOut, &QAbstractButton::clicked, view, &CompositionRenderer::setDestOutMode);
46 rbSourceAtop = new QRadioButton(tr("Source Atop"), modesGroup);
47 connect(rbSourceAtop, &QAbstractButton::clicked, view, &CompositionRenderer::setSourceAtopMode);
48 rbDestAtop = new QRadioButton(tr("Dest Atop"), modesGroup);
49 connect(rbDestAtop, &QAbstractButton::clicked, view, &CompositionRenderer::setDestAtopMode);
50 rbXor = new QRadioButton(tr("Xor"), modesGroup);
51 connect(rbXor, &QAbstractButton::clicked, view, &CompositionRenderer::setXorMode);
52
53 rbPlus = new QRadioButton(tr("Plus"), modesGroup);
54 connect(rbPlus, &QAbstractButton::clicked, view, &CompositionRenderer::setPlusMode);
55 rbMultiply = new QRadioButton(tr("Multiply"), modesGroup);
56 connect(rbMultiply, &QAbstractButton::clicked, view, &CompositionRenderer::setMultiplyMode);
57 rbScreen = new QRadioButton(tr("Screen"), modesGroup);
58 connect(rbScreen, &QAbstractButton::clicked, view, &CompositionRenderer::setScreenMode);
59 rbOverlay = new QRadioButton(tr("Overlay"), modesGroup);
60 connect(rbOverlay, &QAbstractButton::clicked, view, &CompositionRenderer::setOverlayMode);
61 rbDarken = new QRadioButton(tr("Darken"), modesGroup);
62 connect(rbDarken, &QAbstractButton::clicked, view, &CompositionRenderer::setDarkenMode);
63 rbLighten = new QRadioButton(tr("Lighten"), modesGroup);
64 connect(rbLighten, &QAbstractButton::clicked, view, &CompositionRenderer::setLightenMode);
65 rbColorDodge = new QRadioButton(tr("Color Dodge"), modesGroup);
66 connect(rbColorDodge, &QAbstractButton::clicked, view, &CompositionRenderer::setColorDodgeMode);
67 rbColorBurn = new QRadioButton(tr("Color Burn"), modesGroup);
68 connect(rbColorBurn, &QAbstractButton::clicked, view, &CompositionRenderer::setColorBurnMode);
69 rbHardLight = new QRadioButton(tr("Hard Light"), modesGroup);
70 connect(rbHardLight, &QAbstractButton::clicked, view, &CompositionRenderer::setHardLightMode);
71 rbSoftLight = new QRadioButton(tr("Soft Light"), modesGroup);
72 connect(rbSoftLight, &QAbstractButton::clicked, view, &CompositionRenderer::setSoftLightMode);
73 rbDifference = new QRadioButton(tr("Difference"), modesGroup);
74 connect(rbDifference, &QAbstractButton::clicked, view, &CompositionRenderer::setDifferenceMode);
75 rbExclusion = new QRadioButton(tr("Exclusion"), modesGroup);
76 connect(rbExclusion, &QAbstractButton::clicked, view, &CompositionRenderer::setExclusionMode);
77
78 QGroupBox *circleColorGroup = new QGroupBox(mainGroup);
79 circleColorGroup->setTitle(tr("Circle color"));
80 QSlider *circleColorSlider = new QSlider(Qt::Horizontal, circleColorGroup);
81 circleColorSlider->setRange(0, 359);
82 circleColorSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
83 connect(circleColorSlider, &QAbstractSlider::valueChanged, view, &CompositionRenderer::setCircleColor);
84
85 QGroupBox *circleAlphaGroup = new QGroupBox(mainGroup);
86 circleAlphaGroup->setTitle(tr("Circle alpha"));
87 QSlider *circleAlphaSlider = new QSlider(Qt::Horizontal, circleAlphaGroup);
88 circleAlphaSlider->setRange(0, 255);
89 circleAlphaSlider->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
90 connect(circleAlphaSlider, &QAbstractSlider::valueChanged, view, &CompositionRenderer::setCircleAlpha);
91
92 QPushButton *showSourceButton = new QPushButton(mainGroup);
93 showSourceButton->setText(tr("Show Source"));
94 QPushButton *whatsThisButton = new QPushButton(mainGroup);
95 whatsThisButton->setText(tr("What's This?"));
96 whatsThisButton->setCheckable(true);
97
98 QPushButton *animateButton = new QPushButton(mainGroup);
99 animateButton->setText(tr("Animated"));
100 animateButton->setCheckable(true);
101 animateButton->setChecked(true);
102
103 QHBoxLayout *viewLayout = new QHBoxLayout(this);
104 viewLayout->addWidget(view);
105 viewLayout->addWidget(mainGroup);
106
107 QVBoxLayout *mainGroupLayout = new QVBoxLayout(mainGroup);
108 mainGroupLayout->addWidget(circleColorGroup);
109 mainGroupLayout->addWidget(circleAlphaGroup);
110 mainGroupLayout->addWidget(modesGroup);
111 mainGroupLayout->addStretch();
112 mainGroupLayout->addWidget(animateButton);
113 mainGroupLayout->addWidget(whatsThisButton);
114 mainGroupLayout->addWidget(showSourceButton);
115
116 QGridLayout *modesLayout = new QGridLayout(modesGroup);
117 modesLayout->addWidget(rbClear, 0, 0);
118 modesLayout->addWidget(rbSource, 1, 0);
119 modesLayout->addWidget(rbDest, 2, 0);
120 modesLayout->addWidget(rbSourceOver, 3, 0);
121 modesLayout->addWidget(rbDestOver, 4, 0);
122 modesLayout->addWidget(rbSourceIn, 5, 0);
123 modesLayout->addWidget(rbDestIn, 6, 0);
124 modesLayout->addWidget(rbSourceOut, 7, 0);
125 modesLayout->addWidget(rbDestOut, 8, 0);
126 modesLayout->addWidget(rbSourceAtop, 9, 0);
127 modesLayout->addWidget(rbDestAtop, 10, 0);
128 modesLayout->addWidget(rbXor, 11, 0);
129
130 modesLayout->addWidget(rbPlus, 0, 1);
131 modesLayout->addWidget(rbMultiply, 1, 1);
132 modesLayout->addWidget(rbScreen, 2, 1);
133 modesLayout->addWidget(rbOverlay, 3, 1);
134 modesLayout->addWidget(rbDarken, 4, 1);
135 modesLayout->addWidget(rbLighten, 5, 1);
136 modesLayout->addWidget(rbColorDodge, 6, 1);
137 modesLayout->addWidget(rbColorBurn, 7, 1);
138 modesLayout->addWidget(rbHardLight, 8, 1);
139 modesLayout->addWidget(rbSoftLight, 9, 1);
140 modesLayout->addWidget(rbDifference, 10, 1);
141 modesLayout->addWidget(rbExclusion, 11, 1);
142
143
144 QVBoxLayout *circleColorLayout = new QVBoxLayout(circleColorGroup);
145 circleColorLayout->addWidget(circleColorSlider);
146
147 QVBoxLayout *circleAlphaLayout = new QVBoxLayout(circleAlphaGroup);
148 circleAlphaLayout->addWidget(circleAlphaSlider);
149
150 view->loadDescription(":res/composition/composition.html");
151 view->loadSourceFile(":res/composition/composition.cpp");
152
153 connect(whatsThisButton, &QAbstractButton::clicked, view, &ArthurFrame::setDescriptionEnabled);
154 connect(view, &ArthurFrame::descriptionEnabledChanged, whatsThisButton, &QAbstractButton::setChecked);
155 connect(showSourceButton, &QAbstractButton::clicked, view, &ArthurFrame::showSource);
156 connect(animateButton, &QAbstractButton::toggled, view, &CompositionRenderer::setAnimationEnabled);
157
158 circleColorSlider->setValue(270);
159 circleAlphaSlider->setValue(200);
160 rbSourceOut->animateClick();
161
162 setWindowTitle(tr("Composition Modes"));
163 }
164
165 CompositionWidget::~CompositionWidget()
166 {
167 }
168
169
170 void CompositionWidget::nextMode()
171 {
172 /*
173 if (!m_animation_enabled)
174 return;
175 if (rbClear->isChecked()) rbSource->animateClick();
176 if (rbSource->isChecked()) rbDest->animateClick();
177 if (rbDest->isChecked()) rbSourceOver->animateClick();
178 if (rbSourceOver->isChecked()) rbDestOver->animateClick();
179 if (rbDestOver->isChecked()) rbSourceIn->animateClick();
180 if (rbSourceIn->isChecked()) rbDestIn->animateClick();
181 if (rbDestIn->isChecked()) rbSourceOut->animateClick();
182 if (rbSourceOut->isChecked()) rbDestOut->animateClick();
183 if (rbDestOut->isChecked()) rbSourceAtop->animateClick();
184 if (rbSourceAtop->isChecked()) rbDestAtop->animateClick();
185 if (rbDestAtop->isChecked()) rbXor->animateClick();
186 if (rbXor->isChecked()) rbClear->animateClick();
187 */
188 }
189
190 CompositionRenderer::CompositionRenderer(QWidget *parent)
191 : ArthurFrame(parent)
192 {
193 m_animation_enabled = true;
194 m_animationTimer.start(animationInterval, this);
195 m_image = QImage(":res/composition/flower.jpg");
196 m_image.setAlphaChannel(QImage(":res/composition/flower_alpha.jpg"));
197 m_circle_alpha = 127;
198 m_circle_hue = 255;
199 m_current_object = NoObject;
200 m_composition_mode = QPainter::CompositionMode_SourceOut;
201
202 m_circle_pos = QPoint(200, 100);
203
204 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
205 }
206
207 CompositionRenderer::~CompositionRenderer()
208 {
209 }
210
211 QRectF rectangle_around(const QPointF &p, const QSizeF &size = QSize(250, 200))
212 {
213 QRectF rect(p, size);
214 rect.translate(-size.width()/2, -size.height()/2);
215 return rect;
216 }
217
218 void CompositionRenderer::setAnimationEnabled(bool enabled)
219 {
220 if (m_animation_enabled == enabled)
221 return;
222 m_animation_enabled = enabled;
223 if (enabled) {
224 Q_ASSERT(!m_animationTimer.isActive());
225 m_animationTimer.start(animationInterval, this);
226 } else {
227 m_animationTimer.stop();
228 }
229 }
230
231 void CompositionRenderer::updateCirclePos()
232 {
233 if (m_current_object != NoObject)
234 return;
235 QDateTime dt = QDateTime::currentDateTime();
236 qreal t = dt.toMSecsSinceEpoch() / 1000.0;
237
238 qreal x = width() / qreal(2) + (qCos(t*8/11) + qSin(-t)) * width() / qreal(4);
239 qreal y = height() / qreal(2) + (qSin(t*6/7) + qCos(t * qreal(1.5))) * height() / qreal(4);
240
241 setCirclePos(QLineF(m_circle_pos, QPointF(x, y)).pointAt(0.02));
242 }
243
244 void CompositionRenderer::drawBase(QPainter &p)
245 {
246 p.setPen(Qt::NoPen);
247
248 QLinearGradient rect_gradient(0, 0, 0, height());
249 rect_gradient.setColorAt(0, Qt::red);
250 rect_gradient.setColorAt(.17, Qt::yellow);
251 rect_gradient.setColorAt(.33, Qt::green);
252 rect_gradient.setColorAt(.50, Qt::cyan);
253 rect_gradient.setColorAt(.66, Qt::blue);
254 rect_gradient.setColorAt(.81, Qt::magenta);
255 rect_gradient.setColorAt(1, Qt::red);
256 p.setBrush(rect_gradient);
257 p.drawRect(width() / 2, 0, width() / 2, height());
258
259 QLinearGradient alpha_gradient(0, 0, width(), 0);
260 alpha_gradient.setColorAt(0, Qt::white);
261 alpha_gradient.setColorAt(0.2, Qt::white);
262 alpha_gradient.setColorAt(0.5, Qt::transparent);
263 alpha_gradient.setColorAt(0.8, Qt::white);
264 alpha_gradient.setColorAt(1, Qt::white);
265
266 p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
267 p.setBrush(alpha_gradient);
268 p.drawRect(0, 0, width(), height());
269
270 p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
271
272 p.setPen(Qt::NoPen);
273 p.setRenderHint(QPainter::SmoothPixmapTransform);
274 p.drawImage(rect(), m_image);
275 }
276
277 void CompositionRenderer::drawSource(QPainter &p)
278 {
279 p.setPen(Qt::NoPen);
280 p.setRenderHint(QPainter::Antialiasing);
281 p.setCompositionMode(m_composition_mode);
282
283 QRectF circle_rect = rectangle_around(m_circle_pos);
284 QColor color = QColor::fromHsvF(m_circle_hue / 360.0, 1, 1, m_circle_alpha / 255.0);
285 QLinearGradient circle_gradient(circle_rect.topLeft(), circle_rect.bottomRight());
286 circle_gradient.setColorAt(0, color.lighter());
287 circle_gradient.setColorAt(0.5, color);
288 circle_gradient.setColorAt(1, color.darker());
289 p.setBrush(circle_gradient);
290
291 p.drawEllipse(circle_rect);
292 }
293
294 void CompositionRenderer::paint(QPainter *painter)
295 {
296 if (m_buffer.size() != size()) {
297 m_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
298 m_base_buffer = QImage(size(), QImage::Format_ARGB32_Premultiplied);
299
300 m_base_buffer.fill(0);
301
302 QPainter p(&m_base_buffer);
303
304 drawBase(p);
305 }
306
307 memcpy(m_buffer.bits(), m_base_buffer.bits(), m_buffer.sizeInBytes());
308
309 {
310 QPainter p(&m_buffer);
311 drawSource(p);
312 }
313
314 painter->drawImage(0, 0, m_buffer);
315 }
316
317 void CompositionRenderer::mousePressEvent(QMouseEvent *e)
318 {
319 setDescriptionEnabled(false);
320
321 QRectF circle = rectangle_around(m_circle_pos);
322
323 if (circle.contains(e->pos())) {
324 m_current_object = Circle;
325 m_offset = circle.center() - e->pos();
326 } else {
327 m_current_object = NoObject;
328 }
329 if (m_animation_enabled) {
330 m_animationTimer.stop();
331 }
332 }
333
334 void CompositionRenderer::mouseMoveEvent(QMouseEvent *e)
335 {
336 if (m_current_object == Circle)
337 setCirclePos(e->pos() + m_offset);
338 }
339
340 void CompositionRenderer::mouseReleaseEvent(QMouseEvent *)
341 {
342 m_current_object = NoObject;
343
344 if (m_animation_enabled) {
345 Q_ASSERT(!m_animationTimer.isActive());
346 m_animationTimer.start(animationInterval, this);
347 }
348 }
349
350 void CompositionRenderer::timerEvent(QTimerEvent *event)
351 {
352 if (event->matches(m_animationTimer))
353 updateCirclePos();
354 }
355
356 void CompositionRenderer::setCirclePos(const QPointF &pos)
357 {
358 const QRect oldRect = rectangle_around(m_circle_pos).toAlignedRect();
359 m_circle_pos = pos;
360 const QRect newRect = rectangle_around(m_circle_pos).toAlignedRect();
361 update(oldRect | newRect);
362 }
363