In order to set an object property depending on a transition I need two intermediate states that immediately transition further:
A initial state
Ta Tb different transitions
Aa Ab (these are only used to set an objects property depending on transition)
TB TB both should immediately "transit"
B objects property used via entered()/exited() signals
(Alternatively, B probably could be duplicated to set each respective property directly.)
Could a state's entered() signal be used as its own transition source?
Simply add an unconditional transition to the state:
TB->addTransition(B);
This is idiomatic and also cheaper than using a QSignalTransition coupled to the entered() signal.
Ref: http://doc.qt.io/qt-5/statemachine-api.html#targetless-transitions
Yes, the QState's entered() signal can be used as transition source.
Related
I need create another new Entity in entity preFlush event in case when current entity was changed. In preFlush I don't have computed change set, if I trigger compute change set, to avoid infinite loop preFlush listener should be disabled.
How can I do that? Does another solution to know if entity was really changed exists?
How many event queues are there in Qt application? Events like click etc are enqueued in the queue. And you can also enque events using function like postEvent. Are all these events in the same queue? if yes is there a way to define different queues?
Normally, there is at most one event queue per each thread.
It is possible to have additional event queues, with two implementation strategies that differ on whether you remain compatible with QCoreApplication::postEvent.
If you don't care about QCoreApplication::postEvent working with your queue, everything is up to you. You can implement the queue in whatever way you wish. Note that Qt doesn't implement a way of marking a QObject as belonging to a particular event queue. When you're ready to deliver the events from your queue, you simply invoke QCoreApplication::notify, passing it the target object pointer, and event pointer. Easy-peasy. You don't have any control over the events that are delivered to any and all objects from the default queue, of course.
Another way is to remain compatible with QCoreApplication::postEvent. Namely, you somehow "mark" an object as having its events handled by your queue. You intercept the event about to be delivered to the target, enqueue it, and handle it yourself as needed.
This is the QStateMachine-like hack. It is good. And bad. And things in between. It's worth knowing how it's done and why.
Hierarchical state machines typically need to control the delivery of events and inject their own events into the queue, in front of other events. This is so that the state transition events are delivered in right order in relation to the events that cause the transitions; furthermore sometimes a transition-causing event might need to be delivered multiple times, retained for later delivery, etc.
This is all in the face of the rigid event lifetime enforced by the default event dispatching system. To work around it, QStateMachine implements its own event queue. Whenever you declare a transition on an event that would be delivered to some object, the state machine installs itself as an event filter on that object.
Then, whenever the original event reaches the target object, the filter intercepts the event and prevents its delivery to the target object. Now it must make a copy of the event, and insert it into its own queue. The copy must be made, because the event will be deleted by the event dispatcher as soon as the control leaves the event filter and/or the target object's event() method.
Unfortunately, before Qt 6, QEvents were not cloneable - at least not publicly so. There was some clone functionality hidden in Qt's innards, usable by user code, but it was a private API, and not based on the virtual copy constructor idiom.
Qt 6 has added the QEvent::clone method, and events should be presumed cloneable. Custom events in legacy code that wasn’t ported to Qt 6 won’t support this, and their clones will not be fully functional if they carry data.
Using the filter-based idiom/hack, you can implement a notion of a QObject belonging to a certain event queue. When your queue filters the events on the target object, clones them, and enqueues them for separate delivery, the object functionally resides on your event queue. When no such filtering takes place, the object resides on the default per-thread queue. You can also only intercept events of the type(s) your queue is interested in, just like the QStateMachine does.
In the documentation of Ember.StateManager it's said that : "Inside of an action method the given state should delegate goToState calls on its StateManager". Does it mean that if I send an action message, I necessarily need to transit to another state. Is it possible to stay in the same state but doing some task by sending an action ? For example, I'm in a state "loading" and I run two actions "preprocess" and "display".
Very simply: an action message may but does not have to transition to another state.
Something you didn't ask, but is related and important: it is a bad idea and bad design to call goToState in an enter or exit method.
When dealing with statecharts in general, you can do whatever you want. It's not mandatory to switch states in an event handler. A common case would be an event handler that shows a cancel/save dialog. You can easily put the dialog on the page in the event handler, and proceed accordingly depending on which button is pressed.
A separate question is should every event handler basically just go to another state. In the above scenario, you can certainly go to a "confirm" state, the state-enter method will show the dialog, and there would be two handlers, one for each button. Those handler would in turn go to other states.
Both design choices I think are equally valid, at least in that scenario. If you choose to implement a separate state for every action, you will end up with a lot of small but concise states. If you choose to do stuff in the event handlers themselves, your states will be bigger, but there will be less of them.
One thing I will say is if an event handler is getting complicated, you are probably better of with a new state. Also, be consistent.
For you specific scenario, if I'm reading it right, you want to load data and then change the display to show the data, based on an event. In this case, I would use new states.
So you press a button that starts the process
In the event handler, go to some sort of 'MyDataSection' state
Initial substate is 'loadData'
Enter state method of 'loadData' starts the loading process
Event handler 'dataLoaded' in 'loadData' to handle when the data loads; this means you need to fire an event when the data loads
'dataLoaded' event goes to the 'show' state
show state shows the view (or removes activity indicator etc) and handles any events that come from the display.
What's good here is that if you have multiple ways to get to this section of the app, all actions that lead to this section only need to go to this state, and everything will always happen the same. Also note that since the view event handlers are on the show state, if the user hits a button while the data is loading, nothing will happen.
I just start to learn ember.js
Why this code http://jsfiddle.net/alexchetv/RVNzP/ don't work properly -- App.MyTextField.change() execution is triggered only after MyTextField loses focus?
Alternative code with the same functionality works as expected.
Keep in mind that handlers on your Ember.Views (change, select, click, etc) are bound to normal HTML DOM events. "onchange" only gets called on an input field when the field loses focus and has changed, not any time the value is modified. You should observe 'value' if you want to be notified of changes to the displayed value.
Here's a working solution.
What I've done is made formDirty a computed value, which recomputes upon changes in the input. Ember, unlike the native "change" event, updates the the Input's value on each keystroke (copy/paste event etc).
Ember's binding makes sure the peopleController is always updated with the input value, thus also updating the computed field formDirty.
Note that if you add more inputs, you must tell the computed property to listen to its events, e.g.
formDirty: function() {
return !Ember.empty(this.get('fName')) && !Ember.empty(this.get('lName'));;
}.property('lName', 'fName').cacheable() // <- dependencies
cacheable() is used for performance sake only, meaning don't computed again until the dependencies change.
I have a rather large application I'm trying to make in Visual-C++, and I am now trying to add undo/redo functionality.
Having a rather large amount of events (button clicks, label text changed, etc.), I would like to find a way to undo/redo without adding code to every function.
For Instance, I would like a class that can read every event done and store it automatically. Then in my undo/redo events, I can just get the latest action stored.
If that is not possible, I would not mind some other way.
Any help?
Declare a class that represent two operations - undo and redo.
Also create two vectors of that class.
For each operation you want to apply undo/redo, push an instance of that class into the undo vector. There should be as many derived classes as there are opreations you want to undo.
For example, if a button click paints the background to green, you create a class instance whose undo metdho paints the background to the previous color, and its redo method paints the background to green, and stuff it into the undo vector.
When you undo - you pop the last class instance and call its undo method, which will paint the background to the previous color. Then you push it to the redo vector.
When you redo, you pop the redo vector for the class instance at the top and invoke its redo method, them stuff it back to the undo vector.
There are some corner cases (boundaries), you'll tackle them when encountered.. :-)
Do all of your events pass through a queue of some kind? by default in c++ there is no queue like this (there is a windows os level event queue, but that is likely managed already and unusable in c++-cli and you did not indicate if this closely mapped onto your problem), there may be some other construct I am unaware of.
If you have some central queue, then it is just a matter of capturing events as they pass through and knowing how to undo each action. If no central queue is present then I see no other way easier than changing each undo-able function to create an undo object of some kind and storing it in and undo queue of some kind.
In a pure .net or a C++ environment without a large central work queue I would make a class that is and undo entry, that implements a method/member function to undo and another to redo/do the work. But for just undo functionality, this could be just a .net delegate or a c style function pointer, and a list of arguments. If you make an action undo/redo class it could be a template or generic that stores pointers/delegates to the the do and undo functions, and a list of arguments from when it was originally called.
These can be run to undo the actions that have been done. They will be inserted into a queue container of some kind, the kind of container doesn't seem to matter as longer as it preserves order, your should pick the best std, .net or other container for your application. You can discard older ones when you no longer need them. When executed the entry last inserted into the queue must be removed to preserve consistency.
If you also need redo functionality, then your queue of actions done must be iterable, and it would be easiest to use the class that was and action had a method/member function that could undo/redo the desired actions. You would have and iterator, pointer, index or marker of some kind indicating how far back you have undone. Every time an undo is requested you must move the marker backward (earlier chronologically) and execute the undo command at that point in the queue. If a redo is requested then the current item indicated executes its redo instruction and then the iterator is advanced forward (chronologically) through the queue, or ignored (I presume) if you are at the forward-most item in the queue.
If you wanted to go off the deep end, which you have no way indicated you want to, you could center you app around the action queue. You might not need to change you functions implementing this approach. Your user interface (I assumed, could just as easily be your API) functions, insert actions (which support doing and undoing) into a queue, and then command the queue to do. You would not have to change your existing functions if their side effects are known and reversible. However, you would need to change all the callers to make actions instead of directly calling, and you would need to write counterparts that do the undoing.
I've tried to achieve something like that in a small experimental library: https://github.com/d-led/undoredo-cpp. It contains an implementation of a TransactionStore similar to what CodeChords man suggested. You might need to really add functionality to each of your undoable objects, and also take care of object lifetimes, in case your actions involve object construction or destruction