Qt 4.8
qfuturewatcher.h
Go to the documentation of this file.
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 
51 
52 QT_MODULE(Core)
53 
54 class QEvent;
55 
57 
58 #ifdef QT_NO_QFUTURE
60 #endif
61 
63 {
64  Q_OBJECT
66 
67 public:
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 
87 Q_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 
99 public Q_SLOTS:
100  void cancel();
101  void setPaused(bool paused);
102  void pause();
103  void resume();
104  void togglePaused();
105 
106 protected:
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 
114 private:
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 
122 template <typename T>
123 class QFutureWatcher : public QFutureWatcherBase
124 {
125 public:
126  QFutureWatcher(QObject *_parent = 0)
127  : QFutureWatcherBase(_parent)
128  { }
130  { disconnectOutputInterface(); }
131 
132  void setFuture(const QFuture<T> &future);
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 
155 Q_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 
167 public Q_SLOTS:
168  void cancel();
169  void setPaused(bool paused);
170  void pause();
171  void resume();
172  void togglePaused();
173 #endif
174 
175 private:
177  const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
178  QFutureInterfaceBase &futureInterface() { return m_future.d; }
179 };
180 
181 template <typename T>
183 {
184  if (_future == m_future)
185  return;
186 
187  disconnectOutputInterface(true);
188  m_future = _future;
189  connectOutputInterface();
190 }
191 
192 template <>
193 class QFutureWatcher<void> : public QFutureWatcherBase
194 {
195 public:
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 
206 private:
207  QFuture<void> m_future;
208  const QFutureInterfaceBase &futureInterface() const { return m_future.d; }
209  QFutureInterfaceBase &futureInterface() { return m_future.d; }
210 };
211 
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 
226 
227 #endif // QFUTUREWATCHER_H
QFuture< T > m_future
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
QFuture< T > future() const
Returns the watched future.
~QFutureWatcher()
Destroys the QFutureWatcher.
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
virtual bool event(QEvent *)
This virtual function receives events to an object and should return true if the event e was recogniz...
Definition: qobject.cpp:1200
#define Q_SIGNALS
Definition: qobjectdefs.h:72
virtual void connectNotify(const char *signal)
This virtual function is called when something has been connected to signal in this object...
Definition: qobject.cpp:3142
T resultAt(int index) const
Returns the result at index in the future().
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QFutureInterfaceBase & futureInterface()
#define Q_INLINE_TEMPLATE
Definition: qglobal.h:1713
#define Q_OBJECT
Definition: qobjectdefs.h:157
The QFuture class represents the result of an asynchronous computation.
Definition: qfuture.h:64
const QFutureInterfaceBase & futureInterface() const
#define Q_CORE_EXPORT
Definition: qglobal.h:1449
virtual void disconnectNotify(const char *signal)
This virtual function is called when something has been disconnected from signal in this object...
Definition: qobject.cpp:3162
T result() const
Returns the first result in the future().
quint16 index
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
void setFuture(const QFuture< T > &future)
Starts watching the given future.
QFutureInterface< T > d
Definition: qfuture.h:161
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
#define QT_END_HEADER
Definition: qglobal.h:137
QFutureWatcher(QObject *_parent=0)
Constructs a new QFutureWatcher with the given parent.
The QFutureWatcher class allows monitoring a QFuture using signals and slots.
Definition: qfuture.h:59