[518d35d] | 1 | |
---|
| 2 | |
---|
[d7a39e5] | 3 | |
---|
| 4 | ################################################################################ |
---|
| 5 | #This software was developed by the University of Tennessee as part of the |
---|
| 6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 7 | #project funded by the US National Science Foundation. |
---|
| 8 | # |
---|
| 9 | #See the license text in license.txt |
---|
| 10 | # |
---|
| 11 | #copyright 2009, University of Tennessee |
---|
| 12 | ################################################################################ |
---|
| 13 | |
---|
| 14 | |
---|
[518d35d] | 15 | import wx |
---|
[cacbd7d] | 16 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
[518d35d] | 17 | |
---|
[cacbd7d] | 18 | WIDTH = 400 |
---|
| 19 | HEIGHT = 200 |
---|
| 20 | |
---|
[518d35d] | 21 | class InvTextCtrl(wx.TextCtrl): |
---|
| 22 | """ |
---|
[d7a39e5] | 23 | Text control for model and fit parameters. |
---|
| 24 | Binds the appropriate events for user interactions. |
---|
[518d35d] | 25 | """ |
---|
| 26 | def __init__(self, *args, **kwds): |
---|
| 27 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
[4a2b054] | 28 | ## Set to True when the mouse is clicked while |
---|
| 29 | #the whole string is selected |
---|
[4e1c362] | 30 | self.full_selection = False |
---|
[518d35d] | 31 | ## Call back for EVT_SET_FOCUS events |
---|
| 32 | _on_set_focus_callback = None |
---|
[4e1c362] | 33 | |
---|
[518d35d] | 34 | # Bind appropriate events |
---|
| 35 | self.Bind(wx.EVT_LEFT_UP, self._highlight_text) |
---|
| 36 | self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus) |
---|
[4e1c362] | 37 | |
---|
[518d35d] | 38 | def _on_set_focus(self, event): |
---|
| 39 | """ |
---|
[d7a39e5] | 40 | Catch when the text control is set in focus to highlight the whole |
---|
| 41 | text if necessary |
---|
| 42 | |
---|
| 43 | :param event: mouse event |
---|
[518d35d] | 44 | """ |
---|
| 45 | event.Skip() |
---|
| 46 | self.full_selection = True |
---|
| 47 | |
---|
| 48 | def _highlight_text(self, event): |
---|
| 49 | """ |
---|
[d7a39e5] | 50 | Highlight text of a TextCtrl only of no text has be selected |
---|
| 51 | |
---|
| 52 | :param event: mouse event |
---|
[518d35d] | 53 | """ |
---|
| 54 | # Make sure the mouse event is available to other listeners |
---|
| 55 | event.Skip() |
---|
| 56 | control = event.GetEventObject() |
---|
| 57 | if self.full_selection: |
---|
| 58 | self.full_selection = False |
---|
| 59 | # Check that we have a TextCtrl |
---|
| 60 | if issubclass(control.__class__, wx.TextCtrl): |
---|
| 61 | # Check whether text has been selected, |
---|
| 62 | # if not, select the whole string |
---|
| 63 | (start, end) = control.GetSelection() |
---|
[4a2b054] | 64 | if start == end: |
---|
| 65 | control.SetSelection(-1, -1) |
---|
[4e1c362] | 66 | |
---|
[518d35d] | 67 | |
---|
| 68 | class OutputTextCtrl(wx.TextCtrl): |
---|
| 69 | """ |
---|
[d7a39e5] | 70 | Text control used to display outputs. |
---|
| 71 | No editing allowed. The background is |
---|
| 72 | grayed out. User can't select text. |
---|
[518d35d] | 73 | """ |
---|
| 74 | def __init__(self, *args, **kwds): |
---|
| 75 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
| 76 | self.SetEditable(False) |
---|
| 77 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
| 78 | |
---|
| 79 | # Bind to mouse event to avoid text highlighting |
---|
| 80 | # The event will be skipped once the call-back |
---|
| 81 | # is called. |
---|
[4e1c362] | 82 | |
---|
[518d35d] | 83 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
[4e1c362] | 84 | |
---|
[518d35d] | 85 | |
---|
| 86 | def _click(self, event): |
---|
| 87 | """ |
---|
[d7a39e5] | 88 | Prevent further handling of the mouse event |
---|
| 89 | by not calling Skip(). |
---|
[518d35d] | 90 | """ |
---|
| 91 | pass |
---|
[4e1c362] | 92 | |
---|
[cacbd7d] | 93 | class DataDialog(wx.Dialog): |
---|
| 94 | """ |
---|
| 95 | Allow file selection at loading time |
---|
| 96 | """ |
---|
| 97 | def __init__(self, data_list, parent=None, text='', *args, **kwds): |
---|
| 98 | wx.Dialog.__init__(self, parent, *args, **kwds) |
---|
| 99 | self.SetTitle("Data Selection") |
---|
| 100 | self.SetSize((WIDTH, HEIGHT)) |
---|
| 101 | self.list_of_ctrl = [] |
---|
| 102 | if not data_list: |
---|
| 103 | return |
---|
| 104 | self._sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
| 105 | self._sizer_txt = wx.BoxSizer(wx.VERTICAL) |
---|
| 106 | self._sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 107 | self._choice_sizer = wx.GridBagSizer(5, 5) |
---|
| 108 | self._panel = ScrolledPanel(self, style=wx.RAISED_BORDER, |
---|
| 109 | size=(WIDTH-20, HEIGHT-50)) |
---|
| 110 | self._panel.SetupScrolling() |
---|
| 111 | self.__do_layout(data_list, text=text) |
---|
| 112 | |
---|
| 113 | def __do_layout(self, data_list, text=''): |
---|
| 114 | """ |
---|
| 115 | layout the dialog |
---|
| 116 | """ |
---|
| 117 | if not data_list or len(data_list) <= 1: |
---|
| 118 | return |
---|
| 119 | #add text |
---|
| 120 | if text.strip() == "": |
---|
| 121 | text = "This Perspective does not allow multiple data !\n" |
---|
| 122 | text += "Please select only one Data.\n" |
---|
| 123 | text_ctrl = wx.StaticText(self, -1, str(text)) |
---|
| 124 | self._sizer_txt.Add(text_ctrl) |
---|
| 125 | iy = 0 |
---|
| 126 | ix = 0 |
---|
| 127 | rbox = wx.RadioButton(self._panel, -1, str(data_list[0].name), |
---|
| 128 | (10, 10), style= wx.RB_GROUP) |
---|
| 129 | rbox.SetValue(True) |
---|
| 130 | self.list_of_ctrl.append((rbox, data_list[0])) |
---|
| 131 | self._choice_sizer.Add(rbox, (iy, ix), (1, 1), |
---|
| 132 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 133 | for i in range(1, len(data_list)): |
---|
| 134 | iy += 1 |
---|
| 135 | rbox = wx.RadioButton(self._panel, -1, |
---|
| 136 | str(data_list[i].name), (10, 10)) |
---|
| 137 | rbox.SetValue(False) |
---|
| 138 | self.list_of_ctrl.append((rbox, data_list[i])) |
---|
| 139 | self._choice_sizer.Add(rbox, (iy, ix), |
---|
| 140 | (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 141 | self._panel.SetSizer(self._choice_sizer) |
---|
| 142 | #add sizer |
---|
| 143 | self._sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 144 | button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
| 145 | self._sizer_button.Add(button_cancel, 0, |
---|
| 146 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 147 | button_OK = wx.Button(self, wx.ID_OK, "Ok") |
---|
| 148 | button_OK.SetFocus() |
---|
| 149 | self._sizer_button.Add(button_OK, 0, |
---|
| 150 | wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 151 | static_line = wx.StaticLine(self, -1) |
---|
| 152 | |
---|
| 153 | self._sizer_txt.Add(self._panel, 1, wx.EXPAND|wx.LEFT|wx.RIGHT, 5) |
---|
| 154 | self._sizer_main.Add(self._sizer_txt, 1, wx.EXPAND|wx.ALL, 10) |
---|
| 155 | self._sizer_main.Add(static_line, 0, wx.EXPAND, 0) |
---|
| 156 | self._sizer_main.Add(self._sizer_button, 0, wx.EXPAND|wx.ALL, 10) |
---|
| 157 | self.SetSizer(self._sizer_main) |
---|
| 158 | self.Layout() |
---|
| 159 | |
---|
| 160 | def get_data(self): |
---|
| 161 | """ |
---|
| 162 | return the selected data |
---|
| 163 | """ |
---|
| 164 | for item in self.list_of_ctrl: |
---|
| 165 | rbox, data = item |
---|
| 166 | if rbox.GetValue(): |
---|
| 167 | return data |
---|
| 168 | |
---|
[4e1c362] | 169 | |
---|
| 170 | |
---|
[518d35d] | 171 | |
---|