In this Developer's Notebook, I want to share how I arrived at the new code 
to place the "Chapters" dropdown at the left of the Iconbar instead of the 
usual right.  Mainly there are a couple of tricks that I have found useful 
and would like to pass on to others who haven't come across them.

As background, a user had asked if this could be done.  I have written a 
few Leo plugins and a bit of infrastructure (e.g., the highlighting of the 
current line in the body), so I have some general knowledge about Leo.  But 
I don't know much about most of the details.  One helpful skills for a Leo 
developer is finding a good starting point.  All the Leo code can be found 
in the LeoPyRef outline.

@edward made a comment on the issue discussion page about the Iconbar being 
involved.  I figured that the Iconbar would be attached somehow to c.frame, 
and so I started searching.  I like to search using the Nav bar because it 
gives results that are easy to scan.  I searched for *frame.ic* and found 
this:

Official Ivars
    c.frame.iconBar

Here was a starting point.  I explored it in my Workbook outline.  
Searching for *iconbar* I found *QtIconBarClass*, which seemed a likely 
find.

Next I looked for the most characteristic thing I could think of, which is 
the label *Chapters:* for the Chapters dropdown list.  I found this:

tt.createControl (defines class LeoQComboBox)
    frame = QtWidgets.QLabel('Chapters: ')

A glance at the code shows that it is here that the label and dropdown are 
created and added to the Iconbar.  They are added with a Qt method: 
addWidget().  Presumably we would like to use something like 
insertWidget(position, 
widget) if there is such a method. So we need to find the Qt class involved 
so we can look up its methods.

In a node in the workbook, I got the class:

f = c.frame
icb = f.iconBar
g.es(type(icb))

Running this with CNTL-b gave as we had surmised earlier <class 
'leo.plugins.qt_frame.LeoQtFrame.QtIconBarClass'>. 

This is a Leo class. Searching for its code, we find this in its 
constructor:

self.w = c.frame.top.iconBar  # A QToolBar

We also find a method addWidget(), which calls the same method on the 
*QToolBar.  
*So we look up *QToolbar* on the internet and look through its API page.  
But there is no method like *insertWidget()*. Here is a very useful trick 
for working with Qt classes - they have many more methods than are listed 
on the API page.  All widgets inherit the methods of the generic Qt Widget, 
and sometimes other specialized methods as well.  The page for the API of a 
widget usually has a link to their inherited methods.  It can be somewhat 
overwhelming because there are so many, but in the list we find 

QAction <https://doc.qt.io/qt-6/qaction.html> *QToolBar::insertWidget(
QAction <https://doc.qt.io/qt-6/qaction.html> **before*, QWidget 
<https://doc.qt.io/qt-6/qwidget.html#QWidget> **widget*)

We can insert our widget before something that is a kind of *action*, but 
what is that and how do we find the right one?  It turns out that the 
QToolbar has a method actions().  Presumably the actions[0] item will be 
the left-most one on the IconBar, but it would be good to check.  We can 
get its type by modifying our test code like this:

f = c.frame
icb = f.iconBar
ibw = icb.w
actions = ibw.actions()
g.es(actions[0])

>From this we learn that our actions[0] item is of type *leoIconBarButton*. 
Searching for its constructor in LeoPyRef  discloses that it has a property 
*text*. By changing the *g.es()* line to 

g.es(actions[0].text)

we see that its label is *script-button*, so it is indeed the first item on 
the IconBar.  So we can change instances of addWidget(w) to 
insertWidget(actions[0], 
w).

This turned out to be the solution (with a little more checking and 
structuring).

I hope these notes will be helpful even if they were a little on  the long 
side.

-- 
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/5dda940c-67a6-48a7-b8cf-836e58e13cf6n%40googlegroups.com.

Reply via email to