1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 **   * Redistributions of source code must retain the above copyright
25 **     notice, this list of conditions and the following disclaimer.
26 **   * Redistributions in binary form must reproduce the above copyright
27 **     notice, this list of conditions and the following disclaimer in
28 **     the documentation and/or other materials provided with the
29 **     distribution.
30 **   * Neither the name of The Qt Company Ltd nor the names of its
31 **     contributors may be used to endorse or promote products derived
32 **     from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
51 //! [0]
52 U function(const T &t);
53 //! [0]
54 
55 
56 //! [1]
scaled(const QImage & image)57 QImage scaled(const QImage &image)
58 {
59     return image.scaled(100, 100);
60 }
61 
62 QList<QImage> images = ...;
63 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scaled);
64 //! [1]
65 
66 
67 //! [2]
68 U function(T &t);
69 //! [2]
70 
71 
72 //! [3]
scale(QImage & image)73 void scale(QImage &image)
74 {
75     image = image.scaled(100, 100);
76 }
77 
78 QList<QImage> images = ...;
79 QFuture<void> future = QtConcurrent::map(images, scale);
80 //! [3]
81 
82 
83 //! [4]
function(T & result,const U & intermediate)84 V function(T &result, const U &intermediate)
85 //! [4]
86 
87 
88 //! [5]
89 void addToCollage(QImage &collage, const QImage &thumbnail)
90 {
91     QPainter p(&collage);
92     static QPoint offset = QPoint(0, 0);
93     p.drawImage(offset, thumbnail);
94     offset += ...;
95 }
96 
97 QList<QImage> images = ...;
98 QFuture<QImage> collage = QtConcurrent::mappedReduced(images, scaled, addToCollage);
99 //! [5]
100 
101 
102 //! [6]
103 QList<QImage> images = ...;
104 
105 QFuture<QImage> thumbnails = QtConcurrent::mapped(images.constBegin(), images.constEnd(), scaled);
106 
107 // Map in-place only works on non-const iterators.
108 QFuture<void> future = QtConcurrent::map(images.begin(), images.end(), scale);
109 
110 QFuture<QImage> collage = QtConcurrent::mappedReduced(images.constBegin(), images.constEnd(), scaled, addToCollage);
111 //! [6]
112 
113 
114 //! [7]
115 QList<QImage> images = ...;
116 
117 // Each call blocks until the entire operation is finished.
118 QList<QImage> future = QtConcurrent::blockingMapped(images, scaled);
119 
120 QtConcurrent::blockingMap(images, scale);
121 
122 QImage collage = QtConcurrent::blockingMappedReduced(images, scaled, addToCollage);
123 //! [7]
124 
125 
126 //! [8]
127 // Squeeze all strings in a QStringList.
128 QStringList strings = ...;
129 QFuture<void> squeezedStrings = QtConcurrent::map(strings, &QString::squeeze);
130 
131 // Swap the rgb values of all pixels on a list of images.
132 QList<QImage> images = ...;
133 QFuture<QImage> bgrImages = QtConcurrent::mapped(images, &QImage::rgbSwapped);
134 
135 // Create a set of the lengths of all strings in a list.
136 QStringList strings = ...;
137 QFuture<QSet<int> > wordLengths = QtConcurrent::mappedReduced(strings, &QString::length, &QSet<int>::insert);
138 //! [8]
139 
140 
141 //! [9]
142 // Can mix normal functions and member functions with QtConcurrent::mappedReduced().
143 
144 // Compute the average length of a list of strings.
145 extern void computeAverage(int &average, int length);
146 QStringList strings = ...;
147 QFuture<int> averageWordLength = QtConcurrent::mappedReduced(strings, &QString::length, computeAverage);
148 
149 // Create a set of the color distribution of all images in a list.
150 extern int colorDistribution(const QImage &string);
151 QList<QImage> images = ...;
152 QFuture<QSet<int> > totalColorDistribution = QtConcurrent::mappedReduced(images, colorDistribution, QSet<int>::insert);
153 //! [9]
154 
155 
156 //! [10]
157 QImage QImage::scaledToWidth(int width, Qt::TransformationMode) const;
158 //! [10]
159 
160 //! [11]
161 struct ImageTransform
162 {
163     void operator()(QImage &result, const QImage &value);
164 };
165 
166 QFuture<QImage> thumbNails =
167   QtConcurrent::mappedReduced<QImage>(images,
168                                       Scaled(100),
169                                       ImageTransform(),
170                                       QtConcurrent::SequentialReduce);
171 //! [11]
172 
173 //! [13]
174 QList<QImage> images = ...;
__anonf579fd820102(const QImage &img) 175 std::function<QImage(const QImage &)> scale = [](const QImage &img) {
176     return img.scaledToWidth(100, Qt::SmoothTransformation);
177 };
178 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, scale);
179 //! [13]
180 
181 //! [14]
182 struct Scaled
183 {
ScaledScaled184     Scaled(int size)
185     : m_size(size) { }
186 
187     typedef QImage result_type;
188 
operator ()Scaled189     QImage operator()(const QImage &image)
190     {
191         return image.scaled(m_size, m_size);
192     }
193 
194     int m_size;
195 };
196 
197 QList<QImage> images = ...;
198 QFuture<QImage> thumbnails = QtConcurrent::mapped(images, Scaled(100));
199 //! [14]
200