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
31 \ingroup examples-painting
32 \brief Shows how composition modes work in QPainter.
34 \brief The Image Composition example lets the user combine images
35 together using any composition mode supported by QPainter, described
36 in detail in \l{QPainter#Composition Modes}{Composition Modes}.
38 \image imagecomposition-example.png
40 \section1 Setting Up The Resource File
42 The Image Composition example requires two source images,
43 \e butterfly.png and \e checker.png that are embedded within
44 \e imagecomposition.qrc. The file contains the following code:
46 \quotefile painting/imagecomposition/imagecomposition.qrc
48 For more information on resource files, see \l{The Qt Resource System}.
50 \section1 ImageComposer Class Definition
52 The \c ImageComposer class is a subclass of QWidget that implements three
53 private slots, \c chooseSource(), \c chooseDestination(), and
54 \c recalculateResult().
56 \snippet painting/imagecomposition/imagecomposer.h 0
58 In addition, \c ImageComposer consists of five private functions,
59 \c addOp(), \c chooseImage(), \c loadImage(), \c currentMode(), and
60 \c imagePos(), as well as private instances of QToolButton, QComboBox,
63 \snippet painting/imagecomposition/imagecomposer.h 1
65 \section1 ImageComposer Class Implementation
67 We declare a QSize object, \c resultSize, as a static constant with width
68 and height equal to 200.
70 \snippet painting/imagecomposition/imagecomposer.cpp 0
72 Within the constructor, we instantiate a QToolButton object,
73 \c sourceButton and set its \l{QAbstractButton::setIconSize()}{iconSize}
74 property to \c resultSize. The \c operatorComboBox is instantiated and
75 then populated using the \c addOp() function. This function accepts a
76 QPainter::CompositionMode, \a mode, and a QString, \a name, representing
77 the name of the composition mode.
79 \snippet painting/imagecomposition/imagecomposer.cpp 1
81 The \c destinationButton is instantiated and its
82 \l{QAbstractButton::setIconSize()}{iconSize} property is set to
83 \c resultSize as well. The \l{QLabel}s \c equalLabel and \c resultLabel
84 are created and \c{resultLabel}'s \l{QWidget::setMinimumWidth()}
85 {minimumWidth} is set.
87 \snippet painting/imagecomposition/imagecomposer.cpp 2
89 We connect the following signals to their corresponding slots:
91 \li \c{sourceButton}'s \l{QPushButton::clicked()}{clicked()} signal is
92 connected to \c chooseSource(),
93 \li \c{operatorComboBox}'s \l{QComboBox::activated()}{activated()}
94 signal is connected to \c recalculateResult(), and
95 \li \c{destinationButton}'s \l{QToolButton::clicked()}{clicked()} signal
96 is connected to \c chooseDestination().
99 \snippet painting/imagecomposition/imagecomposer.cpp 3
101 A QGridLayout, \c mainLayout, is used to place all the widgets. Note
102 that \c{mainLayout}'s \l{QLayout::setSizeConstraint()}{sizeConstraint}
103 property is set to QLayout::SetFixedSize, which means that
104 \c{ImageComposer}'s size cannot be resized at all.
106 \snippet painting/imagecomposition/imagecomposer.cpp 4
108 We create a QImage, \c resultImage, and we invoke \c loadImage() twice
109 to load both the image files in our \e imagecomposition.qrc file. Then,
110 we set the \l{QWidget::setWindowTitle()}{windowTitle} property to
113 \snippet painting/imagecomposition/imagecomposer.cpp 5
115 The \c chooseSource() and \c chooseDestination() functions are
116 convenience functions that invoke \c chooseImage() with specific
119 \snippet painting/imagecomposition/imagecomposer.cpp 6
121 \snippet painting/imagecomposition/imagecomposer.cpp 7
123 The \c chooseImage() function loads an image of the user's choice,
124 depending on the \a title, \a image, and \a button.
126 \snippet painting/imagecomposition/imagecomposer.cpp 10
128 The \c recalculateResult() function is used to calculate amd display the
129 result of combining the two images together with the user's choice of
132 \snippet painting/imagecomposition/imagecomposer.cpp 8
134 The \c addOp() function adds an item to the \c operatorComboBox using
135 \l{QComboBox}'s \l{QComboBox::addItem()}{addItem} function. This function
136 accepts a QPainter::CompositionMode, \a mode, and a QString, \a name. The
137 rectangle is filled with Qt::Transparent and both the \c sourceImage and
138 \c destinationImage are painted, before displaying it on \c resultLabel.
140 \snippet painting/imagecomposition/imagecomposer.cpp 9
142 The \c loadImage() function paints a transparent background using
143 \l{QPainter::fillRect()}{fillRect()} and draws \c image in a
144 centralized position using \l{QPainter::drawImage()}{drawImage()}.
145 This \c image is then set as the \c{button}'s icon.
147 \snippet painting/imagecomposition/imagecomposer.cpp 11
149 The \c currentMode() function returns the composition mode currently
150 selected in \c operatorComboBox.
152 \snippet painting/imagecomposition/imagecomposer.cpp 12
154 We use the \c imagePos() function to ensure that images loaded onto the
155 QToolButton objects, \c sourceButton and \c destinationButton, are
158 \snippet painting/imagecomposition/imagecomposer.cpp 13
160 \section1 The \c main() Function
162 The \c main() function instantiates QApplication and \c ImageComposer
163 and invokes its \l{QWidget::show()}{show()} function.
165 \snippet painting/imagecomposition/main.cpp 0