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