1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtCore module 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 [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "qfuturewatcher.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_QFUTURE
|
---|
45 |
|
---|
46 | #include <QEvent>
|
---|
47 | #include <QCoreApplication>
|
---|
48 | #include <QThread>
|
---|
49 |
|
---|
50 | #include "qfuturewatcher_p.h"
|
---|
51 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | /*! \class QFutureWatcher
|
---|
55 | \reentrant
|
---|
56 | \since 4.4
|
---|
57 |
|
---|
58 | \brief The QFutureWatcher class allows monitoring a QFuture using signals
|
---|
59 | and slots.
|
---|
60 |
|
---|
61 | QFutureWatcher provides information and notifications about a QFuture. Use
|
---|
62 | the setFuture() function to start watching a particular QFuture. The
|
---|
63 | future() function returns the future set with setFuture().
|
---|
64 |
|
---|
65 | For convenience, several of QFuture's functions are also available in
|
---|
66 | QFutureWatcher: progressValue(), progressMinimum(), progressMaximum(),
|
---|
67 | progressText(), isStarted(), isFinished(), isRunning(), isCanceled(),
|
---|
68 | isPaused(), waitForFinished(), result(), and resultAt(). The cancel(),
|
---|
69 | setPaused(), pause(), resume(), and togglePaused() functions are slots in
|
---|
70 | QFutureWatcher.
|
---|
71 |
|
---|
72 | Status changes are reported via the started(), finished(), canceled(),
|
---|
73 | paused(), resumed(), resultReadyAt(), and resultsReadyAt() signals.
|
---|
74 | Progress information is provided from the progressRangeChanged(),
|
---|
75 | void progressValueChanged(), and progressTextChanged() signals.
|
---|
76 |
|
---|
77 | Throttling control is provided by the setPendingResultsLimit() function.
|
---|
78 | When the number of pending resultReadyAt() or resultsReadyAt() signals
|
---|
79 | exceeds the limit, the computation represented by the future will be
|
---|
80 | throttled automatically. The computation will resume once the number of
|
---|
81 | pending signals drops below the limit.
|
---|
82 |
|
---|
83 | Example: Starting a computation and getting a slot callback when it's
|
---|
84 | finished:
|
---|
85 |
|
---|
86 | \snippet doc/src/snippets/code/src_corelib_concurrent_qfuturewatcher.cpp 0
|
---|
87 |
|
---|
88 | Be aware that not all asynchronous computations can be canceled or paused.
|
---|
89 | For example, the future returned by QtConcurrent::run() cannot be
|
---|
90 | canceled; but the future returned by QtConcurrent::mappedReduced() can.
|
---|
91 |
|
---|
92 | QFutureWatcher<void> is specialized to not contain any of the result
|
---|
93 | fetching functions. Any QFuture<T> can be watched by a
|
---|
94 | QFutureWatcher<void> as well. This is useful if only status or progress
|
---|
95 | information is needed; not the actual result data.
|
---|
96 |
|
---|
97 | \sa QFuture, {threads.html#qtconcurrent-intro}{Qt Concurrent}
|
---|
98 | */
|
---|
99 |
|
---|
100 | /*! \fn QFutureWatcher::QFutureWatcher(QObject *parent)
|
---|
101 |
|
---|
102 | Constructs a new QFutureWatcher with the given \a parent.
|
---|
103 | */
|
---|
104 | QFutureWatcherBase::QFutureWatcherBase(QObject *parent)
|
---|
105 | :QObject(*new QFutureWatcherBasePrivate, parent)
|
---|
106 | { }
|
---|
107 |
|
---|
108 | /*! \fn QFutureWatcher::~QFutureWatcher()
|
---|
109 |
|
---|
110 | Destroys the QFutureWatcher.
|
---|
111 | */
|
---|
112 |
|
---|
113 | /*! \fn void QFutureWatcher::cancel()
|
---|
114 |
|
---|
115 | Cancels the asynchronous computation represented by the future(). Note that
|
---|
116 | the cancelation is asynchronous. Use waitForFinished() after calling
|
---|
117 | cancel() when you need synchronous cancelation.
|
---|
118 |
|
---|
119 | Currently available results may still be accessed on a canceled QFuture,
|
---|
120 | but new results will \e not become available after calling this function.
|
---|
121 | Also, this QFutureWatcher will not deliver progress and result ready
|
---|
122 | signals once canceled. This includes the progressValueChanged(),
|
---|
123 | progressRangeChanged(), progressTextChanged(), resultReadyAt(), and
|
---|
124 | resultsReadyAt() signals.
|
---|
125 |
|
---|
126 | Be aware that not all asynchronous computations can be canceled. For
|
---|
127 | example, the QFuture returned by QtConcurrent::run() cannot be canceled;
|
---|
128 | but the QFuture returned by QtConcurrent::mappedReduced() can.
|
---|
129 | */
|
---|
130 | void QFutureWatcherBase::cancel()
|
---|
131 | {
|
---|
132 | futureInterface().cancel();
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*! \fn void QFutureWatcher::setPaused(bool paused)
|
---|
136 |
|
---|
137 | If \a paused is true, this function pauses the asynchronous computation
|
---|
138 | represented by the future(). If the computation is already paused, this
|
---|
139 | function does nothing. This QFutureWatcher will stop delivering progress
|
---|
140 | and result ready signals while the future is paused. Signal delivery will
|
---|
141 | continue once the computation is resumed.
|
---|
142 |
|
---|
143 | If \a paused is false, this function resumes the asynchronous computation.
|
---|
144 | If the computation was not previously paused, this function does nothing.
|
---|
145 |
|
---|
146 | Be aware that not all computations can be paused. For example, the
|
---|
147 | QFuture returned by QtConcurrent::run() cannot be paused; but the QFuture
|
---|
148 | returned by QtConcurrent::mappedReduced() can.
|
---|
149 |
|
---|
150 | \sa pause(), resume(), togglePaused()
|
---|
151 | */
|
---|
152 | void QFutureWatcherBase::setPaused(bool paused)
|
---|
153 | {
|
---|
154 | futureInterface().setPaused(paused);
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*! \fn void QFutureWatcher::pause()
|
---|
158 |
|
---|
159 | Pauses the asynchronous computation represented by the future(). This is a
|
---|
160 | convenience method that simply calls setPaused(true).
|
---|
161 |
|
---|
162 | \sa resume()
|
---|
163 | */
|
---|
164 | void QFutureWatcherBase::pause()
|
---|
165 | {
|
---|
166 | futureInterface().setPaused(true);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /*! \fn void QFutureWatcher::resume()
|
---|
170 |
|
---|
171 | Resumes the asynchronous computation represented by the future(). This is
|
---|
172 | a convenience method that simply calls setPaused(false).
|
---|
173 |
|
---|
174 | \sa pause()
|
---|
175 | */
|
---|
176 | void QFutureWatcherBase::resume()
|
---|
177 | {
|
---|
178 | futureInterface().setPaused(false);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /*! \fn void QFutureWatcher::togglePaused()
|
---|
182 |
|
---|
183 | Toggles the paused state of the asynchronous computation. In other words,
|
---|
184 | if the computation is currently paused, calling this function resumes it;
|
---|
185 | if the computation is running, it becomes paused. This is a convenience
|
---|
186 | method for calling setPaused(!isPaused()).
|
---|
187 |
|
---|
188 | \sa setPaused(), pause(), resume()
|
---|
189 | */
|
---|
190 | void QFutureWatcherBase::togglePaused()
|
---|
191 | {
|
---|
192 | futureInterface().togglePaused();
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*! \fn int QFutureWatcher::progressValue() const
|
---|
196 |
|
---|
197 | Returns the current progress value, which is between the progressMinimum()
|
---|
198 | and progressMaximum().
|
---|
199 |
|
---|
200 | \sa progressMinimum(), progressMaximum()
|
---|
201 | */
|
---|
202 | int QFutureWatcherBase::progressValue() const
|
---|
203 | {
|
---|
204 | return futureInterface().progressValue();
|
---|
205 | }
|
---|
|
---|