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