Qt 4.8
qfuturewatcher.cpp
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 #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 
53 
110  :QObject(*new QFutureWatcherBasePrivate, parent)
111 { }
112 
142 {
144 }
145 
167 {
168  futureInterface().setPaused(paused);
169 }
170 
182 {
183  futureInterface().setPaused(true);
184 }
185 
197 {
198  futureInterface().setPaused(false);
199 }
200 
214 {
216 }
217 
229 {
230  return futureInterface().progressValue();
231 }
232 
243 {
244  return futureInterface().progressMinimum();
245 }
246 
257 {
258  return futureInterface().progressMaximum();
259 }
260 
273 {
274  return futureInterface().progressText();
275 }
276 
286 {
288 }
289 
299 {
300  Q_D(const QFutureWatcherBase);
301  return d->finished;
302 }
303 
313 {
315 }
316 
329 {
331 }
332 
347 {
349 }
350 
360 {
362 }
363 
377 {
379  if (event->type() == QEvent::FutureCallOut) {
380  QFutureCallOutEvent *callOutEvent = static_cast<QFutureCallOutEvent *>(event);
381 
382  if (futureInterface().isPaused()) {
383  d->pendingCallOutEvents.append(callOutEvent->clone());
384  return true;
385  }
386 
387  if (callOutEvent->callOutType == QFutureCallOutEvent::Resumed
388  && !d->pendingCallOutEvents.isEmpty()) {
389  // send the resume
390  d->sendCallOutEvent(callOutEvent);
391 
392  // next send all pending call outs
393  for (int i = 0; i < d->pendingCallOutEvents.count(); ++i)
394  d->sendCallOutEvent(d->pendingCallOutEvents.at(i));
395  qDeleteAll(d->pendingCallOutEvents);
396  d->pendingCallOutEvents.clear();
397  } else {
398  d->sendCallOutEvent(callOutEvent);
399  }
400  return true;
401  }
402  return QObject::event(event);
403 }
404 
406 {
408  d->maximumPendingResultsReady = limit;
409 }
410 
411 void QFutureWatcherBase::connectNotify(const char * signal)
412 {
414  if (qstrcmp(signal, SIGNAL(resultReadyAt(int))) == 0)
415  d->resultAtConnected.ref();
416 #ifndef QT_NO_DEBUG
417  if (qstrcmp(signal, SIGNAL(finished())) == 0) {
418  if (futureInterface().isRunning()) {
419  //connections should be established before calling stFuture to avoid race.
420  // (The future could finish before the connection is made.)
421  qWarning("QFutureWatcher::connect: connecting after calling setFuture() is likely to produce race");
422  }
423  }
424 #endif
425 }
426 
427 void QFutureWatcherBase::disconnectNotify(const char * signal)
428 {
430  if (qstrcmp(signal, SIGNAL(resultReadyAt(int))) == 0)
431  d->resultAtConnected.deref();
432 }
433 
438  : maximumPendingResultsReady(QThread::idealThreadCount() * 2),
439  resultAtConnected(0)
440 { }
441 
446 {
447  futureInterface().d->connectOutputInterface(d_func());
448 }
449 
454 {
455  if (pendingAssignment) {
457  d->pendingResultsReady = 0;
458  qDeleteAll(d->pendingCallOutEvents);
459  d->pendingCallOutEvents.clear();
460  d->finished = false;
461  }
462 
463  futureInterface().d->disconnectOutputInterface(d_func());
464 }
465 
467 {
469 
470  if (callOutEvent.callOutType == QFutureCallOutEvent::ResultsReady) {
472  q->futureInterface().d->internal_setThrottled(true);
473  }
474 
475  QCoreApplication::postEvent(q, callOutEvent.clone());
476 }
477 
479 {
481 }
482 
484 {
486 
487  switch (event->callOutType) {
489  emit q->started();
490  break;
492  finished = true;
493  emit q->finished();
494  break;
497  emit q->canceled();
498  break;
500  if (q->futureInterface().isCanceled())
501  break;
502  emit q->paused();
503  break;
505  if (q->futureInterface().isCanceled())
506  break;
507  emit q->resumed();
508  break;
510  if (q->futureInterface().isCanceled())
511  break;
512 
514  q->futureInterface().setThrottled(false);
515 
516  const int beginIndex = event->index1;
517  const int endIndex = event->index2;
518 
519  emit q->resultsReadyAt(beginIndex, endIndex);
520 
521  if (int(resultAtConnected) <= 0)
522  break;
523 
524  for (int i = beginIndex; i < endIndex; ++i)
525  emit q->resultReadyAt(i);
526 
527  } break;
529  if (q->futureInterface().isCanceled())
530  break;
531 
532  emit q->progressValueChanged(event->index1);
533  if (!event->text.isNull()) // ###
534  q->progressTextChanged(event->text);
535  break;
537  emit q->progressRangeChanged(event->index1, event->index2);
538  break;
539  default: break;
540  }
541 }
542 
543 
684 
685 #else
686 
687 // On Symbian winscw target QT_NO_QFUTURE and QT_NO_CONCURRENT are both defined.
688 // However moc will be run without having them set, so provide a dummy stub at
689 // least for the slots to prevent linker errors.
690 
692 void QFutureWatcherBase::setPaused(bool) { }
693 void QFutureWatcherBase::pause() { }
696 bool QFutureWatcherBase::event(QEvent *) { return false; }
697 void QFutureWatcherBase::connectNotify(const char *) { }
698 void QFutureWatcherBase::disconnectNotify(const char *) { }
699 
700 #endif // QT_NO_QFUTURE
double d
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
EventRef event
bool isFinished() const
bool isCanceled() const
static void postEvent(QObject *receiver, QEvent *event)
Adds the event event, with the object receiver as the receiver of the event, to an event queue and re...
bool queryState(State state) const
void disconnectOutputInterface(bool pendingAssignment=false)
QString progressText() const
int progressValue() const
void connectNotify(const char *signal)
This virtual function is called when something has been connected to signal in this object...
QFutureCallOutEvent * clone() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
QFutureWatcherBase(QObject *parent=0)
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_D(Class)
Definition: qglobal.h:2482
bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
#define Q_Q(Class)
Definition: qglobal.h:2483
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
#define emit
Definition: qobjectdefs.h:76
Q_CORE_EXPORT void qWarning(const char *,...)
void setPaused(bool paused)
void resultReadyAt(int resultIndex)
int fetchAndAddRelaxed(int valueToAdd)
Atomic fetch-and-add.
void setPendingResultsLimit(int limit)
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
void setPaused(bool paused)
QString progressText() const
int progressMaximum() const
virtual const QFutureInterfaceBase & futureInterface() const =0
int qstrcmp(const QByteArray &str1, const char *str2)
Definition: qbytearray.cpp:336
removePostedEvents
Removes all events of the given eventType that were posted using postEvent() for receiver.
void postCallOutEvent(const QFutureCallOutEvent &callOutEvent)
void disconnectNotify(const char *signal)
This virtual function is called when something has been disconnected from signal in this object...
The QEvent class is the base class of all event classes.
Definition: qcoreevent.h:56
bool isRunning() const
void sendCallOutEvent(QFutureCallOutEvent *event)
Type type() const
Returns the event type.
Definition: qcoreevent.h:303
The QThread class provides a platform-independent way to manage threads.
Definition: qthread.h:59
bool isStarted() const
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
int progressMinimum() const