source: trunk/src/corelib/concurrent/qfuturewatcher.cpp@ 769

Last change on this file since 769 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

File size: 18.3 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qfuturewatcher.h"
43
44#ifndef QT_NO_QFUTURE
45
46#include <QtCore/qcoreevent.h>
47#include <QtCore/qcoreapplication.h>
48#include <QtCore/qthread.h>
49
50#include "qfuturewatcher_p.h"
51
52QT_BEGIN_NAMESPACE
53
54/*! \class QFutureWatcher
55 \reentrant
56 \since 4.4
57
58 \ingroup thread
59
60 \brief The QFutureWatcher class allows monitoring a QFuture using signals
61 and slots.
62
63 QFutureWatcher provides information and notifications about a QFuture. Use
64 the setFuture() function to start watching a particular QFuture. The
65 future() function returns the future set with setFuture().
66
67 For convenience, several of QFuture's functions are also available in
68 QFutureWatcher: progressValue(), progressMinimum(), progressMaximum(),
69 progressText(), isStarted(), isFinished(), isRunning(), isCanceled(),
70 isPaused(), waitForFinished(), result(), and resultAt(). The cancel(),
71 setPaused(), pause(), resume(), and togglePaused() functions are slots in
72 QFutureWatcher.
73
74 Status changes are reported via the started(), finished(), canceled(),
75 paused(), resumed(), resultReadyAt(), and resultsReadyAt() signals.
76 Progress information is provided from the progressRangeChanged(),
77 void progressValueChanged(), and progressTextChanged() signals.
78
79 Throttling control is provided by the setPendingResultsLimit() function.
80 When the number of pending resultReadyAt() or resultsReadyAt() signals
81 exceeds the limit, the computation represented by the future will be
82 throttled automatically. The computation will resume once the number of
83 pending signals drops below the limit.
84
85 Example: Starting a computation and getting a slot callback when it's
86 finished:
87
88 \snippet doc/src/snippets/code/src_corelib_concurrent_qfuturewatcher.cpp 0
89
90 Be aware that not all asynchronous computations can be canceled or paused.
91 For example, the future returned by QtConcurrent::run() cannot be
92 canceled; but the future returned by QtConcurrent::mappedReduced() can.
93
94 QFutureWatcher<void> is specialized to not contain any of the result
95 fetching functions. Any QFuture<T> can be watched by a
96 QFutureWatcher<void> as well. This is useful if only status or progress
97 information is needed; not the actual result data.
98
99 \sa QFuture, {Concurrent Programming}{Qt Concurrent}
100*/
101
102/*! \fn QFutureWatcher::QFutureWatcher(QObject *parent)
103
104 Constructs a new QFutureWatcher with the given \a parent.
105*/
106QFutureWatcherBase::QFutureWatcherBase(QObject *parent)
107 :QObject(*new QFutureWatcherBasePrivate, parent)
108{ }
109
110/*! \fn QFutureWatcher::~QFutureWatcher()
111
112 Destroys the QFutureWatcher.
113*/
114
115/*! \fn void QFutureWatcher::cancel()
116
117 Cancels the asynchronous computation represented by the future(). Note that
118 the cancelation is asynchronous. Use waitForFinished() after calling
119 cancel() when you need synchronous cancelation.
120
121 Currently available results may still be accessed on a canceled QFuture,
122 but new results will \e not become available after calling this function.
123 Also, this QFutureWatcher will not deliver progress and result ready
124 signals once canceled. This includes the progressValueChanged(),
125 progressRangeChanged(), progressTextChanged(), resultReadyAt(), and
126 resultsReadyAt() signals.
127
128 Be aware that not all asynchronous computations can be canceled. For
129 example, the QFuture returned by QtConcurrent::run() cannot be canceled;
130 but the QFuture returned by QtConcurrent::mappedReduced() can.
131*/
132void QFutureWatcherBase::cancel()
133{
134 futureInterface().cancel();
135}
136
137/*! \fn void QFutureWatcher::setPaused(bool paused)
138
139 If \a paused is true, this function pauses the asynchronous computation
140 represented by the future(). If the computation is already paused, this
141 function does nothing. This QFutureWatcher will stop delivering progress
142 and result ready signals while the future is paused. Signal delivery will
143 continue once the computation is resumed.
144
145 If \a paused is false, this function resumes the asynchronous computation.
146 If the computation was not previously paused, this function does nothing.
147
148 Be aware that not all computations can be paused. For example, the
149 QFuture returned by QtConcurrent::run() cannot be paused; but the QFuture
150 returned by QtConcurrent::mappedReduced() can.
151
152 \sa pause(), resume(), togglePaused()
153*/
154void QFutureWatcherBase::setPaused(bool paused)
155{
156 futureInterface().setPaused(paused);
157}
158
159/*! \fn void QFutureWatcher::pause()
160
161 Pauses the asynchronous computation represented by the future(). This is a
162 convenience method that simply calls setPaused(true).
163
164 \sa resume()
165*/
166void QFutureWatcherBase::pause()
167{
168 futureInterface().setPaused(true);
169}
170
171/*! \fn void QFutureWatcher::resume()
172
173 Resumes the asynchronous computation represented by the future(). This is
174 a convenience method that simply calls setPaused(false).
175
176 \sa pause()
177*/
178void QFutureWatcherBase::resume()
179{
180 futureInterface().setPaused(false);
181}
182
183/*! \fn void QFutureWatcher::togglePaused()
184
185 Toggles the paused state of the asynchronous computation. In other words,
186 if the computation is currently paused, calling this function resumes it;
187 if the computation is running, it becomes paused. This is a convenience
188 method for calling setPaused(!isPaused()).
189
190 \sa setPaused(), pause(), resume()
191*/
192void QFutureWatcherBase::togglePaused()
193{
194 futureInterface().togglePaused();
195}
196
197/*! \fn int QFutureWatcher::progressValue() const
198
199 Returns the current progress value, which is between the progressMinimum()
200 and progressMaximum().
201
202 \sa progressMinimum(), progressMaximum()
203*/
204int QFutureWatcherBase::progressValue() const
205{
206 return futureInterface().progressValue();
207}
208
209/*! \fn int QFutureWatcher::progressMinimum() const
210
211 Returns the minimum progressValue().
212
213 \sa progressValue(), progressMaximum()
214*/
215int QFutureWatcherBase::progressMinimum() const
216{