<commit_before>"""
Generic property list frame that can be used for any object's properties.

Makes a frame and allows the caller to add properties of various types to it.

$Id$
"""
__version__='$Revision$'

from Tkinter import Frame, StringVar, Message, Label, IntVar, N,S,E,W,X, NORMAL
import tkFont
from translatorwidgets import TaggedSlider,EntryTranslator,ComboBoxTranslator,CheckbuttonTranslator


class PropertiesFrame(Frame):
    """
    GUI window for displaying and manipulating the properties of an
    object.  This class is very general, and could be used for
    manipulating any object that has enumerable properties.
    """
    def __init__(self, parent=None, padding=2,**config):
        self.parent = parent
        self.properties = {}
        self.padding = padding
        Frame.__init__(self,parent,config)


    def optional_refresh(self,event=None):
        try:
            if self.parent.auto_refresh: self.parent.refresh()
        except AttributeError:
            pass
        


    def add_property(self,name,var,control,value):
        p = Message(self,text=name,aspect=5000)
        p.grid(row=len(self.properties),column=0,padx=self.padding,
               pady=self.padding,sticky=E)
        control.grid(row=len(self.properties),
                     column=1,
                     padx=self.padding,
                     pady=self.padding,
                     sticky=N+S+W+E)

        self.properties[name] = var
        self.properties[name].set(value)
        return (p,control)


    def add_text_property(self,name,value="",readonly=False,**kw):
        """
        Create a TKInter.Entry box and add it to self.properties, unless readonly is
        True - in which case create a Tkinter.Label.
        """
        var = StringVar()

        if readonly:
            control = Label(self,
                            textvariable = var,
                            font=tkFont.Font(weight=tkFont.NORMAL),
                            **kw)
        else:
            control = EntryTranslator(self,
                                textvariable = var,
                                **kw)
            control.bind('<Return>', self.optional_refresh)
            
        return self.add_property(name,var,control,value)

        
    def add_tagged_slider_property(self, name, value, **kw):
        """
        Create a TaggedSlider and add it to self.properties.
        """
        var = StringVar()
        var.set(value)
        control = TaggedSlider(self,
                               tagvariable=var,
                               **kw)
        return self.add_property(name,var,control,value)


    def add_combobox_property(self,name,value='',**kw):
        """
        Create a ComboBox that can convert its string variable using the given translator and add it to self.properties.
        """        
        var = StringVar()
        control = ComboBoxTranslator(self,
                               selectioncommand=(lambda value: self.properties[name].set(value)),**kw)
                
        control.selectitem(value)
        control.bind('<Return>', self.optional_refresh)
        return self.add_property(name,var,control,value)


    def add_checkbutton_property(self,name,value=0,**kw):
         """
         """
         var = IntVar()
         control = CheckbuttonTranslator(self, variable=var, **kw)
         return self.add_property(name,var,control,value)


    # CEBHACKALERT: currently unused, but could be used in the future to set the
    # properties from a dictionary (see CEBHACKALERT in ParametersFrame).
##     def set_values(self,values):
##         for (name,value) in values.items():
##             self.properties[name].set(value)

        <commit_msg>Added HACKALERT about binding return key to combobox widget.<commit_after>"""
Generic property list frame that can be used for any object's properties.

Makes a frame and allows the caller to add properties of various types to it.

$Id$
"""
__version__='$Revision$'

from Tkinter import Frame, StringVar, Message, Label, IntVar, N,S,E,W,X, NORMAL
import tkFont
from translatorwidgets import TaggedSlider,EntryTranslator,ComboBoxTranslator,CheckbuttonTranslator


class PropertiesFrame(Frame):
    """
    GUI window for displaying and manipulating the properties of an
    object.  This class is very general, and could be used for
    manipulating any object that has enumerable properties.
    """
    def __init__(self, parent=None, padding=2,**config):
        self.parent = parent
        self.properties = {}
        self.padding = padding
        Frame.__init__(self,parent,config)


    def optional_refresh(self,event=None):
        try:
            if self.parent.auto_refresh: self.parent.refresh()
        except AttributeError:
            pass
        


    def add_property(self,name,var,control,value):
        p = Message(self,text=name,aspect=5000)
        p.grid(row=len(self.properties),column=0,padx=self.padding,
               pady=self.padding,sticky=E)
        control.grid(row=len(self.properties),
                     column=1,
                     padx=self.padding,
                     pady=self.padding,
                     sticky=N+S+W+E)

        self.properties[name] = var
        self.properties[name].set(value)
        return (p,control)


    def add_text_property(self,name,value="",readonly=False,**kw):
        """
        Create a TKInter.Entry box and add it to self.properties, unless readonly is
        True - in which case create a Tkinter.Label.
        """
        var = StringVar()

        if readonly:
            control = Label(self,
                            textvariable = var,
                            font=tkFont.Font(weight=tkFont.NORMAL),
                            **kw)
        else:
            control = EntryTranslator(self,
                                textvariable = var,
                                **kw)
            control.bind('<Return>', self.optional_refresh)
            
        return self.add_property(name,var,control,value)

        
    def add_tagged_slider_property(self, name, value, **kw):
        """
        Create a TaggedSlider and add it to self.properties.
        """
        var = StringVar()
        var.set(value)
        control = TaggedSlider(self,
                               tagvariable=var,
                               **kw)
        return self.add_property(name,var,control,value)


    def add_combobox_property(self,name,value='',**kw):
        """
        Create a ComboBox that can convert its string variable using the given translator and add it to self.properties.
        """        
        var = StringVar()
        control = ComboBoxTranslator(self,
                               selectioncommand=(lambda value: self.properties[name].set(value)),**kw)
                
        control.selectitem(value)

        # CEBHACKALERT: this doesn't work for PMW. How do we bind the return
        # key to a PMW combobox? I couldn't figure it out.
        control.bind('<Return>', self.optional_refresh)
        return self.add_property(name,var,control,value)


    def add_checkbutton_property(self,name,value=0,**kw):
         """
         """
         var = IntVar()
         control = CheckbuttonTranslator(self, variable=var, **kw)
         return self.add_property(name,var,control,value)


    # CEBHACKALERT: currently unused, but could be used in the future to set the
    # properties from a dictionary (see CEBHACKALERT in ParametersFrame).
##     def set_values(self,values):
##         for (name,value) in values.items():
##             self.properties[name].set(value)

        
{'fraction_non_alphanumeric': 0.0973092998955068, 'fraction_numerical': 0.0020898641588296763, 'mean_word_length': 2.0738659173022884, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5424154', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2033, 'n_words': 518}