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

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 5876f7e 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
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)
[8b21fa7]5#project funded by the US National Science Foundation.
[7116b6e0]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
[3ecaa2b]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()
[8b21fa7]28
29
[9ff861b]30class PrTextCtrl(wx.TextCtrl):
31    """
[7116b6e0]32    Text control for model and fit parameters.
33    Binds the appropriate events for user interactions.
[9ff861b]34    """
35    def __init__(self, *args, **kwds):
[8b21fa7]36
[9ff861b]37        wx.TextCtrl.__init__(self, *args, **kwds)
[8b21fa7]38
[8eeb0b6]39        ## Set to True when the mouse is clicked while the whole string is selected
[a07e72f]40        self.full_selection = False
[8eeb0b6]41        ## Call back for EVT_SET_FOCUS events
42        _on_set_focus_callback = None
[9ff861b]43        # Bind appropriate events
44        self.Bind(wx.EVT_LEFT_UP, self._highlight_text)
[8eeb0b6]45        self.Bind(wx.EVT_SET_FOCUS, self._on_set_focus)
46
47    def _on_set_focus(self, event):
48        """
[7116b6e0]49        Catch when the text control is set in focus to highlight the whole
50        text if necessary
[8b21fa7]51
[7116b6e0]52        :param event: mouse event
[8b21fa7]53
[8eeb0b6]54        """
55        event.Skip()
56        self.full_selection = True
[8b21fa7]57
[9ff861b]58    def _highlight_text(self, event):
59        """
[7116b6e0]60        Highlight text of a TextCtrl only of no text has be selected
[8b21fa7]61
[7116b6e0]62        :param event: mouse event
[8b21fa7]63
[9ff861b]64        """
65        # Make sure the mouse event is available to other listeners
66        event.Skip()
[8b21fa7]67        control = event.GetEventObject()
[8eeb0b6]68        if self.full_selection:
69            self.full_selection = False
70            # Check that we have a TextCtrl
71            if issubclass(control.__class__, wx.TextCtrl):
[8b21fa7]72                # Check whether text has been selected,
[8eeb0b6]73                # if not, select the whole string
74                (start, end) = control.GetSelection()
[8b21fa7]75                if start == end:
76                    control.SetSelection(-1, -1)
[9ff861b]77
78class OutputTextCtrl(wx.TextCtrl):
79    """
[7116b6e0]80    Text control used to display outputs.
[8b21fa7]81    No editing allowed. The background is
[7116b6e0]82    grayed out. User can't select text.
[9ff861b]83    """
84    def __init__(self, *args, **kwds):
[7116b6e0]85        """
86        """
[9ff861b]87        wx.TextCtrl.__init__(self, *args, **kwds)
88        self.SetEditable(False)
89        self.SetBackgroundColour(self.GetParent().GetBackgroundColour())
[8b21fa7]90
[9ff861b]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)
[8b21fa7]95
[9ff861b]96    def _click(self, event):
97        """
[7116b6e0]98        Prevent further handling of the mouse event
99        by not calling Skip().
[8b21fa7]100        """
[9ff861b]101        pass
[8b21fa7]102
[9ff861b]103
104class DataFileTextCtrl(OutputTextCtrl):
105    """
[7116b6e0]106    Text control used to display only the file name
107    given a full path.
[8b21fa7]108
[7116b6e0]109    :TODO: now that we no longer choose the data file from the panel,
[9ff861b]110        it's no longer necessary to pass around the file path. That code
[8b21fa7]111        should be refactored away and simplified.
[9ff861b]112    """
113    def __init__(self, *args, **kwds):
[7116b6e0]114        """
115        """
[9ff861b]116        OutputTextCtrl.__init__(self, *args, **kwds)
117        self._complete_path = None
[8b21fa7]118
[9ff861b]119    def SetValue(self, value):
120        """
[7116b6e0]121        Sets the file name given a path
[9ff861b]122        """
123        self._complete_path = str(value)
[8b21fa7]124        file_path = os.path.basename(self._complete_path)
125        OutputTextCtrl.SetValue(self, file_path)
126
[9ff861b]127    def GetValue(self):
128        """
[7116b6e0]129        Return the full path
[9ff861b]130        """
131        return self._complete_path
[3ecaa2b]132
[8b21fa7]133
[3ecaa2b]134def load_error(error=None):
135    """
136    Pop up an error message.
[8b21fa7]137
[3ecaa2b]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"
[8b21fa7]142
[3ecaa2b]143    if error is not None:
144        message += "When contacting the DANSE team, mention the"
145        message += " following:\n%s" % str(error)
[8b21fa7]146
[3ecaa2b]147    dial = wx.MessageDialog(None, message, 'Error Loading File',
148                            wx.OK | wx.ICON_EXCLAMATION)
[8b21fa7]149    dial.ShowModal()
150
[3ecaa2b]151
[75a7ece]152class DataDialog(wx.Dialog):
153    """
154    Allow file selection at loading time
155    """
156    def __init__(self, data_list, parent=None, text='', *args, **kwds):
[3ecaa2b]157        kwds['size'] = (WIDTH, HEIGHT)
158        kwds['title'] = "Data Selection"
[75a7ece]159        wx.Dialog.__init__(self, parent, *args, **kwds)
160        self.list_of_ctrl = []
[3ecaa2b]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,
[8b21fa7]166                                  size=(WIDTH - 20, HEIGHT / 3))
167
[3ecaa2b]168        self.__do_layout(data_list, text=text)
[8b21fa7]169
[3ecaa2b]170    def __do_layout(self, data_list, text=''):
[75a7ece]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"
[3ecaa2b]178        text_ctrl = wx.TextCtrl(self, -1, str(text), style=wx.TE_MULTILINE,
[8b21fa7]179                                size=(-1, HEIGHT / 3))
[3ecaa2b]180        text_ctrl.SetEditable(False)
[8b21fa7]181        self._sizer_txt.Add(text_ctrl, 1, wx.EXPAND | wx.ALL, 10)
[75a7ece]182        iy = 0
183        ix = 0
[8b21fa7]184        rbox = wx.RadioButton(self._panel, -1, str(data_list[0].name),
185                              (10, 10), style=wx.RB_GROUP)
[75a7ece]186        rbox.SetValue(True)
187        self.list_of_ctrl.append((rbox, data_list[0]))
[3ecaa2b]188        self._choice_sizer.Add(rbox, (iy, ix), (1, 1),
[8b21fa7]189                               wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[75a7ece]190        for i in range(1, len(data_list)):
191            iy += 1
[8b21fa7]192            rbox = wx.RadioButton(self._panel, -1,
[3ecaa2b]193                                  str(data_list[i].name), (10, 10))
[75a7ece]194            rbox.SetValue(False)
195            self.list_of_ctrl.append((rbox, data_list[i]))
[3ecaa2b]196            self._choice_sizer.Add(rbox, (iy, ix),
[8b21fa7]197                                   (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15)
[3ecaa2b]198        self._panel.SetSizer(self._choice_sizer)
[75a7ece]199        #add sizer
[8b21fa7]200        self._sizer_button.Add((20, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0)
[75a7ece]201        button_cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
[3ecaa2b]202        self._sizer_button.Add(button_cancel, 0,
[8b21fa7]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)
[75a7ece]208        static_line = wx.StaticLine(self, -1)
[8b21fa7]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)
[3ecaa2b]212        self._sizer_main.Add(static_line, 0, wx.EXPAND, 0)
[8b21fa7]213        self._sizer_main.Add(self._sizer_button, 0, wx.EXPAND | wx.ALL, 10)
[6813da7d]214        self.SetSizer(self._sizer_main)
[8b21fa7]215
[75a7ece]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():
[8b21fa7]223                return data
Note: See TracBrowser for help on using the repository browser.