Here is a tweaked version of the highlight script.  I added a setting to 
specify a highlight color - *@string line-highlight-color.* This string can 
be any CSS color name that Qt recognizes, such as *black, cyan, lightgrey*, 
etc.  Or it can be a *#xxxxxx*  CSS color number such as *#839496*.  If the 
setting contains an invalid color string, the script will ignore it and 
compute a highlight color.

I also brightened up the highlight color a bit for very dark backgrounds.

As before, to activate the line highlighting, select the top level node and 
press CTRL-b to run it.  After this, the currently selected line in the 
outline should be highlighted.  To try it in another outline, copy the 
entire script to the other outline and activate it.  To check out the color 
setting, add or change the *@string line-highlight-color *setting and 
reload settings.
On Saturday, August 28, 2021 at 3:01:34 PM UTC-4 [email protected] wrote:

> Here are screen shots for the line highlighting for nine of Leo's themes.  
> I got them by starting Leo with *--theme=<theme file> * and pressing 
> CTRL-B after selecting the script's top-level node.  The highlight color 
> may seem a little faint for the very dark themes, and maybe it should be 
> changed in some way.  Don't forget, though, that with the very dark themes 
> one's eyes will be used to seeing the dark screen, and the highlight color 
> will stand out more than appears in these little swatches.
>
> The basic approach for selecting the highlight color is that it should 
> have the same hue as the theme's background color, but a lighter or darker 
> luminosity value.  Otherwise we would have to figure out a way to change 
> the hue, and that is no simple problem if it is to be done harmoniously.
>
> On Saturday, August 28, 2021 at 2:00:07 PM UTC-4 [email protected] wrote:
>
>> Odd, I tried it out on every theme in Leo's theme directory and 
>> highlighting showed up on all of them, even the darkest.  An earlier 
>> version didn't show on very dark themes, so I changed the way the highlight 
>> color is calculated.
>>
>> Well, we'll get that sorted out. I agree, it would be better for 6.5 
>> instead of trying to get something so new into 6.4.
>>
>> I have a technical question.  The code I'm using gets the fg and bg 
>> colors from *c.active_stylesheet*. Can this ever change during a Leo 
>> session? I would rather do it once during initialization than each time the 
>> cursor moves.  It seems to me that the active stylesheet won't change 
>> unless the theme is changed, but currently the theme can't be changed 
>> without closing Leo and restarting.
>>
>> On Saturday, August 28, 2021 at 11:30:50 AM UTC-4 Edward K. Ream wrote:
>>
>>> On Fri, Aug 27, 2021 at 4:36 PM [email protected] <[email protected]> 
>>> wrote:
>>>
>>>> The attached Leo outline contains a script to highlight the line in the 
>>>> body that has the cursor.  You can activate it by selecting the top-most 
>>>> node, then pressing CTRL-b.  
>>>
>>>
>>> Thanks for this work.  I didn't see any highlighting, but then I'm using 
>>> a dark theme.
>>>
>>> I'd like to add this feature early in the 6.5 development cycle. I would 
>>> rather not complicate the 6.4 release work.
>>>
>>> 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/a98e9bb2-b088-47b7-a94f-b76aed97e367n%40googlegroups.com.
<?xml version="1.0" encoding="utf-8"?>
<!-- Created by Leo: http://leoeditor.com/leo_toc.html -->
<leo_file xmlns:leo="http://leoeditor.com/namespaces/leo-python-editor/1.1"; >
<leo_header file_format="2"/>
<globals/>
<preferences/>
<find_panel_settings/>
<vnodes>
<v t="tom.20210827170535.1"><vh>Highlight Current Line</vh>
<v t="tom.20210827170535.2"><vh>highlightCurrentLine</vh>
<v t="tom.20210827170535.3"><vh>parse_css</vh></v>
<v t="tom.20210827170535.4"><vh>assign_bg</vh></v>
<v t="tom.20210827170535.5"><vh>calc_hl</vh></v>
</v>
</v>
</vnodes>
<tnodes>
<t tx="tom.20210827170535.1">@language python
from leo.core.leoQt import QtGui
QColor = QtGui.QColor
FullWidthSelection = 0x06000 # works for both Qt5 and Qt6

w = c.frame.body.wrapper
editor = w.widget

@others

editor.cursorPositionChanged.connect(highlightCurrentLine)</t>
<t tx="tom.20210827170535.2">@language python
def highlightCurrentLine():
    """Highlight cursor line.
    
    Based in part on code from
    https://doc.qt.io/qt-5/qtwidgets-widgets-codeeditor-example.html
    """

    ssm = c.styleSheetManager
    sheet = ssm.expand_css_constants(c.active_stylesheet)

    bg = c.config.getString('line-highlight-color') or ''
    hl_color = QColor(bg)
    if hl_color.getHsv() == (0, 0, 0, 255) and bg != 'black':
        # Always returns black for an invalid color
        # So compute the color instead
        fg, bg = parse_css(sheet, 'QTextEdit')
        bg_color = QColor(bg) if bg else assign_bg(fg)
        hl_color = calc_hl(bg_color)

    selection = editor.ExtraSelection()
    selection.format.setBackground(hl_color)
    selection.format.setProperty(FullWidthSelection, True)
    selection.cursor = editor.textCursor()
    selection.cursor.clearSelection()

    editor.setExtraSelections([selection])
</t>
<t tx="tom.20210827170535.3">@language python
def parse_css(css_string, clas=''):
    """Extract colors from a css stylesheet string. 
    
    This is an extremely simple-minded function. It assumes
    that no quotation marks are being used, and that the
    first block in braces with the name clas is the controlling
    css for our widget.
    
    Returns a tuple of strings (foregound, background).
    """
    # Get first block with name matching "clas'
    block = css_string.split(clas, 1)
    block = block[1].split('{', 1)
    block = block[1].split('}', 1)

    # Split into styles separated by ";"
    styles = block[0].split(';')

    # Split into fields separated by ":"
    fields = [style.split(':') for style in styles if style.strip()]

    # Only get fields whose names are "color" and "background"
    color = bg = ''
    for style, val in fields:
        style = style.strip()
        if style == 'color':
            color = val.strip()
        elif style == 'background':
            bg = val.strip()
    return color, bg

</t>
<t tx="tom.20210827170535.4">@language python
def assign_bg(fg):
    """If fg or bg colors are missing, assign
    reasonable values.  Can happen with incorrectly
    constructed themes, or no-theme color schemes.
    
    RETURNS
    a QColor object for the background color
    """
    if not fg:
        fg = 'black' # QTextEdit default
        bg = 'white' # QTextEdit default
    if fg == 'black':
        bg = 'white' # QTextEdit default
    else:
        fg_color = QColor(fg)
        h, s, v, a = fg_color.getHsv()
        if v &lt; 128: # dark foreground
            bg = 'white'
        else:
            bg = 'black'
    return QColor(bg)
</t>
<t tx="tom.20210827170535.5">@language python
def calc_hl(bg_color):
    """Return the line highlight color.
    
    ARGUMENT
    bg_color -- a QColor object for the background color
    
    RETURNS
    a QColor object for the highlight color
    """
    h, s, v, a = bg_color.getHsv()

    if v &lt; 40:
        v = 60
        bg_color.setHsv(h, s, v, a)
    elif v &gt; 240:
        v = 220
        bg_color.setHsv(h, s, v, a)
    elif v &lt; 128:
        bg_color = bg_color.lighter(130)
    else:
        bg_color = bg_color.darker(130)

    return bg_color
</t>
</tnodes>
</leo_file>

Reply via email to