1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Free Documentation License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Free
19 ** Documentation License version 1.3 as published by the Free Software
20 ** Foundation and appearing in the file included in the packaging of
21 ** this file. Please review the following information to ensure
22 ** the GNU Free Documentation License version 1.3 requirements
23 ** will be met: http://www.gnu.org/copyleft/fdl.html.
26 ****************************************************************************/
29 \example painting/imagecomposition
30 \title Image Composition Example
32 \brief The Image Composition example lets the user combine images
33 together using any composition mode supported by QPainter, described
34 in detail in \l{QPainter#Composition Modes}{Composition Modes}.
36 \image imagecomposition-example.png
38 \section1 Setting Up The Resource File
40 The Image Composition example requires two source images,
41 \e butterfly.png and \e checker.png that are embedded within
42 \e imagecomposition.qrc. The file contains the following code:
44 \quotefile painting/imagecomposition/imagecomposition.qrc
46 For more information on resource files, see \l{The Qt Resource System}.
48 \section1 ImageComposer Class Definition
50 The \c ImageComposer class is a subclass of QWidget that implements three
51 private slots, \c chooseSource(), \c chooseDestination(), and
52 \c recalculateResult().
54 \snippet painting/imagecomposition/imagecomposer.h 0
56 In addition, \c ImageComposer consists of five private functions,
57 \c addOp(), \c chooseImage(), \c loadImage(), \c currentMode(), and
58 \c imagePos(), as well as private instances of QToolButton, QComboBox,
61 \snippet painting/imagecomposition/imagecomposer.h 1
63 \section1 ImageComposer Class Implementation
65 We declare a QSize object, \c resultSize, as a static constant with width
66 and height equal to 200.
68 \snippet painting/imagecomposition/imagecomposer.cpp 0
70 Within the constructor, we instantiate a QToolButton object,
71 \c sourceButton and set its \l{QAbstractButton::setIconSize()}{iconSize}
72 property to \c resultSize. The \c operatorComboBox is instantiated and
73 then populated using the \c addOp() function. This function accepts a
74 QPainter::CompositionMode, \a mode, and a QString, \a name, representing
75 the name of the composition mode.
77 \snippet painting/imagecomposition/imagecomposer.cpp 1
79 The \c destinationButton is instantiated and its
80 \l{QAbstractButton::setIconSize()}{iconSize} property is set to
81 \c resultSize as well. The \l{QLabel}s \c equalLabel and \c resultLabel
82 are created and \c{resultLabel}'s \l{QWidget::setMinimumWidth()}
83 {minimumWidth} is set.
85 \snippet painting/imagecomposition/imagecomposer.cpp 2
87 We connect the following signals to their corresponding slots:
89 \li \c{sourceButton}'s \l{QPushButton::clicked()}{clicked()} signal is
90 connected to \c chooseSource(),
91 \li \c{operatorComboBox}'s \l{QComboBox::activated()}{activated()}
92 signal is connected to \c recalculateResult(), and
93 \li \c{destinationButton}'s \l{QToolButton::clicked()}{clicked()} signal
94 is connected to \c chooseDestination().
97 \snippet painting/imagecomposition/imagecomposer.cpp 3
99 A QGridLayout, \c mainLayout, is used to place all the widgets. Note
100 that \c{mainLayout}'s \l{QLayout::setSizeConstraint()}{sizeConstraint}
101 property is set to QLayout::SetFixedSize, which means that
102 \c{ImageComposer}'s size cannot be resized at all.
104 \snippet painting/imagecomposition/imagecomposer.cpp 4
106 We create a QImage, \c resultImage, and we invoke \c loadImage() twice
107 to load both the image files in our \e imagecomposition.qrc file. Then,
108 we set the \l{QWidget::setWindowTitle()}{windowTitle} property to
111 \snippet painting/imagecomposition/imagecomposer.cpp 5
113 The \c chooseSource() and \c chooseDestination() functions are
114 convenience functions that invoke \c chooseImage() with specific
117 \snippet painting/imagecomposition/imagecomposer.cpp 6
119 \snippet painting/imagecomposition/imagecomposer.cpp 7
121 The \c chooseImage() function loads an image of the user's choice,
122 depending on the \a title, \a image, and \a button.
124 \snippet painting/imagecomposition/imagecomposer.cpp 10
126 The \c recalculateResult() function is used to calculate amd display the
127 result of combining the two images together with the user's choice of
130 \snippet painting/imagecomposition/imagecomposer.cpp 8
132 The \c addOp() function adds an item to the \c operatorComboBox using
133 \l{QComboBox}'s \l{QComboBox::addItem()}{addItem} function. This function
134 accepts a QPainter::CompositionMode, \a mode, and a QString, \a name. The
135 rectangle is filled with Qt::Transparent and both the \c sourceImage and
136 \c destinationImage are painted, before displaying it on \c resultLabel.
138 \snippet painting/imagecomposition/imagecomposer.cpp 9
140 The \c loadImage() function paints a transparent background using
141 \l{QPainter::fillRect()}{fillRect()} and draws \c image in a
142 centralized position using \l{QPainter::drawImage()}{drawImage()}.
143 This \c image is then set as the \c{button}'s icon.
145 \snippet painting/imagecomposition/imagecomposer.cpp 11
147 The \c currentMode() function returns the composition mode currently
148 selected in \c operatorComboBox.
150 \snippet painting/imagecomposition/imagecomposer.cpp 12
152 We use the \c imagePos() function to ensure that images loaded onto the
153 QToolButton objects, \c sourceButton and \c destinationButton, are
156 \snippet painting/imagecomposition/imagecomposer.cpp 13
158 \section1 The \c main() Function
160 The \c main() function instantiates QApplication and \c ImageComposer
161 and invokes its \l{QWidget::show()}{show()} function.
163 \snippet painting/imagecomposition/main.cpp 0