summaryrefslogtreecommitdiffstats
path: root/src/corelib/statemachine/qstatemachine.cpp
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-09-29 13:04:09 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-09-29 13:09:36 (GMT)
commitf6a6e30eb16616b90d90fd6e20f9d840da41b9d1 (patch)
tree5e3a5ca259a06d78cb7cc2777084bc4118724322 /src/corelib/statemachine/qstatemachine.cpp
parent1fd189619149c6d3489a35e42c4034d4145237dc (diff)
downloadQt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.zip
Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.tar.gz
Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.tar.bz2
Introduce state machine event priority, make it possible to cancel events
The priority specifies whether the event should be posted to what the SCXML spec refers to as the "external" (NormalPriority) queue, or the "internal" (HighPriority) queue. Delayed events are now posted through a separate function, postDelayedEvent(). That function returns an id that can be passed to cancelDelayedEvent() to cancel it. Reviewed-by: Eskil Abrahamsen Blomfeldt
Diffstat (limited to 'src/corelib/statemachine/qstatemachine.cpp')
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp98
1 files changed, 81 insertions, 17 deletions
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index 8d50870c..256763b 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -1587,6 +1587,18 @@ QStateMachine::~QStateMachine()
{
}
+/*!
+ \enum QStateMachine::EventPriority
+
+ This enum type specifies the priority of an event posted to the state
+ machine using postEvent().
+
+ Events of high priority are processed before events of normal priority.
+
+ \value NormalPriority The event has normal priority.
+ \value HighPriority The event has high priority.
+*/
+
/*! \enum QStateMachine::Error
This enum type defines errors that can occur in the state machine at run time. When the state
@@ -1798,47 +1810,99 @@ void QStateMachine::stop()
}
/*!
- Posts the given \a event for processing by this state machine, with a delay
- of \a delay milliseconds.
+ Posts the given \a event of the given \a priority for processing by this
+ state machine.
This function returns immediately. The event is added to the state machine's
event queue. Events are processed in the order posted. The state machine
takes ownership of the event and deletes it once it has been processed.
You can only post events when the state machine is running.
+
+ \sa postDelayedEvent()
*/
-void QStateMachine::postEvent(QEvent *event, int delay)
+void QStateMachine::postEvent(QEvent *event, EventPriority priority)
{
Q_D(QStateMachine);
if (d->state != QStateMachinePrivate::Running) {
qWarning("QStateMachine::postEvent: cannot post event when the state machine is not running");
return;
}
+ if (!event) {
+ qWarning("QStateMachine::postEvent: cannot post null event");
+ return;
+ }
#ifdef QSTATEMACHINE_DEBUG
- qDebug() << this << ": posting external event" << event << "with delay" << delay;
+ qDebug() << this << ": posting event" << event;
#endif
- if (delay) {
- int tid = startTimer(delay);
- d->delayedEvents[tid] = event;
- } else {
+ switch (priority) {
+ case NormalPriority:
d->externalEventQueue.append(event);
- d->processEvents(QStateMachinePrivate::QueuedProcessing);
+ break;
+ case HighPriority:
+ d->internalEventQueue.append(event);
+ break;
}
+ d->processEvents(QStateMachinePrivate::QueuedProcessing);
}
/*!
- \internal
+ Posts the given \a event for processing by this state machine, with the
+ given \a delay in milliseconds. Returns an identifier associated with the
+ delayed event, or -1 if the event could not be posted.
+
+ This function returns immediately. When the delay has expired, the event
+ will be added to the state machine's event queue for processing. The state
+ machine takes ownership of the event and deletes it once it has been
+ processed.
+
+ You can only post events when the state machine is running.
- Posts the given internal \a event for processing by this state machine.
+ \sa cancelDelayedEvent(), postEvent()
*/
-void QStateMachine::postInternalEvent(QEvent *event)
+int QStateMachine::postDelayedEvent(QEvent *event, int delay)
{
Q_D(QStateMachine);
+ if (d->state != QStateMachinePrivate::Running) {
+ qWarning("QStateMachine::postDelayedEvent: cannot post event when the state machine is not running");
+ return -1;
+ }
+ if (!event) {
+ qWarning("QStateMachine::postDelayedEvent: cannot post null event");
+ return -1;
+ }
+ if (delay < 0) {
+ qWarning("QStateMachine::postDelayedEvent: delay cannot be negative");
+ return -1;
+ }
#ifdef QSTATEMACHINE_DEBUG
- qDebug() << this << ": posting internal event" << event;
+ qDebug() << this << ": posting event" << event << "with delay" << delay;
#endif
- d->internalEventQueue.append(event);
- d->processEvents(QStateMachinePrivate::QueuedProcessing);
+ int tid = startTimer(delay);
+ d->delayedEvents[tid] = event;
+ return tid;
+}
+
+/*!
+ Cancels the delayed event identified by the given \a id. The id should be a
+ value returned by a call to postDelayedEvent(). Returns true if the event
+ was successfully cancelled, otherwise returns false.
+
+ \sa postDelayedEvent()
+*/
+bool QStateMachine::cancelDelayedEvent(int id)
+{
+ Q_D(QStateMachine);
+ if (d->state != QStateMachinePrivate::Running) {
+ qWarning("QStateMachine::cancelDelayedEvent: the machine is not running");
+ return false;
+ }
+ QEvent *e = d->delayedEvents.take(id);
+ if (!e)
+ return false;
+ killTimer(id);
+ delete e;
+ return true;
}
/*!
@@ -1882,9 +1946,9 @@ bool QStateMachine::event(QEvent *e)
if (e->type() == QEvent::Timer) {
QTimerEvent *te = static_cast<QTimerEvent*>(e);
int tid = te->timerId();
- if (d->delayedEvents.contains(tid)) {
+ QEvent *ee = d->delayedEvents.take(tid);
+ if (ee != 0) {
killTimer(tid);
- QEvent *ee = d->delayedEvents.take(tid);
d->externalEventQueue.append(ee);
d->processEvents(QStateMachinePrivate::DirectProcessing);
return true;