| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com) | 
|---|
| 5 | ** | 
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit. | 
|---|
| 7 | ** | 
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 9 | ** Commercial Usage | 
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 13 | ** a written agreement between you and Nokia. | 
|---|
| 14 | ** | 
|---|
| 15 | ** GNU Lesser General Public License Usage | 
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 19 | ** packaging of this file.  Please review the following information to | 
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 22 | ** | 
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain | 
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL | 
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this | 
|---|
| 26 | ** package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please | 
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com. | 
|---|
| 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] | 
|---|