I am trying to implement a state machine for handling a complex set of mouse and keyboard interactions with a graphical data plot. I have partially finished the state chart, and have more than 25 states defined so far. The types of interactions include zooming in and out, selecting, moving, and resizing objects, panning, etc. Nearly all of the transitions between states occur on mouse, key, or mouse wheel events. There are a few transitions which could be driven by external events (for example, a button or menu signal to transition between zoom and pan modes).
I am having trouble understanding how to extract event information from transitions.
For example, a mouse-based zoom operation involves 4 states: Idle, ZoomStart, ZoomTrack, and ZoomEnd. The transition from the Idle state into ZoomStart occurs when the left mouse button is pressed. The transition from ZoomStart to ZoomTrack occurs on mouse move, and from ZoomTrack to ZoomEnd on mouse up. On entry into each of the Zoom... states, I need to capture the mouse position.
In the widget that owns the state machine, I can define Q_PROPERTY variables to hold the initial, current, and final positions, but how do I set them when the transition occurs? All of the examples I have seen for QState::assignProperty() use essentially a constant for the QVariant, not some value that changes dynamically like a mouse position.
Should I derive custom QState classes and reimplement the QState:nEntry() method? Or should I connect to the QEventTransition::triggered() signals and somehow get back to the QEvent instance to pull out the data?
It would seem this is a common problem, but I haven't found an example (or answer here on the forum) that addresses this issue.
Can anyone give me some help, please? Simply a link to some example code would be sufficient. Thanks in advance.
Edit: a secondary question - is it possible explicitly set up a transition from a state back into itself? For example, ZoomTrack needs to be re-entered each time the mouse moves so the current mouse position can be updated (and a new rubberband drawn). Can I add a transition from ZoomTrack to ZoomTrack on QEvent::MouseMove?
Bookmarks