For the last several days I have been studying the FreeLayoutController and 
NestedSplitter classes, with an eye to understanding how those classes 
collaborate to save and restore layouts.  All the following work is 
background to #1686 <https://github.com/leo-editor/leo-editor/issues/1686>. 
It's not an easy project.

Today I attempted to simplify ns.get_layout and ns.get_saveable_layout. 
Here are the original versions, sans docstrings:

def get_layout(self, _saveable=False):
    ans = {
        'orientation': self.orientation(),
        'content': []
    }
    if not _saveable:
        ans['splitter'] = self
    ans['sizes'] = self.sizes()
    for i in range(self.count()):
        w = self.widget(i)
        if isinstance(w, NestedSplitter):
            ans['content'].append(w.get_layout(_saveable=_saveable))
        else:
            if _saveable:
                ans['content'].append(getattr(w, '_ns_id', 'UNKNOWN'))
            else:
                ans['content'].append(w)
    return ans

def get_saveable_layout(self):
    return self.get_layout(_saveable=True)

Vitalije's trick of removing kwargs simplifies ns.get_layout as follows:

def get_layout(self):
    """
    Return a dict describing the layout.

    Usually you would call ns.top().get_layout()
    """
    def content(w):
        return w.get_layout() if isinstance(w, NestedSplitter) else w
        
    def widgets():
        """Yield an ordered list of the contained widgets."""
        return (self.widget(i) for i in range(self.count()))
    
    return {
        'content': [content(w) for w in widgets()],
        'orientation': self.orientation(),
        'sizes': self.sizes(),
        'splitter': self,
    }

My first thought was to wonder why ns.get_saveable_layout isn't exactly the 
same. The answer is that the dict returned by ns.get_saveable_layout will 
eventually be picked. But Qt widgets can not be pickled.  Instead, here is 
the new version of ns.get_saveable_layout:

def get_saveable_layout(self):
    """
    Return a dict describing the layout.

    Usually you would call ns.top().get_layout()
    """
    def content(w):
        return w.get_saveable_layout() if isinstance(w, NestedSplitter) else 
getattr(w, '_ns_id', 'UNKNOWN')
        
    def widgets():
        """Yield an ordered list of the contained widgets."""
        return (self.widget(i) for i in range(self.count()))
    
    return {
        'content': [content(w) for w in widgets()],
        'orientation': self.orientation(),
        'sizes': self.sizes(),
    }

The two methods are similar, but ns.get_layout returns a dict containing Qt 
widgets (and includes a 'splitter' key) while ns.get_saveable_layout 
substitutes id *strings* for the actual widgets.

*Summary*

I believe the new code is equivalent to the old. However, it's not clear 
that changing the two methods is worthwhile.

Anyway, I now understand what is going on in much more detail.

I will eventually make at least one change. ns.layout_to_text is buggy and 
should be replaced by g.printObj.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/19f08b98-0a6e-48ce-b742-686836a04be9o%40googlegroups.com.

Reply via email to