How to tell a Gramps object is being edited?

I am working on an “enhancement” of URL link specification in Notes. Schematically, I break a URL into fragments which are stored as auxiliary data (Notes) of repository, source and citation records. This is an attempt to manage “easily” “internet rot” where even permanent links change over time.

When the mouse hovers over such a fragment, a tooltip pops up to report the closed form of the URL. This is done by querying the database. But, if a fragment is currently edited in a Note window, there is a high probability of a discrepancy between what is edited and what is committed to the DB. In this case, I want to replace the closed form in the tooltip by a warning.

My manipulations are done at very low level because they must work both in GUI and in CLI (report generation; if something goes wrong, the link is replaced by a “message” easily identifiable in the report).

URL closure is computed exclusively by querying the DB. I have then no idea whether the DB record is currently edited or not.

Looking at the code for GUI warning “the object cannot be edited at this time because it is being edited. You need to close it” (see e.g. eventembedlist.py), this warning is issued after raising WindowActiveError. This error itself is emitted in __init__ of ManagedWindow attributes of uistate which an instantiation parameter.

During my DB query, I am not associated at all with any GUI element (window). So, how can I interfere with the ManagedWindow machinery to tell if the Note I am scanning for link fragments is opened in some window?

Schematically, I need to guess a possible “id” (window_key) for my Note and ask if it is known. How can I do this in the simplest possible way?

For primary object editors, the window ID will just be the handle. The GrampsWindowManager instance is available in the DisplayState so you can use code like:

self.uistate.gwm.get_item_from_id(note_handle)

It will return None if the note is not being edited.

Do you mean as can use note_handle just after retrieving it from the DB?

In this case, what is self? My code snippet is a general “utility” not a method of some class of object. I need to “attach” self to some relevant object.

Your utility will have to obtain the DisplayState from somewhere if it needs to know what the GUI is doing (editing a note in this case).

Got it. It works.

But, as an afterthought, my goal is nonsense. If I am hovering my mouse over a candidate fragment, I am ipso facto in an editing window. If I disable my utility function, I contradict my goal of displaying the closed form.

Perhaps, I should only consider Notes which are not my target (the parent fragments necessary to compute the closed form). If it results in too intricate a code, it might be better to drop the idea and just add a caveat in the feature description.

UPDATE:
After experimenting, it looks like there is some cache for regexp match in Note. This cache is not invalidated when UI context changes.

Test scenario:

  1. only the target note is open
    Tooltip shows what is expected
  2. another note containing a required parent fragment is opened
  3. focus goes back to first note
    I expect to be warned of the existence of an edit window on an intermediate required fragment. This is not the case: tooltip is unchanged. If I close and reopen the note, the tooltip is recomputed and shows the warning.

Conclusion: I drop the idea. Since the feature is really advanced, I expect user (me first) to be aware of the limitation.