|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <QtGui> |
|
43 |
|
44 #include "imagecomposer.h" |
|
45 |
|
46 //! [0] |
|
47 static const QSize resultSize(200, 200); |
|
48 //! [0] |
|
49 |
|
50 //! [1] |
|
51 ImageComposer::ImageComposer() |
|
52 { |
|
53 sourceButton = new QToolButton; |
|
54 sourceButton->setIconSize(resultSize); |
|
55 |
|
56 operatorComboBox = new QComboBox; |
|
57 addOp(QPainter::CompositionMode_SourceOver, tr("SourceOver")); |
|
58 addOp(QPainter::CompositionMode_DestinationOver, tr("DestinationOver")); |
|
59 addOp(QPainter::CompositionMode_Clear, tr("Clear")); |
|
60 addOp(QPainter::CompositionMode_Source, tr("Source")); |
|
61 addOp(QPainter::CompositionMode_Destination, tr("Destination")); |
|
62 addOp(QPainter::CompositionMode_SourceIn, tr("SourceIn")); |
|
63 addOp(QPainter::CompositionMode_DestinationIn, tr("DestinationIn")); |
|
64 addOp(QPainter::CompositionMode_SourceOut, tr("SourceOut")); |
|
65 addOp(QPainter::CompositionMode_DestinationOut, tr("DestinationOut")); |
|
66 addOp(QPainter::CompositionMode_SourceAtop, tr("SourceAtop")); |
|
67 addOp(QPainter::CompositionMode_DestinationAtop, tr("DestinationAtop")); |
|
68 addOp(QPainter::CompositionMode_Xor, tr("Xor")); |
|
69 addOp(QPainter::CompositionMode_Plus, tr("Plus")); |
|
70 addOp(QPainter::CompositionMode_Multiply, tr("Multiply")); |
|
71 addOp(QPainter::CompositionMode_Screen, tr("Screen")); |
|
72 addOp(QPainter::CompositionMode_Overlay, tr("Overlay")); |
|
73 addOp(QPainter::CompositionMode_Darken, tr("Darken")); |
|
74 addOp(QPainter::CompositionMode_Lighten, tr("Lighten")); |
|
75 addOp(QPainter::CompositionMode_ColorDodge, tr("ColorDodge")); |
|
76 addOp(QPainter::CompositionMode_ColorBurn, tr("ColorBurn")); |
|
77 addOp(QPainter::CompositionMode_HardLight, tr("HardLight")); |
|
78 addOp(QPainter::CompositionMode_SoftLight, tr("SoftLight")); |
|
79 addOp(QPainter::CompositionMode_Difference, tr("Difference")); |
|
80 addOp(QPainter::CompositionMode_Exclusion, tr("Exclusion")); |
|
81 //! [1] |
|
82 |
|
83 //! [2] |
|
84 destinationButton = new QToolButton; |
|
85 destinationButton->setIconSize(resultSize); |
|
86 |
|
87 equalLabel = new QLabel(tr("=")); |
|
88 |
|
89 resultLabel = new QLabel; |
|
90 resultLabel->setMinimumWidth(resultSize.width()); |
|
91 //! [2] |
|
92 |
|
93 //! [3] |
|
94 connect(sourceButton, SIGNAL(clicked()), this, SLOT(chooseSource())); |
|
95 connect(operatorComboBox, SIGNAL(activated(int)), |
|
96 this, SLOT(recalculateResult())); |
|
97 connect(destinationButton, SIGNAL(clicked()), |
|
98 this, SLOT(chooseDestination())); |
|
99 //! [3] |
|
100 |
|
101 //! [4] |
|
102 QGridLayout *mainLayout = new QGridLayout; |
|
103 mainLayout->addWidget(sourceButton, 0, 0, 3, 1); |
|
104 mainLayout->addWidget(operatorComboBox, 1, 1); |
|
105 mainLayout->addWidget(destinationButton, 0, 2, 3, 1); |
|
106 mainLayout->addWidget(equalLabel, 1, 3); |
|
107 mainLayout->addWidget(resultLabel, 0, 4, 3, 1); |
|
108 mainLayout->setSizeConstraint(QLayout::SetFixedSize); |
|
109 setLayout(mainLayout); |
|
110 //! [4] |
|
111 |
|
112 //! [5] |
|
113 resultImage = QImage(resultSize, QImage::Format_ARGB32_Premultiplied); |
|
114 |
|
115 loadImage(":/images/butterfly.png", &sourceImage, sourceButton); |
|
116 loadImage(":/images/checker.png", &destinationImage, destinationButton); |
|
117 |
|
118 setWindowTitle(tr("Image Composition")); |
|
119 } |
|
120 //! [5] |
|
121 |
|
122 //! [6] |
|
123 void ImageComposer::chooseSource() |
|
124 { |
|
125 chooseImage(tr("Choose Source Image"), &sourceImage, sourceButton); |
|
126 } |
|
127 //! [6] |
|
128 |
|
129 //! [7] |
|
130 void ImageComposer::chooseDestination() |
|
131 { |
|
132 chooseImage(tr("Choose Destination Image"), &destinationImage, |
|
133 destinationButton); |
|
134 } |
|
135 //! [7] |
|
136 |
|
137 //! [8] |
|
138 void ImageComposer::recalculateResult() |
|
139 { |
|
140 QPainter::CompositionMode mode = currentMode(); |
|
141 |
|
142 QPainter painter(&resultImage); |
|
143 painter.setCompositionMode(QPainter::CompositionMode_Source); |
|
144 painter.fillRect(resultImage.rect(), Qt::transparent); |
|
145 painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
|
146 painter.drawImage(0, 0, destinationImage); |
|
147 painter.setCompositionMode(mode); |
|
148 painter.drawImage(0, 0, sourceImage); |
|
149 painter.setCompositionMode(QPainter::CompositionMode_DestinationOver); |
|
150 painter.fillRect(resultImage.rect(), Qt::white); |
|
151 painter.end(); |
|
152 |
|
153 resultLabel->setPixmap(QPixmap::fromImage(resultImage)); |
|
154 } |
|
155 //! [8] |
|
156 |
|
157 //! [9] |
|
158 void ImageComposer::addOp(QPainter::CompositionMode mode, const QString &name) |
|
159 { |
|
160 operatorComboBox->addItem(name, mode); |
|
161 } |
|
162 //! [9] |
|
163 |
|
164 //! [10] |
|
165 void ImageComposer::chooseImage(const QString &title, QImage *image, |
|
166 QToolButton *button) |
|
167 { |
|
168 QString fileName = QFileDialog::getOpenFileName(this, title); |
|
169 if (!fileName.isEmpty()) |
|
170 loadImage(fileName, image, button); |
|
171 } |
|
172 //! [10] |
|
173 |
|
174 //! [11] |
|
175 void ImageComposer::loadImage(const QString &fileName, QImage *image, |
|
176 QToolButton *button) |
|
177 { |
|
178 image->load(fileName); |
|
179 |
|
180 QImage fixedImage(resultSize, QImage::Format_ARGB32_Premultiplied); |
|
181 QPainter painter(&fixedImage); |
|
182 painter.setCompositionMode(QPainter::CompositionMode_Source); |
|
183 painter.fillRect(fixedImage.rect(), Qt::transparent); |
|
184 painter.setCompositionMode(QPainter::CompositionMode_SourceOver); |
|
185 painter.drawImage(imagePos(*image), *image); |
|
186 painter.end(); |
|
187 button->setIcon(QPixmap::fromImage(fixedImage)); |
|
188 |
|
189 *image = fixedImage; |
|
190 |
|
191 recalculateResult(); |
|
192 } |
|
193 //! [11] |
|
194 |
|
195 //! [12] |
|
196 QPainter::CompositionMode ImageComposer::currentMode() const |
|
197 { |
|
198 return (QPainter::CompositionMode) |
|
199 operatorComboBox->itemData(operatorComboBox->currentIndex()).toInt(); |
|
200 } |
|
201 //! [12] |
|
202 |
|
203 //! [13] |
|
204 QPoint ImageComposer::imagePos(const QImage &image) const |
|
205 { |
|
206 return QPoint((resultSize.width() - image.width()) / 2, |
|
207 (resultSize.height() - image.height()) / 2); |
|
208 } |
|
209 //! [13] |