| 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 documentation 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 | /*! | 
|---|
| 43 | \example painting/imagecomposition | 
|---|
| 44 | \title Image Composition Example | 
|---|
| 45 |  | 
|---|
| 46 | The Image Composition example lets the user combine images | 
|---|
| 47 | together using any composition mode supported by QPainter, described | 
|---|
| 48 | in detail in \l{QPainter#Composition Modes}{Composition Modes}. | 
|---|
| 49 |  | 
|---|
| 50 | \image imagecomposition-example.png | 
|---|
| 51 |  | 
|---|
| 52 | \section1 Setting Up The Resource File | 
|---|
| 53 |  | 
|---|
| 54 | The Image Composition example requires two source images, | 
|---|
| 55 | \e butterfly.png and \e checker.png that are embedded within | 
|---|
| 56 | \e imagecomposition.qrc. The file contains the following code: | 
|---|
| 57 |  | 
|---|
| 58 | \quotefile examples/painting/imagecomposition/imagecomposition.qrc | 
|---|
| 59 |  | 
|---|
| 60 | For more information on resource files, see \l{The Qt Resource System}. | 
|---|
| 61 |  | 
|---|
| 62 | \section1 ImageComposer Class Definition | 
|---|
| 63 |  | 
|---|
| 64 | The \c ImageComposer class is a subclass of QWidget that implements three | 
|---|
| 65 | private slots, \c chooseSource(), \c chooseDestination(), and | 
|---|
| 66 | \c recalculateResult(). | 
|---|
| 67 |  | 
|---|
| 68 | \snippet examples/painting/imagecomposition/imagecomposer.h 0 | 
|---|
| 69 |  | 
|---|
| 70 | In addition, \c ImageComposer consists of five private functions, | 
|---|
| 71 | \c addOp(), \c chooseImage(), \c loadImage(), \c currentMode(), and | 
|---|
| 72 | \c imagePos(), as well as private instances of QToolButton, QComboBox, | 
|---|
| 73 | QLabel, and QImage. | 
|---|
| 74 |  | 
|---|
| 75 | \snippet examples/painting/imagecomposition/imagecomposer.h 1 | 
|---|
| 76 |  | 
|---|
| 77 | \section1 ImageComposer Class Implementation | 
|---|
| 78 |  | 
|---|
| 79 | We declare a QSize object, \c resultSize, as a static constant with width | 
|---|
| 80 | and height equal to 200. | 
|---|
| 81 |  | 
|---|
| 82 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 0 | 
|---|
| 83 |  | 
|---|
| 84 | Within the constructor, we instantiate a QToolButton object, | 
|---|
| 85 | \c sourceButton and set its \l{QAbstractButton::setIconSize()}{iconSize} | 
|---|
| 86 | property to \c resultSize. The \c operatorComboBox is instantiated and | 
|---|
| 87 | then populated using the \c addOp() function. This function accepts a | 
|---|
| 88 | QPainter::CompositionMode, \a mode, and a QString, \a name, representing | 
|---|
| 89 | the name of the composition mode. | 
|---|
| 90 |  | 
|---|
| 91 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 1 | 
|---|
| 92 |  | 
|---|
| 93 | The \c destinationButton is instantiated and its | 
|---|
| 94 | \l{QAbstractButton::setIconSize()}{iconSize} property is set to | 
|---|
| 95 | \c resultSize as well. The \l{QLabel}s \c equalLabel and \c resultLabel | 
|---|
| 96 | are created and \c{resultLabel}'s \l{QWidget::setMinimumWidth()} | 
|---|
| 97 | {minimumWidth} is set. | 
|---|
| 98 |  | 
|---|
| 99 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 2 | 
|---|
| 100 |  | 
|---|
| 101 | We connect the following signals to their corresponding slots: | 
|---|
| 102 | \list | 
|---|
| 103 | \o \c{sourceButton}'s \l{QPushButton::clicked()}{clicked()} signal is | 
|---|
| 104 | connected to \c chooseSource(), | 
|---|
| 105 | \o \c{operatorComboBox}'s \l{QComboBox::activated()}{activated()} | 
|---|
| 106 | signal is connected to \c recalculateResult(), and | 
|---|
| 107 | \o \c{destinationButton}'s \l{QToolButton::clicked()}{clicked()} signal | 
|---|
| 108 | is connected to \c chooseDestination(). | 
|---|
| 109 | \endlist | 
|---|
| 110 |  | 
|---|
| 111 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 3 | 
|---|
| 112 |  | 
|---|
| 113 | A QGridLayout, \c mainLayout, is used to place all the widgets. Note | 
|---|
| 114 | that \c{mainLayout}'s \l{QLayout::setSizeConstraint()}{sizeConstraint} | 
|---|
| 115 | property is set to QLayout::SetFixedSize, which means that | 
|---|
| 116 | \c{ImageComposer}'s size cannot be resized at all. | 
|---|
| 117 |  | 
|---|
| 118 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 4 | 
|---|
| 119 |  | 
|---|
| 120 | We create a QImage, \c resultImage, and we invoke \c loadImage() twice | 
|---|
| 121 | to load both the image files in our \e imagecomposition.qrc file. Then, | 
|---|
| 122 | we set the \l{QWidget::setWindowTitle()}{windowTitle} property to | 
|---|
| 123 | "Image Composition". | 
|---|
| 124 |  | 
|---|
| 125 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 5 | 
|---|
| 126 |  | 
|---|
| 127 | The \c chooseSource() and \c chooseDestination() functions are | 
|---|
| 128 | convenience functions that invoke \c chooseImage() with specific | 
|---|
| 129 | parameters. | 
|---|
| 130 |  | 
|---|
| 131 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 6 | 
|---|
| 132 | \codeline | 
|---|
| 133 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 7 | 
|---|
| 134 |  | 
|---|
| 135 | The \c chooseImage() function loads an image of the user's choice, | 
|---|
| 136 | depending on the \a title, \a image, and \a button. | 
|---|
| 137 |  | 
|---|
| 138 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 10 | 
|---|
| 139 |  | 
|---|
| 140 | The \c recalculateResult() function is used to calculate amd display the | 
|---|
| 141 | result of combining the two images together with the user's choice of | 
|---|
| 142 | composition mode. | 
|---|
| 143 |  | 
|---|
| 144 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 8 | 
|---|
| 145 |  | 
|---|
| 146 | The \c addOp() function adds an item to the \c operatorComboBox using | 
|---|
| 147 | \l{QComboBox}'s \l{QComboBox::addItem()}{addItem} function. This function | 
|---|
| 148 | accepts a QPainter::CompositionMode, \a mode, and a QString, \a name. The | 
|---|
| 149 | rectangle is filled with Qt::Transparent and both the \c sourceImage and | 
|---|
| 150 | \c destinationImage are painted, before displaying it on \c resultLabel. | 
|---|
| 151 |  | 
|---|
| 152 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 9 | 
|---|
| 153 |  | 
|---|
| 154 | The \c loadImage() function paints a transparent background using | 
|---|
| 155 | \l{QPainter::fillRect()}{fillRect()} and draws \c image in a | 
|---|
| 156 | centralized position using \l{QPainter::drawImage()}{drawImage()}. | 
|---|
| 157 | This \c image is then set as the \c{button}'s icon. | 
|---|
| 158 |  | 
|---|
| 159 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 11 | 
|---|
| 160 |  | 
|---|
| 161 | The \c currentMode() function returns the composition mode currently | 
|---|
| 162 | selected in \c operatorComboBox. | 
|---|
| 163 |  | 
|---|
| 164 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 12 | 
|---|
| 165 |  | 
|---|
| 166 | We use the \c imagePos() function to ensure that images loaded onto the | 
|---|
| 167 | QToolButton objects, \c sourceButton and \c destinationButton, are | 
|---|
| 168 | centralized. | 
|---|
| 169 |  | 
|---|
| 170 | \snippet examples/painting/imagecomposition/imagecomposer.cpp 13 | 
|---|
| 171 |  | 
|---|
| 172 | \section1 The \c main() Function | 
|---|
| 173 |  | 
|---|
| 174 | The \c main() function instantiates QApplication and \c ImageComposer | 
|---|
| 175 | and invokes its \l{QWidget::show()}{show()} function. | 
|---|
| 176 |  | 
|---|
| 177 | \snippet examples/painting/imagecomposition/main.cpp 0 | 
|---|
| 178 |  | 
|---|
| 179 | */ | 
|---|