source: sasview/src/sas/sasgui/guiframe/utils.py @ 0c10782

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 0c10782 was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[0d9dae8]1"""
[d955bf19]2Contains common classes and functions
[0d9dae8]3"""
[32c0841]4import wx
5import re
[0d9dae8]6
[f444b20]7def parse_name(name, expression):
8    """
9    remove "_" in front of a name
10    """
11    if re.match(expression, name) is not None:
12        word = re.split(expression, name, 1)
13        for item in word:           
14            if item.lstrip().rstrip() != '':
15                return item
16    else:
17        return name
[0d9dae8]18def format_number(value, high=False):
19    """
[d955bf19]20    Return a float in a standardized, human-readable formatted string
[0d9dae8]21    """
22    try: 
23        value = float(value)
24    except:
[32c0841]25        output = "NaN"
[4ba846b]26        return output.lstrip().rstrip()
[0d9dae8]27    if high:
[21969a4a]28        output = "%-7.5g" % value
[0d9dae8]29    else:
[32c0841]30        output = "%-5.3g" % value
[4ba846b]31    return output.lstrip().rstrip()
[31f3f661]32
[74ad4b1]33def check_float(item):
34    """
[d955bf19]35    :param item: txtcrtl containing a value
[74ad4b1]36    """
[32c0841]37    flag = True
[74ad4b1]38    try:
39        mini = float(item.GetValue())
40        item.SetBackgroundColour(wx.WHITE)
41        item.Refresh()
42    except:
43        flag = False
44        item.SetBackgroundColour("pink")
45        item.Refresh()
46    return flag
[a208464]47
[0d9dae8]48   
49class PanelMenu(wx.Menu):
[d955bf19]50    """
51    """
[0d9dae8]52    plots = None
53    graph = None
54   
55    def set_plots(self, plots):
[d955bf19]56        """
57        """
[0d9dae8]58        self.plots = plots
59   
60    def set_graph(self, graph):
[d955bf19]61        """
62        """
[2bd9927]63        self.graph = graph
64       
65
66def split_list(separator, mylist, n=0):
67    """
[d955bf19]68    returns a list of string without white space of separator
69   
70    :param separator: the string to remove
71   
[2bd9927]72    """
[32c0841]73    list = []
[2bd9927]74    for item in mylist:
[32c0841]75        if re.search(separator,item)!= None:
76            if n > 0:
77                word = re.split(separator, item, int(n))
[2bd9927]78            else:
[32c0841]79                word = re.split(separator, item)
[2bd9927]80            for new_item in word: 
[32c0841]81                if new_item.lstrip().rstrip() != '':
[2bd9927]82                    list.append(new_item.lstrip().rstrip())
83    return list
[d955bf19]84
[2bd9927]85def split_text(separator, string1, n=0):
86    """
[d955bf19]87    return a list of string without white space of separator
88   
89    :param separator: the string to remove
90   
[2bd9927]91    """
[32c0841]92    list = []
93    if re.search(separator, string1) is not None:
94        if n > 0:
95            word = re.split(separator,string1,int(n))
[2bd9927]96        else:
[32c0841]97            word = re.split(separator,string1)
[2bd9927]98        for item in word: 
[32c0841]99            if item.lstrip().rstrip() != '':
[2bd9927]100                list.append(item.lstrip().rstrip())
101    return list
[d955bf19]102
103def look_for_tag(string1, begin, end=None):
[2bd9927]104    """
[d955bf19]105    this method  remove the begin and end tags given by the user
106    from the string .
107   
108    :param begin: the initial tag
109    :param end: the final tag
110    :param string: the string to check
111   
112    :return: begin_flag==True if begin was found,
113     end_flag==if end was found else return false, false
114     
[2bd9927]115    """
[32c0841]116    begin_flag = False
117    end_flag = False
118    if  re.search(begin,string1) is not None:
119        begin_flag = True
120    if end  is not None:
121        if  re.search(end,string1) is not None:
122            end_flag = True
[2bd9927]123    return begin_flag, end_flag
[32c0841]124
[6f16e25]125class IdList:
126    """
127    Create a list of wx ids that can be reused.
128
129    Ids for items need to be unique within their context.  In a dynamic
130    application where the number of ids needed different each time the
131    form is created, depending for example, on the number of items that
132    need to be shown in the context menu, you cannot preallocate the
133    ids that you are going to use for the form.  Instead, you can use
134    an IdList, which will reuse ids from context to context, adding new
135    ones if the new context requires more than a previous context.
136
137    IdList is set up as an iterator, which returns new ids forever
138    or until it runs out.  This makes it pretty useful for defining
139    menus::
140
141        class Form(wx.Dialog):
142            _form_id_pool = IdList()
143            def __init__(self):
144                ...
145                menu = wx.Menu()
146                for item, wx_id in zip(menu_items, self._form_id_pool):
147                    name, description, callback = item
148                    menu.Append(wx_id, name, description)
149                    wx.EVT_MENU(self, wx_id, callback)
150                ...
151
152    It is a little unusual to use an iterator outside of a loop, but it is
153    supported. For example, when defining a form, your class definition
154    might look something like::
155
156        class Form(wx.Dialog):
157            _form_id_pool = IdList()
158            def __init__(self, pairs, ...):
159                ids = iter(_form_id_pool)
160                ...
161                wx.StaticText(self, ids.next(), "Some key-value pairs")
162                for name, value in pairs:
163                    label = wx.StaticText(self, ids.next(), name)
164                    input = wx.TextCtrl(self, ids.next(), value=str(value))
165                    ...
166                ...
167
168    If the dialog is really dynamic, and not defined all in one place, then
169    save the id list iterator as *self._ids = iter(_form_id_pool)* in the
170    constructor.
171
172    The wx documentation is not clear on whether ids need to be unique.
173    Clearly different dialogs can use the same ids, as this is done for the
174    standard button ids such as wx.ID_HELP.  Presumably each widget on the
175    form needs its own id, but whether these ids can match the ids of menu
176    items is not indicated, or whether different submenus need their own
177    ids.  Using different id lists for menu items and widgets is safest,
178    but probably not necessary.  And what about notebook tabs.  Do the
179    ids need to be unique across all tabs?
180    """
181    def __init__(self):
182        self._ids = []
183    def __iter__(self):
184        return _IdListIterator(self)
185    def __getitem__(self, index):
186        while index >= len(self._ids):
187            self._ids.append(wx.NewId())
188        return self._ids[index]
189
190class _IdListIterator:
191    def __init__(self, id_list):
192        self.id_list = id_list
193        self.index = -1
194    def next(self):
195        self.index += 1
196        return self.id_list[self.index]
197
Note: See TracBrowser for help on using the repository browser.