source: sasview/src/sas/sasgui/perspectives/pr/pr_widgets.py @ bacc04b

Last change on this file since bacc04b was d85c194, checked in by Piotr Rozyczko <piotr.rozyczko@…>, 9 years ago

Remaining modules refactored

  • Property mode set to 100644
File size: 7.6 KB
Line 
1
2################################################################################
3#This software was developed by the University of Tennessee as part of the
4#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
5#project funded by the US National Science Foundation.
6#
7#See the license text in license.txt
8#
9#copyright 2009, University of Tennessee
10################################################################################
11
12"""
13Text controls for input/output of the main PrView panel
14"""
15
16import wx
17import os
18from wx.lib.scrolledpanel import ScrolledPanel
19
20WIDTH = 400
21HEIGHT = 350
22
23
24class DialogPanel(ScrolledPanel):
25    def __init__(self, *args, **kwds):
26        ScrolledPanel.__init__(self, *args, **kwds)
27        self.SetupScrolling()
28
29
30class PrTextCtrl(wx.TextCtrl):
31    """
32    Text control for model and fit parameters.
33    Binds the appropriate events for user interactions.
34    """
35    def __init__(self, *args, **kwds):
36
37        wx.TextCtrl.__init__(self, *args, **kwds)
38
39        ## Set to True when the mouse is clicked while the whole string is selected
40        self.full_selection = False
41        ## Call back for EVT_SET_FOCUS events
42        _on_set_focus_callback = None
43        # Bind appropriate events
44        self.Bind(wx.EVT_LEFT_UP, self._highlight_text)
45        self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
46
47    def _on_set_focus(self, event):
48        """
49        Catch when the text control is set in focus to highlight the whole
50        text if necessary
51
52        :param event: mouse event
53
54        """
55        event.Skip()
56        self.full_selection = True
57
58    def _highlight_text(self, event):
59        """
60        Highlight text of a TextCtrl only of no text has be selected
61
62        :param event: mouse event
63
64        """
65        # Make sure the mouse event is available to other listeners
66        event.Skip()
67        control = event.GetEventObject()
68        if self.full_selection:
69            self.full_selection = False
70            # Check that we have a TextCtrl
71            if issubclass(control.__class__, wx.TextCtrl):
72                # Check whether text has been selected,
73                # if not, select the whole string
74                (start, end) = control.GetSelection()
75                if start == end:
76                    control.SetSelection(-1, -1)
77
78class OutputTextCtrl(wx.TextCtrl):
79    """
80    Text control used to display outputs.
81    No editing allowed. The background is
82    grayed out. User can't select text.
83    """
84    def __init__(self, *args, **kwds):
85        """
86        """
87        wx.TextCtrl.__init__(self, *args, **kwds)
88        self.SetEditable(False)
89        self.SetBackgroundColour(self.GetParent().GetBackgroundColour())
90
91        # Bind to mouse event to avoid text highlighting
92        # The event will be skipped once the call-back
93        # is called.
94        self.Bind(wx.EVT_MOUSE_EVENTS, self._click)
95
96    def _click(self, event):
97        """
98        Prevent further handling of the mouse event
99        by not calling Skip().
100        """
101        pass
102
103
104class DataFileTextCtrl(OutputTextCtrl):
105    """
106    Text control used to display only the file name
107    given a full path.
108
109    :TODO: now that we no longer choose the data file from the panel,
110        it's no longer necessary to pass around the file path. That code
111        should be refactored away and simplified.
112    """
113    def __init__(self, *args, **kwds):
114        """
115        """
116        OutputTextCtrl.__init__(self, *args, **kwds)
117        self._complete_path = None
118
119    def SetValue(self, value):
120        """
121        Sets the file name given a path
122        """
123        self._complete_path = str(value)
124        file_path = os.path.basename(self._complete_path)
125        OutputTextCtrl.SetValue(self, file_path)
126
127    def GetValue(self):
128        """
129        Return the full path
130        """
131        return self._complete_path
132
133
134def load_error(error=None):
135    """
136    Pop up an error message.
137
138    :param error: details error message to be displayed
139    """
140    message = "The data file you selected could not be loaded.\n"
141    message += "Make sure the content of your file is properly formatted.\n\n"
142
143    if error is not None:
144        message += "When contacting the DANSE team, mention the"
145        message += " following:\n%s" % str(error)
146
147    dial = wx.MessageDialog(None, message, 'Error Loading File',
148                            wx.OK | wx.ICON_EXCLAMATION)
149    dial.ShowModal()
150
151
152class DataDialog(wx.Dialog):
153    """
154    Allow file selection at loading time
155    """
156    def __init__(self, data_list, parent=None, text='', *args, **kwds):
157        kwds['size'] = (WIDTH, HEIGHT)
158        kwds['title'] = "Data Selection"
159        wx.Dialog.__init__(self, parent, *args, **kwds)
160        self.list_of_ctrl = []
161        self._sizer_main = wx.BoxSizer(wx.VERTICAL)
162        self._sizer_txt = wx.BoxSizer(wx.VERTICAL)
163        self._sizer_button = wx.BoxSizer(wx.HORIZONTAL)
164        self._choice_sizer = wx.GridBagSizer(5, 5)
165        self._panel = DialogPanel(self, style=wx.RAISED_BORDER,
166                                  size=(WIDTH - 20, HEIGHT / 3))
167
168        self.__do_layout(data_list, text=text)
169
170    def __do_layout(self, data_list, text=''):
171        """
172        layout the dialog
173        """
174        #add text
175        if text.strip() == "":
176            text = "This Perspective does not allow multiple data !\n"
177            text += "Please select only one Data.\n"
178        text_ctrl = wx.TextCtrl(self, -1, str(text), style=wx.TE_MULTILINE,
179                                size=(-1, HEIGHT / 3))
180        text_ctrl.SetEditable(False)
181        self._sizer_txt.Add(text_ctrl, 1, wx.EXPAND | wx.ALL, 10)
182        iy = 0
183        ix = 0
184        rbox = wx.RadioButton(self._panel, -1, str(data_list[0].name),
185                              (10, 10), style=wx.RB_GROUP)
186        rbox.SetValue(True)
187        self.list_of_ctrl.append((rbox, data_list[0]))
188        self._choice_sizer.Add(rbox, (iy, ix), (1, 1),
189                               wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
190        for i in range(1, len(data_list)):
191            iy += 1
192            rbox = wx.RadioButton(self._panel, -1,
193                                  str(data_list[i].name), (10, 10))
194            rbox.SetValue(False)
195            self.list_of_ctrl.append((rbox, data_list[i]))
196            self._choice_sizer.Add(rbox, (iy, ix),
197                                   (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
198        self._panel.SetSizer(self._choice_sizer)
199        #add sizer
200        self._sizer_button.Add((20, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
201        button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
202        self._sizer_button.Add(button_cancel, 0,
203                               wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10)
204        button_ok = wx.Button(self, wx.ID_OK, "Ok")
205        button_ok.SetFocus()
206        self._sizer_button.Add(button_ok, 0,
207                               wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10)
208        static_line = wx.StaticLine(self, -1)
209
210        self._sizer_txt.Add(self._panel, 0, wx.EXPAND | wx.ALL, 5)
211        self._sizer_main.Add(self._sizer_txt, 0, wx.EXPAND | wx.ALL, 5)
212        self._sizer_main.Add(static_line, 0, wx.EXPAND, 0)
213        self._sizer_main.Add(self._sizer_button, 0, wx.EXPAND | wx.ALL, 10)
214        self.SetSizer(self._sizer_main)
215
216    def get_data(self):
217        """
218        return the selected data
219        """
220        for item in self.list_of_ctrl:
221            rbox, data = item
222            if rbox.GetValue():
223                return data
Note: See TracBrowser for help on using the repository browser.