Hi,

So I have been studying how to write plugins, and since this seems to be my 
best effort so far I thought I'd give it away.

It is a simple colorizer that uses a yaml based node in your outline to 
color nodes.

<https://lh3.googleusercontent.com/-t1KbXHbGO50/WTv9zkLFo4I/AAAAAAAAANc/Xp5tH_Xu_zsbAFdY9hL2qE5vMOmFaoEJQCLcB/s1600/Capture.PNG>


Notes and room for improvement:


   1. ` delimits multiple headlines with same format
   2. Colors must be hex, without a # preceding
   3. Only two current options, color and font-weight
   4. Of course, there are many other ways to do this..attributes, child 
   nodes, etc
   5. Uses g.app.permanentScriptDict to store yaml files.  They get updated 
   upoon save, which also redraws the outline.
   6. Each outline has its own separate config so there will not be 
   conflicts.

Regards,
Adrian





-- 
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.
''' Manipulates appearance of individual tree widget items based on Yaml file.

Settings are defined in a node labeled "Headline Formats"""
'''
import sys
import leo.core.leoGlobals as g
import leo.core.leoPlugins as leoPlugins
from PyQt5.QtGui import QColor
from PyQt5.QtGui import QBrush
from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QPixmap
import yaml
import time
import datetime

def onCreate (tag, keys):
    '''auto_colorize onCreate handler.'''
 
    try:
        c = keys.get('c')
        init_dict(c)
        g.visit_tree_item.add(colorize) 
    except Exception as e:
        g.es_trace("Could not load commander." + str(e))
def init():
    def on_save(tag, key):
        c = key['c']
        init_dict(c)    
        c.redraw()
    g.registerHandler("save2", on_save)
    g.registerHandler('after-create-leo-frame', onCreate)
    return True
def init_dict(c):
    """ (Re)Initialize the formats dictionary """
    
    cs = str(c)
    try:
        fbody = g.findNodeAnywhere(c, "Headline Formats").b
    except Exception as e:
        g.es_trace("This outline has no Headline Formats node\n" + str(e))
        return
    
    try:        
        formats = yaml.load(fbody)
    except Exception as e:
        g.es_trace("Could not parse Headline Format yaml file\n" + str(e))
        return
        
    try:
        formats = formats["Headline Formats"]        
    except Exception as e:
        g.es_trace("Yaml file does not have proper heading.\n" + str(e))
        return
    
    #preprocess multi headline styles
    g.app.permanentScriptDict[cs + 'formats'] = {}
    
    
    try:
        for k, f in formats.items(): 
            if "`" in k:
                _ = k.split("`")
                for _1 in _:
                    g.app.permanentScriptDict[cs + 'formats'][_1.strip()] = f
            else:
                g.app.permanentScriptDict[cs + 'formats'][k] = f         
    except Exception as e:
        g.es_error(e)      
        pass
def colorize(c,p, item):
    """Colorize by reading "Headline Formats" node, or symbol in headline"""
    cs = str(c)
    font = item.font(0)      
    try:
        g.app.permanentScriptDict[cs  + 'formats']
    except:
        g.app.permanentScriptDict[cs + 'formats'] = {}
    for k, f in g.app.permanentScriptDict[cs + 'formats'].items():
        def format_one(f): 
            #color
            try:
                if f['color']:
                    item.setForeground(0, QBrush(QColor("#" + str(f['color']))))
            except:
                print(item)
                pass
            #weight    
            try:
                if f['font-weight']:
                    font.setBold(True)
            except:
                pass
            #icon
            """ if f['icon']:
                com = c.editCommands                        
                allIcons = com.getIconList(p)                    
                icons = [i for i in allIcons if f['icon'] not in i]
                in_list = False
                for i in icons:
                    print("%s : %s" % (f['icon'], i))
                    if f['icon'] in i:
                        in_list = True
                        break
                
                if in_list != True:
                    com.appendImageDictToList(icons, f['icon_dir'], f['icon'], 1)
                    com.setIconList(p, icons, True)"""
        if k == p.h:
            format_one(f)    
        """ else:
            if "++" in p.h:
                color = "#999999"
            try:
                item.setForeground(0, QBrush(QColor(color)))
            except:
                pass
        """
    item.setFont(0, font)
    



    


Reply via email to