source: sasview/inversionview/src/sans/perspectives/pr/pr_widgets.py @ 889de4d9

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