C

EventHandler Class

class SafeRenderer::EventHandler

The EventHandler class creates a platform specific event loop for receiving QSafeEvents from the other processes. More...

Header: #include <EventHandler>
Since: QtSafeRenderer 1.2

This class was introduced in QtSafeRenderer 1.2.

Public Functions

EventHandler(SafeRenderer::StateManager &manager, SafeRenderer::SafeWindow &windowArg, SafeRenderer::OutputVerifier &outputVerifierArg)
EventHandler(SafeRenderer::StateManager &manager, SafeRenderer::SafeWindow &windowArg)
void handleEvents() const
void installEventFilter(SafeRenderer::EventFilter filterFunction)
void stop() const

Detailed Description

Member Function Documentation

EventHandler::EventHandler(SafeRenderer::StateManager &manager, SafeRenderer::SafeWindow &windowArg, SafeRenderer::OutputVerifier &outputVerifierArg)

Constructs EventHandler class with a OutputVerifier A valid reference to the StateManager instance must be given to manager. The event handler is initialized with windowArg that is a reference to SafeWindow. The reference to the OutputVerifier instance is given to outputVerifierArg.

EventHandler::EventHandler(SafeRenderer::StateManager &manager, SafeRenderer::SafeWindow &windowArg)

Constructs EventHandler class. A valid reference to the StateManager instance must be given to manager. The event handler is initialized with windowArg that is a reference to SafeWindow.

void EventHandler::handleEvents() const

Enters the event receiving loop and waits until stop() is called.

void EventHandler::installEventFilter(SafeRenderer::EventFilter filterFunction)

Install the callback filterFunction which is called when a new event is received.

Then filterFunction is the pointer to the following function type:

bool filterFunction(const QSafeEvent &event);

That function is called before the event is passed to the StateManager class. If the filterFunction returns true, the event is not forwarded to the StateManager class. Otherwise the event is passed to the StateManager class.

With the filterFunction callback, it is possible to hook to the QSafeEvents in the safety application. For example, the hooks can be used for handling the heartbeat timeout or triggering the safety item state changes based on the system events.

The following example shows how to install an event filter and use it to trigger an opacity transition animation using the QSafeEventVisibility event. Similarly, you can trigger state transitions from any external events e.g. SafeRenderer::QSafeEventSystem.

The QML transition to be triggered is defined as follows:

SafePicture {
    id: iconBattery
    objectName: "iconBattery"
    width: 30
    height: 30
    color: "#e41e25"
    source: "qrc:/iso-icons/iso_grs_7000_4_0247.dat"
    states: [
        State { name: "show"; PropertyChanges {target: iconBattery; opacity: 1.0}},
        State { name: "hide"; PropertyChanges {target: iconBattery; opacity: 0.0}}
    ]
    transitions: [
        Transition {
            from: "*"
            to: "*"
            NumberAnimation {
                properties: "opacity"
                duration: 600
                easing.type: Easing.InOutQuad
            }
        }
    ]
}

The application's triggering logic is as follows:

static quint32 strToHash(const qchar *strArg) {
    return qsafe_hash(strArg, safe_strlen(strArg));
}
EventHandler *g_msgHandler = NULL;
static bool filterEvents(const QSafeEvent &event)
    {
    bool eventHandled = false;
    if (event.type() == EventSetVisibility) {
        QSafeEventVisibility visibility(event);
        if ((g_msgHandler) &&
                (visibility.id() == strToHash("iconBattery"))) {
            QSafeEventChangeState stateChange;
            stateChange.setId(visibility.id());
            if (visibility.getValue() == 1U) {
                stateChange.setName(strToHash("show"));
            } else {
                stateChange.setName(strToHash("hide"));
            }
            g_msgHandler->sendEvent(stateChange);
            eventHandled = true;
        }
    }
    return eventHandled;
}

int main(int argc, char **argv)
{
    static QSafeLayoutResourceReader layout("/layoutData/MainForm/MainForm.ui.srl");
    SafeWindow telltaleWindow(layout.size());
    static SafeRenderer::StateManager stateManager(telltaleWindow, layout);
    EventHandler msgHandler(stateManager, telltaleWindow);
    g_msgHandler = &msgHandler;
    msgHandler.installEventFilter(&filterEvents);
    msgHandler.handleEvents();
    return 0;
}

void EventHandler::stop() const

Stops receiving the QSafeEvents. Needed when the application must be stopped.

Available under certain Qt licenses.
Find out more.