Listen and respond to events that happen in Krita
When your script is running, there might be certain things you only want to do when a new image is created. Other things you might want to apply only when Krita changes its configuration. Waiting for these special events from Krita is known as Notifiers. Let's do an example of listening for changes in the configuration.
We wrote out a little more code than we probably need, but it is there to explain how the code works. If you run this code in scripter, you will see a dialog appear and a label that says "new label". Open up Krita's configuration window, make a change, then press ok to get out of it. You will see the label's text change. You will not see the print statement in the output though like we have seen in the past. The notifier did work, though, since we saw the label change at the right time.
Under the hood Notifiers are just signals, which we have already gone over previously. They are just special "Krita signals" that are tied to events in the application. When we put the above code in a dialog, the script keeps listening and running. If you run code that does not have a user interface, or finishes quickly, there is a good change these notifiers will not be picked up. This is an important thing to note when you use notifiers.
You must also make the notifier active before it will send signals. By default notifiers are not active, so nothing will happen if you try to make a connection without setting it active first.
Here is another code snippet with how we can know when new views are added or removed. If you aren't sure what a view is, make sure to check out the Documents lesson for clarification. These notifications can be good for determining if tools should be active, or if any other cleaning up needs to be done.
I added connections for both adding and removing views. When you open the dialog and add and remove documents, the label text will update. These notifier slots have parameters that come with it. When a view is created, it also sends you the new view itself.
While the Notifier object has some signals that you can listen to, there are signals in other areas as well that can be easy to miss. For example if you want to listen for when the theme changes, you do that from the window object like this.
To see a full list of what the Notifier class can do, check out the documentation . If you move around that documentation site a bit, look for other events that might be useful in the Document or Window classes. The events are placed in areas that relate to the classes. This is why the different events are in different locations.
Head to the Krita artists where we have a dedicated area for plugin development and give any feedback you might have.