KTutorial  0.5.1
Public Member Functions | Protected Member Functions
ktutorial::WaitForEvent Class Reference

Waits for an event of some specific type to be sent to an object. More...

#include <WaitForEvent.h>

Inheritance diagram for ktutorial::WaitForEvent:
Inheritance graph
[legend]
Collaboration diagram for ktutorial::WaitForEvent:
Collaboration graph
[legend]

List of all members.

Public Member Functions

Q_INVOKABLE WaitForEvent ()
 Creates a new WaitForEvent.
 WaitForEvent (QObject *object, QEvent::Type type)
 Creates a new WaitForEvent.
virtual ~WaitForEvent ()
 Destroys this WaitForEvent.
Q_INVOKABLE void setEvent (QObject *object, const QString &typeName)
 Sets the event to wait for.
virtual bool eventFilter (QObject *object, QEvent *event)
 Inspects the events sent to the watched object.
virtual bool conditionMet () const
 Returns true if the event was received while active, false otherwise.
virtual void setActive (bool active)
 Sets this WaitForEvent active or inactive.

Protected Member Functions

virtual void handleEvent (QEvent *event)
 Handles the event ending the wait (if this WaitForEvent is active).

Detailed Description

Waits for an event of some specific type to be sent to an object.

When an event of the expected type is sent and the WaitForEvent is active, the wait ends.

Note that if the event is sent while the WaitFor isn't active, it won't be registered and the condition won't be met. In order to met the condition, the event must be sent while the WaitForEvent is active.

In some cases, just waiting for an event of some type isn't enough. For example, you may want to end the waiting if a QEvent::MouseButtonPress is sent, but only if the button pressed is the left. To do this, you can create a subclass of WaitForEvent and redefine handleEvent(QEvent*) method.

 void WaitForEventSubclass::handleEvent(QEvent* event) {
     QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);

     if (mouseEvent->button() == Qt::LeftButton) {
         WaitForEvent::handleEvent(event);
     }
 }

Definition at line 54 of file WaitForEvent.h.


Constructor & Destructor Documentation

Creates a new WaitForEvent.

This constructor is needed to dynamically create WaitForEvent objects in scripts using ScriptingModule::newWaitFor(const QString&). Method setEvent(QObject*, const QString&) must be called to finish setting up the object. For C++ tutorials, use WaitForEvent(QObject*, QEvent::Type) constructor instead of this one.

Definition at line 34 of file WaitForEvent.cpp.

References ktutorial::WaitForEventPrivate::mConditionMet, ktutorial::WaitForEventPrivate::mEventType, and ktutorial::WaitForEventPrivate::mObject.

ktutorial::WaitForEvent::WaitForEvent ( QObject *  object,
QEvent::Type  type 
)

Creates a new WaitForEvent.

Parameters:
objectThe object to watch.
typeThe type of the event to wait for.

Definition at line 41 of file WaitForEvent.cpp.

References ktutorial::WaitForEventPrivate::mConditionMet, ktutorial::WaitForEventPrivate::mEventType, and ktutorial::WaitForEventPrivate::mObject.


Member Function Documentation

bool ktutorial::WaitForEvent::conditionMet ( ) const [virtual]

Returns true if the event was received while active, false otherwise.

Returns:
True if the event was received while active, false otherwise.

Implements ktutorial::WaitFor.

Definition at line 95 of file WaitForEvent.cpp.

References ktutorial::WaitForEventPrivate::mConditionMet.

bool ktutorial::WaitForEvent::eventFilter ( QObject *  object,
QEvent *  event 
) [virtual]

Inspects the events sent to the watched object.

If the event has the type expected by this WaitFor it is handled by handleEvent(QEvent*) which, by default, ends the waiting.

Parameters:
objectThe object that the event was sent to.
eventThe event sent.
Returns:
False, so the event can be handled further.

Definition at line 83 of file WaitForEvent.cpp.

References handleEvent(), ktutorial::WaitFor::isActive(), ktutorial::WaitForEventPrivate::mEventType, and ktutorial::WaitForEventPrivate::mObject.

void ktutorial::WaitForEvent::handleEvent ( QEvent *  event) [protected, virtual]

Handles the event ending the wait (if this WaitForEvent is active).

WaitForEvent subclasses can redefine this method if just receiving an event of the type expected isn't enough to end the waiting and some special check has to be done.

The event is assured to have the type expected by this WaitForEvent and to be sent to the watched object.

Parameters:
eventThe event.

Definition at line 109 of file WaitForEvent.cpp.

References ktutorial::WaitForEventPrivate::mConditionMet, and ktutorial::WaitFor::waitEnded().

Referenced by eventFilter().

void ktutorial::WaitForEvent::setActive ( bool  active) [virtual]

Sets this WaitForEvent active or inactive.

Activating it resets its condition.

Parameters:
activeTrue to set it active, false otherwise.

Reimplemented from ktutorial::WaitFor.

Definition at line 99 of file WaitForEvent.cpp.

References ktutorial::WaitForEventPrivate::mConditionMet.

void ktutorial::WaitForEvent::setEvent ( QObject *  object,
const QString &  typeName 
)

Sets the event to wait for.

Note that the QEvent::Type has to be passed as a string. For example, to wait for a QEvent::Close you have to pass "Close" as the second parameter. This method can be invoked from a script.

In fact, you should only invoke this method from a script, and only once, to set up the object. For C++ tutorials, use WaitForEvent(QObject*, QEvent::Type) constructor when creating this WaitForEvent.

The type is passed as a string with its name instead of using a pure QEvent::Type due to a current limitation of Kross (enumeration in QObject classes seem supported. However, QEvent is not a QObject but a Q_GADGET).

Parameters:
objectThe object to watch.
typeNameThe name of the event type to wait for.

Definition at line 60 of file WaitForEvent.cpp.

References ktutorial::WaitForEventPrivate::mEventType, and ktutorial::WaitForEventPrivate::mObject.


The documentation for this class was generated from the following files: