C
EventHandlerInterface Class
class SafeRenderer::EventHandlerInterfaceEventHandler base class and shared functionality. More...
Header: | #include <EventHandlerInterface> |
Since: | QtSafeRenderer 3.0 |
Public Functions
EventHandlerInterface(SafeRenderer::StateManager &stateManagerArg, SafeRenderer::SafeWindowInterface &windowArg) | |
EventHandlerInterface(SafeRenderer::StateManager &stateManagerArg, SafeRenderer::SafeWindowInterface &windowArg, SafeRenderer::OutputVerifierInterface &outputVerifierArg) | |
virtual bool | createTimers() = 0 |
virtual void | handleEvents() = 0 |
virtual void | handleHeartbeatTimeout(const bool heartbeatLostArg) |
void | installSafeEventFilter(const SafeRenderer::EventFilter filterFunction) |
void | installTouchCallback(SafeRenderer::EventHandlerInterface::TouchEventCallBack callback) |
void | processEvent(const SafeRenderer::QSafeEvent &event) |
virtual bool | restartTimer(const SafeRenderer::TimerType timerTypeToRestart, const SafeRenderer::quint32 intervalInMSec, const bool repeat = false) = 0 |
virtual bool | sendReply(const SafeRenderer::quint32 id, const SafeRenderer::QSafeEvent &reply) const = 0 |
virtual void | stop() = 0 |
void | uninstallTouchCallback() |
virtual bool | updateTimer(const SafeRenderer::TimerType timerTypeToUpdate, const SafeRenderer::quint32 intervalInMSec, const bool repeat = false) = 0 |
Detailed Description
The EventHandlerInterface is an interface class that platform specific EventHandler adaptations need to inherit. It contains all shared functionality for EventHandler adaptations and some pure virtual methods for the adaptation to override. Any adaptation implementations need to inherit this class and implement the required functions with platform specific APIs and coding conventions.
Member Function Documentation
[explicit]
EventHandlerInterface::EventHandlerInterface(SafeRenderer::StateManager &stateManagerArg, SafeRenderer::SafeWindowInterface &windowArg)
Constructs EventHandlerInterface. stateManagerArg Reference to an instance of class implementing StateManager interface windowArg Reference to a class implementing SafeWindow interface
Note: Instances referenced by stateManagerArg and windowArg must stay in scope until handleEvents() call returns to the user code.
[explicit]
EventHandlerInterface::EventHandlerInterface(SafeRenderer::StateManager &stateManagerArg, SafeRenderer::SafeWindowInterface &windowArg, SafeRenderer::OutputVerifierInterface &outputVerifierArg)
Constructs EventHandlerInterface with an OutputVerifierInterface The reference to the OutputVerifierInterface instance is given to outputVerifierArg. stateManagerArg Reference to an instance of class implementing StateManager interface windowArg Reference to a class implementing SafeWindow interface outputVerifierArg Reference to a class implementing OutputVerifier interface
Note: Instances referenced by stateManagerArg , windowArg and outputVerifierArg must stay in scope until handleEvents() call returns to the user code.
[pure virtual]
bool EventHandlerInterface::createTimers()
Creates the platform specific timers inside the EventHandler adpatation.
Returns true if creating the timers succeeded, false if creation failed.
Note: The implementation for this function must throw an exception if creation of any of the timers fail. If this functions return false, EventHandlerInterface::handleEvents() will instead throw the required exception.
[pure virtual]
void EventHandlerInterface::handleEvents()
handleEvents() is the main entry point for running Safe Renderer Runtime. Calling handleEvents() starts the mainloop and the platform specific event listener in the used EventHandler adaptation.
createTimers() call should throw an exception if platform specific timer creation fails inside the EventHandler adpatation.
Note: All handleEvents functions in EventHandlerInterface implementations must call this base class implementation for proper initialization.
[virtual]
void EventHandlerInterface::handleHeartbeatTimeout(const bool heartbeatLostArg)
After heartbeat timeout has been triggered by receiving QSafeEventHeartbeatTimeout, this function will be called unless safe event filter has blocked the sent QSafeEventHeartbeatTimeout.
User implementation of EventHandler can override this function instead of using safe event filter.
heartbeatLostArg True if heartbeat was lost. False if heartbeat was reacquired.
void EventHandlerInterface::installSafeEventFilter(const 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 QSafeEvent processing 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:
EventHandler *g_msgHandler = nullptr; static bool filterEvents(const QSafeEvent &event) { bool eventHandled = false; if (event.type() == EventId::EventSetVisibility) { QSafeEventVisibility visibility(event); if ((g_msgHandler != nullptr) && (visibility.id() == qsafe_hash_string("iconBattery"))) { QSafeEventChangeState stateChange; stateChange.setId(visibility.id()); if (visibility.getValue() == 1U) { stateChange.setName(qsafe_hash_string("show")); } else { stateChange.setName(qsafe_hash_string("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.installSafeEventFilter(&filterEvents); msgHandler.handleEvents(); return 0; }
void EventHandlerInterface::installTouchCallback(SafeRenderer::EventHandlerInterface::TouchEventCallBack callback)
Install the touch event callback callback which is called whenever a touch event occurs.
This function is called when an item is pressed or released, allowing user to handle these events within their application logic.
Example usage from user's main.qml:
Window { id: window width: 200 height: 200 visible: true title: qsTr("Test application") SafeButton { id: button objectName: "button" width: 64 height: 64 anchors.centerIn: parent } }
Then the functionality could be implemented in SafeButton.qml like this:
SafePicture { id: picture objectName: "picture" width: 64 height: 64 color: "gray" source: "qrc:/iso-icons/iso_grs_7000_4_1555.dat" states: [ State { name: "default" PropertyChanges { target: picture color: "gray" } }, State { name: "pressed" PropertyChanges { target: picture color: "green" } } ] MouseArea { id: mouseArea anchors.fill: parent onPressed: { QSafeMessageSender.sendItemTouchEvent("button", true) } onReleased: { QSafeMessageSender.sendItemTouchEvent("button", false) } }
The user's application's triggering logic from main.cpp:
int main(int argc, char **argv) { // Application Initialization logic SafeRenderer::EventHandler eventHandler(...); auto touchEventCall = [](const quint32 itemId, const bool isPressed, SafeRenderer::EventHandlerInterface *eventHandlerPtr) { if (eventHandlerPtr != nullptr) { SafeRenderer::QSafeEventChangeState stateChange; stateChange.setItemId(itemId); if (isPressed) { stateChange.setStateId(SafeRenderer::qsafe_hash_string("pressed")); } else { stateChange.setStateId(SafeRenderer::qsafe_hash_string("default")); } eventHandlerPtr->processEvent(stateChange); } }; eventHandler.installTouchCallback(touchEventCall); eventHandler.handleEvents(); return 0; }
void EventHandlerInterface::processEvent(const SafeRenderer::QSafeEvent &event)
This function is called from the platform specific implementation when any event is received through the platform specific messaging pipe and if the event was not intercepted by the adaptation itself. If event filter is installed and it's intercepting the given event, the default processEvents() is not called for the event.
Also user code may call this function directly to make QSR runtime handle user specific event.
event QSafeEvent containing the data received from messaging pipe
[pure virtual]
bool EventHandlerInterface::restartTimer(const SafeRenderer::TimerType timerTypeToRestart, const SafeRenderer::quint32 intervalInMSec, const bool repeat = false)
Restarts the specified timer with given parameters. timerTypeToRestart Type of the specific timer (see TimerType ) to be restarted intervalInMSec Interval for the timer to trigger in milliseconds repeat If repeat is true, the timer is not destroyed after it's triggered Returns true if restarting timer succeeded, false if it failed
[pure virtual]
bool EventHandlerInterface::sendReply(const SafeRenderer::quint32 id, const SafeRenderer::QSafeEvent &reply) const
Send a reply to the messaging pipe used by platform specific adaptation. id User or system provided id for which the reply is sent for reply The message payload in QSafeEvent format Returns true if sending message succeeded, false if it failed
[pure virtual]
void EventHandlerInterface::stop()
Stops receiving the safe events. Needed when the application must be stopped.
The EventHandler adaptations should send a QSafeEvent with id EventId::EventQuit to handle proper exit.
void EventHandlerInterface::uninstallTouchCallback()
Uninstall the previously installed touch callback function.
[pure virtual]
bool EventHandlerInterface::updateTimer(const SafeRenderer::TimerType timerTypeToUpdate, const SafeRenderer::quint32 intervalInMSec, const bool repeat = false)
Updates the specified timer with given parameters. timerTypeToUpdate Type of the specific timer (see TimerType ) to be updated intervalInMSec Interval for the timer to trigger in milliseconds repeat If repeat is true, the timer is not destroyed after it's triggered Returns true if restarting timer succeeded, false if it failed
Available under certain Qt licenses.
Find out more.