<commit_before># -*- coding: utf8 -*-

"""Quickreport

I widget che corrispondono ai vari tipi di parametri possibili. 
Quando in <progetto>.params.py si specifica un tipo per un parametro, cio' che 
viene visualizzato nella gui e' il widget corrispondente qui definito.

==========================================
:version: vedi quickreport.__version.py__
:copyright: <NAME> 2012
:license: ISC
"""

import wx
import wx.lib.masked as  masked
import datetime

from gui_utils import post_evt_param_changed, EVT_PARAM_CHANGED

__all__ = ['text', 'integer', 'boolean', 
           'symple_list', 'droplist', 
           'currency',
           'date', 'period', 
           'month', 'bimester', 'trimester', 'quadrimester', 'semester']


# 'text' type input parameter =================================================
class TextWidget(wx.TextCtrl):
    def __init__(self, *a, **k):
        use_evt_char = k.pop('track_changes', False)
        wx.TextCtrl.__init__(self, *a, **k)
        self._val = ''
        evt = wx.EVT_CHAR if use_evt_char else wx.EVT_KILL_FOCUS
        self.Bind(evt, self.on_change)
    
    def on_change(self, evt):
        evt.Skip()
        val = self.GetValue() 
        if val != self._val:
            self._val = val
            post_evt_param_changed(evt)
    
    def SetValue (self, val):
        self._val = val
        wx.TextCtrl.SetValue(self, val)
        
    def SetBounds(self, val): pass
        
def text(parent, track_changes=False): 
    return TextWidget(parent, track_changes=track_changes)
    
    
# 'integer' type input parameter ===============================================
class IntegerWidget(wx.SpinCtrl):
    def __init__(self, *a, **k):
        wx.SpinCtrl.__init__(self, *a, **k)
        self.Bind(wx.EVT_SPIN, post_evt_param_changed)
        
    def SetBounds(self, bounds): return wx.SpinCtrl.SetRange(self, *bounds)

def integer(parent): return IntegerWidget(parent)


# 'boolean' type input parameter ===============================================
class BooleanWidget(wx.CheckBox):
    def __init__(self, *a, **k):
        wx.CheckBox.__init__(self, *a, **k)
        self.Bind(wx.EVT_CHECKBOX, post_evt_param_changed)
        
    def SetBounds(self, val): pass
    
def boolean(parent): return BooleanWidget(parent)


# 'symple_list' type input parameter ===========================================
class SimpleListWidget(wx.ListBox):
    def __init__(self, *a, **k):
        self.multichoice = k.pop('multichoice')
        if self.multichoice:
            k['style'] = wx.LB_EXTENDED
        wx.ListBox.__init__(self, *a, **k)
        self.Bind(wx.EVT_LISTBOX, post_evt_param_changed)
        
    SetBounds = wx.ListBox.SetItems
    
    def SetValue(self, val):
        if self.multichoice:
            for v in val:
                self.SetStringSelection(v)
        else:
            self.SetStringSelection(val)
            
    def GetValue(self):
        if self.multichoice:
            return [self.GetString(i) for i in self.GetSelections()]
        else:
            return self.GetStringSelection()

class TwoFieldsListWidget(wx.ListBox):
    def __init__(self, *a, **k):
        self.multichoice = k.pop('multichoice')
        if self.multichoice:
            k['style'] = wx.LB_EXTENDED
        wx.ListBox.__init__(self, *a, **k)
        self.ids = []
        self.Bind(wx.EVT_LISTBOX, post_evt_param_changed)
        
    def SetBounds(self, bounds):
        self.ids = [i[0] for i in bounds]
        self.SetItems([i[1] for i in bounds])
    
    def SetValue(self, val):
        if val is None: 
            self.SetSelection(-1)
            return
        if self.multichoice:
            for v in val:
                self.SetSelection(self.ids.index(v))
        else:
            self.SetSelection(self.ids.index(val))
        
    def GetValue(self):
        if self.multichoice:
            return [self.ids[i] for i in self.GetSelections()]
        else:
            return self.ids[self.GetSelection()]

class MultiChoiceListWidget(wx.CheckListBox):
    def __init__(self, *a, **k):
        self.use_id = k.pop('use_id')
        wx.CheckListBox.__init__(self, *a, **k)
        if self.use_id:
            self.ids = []
        self.Bind(wx.EVT_CHECKLISTBOX, self._on_check)
    
    def _on_check(self, evt):
        self.SetSelection(evt.GetSelection())
        post_evt_param_changed(evt)
        
    def SetBounds(self, bounds):
        if self.use_id:
            self.ids = [i[0] for i in bounds]
            self.SetItems([i[1] for i in bounds]) 
        else:
            self.SetItems(bounds)
        
    def SetValue(self, val):
        if val is None: 
            self.SetSelection(-1)
            return
        if self.use_id:
            for v in val:
                self.SetSelection(self.ids.index(v))
        else:
            self.SetStringSelection(val)
        
    def GetValue(self):
        if self.use_id:
            return [self.ids[i] for i in self.GetSelections()]
        else:
            return self.GetStringSelection()
        
def symple_list(parent, use_id=False, multichoice=False): 
    if multichoice and wx.PlatformInfo[1] in ('wxMSW', 'wxGTK'):
        return MultiChoiceListWidget(parent, use_id=use_id)
    if use_id:
        return TwoFieldsListWidget(parent, multichoice=multichoice)
    else:
        return SimpleListWidget(parent, multichoice=multichoice)
        

# 'droplist' type input parameter ==============================================
class DropDownListWidget(wx.ComboBox):
    def __init__(self, *a, **k):
        k['style'] = wx.CB_DROPDOWN|wx.CB_READONLY
        wx.ComboBox.__init__(self, *a, **k)
        self.Bind(wx.EVT_COMBOBOX, post_evt_param_changed)
    
    SetBounds = wx.ComboBox.SetItems
    
class TwoFieldsDropDownWidget(wx.ComboBox):<commit_msg>added "select all" in multichoice list params<commit_after># -*- coding: utf8 -*-

"""Quickreport

I widget che corrispondono ai vari tipi di parametri possibili. 
Quando in <progetto>.params.py si specifica un tipo per un parametro, cio' che 
viene visualizzato nella gui e' il widget corrispondente qui definito.

==========================================
:version: vedi quickreport.__version.py__
:copyright: <NAME> 2012
:license: ISC
"""

import wx
import wx.lib.masked as  masked
import datetime

from gui_utils import post_evt_param_changed, EVT_PARAM_CHANGED

__all__ = ['text', 'integer', 'boolean', 
           'symple_list', 'droplist', 
           'currency',
           'date', 'period', 
           'month', 'bimester', 'trimester', 'quadrimester', 'semester']


# 'text' type input parameter =================================================
class TextWidget(wx.TextCtrl):
    def __init__(self, *a, **k):
        use_evt_char = k.pop('track_changes', False)
        wx.TextCtrl.__init__(self, *a, **k)
        self._val = ''
        evt = wx.EVT_CHAR if use_evt_char else wx.EVT_KILL_FOCUS
        self.Bind(evt, self.on_change)
    
    def on_change(self, evt):
        evt.Skip()
        val = self.GetValue() 
        if val != self._val:
            self._val = val
            post_evt_param_changed(evt)
    
    def SetValue (self, val):
        self._val = val
        wx.TextCtrl.SetValue(self, val)
        
    def SetBounds(self, val): pass
        
def text(parent, track_changes=False): 
    return TextWidget(parent, track_changes=track_changes)
    
    
# 'integer' type input parameter ===============================================
class IntegerWidget(wx.SpinCtrl):
    def __init__(self, *a, **k):
        wx.SpinCtrl.__init__(self, *a, **k)
        self.Bind(wx.EVT_SPIN, post_evt_param_changed)
        
    def SetBounds(self, bounds): return wx.SpinCtrl.SetRange(self, *bounds)

def integer(parent): return IntegerWidget(parent)


# 'boolean' type input parameter ===============================================
class BooleanWidget(wx.CheckBox):
    def __init__(self, *a, **k):
        wx.CheckBox.__init__(self, *a, **k)
        self.Bind(wx.EVT_CHECKBOX, post_evt_param_changed)
        
    def SetBounds(self, val): pass
    
def boolean(parent): return BooleanWidget(parent)


# 'symple_list' type input parameter ===========================================
class SimpleListWidget(wx.ListBox):
    def __init__(self, *a, **k):
        self.multichoice = k.pop('multichoice')
        self.select_all = k.pop('select_all', '')
        if self.multichoice:
            k['style'] = wx.LB_EXTENDED
        else:
            self.select_all = '' # just in case of user's mistake
        wx.ListBox.__init__(self, *a, **k)
        funct = self.on_select if self.select_all else post_evt_param_changed
        self.Bind(wx.EVT_LISTBOX, funct)
        self._all_selected = False
    
    def on_select(self, evt): # used only in case of self.select_all
        if evt.GetSelection() == 0:
            for i in self.GetItems():
                self.SetStringSelection(i, (not self._all_selected))
            self._all_selected = not self._all_selected
        post_evt_param_changed(evt)
        
    def SetBounds(self, bounds):
        if self.select_all: 
            bounds = [self.select_all] + list(bounds)
        self.SetItems(bounds)
    
    def SetValue(self, val):
        if val is None: 
            self.SetSelection(-1)
            return
        if self.multichoice:
            strings = self.GetStrings()
            if val == 'all':
                val = strings
            elif val == 'none':
                val = []
            for v in strings:
                self.SetStringSelection(v, (v in val))
        else:
            self.SetStringSelection(val)
            
    def GetValue(self):
        if self.multichoice:
            if self.select_all: self.SetSelection(0, False)
            return [self.GetString(i) for i in self.GetSelections()]
        else:
            return self.GetStringSelection()

class TwoFieldsListWidget(wx.ListBox):
    def __init__(self, *a, **k):
        self.multichoice = k.pop('multichoice')
        self.select_all = k.pop('select_all', '')
        if self.multichoice:
            k['style'] = wx.LB_EXTENDED
        else:
            self.select_all = '' # just in case of user's mistake
        wx.ListBox.__init__(self, *a, **k)
        self.ids = []
        self._all_selected = False
        funct = self.on_select if self.select_all else post_evt_param_changed
        self.Bind(wx.EVT_LISTBOX, funct)
        
    def on_select(self, evt): # used only in case of self.select_all
        if evt.GetSelection() == 0:
            for i in self.GetItems():
                self.SetStringSelection(i, (not self._all_selected))
            self._all_selected = not self._all_selected
        post_evt_param_changed(evt)
        
    def SetBounds(self, bounds):
        if self.select_all: 
            bounds = [[None, self.select_all]] + list(bounds)
        self.ids, items = zip(*bounds)
        self.SetItems(items)
    
    def SetValue(self, val):
        if not val: 
            self.SetSelection(-1)
            return
        if self.multichoice:
            if val == 'all':
                val = self.ids
            elif val == 'none':
                val = []
            for n, v in enumerate(self.ids):
                self.SetSelection(n, (v in val))
        else:
            self.SetSelection(self.ids.index(val))
        
    def GetValue(self):
        if self.multichoice:
            if self.select_all: self.SetSelection(0, False)
            return [self.ids[i] for i in self.GetSelections()]
        else:
            return self.ids[self.GetSelection()]

class MultiChoiceListWidget(wx.CheckListBox):
    def __init__(self, *a, **k):
        self.use_id = k.pop('use_id')
        self.select_all = k.pop('select_all', '')
        wx.CheckListBox.__init__(self, *a, **k)
        if self.use_id:
            self.ids = []
        self.Bind(wx.EVT_CHECKLISTBOX, self._on_check)

    def _on_check(self, evt):
        item = evt.GetSelection()
        if self.select_all and (item == 0):
            check = self.IsChecked(0)
            for n, i in enumerate(self.GetItems()[1:]):
                self.Check(n+1, check)
        self.SetSelection(item)
        post_evt_param_changed(evt)
        
    def SetBounds(self, bounds):
        if self.use_id:
            if self.select_all:
                bounds = [[None, self.select_all]] + list(bounds)
            self.ids, items = zip(*bounds)
            self.SetItems(items)
        else:
            if self.select_all: 
                bounds = [self.select_all] + list(bounds)
            self.SetItems(bounds)
        
    def SetValue(self, val):
        if not val: 
            self.SetSelection(-1)
            return
        elif val == 'none':
            val = []
        if self.use_id:
            if val == 'all':
                val = self.ids
            for n, v in enumerate(self.ids):
                self.Check(n, (v in val))
        else:
            strings = self.GetStrings()
            if val == 'all':
                val = strings
            for n, v in enumerate(strings):
                self.Check(n, (v in val))
        
    def GetValue(self):
        if self.use_id:
            return [n+1 for n, i in enumerate(self.ids[1:]) if self.IsChecked(i)]
        else:
            res = list(self.GetCheckedStrings())
            try: res.remove(self.select_all)
            except ValueError: pass
            return res
        
def symple_list(parent, use_id=False, multichoice=False, select_all=False): 
    if multichoice and wx.PlatformInfo[1] in ('wxMSW', 'wxGTK'):
        return MultiChoiceListWidget(parent, use_id=use_id, select_all=select_all)
    if use_id:
        return TwoFieldsListWidget(parent, multichoice=multichoice, select_all=select_all)
    else:
        return SimpleListWidget(parent, multichoice=multichoice, select_all=select_all)
        

# 'droplist' type input parameter ==============================================
class DropDownListWidget(wx.ComboBox):
    def __init__(self, *a, **k):
        k['style'] = wx.CB_DROPDOWN|wx.CB_READONLY
        wx.ComboBox.__init__(self, *a, **k)
        self.Bind(wx.EVT_COMBOBOX, post_evt_param_changed)
    
    SetBounds = wx.ComboBox.SetItems
    
class TwoFieldsDropDownWidget(wx.ComboBox):
{'fraction_non_alphanumeric': 0.14615546071503532, 'fraction_numerical': 0.002168893864129294, 'mean_word_length': 2.2905156537753224, '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': '5739335', 'n_tokens_mistral': 4455, 'n_tokens_neox': 4434, 'n_words': 994}