1 | """ |
---|
2 | Panel class to show the model history |
---|
3 | """ |
---|
4 | #TODO: when a model change is followed by a parameter change, |
---|
5 | # we end up with the same model twice in a row in the history |
---|
6 | #TODO: implement aging of history items |
---|
7 | |
---|
8 | import wx |
---|
9 | import wx.lib.newevent |
---|
10 | import config |
---|
11 | |
---|
12 | # History events |
---|
13 | (HistoryEvent, EVT_HISTORY) = wx.lib.newevent.NewEvent() |
---|
14 | |
---|
15 | # Maximum number of full history events in the buffer |
---|
16 | MAX_BUFF_LEN = 30 |
---|
17 | |
---|
18 | isWindows = False |
---|
19 | import sys |
---|
20 | if sys.platform.count("win32")>0: |
---|
21 | isWindows = True |
---|
22 | |
---|
23 | if isWindows: |
---|
24 | from ctypes import * |
---|
25 | from ctypes.wintypes import DWORD |
---|
26 | |
---|
27 | SIZE_T = c_ulong |
---|
28 | |
---|
29 | class _MEMORYSTATUS(Structure): |
---|
30 | _fields_ = [("dwLength", DWORD), |
---|
31 | ("dwMemoryLength", DWORD), |
---|
32 | ("dwTotalPhys", SIZE_T), |
---|
33 | ("dwAvailPhys", SIZE_T), |
---|
34 | ("dwTotalPageFile", SIZE_T), |
---|
35 | ("dwAvailPageFile", SIZE_T), |
---|
36 | ("dwTotalVirtual", SIZE_T), |
---|
37 | ("dwAvailVirtualPhys", SIZE_T)] |
---|
38 | def show(self): |
---|
39 | for field_name, field_type in self._fields_: |
---|
40 | print field_name, getattr(self, field_name) |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | class HistoryPanel(wx.Panel): |
---|
46 | """ |
---|
47 | Panel to show history items |
---|
48 | """ |
---|
49 | #TODO: show units |
---|
50 | #TODO: order parameters properly |
---|
51 | ## Internal name for the AUI manager |
---|
52 | window_name = "historyPanel" |
---|
53 | ## Title to appear on top of the window |
---|
54 | window_caption = "historyPanel" |
---|
55 | |
---|
56 | CENTER_PANE = False |
---|
57 | def __init__(self, parent, *args, **kwargs): |
---|
58 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
59 | self.parent = parent |
---|
60 | |
---|
61 | # Listen to history events |
---|
62 | self.parent.Bind(EVT_HISTORY, self.onEVT_HISTORY) |
---|
63 | |
---|
64 | # Internal counter of history items |
---|
65 | self.counter = 0 |
---|
66 | |
---|
67 | # Buffer filled flag |
---|
68 | self.buffer_filled = False |
---|
69 | |
---|
70 | # Set up the layout |
---|
71 | self._set_layout() |
---|
72 | |
---|
73 | # Save current flag |
---|
74 | self.current_saved = False |
---|
75 | |
---|
76 | def _set_layout(self): |
---|
77 | """ |
---|
78 | Set up the layout of the panel |
---|
79 | """ |
---|
80 | self.sizer = wx.GridBagSizer(0,5) |
---|
81 | |
---|
82 | title = wx.StaticText(self, -1, "History", style=wx.ALIGN_LEFT) |
---|
83 | self.sizer.Add(title, (0,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=15) |
---|
84 | |
---|
85 | self.view_box = wx.ListBox(self, 101, wx.DefaultPosition, (295, 200), |
---|
86 | [], wx.LB_SINGLE | wx.LB_HSCROLL) |
---|
87 | |
---|
88 | self.sizer.Add(self.view_box, (1,0), flag=wx.TOP, border=0) |
---|
89 | |
---|
90 | self.SetSizer(self.sizer) |
---|
91 | |
---|
92 | self.Bind(wx.EVT_CONTEXT_MENU, self.onShowPopup, id=101) |
---|
93 | self.Bind(wx.EVT_LISTBOX, self.onSelect, id=101) |
---|
94 | |
---|
95 | def onRemove(self, ev): |
---|
96 | """ |
---|
97 | Remove an item |
---|
98 | """ |
---|
99 | indices = self.view_box.GetSelections() |
---|
100 | if len(indices)>0: |
---|
101 | self.view_box.Delete(indices[0]) |
---|
102 | |
---|
103 | def onRename(self, ev): |
---|
104 | """ |
---|
105 | Rename an item |
---|
106 | """ |
---|
107 | indices = self.view_box.GetSelections() |
---|
108 | if len(indices)>0: |
---|
109 | print "NOT YET IMPLMENTED" |
---|
110 | print "renaming", self.view_box.GetString(indices[0]) |
---|
111 | |
---|
112 | def onShowPopup(self, event): |
---|
113 | """ |
---|
114 | Popup context menu event |
---|
115 | """ |
---|
116 | # menu |
---|
117 | popupmenu = wx.Menu() |
---|
118 | id = wx.NewId() |
---|
119 | popupmenu.Append(id, "&Remove Selected") |
---|
120 | #popupmenu.Append(102, "&Rename Selected") |
---|
121 | wx.EVT_MENU(self, id, self.onRemove) |
---|
122 | #wx.EVT_MENU(self, 102, self.onRename) |
---|
123 | |
---|
124 | pos = event.GetPosition() |
---|
125 | pos = self.ScreenToClient(pos) |
---|
126 | self.PopupMenu(popupmenu, pos) |
---|
127 | |
---|
128 | def onSelect(self, event): |
---|
129 | """ |
---|
130 | Process item selection events |
---|
131 | """ |
---|
132 | index = event.GetSelection() |
---|
133 | view_name = self.view_box.GetString(index) |
---|
134 | view = event.GetClientData() |
---|
135 | |
---|
136 | wx.PostEvent(self.parent, view) |
---|
137 | |
---|
138 | def onEVT_HISTORY(self, event): |
---|
139 | """ |
---|
140 | Process EVT_HISTORY events |
---|
141 | When a history event arrives, put it in the list |
---|
142 | of history items |
---|
143 | |
---|
144 | @param event: EVT_HISTORY event |
---|
145 | """ |
---|
146 | print "onEVT_HISTORY",event.name |
---|
147 | #TODO: this will only work on Windows |
---|
148 | import distutils.util |
---|
149 | isWindows = False |
---|
150 | if sys.platform.count("win32")>0: |
---|
151 | isWindows = True |
---|
152 | |
---|
153 | if isWindows: |
---|
154 | memstatus = _MEMORYSTATUS() |
---|
155 | windll.kernel32.GlobalMemoryStatus(byref(memstatus)) |
---|
156 | size_0 = getattr(memstatus, 'dwAvailPhys') |
---|
157 | size_obj = 1000.0 |
---|
158 | if not event.item.state._data_2D == None: |
---|
159 | size_obj = len(event.item.state._data_2D)*len(event.item.state._data_2D)*32.0 |
---|
160 | |
---|
161 | #print "HistoryPanel.EVT_HISTORY" |
---|
162 | self.counter += 1 |
---|
163 | self.view_box.Insert("%d: %s" % (self.counter, event.name), |
---|
164 | self.view_box.GetCount(), event.item) |
---|
165 | |
---|
166 | deleteOldItem = False |
---|
167 | if isWindows: |
---|
168 | # This break our scheme of the history panel not knowing |
---|
169 | # about the events, but we need to protect against |
---|
170 | # filling up the memory. |
---|
171 | # TODO: fix this |
---|
172 | |
---|
173 | if size_0/size_obj < 10: |
---|
174 | deleteOldItem = True |
---|
175 | |
---|
176 | else: |
---|
177 | |
---|
178 | # If the buffer is already full, write the earlier ones |
---|
179 | # in a file. |
---|
180 | if self.view_box.GetCount() > MAX_BUFF_LEN: |
---|
181 | deleteOldItem = True |
---|
182 | |
---|
183 | if deleteOldItem: |
---|
184 | #item_0 = self.view_box.GetString(0) |
---|
185 | #import os |
---|
186 | # Write it |
---|
187 | #filename = "history_%i.dat" % os.getpid() |
---|
188 | #fd = open(filename,'a') |
---|
189 | #fd.write("test\n") |
---|
190 | |
---|
191 | # For now, delete item from list |
---|
192 | if not self.buffer_filled: |
---|
193 | evt = config.StatusBarEvent( \ |
---|
194 | message = "Memory running low, oldest history events will be deleted") |
---|
195 | #message = "Maximum number of history events reached (%g)" % MAX_BUFF_LEN) |
---|
196 | wx.PostEvent(self.parent, evt) |
---|
197 | self.buffer_filled = True |
---|
198 | |
---|
199 | self.view_box.Delete(0) |
---|
200 | |
---|
201 | |
---|