In the last post we saw how to toggle our tab on and off.  But when we 
turned it off, our deleted our widget instance.  The next time we turned 
our tab back on, we had to create a new instance of the widget.

Sometimes it is better to keep the widget in existence.  Then we can open a 
new tab that uses our existing widget instance.  This will maintain the 
state of our widget, so that we can pick up where we left off.

In the demo outline from the last post, we create a few constant names:

  WIDGET_NAME
  VISIBLE

We stored them in the log frame's *contentsDict* and used them to check 
whether our tab was available (open) or not:

# Now you see it
if log.contentsDict.get(VISIBLE, False):
    log.deleteTab(TABNAME)
    # Now you don't
    log.contentsDict[VISIBLE] = False
    log.contentsDict[WIDGET_NAME] = None

log.contentsDict[WIDGET_NAME] stores the widget instance.

To maintain our widget instance while our tab is closed, we need to omit 
the line

log.contentsDict[WIDGET_NAME] = None

We also need to track whether the widget instance has already been loaded 
so that we if it has we can just reuse it but if not we can create it. 
 This constant is named LOADED. We also store it in *contentsDict*.  Here 
is the changed code:

log = c.frame.log
TABNAME = 'Pyplot'

VISIBLE = f'{TABNAME}-visible'
LOADED = f'{TABNAME}-loaded'
WIDGET_NAME = f'{TABNAME}-widget'

# If our tab is visible, remove it
# but keep our widget
if log.contentsDict.get(VISIBLE, False):
    log.deleteTab(TABNAME)
    log.contentsDict[VISIBLE] = False
    font = c.config.getColor('font-family')
else:
    # Show our tab, reusing our widget if already loaded
    if log.contentsDict.get(LOADED, False):
        log.createTab(TABNAME,
                      widget = log.contentsDict[WIDGET_NAME],
                      createText = False)
        log.contentsDict[VISIBLE] = True
        log.selectTab(TABNAME)
    else:
        # Create our tab for the first time
        w = PlotWindow()
        log.createTab(TABNAME, widget = w, createText = False)
        log.selectTab(TABNAME)
        log.contentsDict[LOADED] = True
        log.contentsDict[VISIBLE] = True
        log.contentsDict[WIDGET_NAME] = w

Next time we will pass g and c so that we can interact with Leo itself.  We 
will also show how to change some behavior or state depending on whether 
our tab is visible or not.  This is a kind of parlor trick, yet can be 
useful.

On Thursday, April 13, 2023 at 12:08:21 PM UTC-4 Thomas Passin wrote:

> In my first post on apps in tabs, I wrote this:
>
> There are three parts to these "tabbed" apps:
>
> 1. Creating the top-level QWidget;
> 2. Inserting it into a tab and controlling the tab (e.g., toggling it on 
> and off);
> 3. Interacting with the app.
>
> Instead of "Creating the top-level Widget" I could better have written 
> "Designing the top-level widget".  Item 2. is the heart of the tabbed-app 
> approach, and that's what this post is about.
>
> To make it work, we have to:
>
> 1. Find the log frame;
> 2. Create a new tab;
> 3. Insert our top-level QWidget into it;
> 4. Be able to toggle the tab on and off.
>
> Here is how it's done in the demo outline I posted last time. I have 
> inserted explanatory comment into the code.  We start with setting up some 
> useful constants:
>
>
> """A log tab panel for demonstrating plotting techniques."""
> log = c.frame.log   # Get the log frame
> TABNAME = 'Pyplot'
> # We will use these constants when we control the tab later
> WIDGET_NAME = f'{TABNAME}-widget'
> VISIBLE = f'{TABNAME}-visible'
>
> The core of the code is in the toggle() function:
>
> # The function to toggle the tab on and off.  We use the log's
> # contentsDict to hold our own, private variables, since it 
> # is always going to exist.  These private names are very unlikely
> # to be duplicated by any other tab.
> def toggle(log):
>     """Create, show, or hide pyplot tab in Log pane.
>     
>     ARGUMENT
>     log -- the log panel object for this outline.    
>     """
>
>     # Check whether there is a VISIBLE key.  If so,
>     # delete the tab and destroy our widget.
>     if log.contentsDict.get(VISIBLE, False):
>         log.deleteTab(TABNAME)
>         log.contentsDict[VISIBLE] = False
>         w = log.contentsDict[WIDGET_NAME]
>         w.disconnect_all()
>         w.deleteLater()
>         del(log.contentsDict[WIDGET_NAME])
>     else:
>         # Create our widget and embed it in a tab
>         w = PlotWindow()
>         log.createTab(TABNAME, widget = w, createText = False)
>         log.selectTab(TABNAME)
>         log.contentsDict[VISIBLE] = True
>         log.contentsDict[WIDGET_NAME] = w
>
>
> # If this is the first call of toggle(), create our widget's code
> # This will work for (nearly?) any subclass of QWidget.
> # Define the widget's class.  It will be instantiated when
> # toggle(log) runs.
> if not log.contentsDict.get(WIDGET_NAME, None):
>     << Main Widget >>
>
> toggle(log)
>
> Next time: how to toggle the tab on and off without deleting our widget.
>
> On Thursday, April 13, 2023 at 9:27:39 AM UTC-4 Thomas Passin wrote:
>
>> I have re-organized the code a bit to make to easier to read.  It does 
>> the same job  as before.  Also, I have zipped up the outline to make it 
>> easier to get from the browser.
>>
>> More to come soon.
>>
>> On Sunday, April 9, 2023 at 1:14:13 PM UTC-4 Edward K. Ream wrote:
>>
>>> On Sun, Apr 9, 2023 at 7:32 AM Thomas Passin <tbp1...@gmail.com> wrote:
>>>
>>>> Remind me what you mean by an "info item".  Would this be a GitHub 
>>>> issue with a "devInfo" or "Info" tag?
>>>>
>>>
>>> Exactly.
>>>
>>> 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 leo-editor+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/2b42bdaa-b066-4f23-b344-42656c8d0971n%40googlegroups.com.

Reply via email to