Debian Codegraph @

examples/widgets/painting/imagecomposition/imagecomposer.cpp

raw
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 #include "imagecomposer.h"
5
6 #include <QtWidgets>
7
8 //! [0]
9 static const QSize resultSize(200, 200);
10 //! [0]
11
12 //! [1]
13 ImageComposer::ImageComposer()
14 {
15 sourceButton = new QToolButton;
16 sourceButton->setIconSize(resultSize);
17
18 operatorComboBox = new QComboBox;
19 addOp(QPainter::CompositionMode_SourceOver, tr("SourceOver"));
20 addOp(QPainter::CompositionMode_DestinationOver, tr("DestinationOver"));
21 addOp(QPainter::CompositionMode_Clear, tr("Clear"));
22 addOp(QPainter::CompositionMode_Source, tr("Source"));
23 addOp(QPainter::CompositionMode_Destination, tr("Destination"));
24 addOp(QPainter::CompositionMode_SourceIn, tr("SourceIn"));
25 addOp(QPainter::CompositionMode_DestinationIn, tr("DestinationIn"));
26 addOp(QPainter::CompositionMode_SourceOut, tr("SourceOut"));
27 addOp(QPainter::CompositionMode_DestinationOut, tr("DestinationOut"));
28 addOp(QPainter::CompositionMode_SourceAtop, tr("SourceAtop"));
29 addOp(QPainter::CompositionMode_DestinationAtop, tr("DestinationAtop"));
30 addOp(QPainter::CompositionMode_Xor, tr("Xor"));
31 addOp(QPainter::CompositionMode_Plus, tr("Plus"));
32 addOp(QPainter::CompositionMode_Multiply, tr("Multiply"));
33 addOp(QPainter::CompositionMode_Screen, tr("Screen"));
34 addOp(QPainter::CompositionMode_Overlay, tr("Overlay"));
35 addOp(QPainter::CompositionMode_Darken, tr("Darken"));
36 addOp(QPainter::CompositionMode_Lighten, tr("Lighten"));
37 addOp(QPainter::CompositionMode_ColorDodge, tr("ColorDodge"));
38 addOp(QPainter::CompositionMode_ColorBurn, tr("ColorBurn"));
39 addOp(QPainter::CompositionMode_HardLight, tr("HardLight"));
40 addOp(QPainter::CompositionMode_SoftLight, tr("SoftLight"));
41 addOp(QPainter::CompositionMode_Difference, tr("Difference"));
42 addOp(QPainter::CompositionMode_Exclusion, tr("Exclusion"));
43 //! [1]
44
45 //! [2]
46 destinationButton = new QToolButton;
47 destinationButton->setIconSize(resultSize);
48
49 equalLabel = new QLabel(tr("="));
50
51 resultLabel = new QLabel;
52 resultLabel->setMinimumWidth(resultSize.width());
53 //! [2]
54
55 //! [3]
56 connect(sourceButton, &QAbstractButton::clicked,
57 this, &ImageComposer::chooseSource);
58 connect(operatorComboBox, &QComboBox::activated,
59 this, &ImageComposer::recalculateResult);
60 connect(destinationButton, &QAbstractButton::clicked,
61 this, &ImageComposer::chooseDestination);
62 //! [3]
63
64 //! [4]
65 QGridLayout *mainLayout = new QGridLayout;
66 mainLayout->addWidget(sourceButton, 0, 0, 3, 1);
67 mainLayout->addWidget(operatorComboBox, 1, 1);
68 mainLayout->addWidget(destinationButton, 0, 2, 3, 1);
69 mainLayout->addWidget(equalLabel, 1, 3);
70 mainLayout->addWidget(resultLabel, 0, 4, 3, 1);
71 mainLayout->setSizeConstraint(QLayout::SetFixedSize);
72 setLayout(mainLayout);
73 //! [4]
74
75 //! [5]
76 resultImage = QImage(resultSize, QImage::Format_ARGB32_Premultiplied);
77
78 loadImage(":/images/butterfly.png", &sourceImage, sourceButton);
79 loadImage(":/images/checker.png", &destinationImage, destinationButton);
80
81 setWindowTitle(tr("Image Composition"));
82 }
83 //! [5]
84
85 //! [6]
86 void ImageComposer::chooseSource()
87 {
88 chooseImage(tr("Choose Source Image"), &sourceImage, sourceButton);
89 }
90 //! [6]
91
92 //! [7]
93 void ImageComposer::chooseDestination()
94 {
95 chooseImage(tr("Choose Destination Image"), &destinationImage, destinationButton);
96 }
97 //! [7]
98
99 //! [8]
100 void ImageComposer::recalculateResult()
101 {
102 QPainter::CompositionMode mode = currentMode();
103
104 QPainter painter(&resultImage);
105 painter.setCompositionMode(QPainter::CompositionMode_Source);
106 painter.fillRect(resultImage.rect(), Qt::transparent);
107 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
108 painter.drawImage(0, 0, destinationImage);
109 painter.setCompositionMode(mode);
110 painter.drawImage(0, 0, sourceImage);
111 painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
112 painter.fillRect(resultImage.rect(), Qt::white);
113 painter.end();
114
115 resultLabel->setPixmap(QPixmap::fromImage(resultImage));
116 }
117 //! [8]
118
119 //! [9]
120 void ImageComposer::addOp(QPainter::CompositionMode mode, const QString &name)
121 {
122 operatorComboBox->addItem(name, mode);
123 }
124 //! [9]
125
126 //! [10]
127 void ImageComposer::chooseImage(const QString &title, QImage *image,
128 QToolButton *button)
129 {
130 QString fileName = QFileDialog::getOpenFileName(this, title);
131 if (!fileName.isEmpty())
132 loadImage(fileName, image, button);
133 }
134 //! [10]
135
136 //! [11]
137 void ImageComposer::loadImage(const QString &fileName, QImage *image,
138 QToolButton *button)
139 {
140 image->load(fileName);
141
142 // Scale the image to given size
143 *image = image->scaled(resultSize, Qt::KeepAspectRatio);
144
145 QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied);
146 QPainter painter(&fixedImage);
147 painter.setCompositionMode(QPainter::CompositionMode_Source);
148 painter.fillRect(fixedImage.rect(), Qt::transparent);
149 painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
150 painter.drawImage(imagePos(*image), *image);
151 painter.end();
152 button->setIcon(QPixmap::fromImage(fixedImage));
153
154 *image = fixedImage;
155
156 recalculateResult();
157 }
158 //! [11]
159
160 //! [12]
161 QPainter::CompositionMode ImageComposer::currentMode() const
162 {
163 return (QPainter::CompositionMode)
164 operatorComboBox->itemData(operatorComboBox->currentIndex()).toInt();
165 }
166 //! [12]
167
168 //! [13]
169 QPoint ImageComposer::imagePos(const QImage &image) const
170 {
171 return QPoint((resultSize.width() - image.width()) / 2,
172 (resultSize.height() - image.height()) / 2);
173 }
174 //! [13]