1/****************************************************************************
2**
3** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: http://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QFUTUREWATCHER_H
43#define QFUTUREWATCHER_H
44
45#include <QtCore/qfuture.h>
46
47#include <QtCore/qobject.h>
48
49QT_BEGIN_HEADER
50QT_BEGIN_NAMESPACE
51
52QT_MODULE(Core)
53
54class QEvent;
55
56class QFutureWatcherBasePrivate;
57
58#ifdef QT_NO_QFUTURE
59class QFutureInterfaceBase;
60#endif
61
62class Q_CORE_EXPORT QFutureWatcherBase : public QObject
63{
64 Q_OBJECT
65 Q_DECLARE_PRIVATE(QFutureWatcherBase)
66
67public:
68 QFutureWatcherBase(QObject *parent = 0);
69
70 int progressValue() const;
71 int progressMinimum() const;
72 int progressMaximum() const;
73 QString progressText() const;
74
75 bool isStarted() const;
76 bool isFinished() const;
77 bool isRunning() const;
78 bool isCanceled() const;
79 bool isPaused() const;
80
81 void waitForFinished();
82
83 void setPendingResultsLimit(int limit);
84
85 bool event(QEvent *event);
86
87Q_SIGNALS:
88 void started();
89 void finished();
90 void canceled();
91 void paused();
92 void resumed();
93 void resultReadyAt(int resultIndex);
94 void resultsReadyAt(int beginIndex, int endIndex);
95 void progressRangeChanged(int minimum, int maximum);
96 void progressValueChanged(int progressValue);
97 void progressTextChanged(const QString &progressText);
98
99public Q_SLOTS:
100 void cancel();
101 void setPaused(bool paused);
102 void pause();
103 void resume();
104 void togglePaused();
105
106protected:
107 void connectNotify (const char * signal);
108 void disconnectNotify (const char * signal);
109
110 // called from setFuture() implemented in template sub-classes
111 void connectOutputInterface();
112 void disconnectOutputInterface(bool pendingAssignment = false);
113
114private:
115 // implemented in the template sub-classes
116 virtual const QFutureInterfaceBase &futureInterface() const = 0;
117 virtual QFutureInterfaceBase &futureInterface() = 0;
118};
119
120#ifndef QT_NO_QFUTURE
121
122template <typename T>
123class QFutureWatcher : public QFutureWatcherBase
124{
125public:
126 QFutureWatcher(QObject *_parent = 0)
127 : QFutureWatcherBase(_parent)
128 { }
129 ~QFutureWatcher()
130 { disconnectOutputInterface(); }
131
132 void setFuture(const QFuture<T> &future);
133 QFuture<T> future() const
134 { return m_future; }
135
136 T result() const { return m_future.result(); }
137 T resultAt(int index) const { return m_future.resultAt(index); }
138
139#ifdef qdoc
140 int progressValue() const;
141 int progressMinimum() const;
142 int progressMaximum() const;
143 QString progressText() const;
144
145 bool isStarted() const;
146 bool isFinished() const;
147 bool isRunning() const;
148 bool isCanceled() const;
149 bool isPaused() const;
150
151 void waitForFinished();
152
153 void setPendingResultsLimit(int limit);
154
155Q_SIGNALS:
156 void started();
157 void finished();
158 void canceled();
159 void paused();
160 void resumed();
161 void resultReadyAt(int resultIndex);
162 void resultsReadyAt(int beginIndex, int endIndex);
163 void progressRangeChanged(int minimum, int maximum);
164 void progressValueChanged(int progressValue);
165 void progressTextChanged(const QString &progressText);
166
167public Q_SLOTS:
168 void cancel();
169 void setPaused(bool paused);
170 void pause();
171 void resume();
172 void togglePaused();
173#endif
174
175private:
176 QFuture<T> m_future;
177 const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
178 QFutureInterfaceBase &futureInterface() { return m_future.d; }
179};
180
181template <typename T>
182Q_INLINE_TEMPLATE void QFutureWatcher<T>::setFuture(const QFuture<T> &_future)
183{
184 if (_future == m_future)
185 return;
186
187 disconnectOutputInterface(true);
188 m_future = _future;
189 connectOutputInterface();
190}
191
192template <>
193class QFutureWatcher<void> : public QFutureWatcherBase
194{
195public:
196 QFutureWatcher(QObject *_parent = 0)
197 : QFutureWatcherBase(_parent)
198 { }
199 ~QFutureWatcher()
200 { disconnectOutputInterface(); }
201
202 void setFuture(const QFuture<void> &future);
203 QFuture<void> future() const
204 { return m_future; }
205
206private:
207 QFuture<void> m_future;
208 const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
209 QFutureInterfaceBase &futureInterface() { return m_future.d; }
210};
211
212Q_INLINE_TEMPLATE void QFutureWatcher<void>::setFuture(const QFuture<void> &_future)
213{
214 if (_future == m_future)
215 return;
216
217 disconnectOutputInterface(true);
218 m_future = _future;
219 connectOutputInterface();
220}
221
222#endif // QT_NO_QFUTURE
223
224QT_END_NAMESPACE
225QT_END_HEADER
226
227#endif // QFUTUREWATCHER_H
228