source: sasview/calculatorview/src/sans/perspectives/calculator/data_operator.py @ b82e536

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 b82e536 was 6e48fd0, checked in by Jae Cho <jhjcho@…>, 12 years ago

data operation: added scale for 2D plot

  • Property mode set to 100644
File size: 34.2 KB
Line 
1"""
2GUI for the data operation
3"""
4import wx
5import sys
6import time
7import numpy
8from sans.dataloader.data_info import Data2D
9from sans.dataloader.data_info import Data1D
10from danse.common.plottools.PlotPanel import PlotPanel
11from danse.common.plottools.plottables import Graph
12from danse.common.plottools.canvas import FigureCanvas
13from danse.common.plottools import transform
14from matplotlib.font_manager import FontProperties
15from matplotlib.figure import Figure
16from sans.guiframe.events import StatusEvent
17       
18#Control panel width
19if sys.platform.count("win32") > 0:
20    PANEL_WIDTH = 790
21    PANEL_HEIGTH = 370
22    FONT_VARIANT = 0
23    _BOX_WIDTH = 200
24    ON_MAC = False
25else:
26    _BOX_WIDTH = 230
27    PANEL_WIDTH = 900
28    PANEL_HEIGTH = 430
29    FONT_VARIANT = 1
30    ON_MAC = True
31     
32class DataOperPanel(wx.ScrolledWindow):
33    """
34    :param data: when not empty the class can
35                same information into a data object
36        and post event containing the changed data object to some other frame
37    """
38    def __init__(self, parent, *args, **kwds):
39        kwds['name'] = "Data Operation"
40        kwds["size"] = (PANEL_WIDTH, PANEL_HEIGTH)
41        wx.ScrolledWindow.__init__(self, parent, *args, **kwds)
42        self.parent = parent
43        #sizers etc.
44        self.main_sizer = None
45        self.name_sizer = None
46        self.button_sizer = None
47        self.data_namectr = None
48        self.numberctr = None
49        self.data1_cbox = None
50        self.operator_cbox = None
51        self.data2_cbox = None
52        self.data_title_tcl = None
53        self.out_pic = None
54        self.equal_pic = None
55        self.data1_pic = None
56        self.operator_pic = None
57        self.data2_pic = None
58        self.output = None
59        self._notes = None
60        #text grayed color
61        self.color = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND)
62        #data
63        self._data = self.get_datalist()
64        self._do_layout()
65        self.fill_data_combox()
66        self.fill_oprator_combox()
67        self.Bind(wx.EVT_PAINT, self.set_panel_on_focus)
68             
69    def _define_structure(self):
70        """
71        define initial sizer
72        """
73        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
74        title = "Data Operation "
75        title += "[ + (add); - (subtract); "
76        title += "* (multiply); / (divide); "
77        title += "| (append) ]"
78        name_box = wx.StaticBox(self, -1, title)
79        self.name_sizer = wx.StaticBoxSizer(name_box, wx.HORIZONTAL)
80        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
81     
82    def _layout_name(self):
83        """
84        Do the layout for data name related widgets
85        """
86        new_data_sizer = wx.BoxSizer(wx.VERTICAL)
87        equal_sizer =  wx.BoxSizer(wx.VERTICAL)
88        old_data1_sizer = wx.BoxSizer(wx.VERTICAL)
89        operator_sizer = wx.BoxSizer(wx.VERTICAL)
90        old_data2_sizer = wx.BoxSizer(wx.VERTICAL)
91        data2_hori_sizer = wx.BoxSizer(wx.HORIZONTAL)
92        data_name = wx.StaticText(self, -1, 'Output Data Name') 
93        equal_name = wx.StaticText(self, -1, ' =', size=(50, 25)) 
94        data1_name = wx.StaticText(self, -1, 'Data1')
95        operator_name = wx.StaticText(self, -1, 'Operator')
96        data2_name = wx.StaticText(self, -1, 'Data2 (or Number)')
97        self.data_namectr = wx.TextCtrl(self, -1, size=(_BOX_WIDTH, 25), style=wx.TE_PROCESS_ENTER) 
98        self.data_namectr.SetToolTipString("Hit 'Enter' key after typing.")
99        self.data_namectr.SetValue(str('MyNewDataName'))
100        self.numberctr = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/3, 25), style=wx.TE_PROCESS_ENTER) 
101        self.numberctr.SetToolTipString("Hit 'Enter' key after typing.")
102        self.numberctr.SetValue(str(1.0))
103        self.data1_cbox = wx.ComboBox(self, -1, size=(_BOX_WIDTH, 25), 
104                                      style=wx.CB_READONLY)
105        self.operator_cbox = wx.ComboBox(self, -1, size=(70, 25), 
106                                         style=wx.CB_READONLY)
107        operation_tip = "Add: +, Subtract: -, "
108        operation_tip += "Multiply: *, Divide: /, "
109        operation_tip += "Append(Combine): | "
110        self.operator_cbox.SetToolTipString(operation_tip)
111        self.data2_cbox = wx.ComboBox(self, -1, size=(_BOX_WIDTH*2/3, 25),
112                                       style=wx.CB_READONLY)
113
114        self.out_pic = SmallPanel(self, -1, True, 
115                                    size=(_BOX_WIDTH, _BOX_WIDTH), 
116                                    style=wx.NO_BORDER)
117        self.equal_pic = SmallPanel(self, -1, True, '=', 
118                                    size=(50, _BOX_WIDTH), 
119                                    style=wx.NO_BORDER)
120        self.data1_pic = SmallPanel(self, -1, True, 
121                                    size=(_BOX_WIDTH, _BOX_WIDTH), 
122                                    style=wx.NO_BORDER)
123        self.operator_pic = SmallPanel(self, -1, True, '+',
124                                    size=(70, _BOX_WIDTH), 
125                                    style=wx.NO_BORDER)
126        self.data2_pic = SmallPanel(self, -1, True, 
127                                    size=(_BOX_WIDTH, _BOX_WIDTH), 
128                                    style=wx.NO_BORDER)
129        for ax in self.equal_pic.axes:
130            ax.set_frame_on(False)
131        for ax in self.operator_pic.axes:
132            ax.set_frame_on(False)
133
134        new_data_sizer.AddMany([(data_name, 0, wx.LEFT, 3),
135                                       (self.data_namectr, 0, wx.LEFT, 3),
136                                       (self.out_pic, 0, wx.LEFT, 3)])
137        equal_sizer.AddMany([(13, 13), (equal_name, 0, wx.LEFT, 3),
138                                       (self.equal_pic, 0, wx.LEFT, 3)])
139        old_data1_sizer.AddMany([(data1_name, 0, wx.LEFT, 3),
140                                       (self.data1_cbox, 0, wx.LEFT, 3),
141                                       (self.data1_pic, 0, wx.LEFT, 3)])
142        operator_sizer.AddMany([(operator_name, 0, wx.LEFT, 3),
143                                 (self.operator_cbox, 0, wx.LEFT, 3),
144                                 (self.operator_pic, 0, wx.LEFT, 3)])
145        data2_hori_sizer.AddMany([(self.data2_cbox, 0, wx.LEFT, 0),
146                                       (self.numberctr, 0, wx.RIGHT, 0)])
147        old_data2_sizer.AddMany([(data2_name, 0, wx.LEFT, 3),
148                                       (data2_hori_sizer, 0, wx.LEFT, 3),
149                                       (self.data2_pic, 0, wx.LEFT, 3)])
150        self.name_sizer.AddMany([(new_data_sizer, 0, wx.LEFT|wx.TOP, 5),
151                                       (equal_sizer, 0, wx.TOP, 5),
152                                       (old_data1_sizer, 0, wx.TOP, 5),
153                                       (operator_sizer, 0, wx.TOP, 5),
154                                       (old_data2_sizer, 0, wx.TOP, 5)])
155        self.data2_cbox.Show(True)
156
157        self._show_numctrl(self.numberctr, False)
158       
159        wx.EVT_TEXT_ENTER(self.data_namectr, -1, self.on_name)
160        wx.EVT_TEXT_ENTER(self.numberctr, -1, self.on_number) 
161        wx.EVT_COMBOBOX(self.data1_cbox, -1, self.on_select_data1) 
162        wx.EVT_COMBOBOX(self.operator_cbox, -1, self.on_select_operator) 
163        wx.EVT_COMBOBOX(self.data2_cbox, -1, self.on_select_data2)
164   
165    def _show_numctrl(self, ctrl, enable=True): 
166        """
167        Show/Hide on Win
168        Enable/Disable on MAC
169        """
170        if ON_MAC:
171            ctrl.Enable(enable)
172            children = ctrl.GetChildren()
173            if len(children) > 0:
174                ctrl.GetChildren()[0].SetBackGroundColour(self.color)
175            if enable:
176                wx.EVT_TEXT_ENTER(self.numberctr, -1, self.on_number) 
177        else:
178            if not ctrl.IsEnabled():
179                ctrl.Enable(True)
180            ctrl.Show(enable)
181           
182    def on_name(self, event=None):
183        """
184        On data name typing
185        """
186        if event != None:
187            event.Skip()
188        item = event.GetEventObject()
189        if item.IsEnabled():
190            self._set_textctrl_color(item, 'white')
191        else:
192            self._set_textctrl_color(item, self.color)
193        text = item.GetValue().strip()
194        self._check_newname(text)
195   
196    def _check_newname(self, name=None):
197        """
198        Check name ctr strings
199        """
200        self.send_warnings('')
201        msg = ''
202        if name == None:
203            text = self.data_namectr.GetValue().strip()
204        else:
205            text = name
206        state_list = self.get_datalist().values()
207        name_list = []
208        for state in state_list:
209            if state.data == None:
210                theory_list = state.get_theory()
211                theory, _ = theory_list.values()[0]
212                d_name = str(theory.name)
213            else:
214                d_name = str(state.data.name)
215            name_list.append(d_name)
216        if text in name_list:
217            self._set_textctrl_color(self.data_namectr, 'pink')
218            msg = "DataOperation: The name already exists."
219        if len(text) == 0:
220            self._set_textctrl_color(self.data_namectr, 'pink')
221            msg = "DataOperation: Type the data name first."
222        if self._notes:
223            self.send_warnings(msg, 'error')
224        self.name_sizer.Layout()
225        self.Refresh()
226   
227    def _set_textctrl_color(self, ctrl, color): 
228        """
229        Set TextCtrl color
230        """
231        if ON_MAC:
232            children = ctrl.GetChildren()
233            if len(children) > 0:
234                children[0].SetBackgroundColour(color) 
235        else:
236            ctrl.SetBackgroundColour(color) 
237        self.name_sizer.Layout()
238                     
239    def on_number(self, event=None):
240        """
241        On selecting Number for Data2
242        """
243        self.send_warnings('')
244        item = event.GetEventObject()
245        text = item.GetValue().strip()
246        if self.numberctr.IsShown():
247            if self.numberctr.IsEnabled():
248                self._set_textctrl_color(self.numberctr, 'white')
249                try:
250                    val = float(text)
251                    pos = self.data2_cbox.GetCurrentSelection()
252                    self.data2_cbox.SetClientData(pos, val)
253                except:
254                    self._set_textctrl_color(self.numberctr, 'pink')
255                    msg = "DataOperation: Number requires a float number."
256                    self.send_warnings(msg, 'error')
257                    return
258            else:
259                self._set_textctrl_color(self.numberctr, self.color)
260       
261        self.put_text_pic(self.data2_pic, content=str(val))
262        self.check_data_inputs()
263        if self.output != None:
264            self.output.name = str(self.data_namectr.GetValue())
265        self.draw_output(self.output)
266        self.Refresh()
267       
268    def on_select_data1(self, event=None):
269        """
270        On select data1
271        """
272        self.send_warnings('')
273        item = event.GetEventObject()
274        pos = item.GetCurrentSelection()
275        data = item.GetClientData(pos)
276        if data == None:
277            content = "?"
278            self.put_text_pic(self.data1_pic, content) 
279        else:
280            self.data1_pic.add_image(data)
281        self.check_data_inputs()
282        if self.output != None:
283            self.output.name = str(self.data_namectr.GetValue())
284        self.draw_output(self.output)
285       
286    def on_select_operator(self, event=None):
287        """
288        On Select an Operator
289        """
290        self.send_warnings('')
291        item = event.GetEventObject()
292        text = item.GetValue().strip()
293        self.put_text_pic(self.operator_pic, content=text) 
294        self.check_data_inputs()
295        if self.output != None:
296            self.output.name = str(self.data_namectr.GetValue())
297        self.draw_output(self.output)
298       
299    def on_select_data2(self, event=None):
300        """
301        On Selecting Data2
302        """
303        self.send_warnings('')
304        item = event.GetEventObject()
305        text = item.GetValue().strip().lower()
306        self._show_numctrl(self.numberctr, text=='number')
307        pos = item.GetCurrentSelection()
308        data = item.GetClientData(pos)
309        content = "?"
310        if not (self.numberctr.IsShown() and self.numberctr.IsEnabled()):
311            if data == None:
312                content = "?"
313                self.put_text_pic(self.data2_pic, content)
314            else:
315                self.data2_pic.add_image(data)
316            self.check_data_inputs() 
317        else:
318            content = str(self.numberctr.GetValue().strip())
319            try:
320                content = float(content)
321                data = content
322            except:
323                self._set_textctrl_color(self.numberctr, 'pink')
324                content = "?"
325                data = None
326            item.SetClientData(pos, data)
327            if data != None:
328                self.check_data_inputs()
329               
330            self.put_text_pic(self.data2_pic, content)   
331       
332        if self.output != None:
333            self.output.name = str(self.data_namectr.GetValue())
334        self.draw_output(self.output)
335       
336    def put_text_pic(self, pic=None, content=''): 
337        """
338        Put text to the pic
339        """
340        pic.set_content(content) 
341        pic.add_text()
342        pic.draw()
343                 
344    def check_data_inputs(self):
345        """
346        Check data1 and data2 whether or not they are ready for operation
347        """
348        self._set_textctrl_color(self.data1_cbox, 'white')
349        self._set_textctrl_color(self.data2_cbox, 'white')
350        flag = False
351        pos1 = self.data1_cbox.GetCurrentSelection()
352        data1 = self.data1_cbox.GetClientData(pos1)
353        if data1 == None:
354            self.output = None
355            return flag
356        pos2 = self.data2_cbox.GetCurrentSelection()
357        data2 = self.data2_cbox.GetClientData(pos2)
358       
359        if data2 == None:
360            self.output = None
361            return flag
362        if self.numberctr.IsShown():
363            if self.numberctr.IsEnabled():
364                self._set_textctrl_color(self.numberctr, 'white')
365                try:
366                    float(data2)
367                    if self.operator_cbox.GetValue().strip() == '|':
368                        msg = "DataOperation: This operation can not accept "
369                        msg += "a float number."
370                        self.send_warnings(msg, 'error')
371                        self._set_textctrl_color(self.numberctr, 'pink')
372                        self.output = None
373                        return flag
374                except:
375                    msg = "DataOperation: Number requires a float number."
376                    self.send_warnings(msg, 'error')
377                    self._set_textctrl_color(self.numberctr, 'pink')
378                    self.output = None
379                    return flag
380            else:
381                self._set_textctrl_color(self.numberctr, self.color )
382        elif data1.__class__.__name__ != data2.__class__.__name__:
383            self._set_textctrl_color(self.data1_cbox, 'pink')
384            self._set_textctrl_color(self.data2_cbox, 'pink')
385            msg = "DataOperation: Data types must be same."
386            self.send_warnings(msg, 'error')
387            self.output = None
388            return flag
389        try:
390            self.output = self.make_data_out(data1, data2)
391        except:
392            self._check_newname()
393            self._set_textctrl_color(self.data1_cbox, 'pink')
394            self._set_textctrl_color(self.data2_cbox, 'pink')
395            msg = "DataOperation: Data types must be same."
396            self.send_warnings(msg, 'error')
397            self.output = None
398            return flag
399        return True
400   
401    def make_data_out(self, data1, data2):
402        """
403        Make a temp. data output set
404        """
405        output = None
406        pos = self.operator_cbox.GetCurrentSelection()
407        operator = self.operator_cbox.GetClientData(pos)
408        try:
409            exec "output = data1 %s data2"% operator
410        except:
411            raise
412        return output
413   
414   
415    def draw_output(self, output):
416        """
417        Draw output data(temp)
418        """
419        out = self.out_pic
420        if output == None:
421            content = "?"
422            self.put_text_pic(out, content) 
423        else:
424            out.add_image(output)
425        wx.CallAfter(self.name_sizer.Layout)
426        self.Layout()
427        self.Refresh()
428                   
429    def _layout_button(self): 
430        """
431            Do the layout for the button widgets
432        """ 
433        self.bt_apply = wx.Button(self, -1, "Apply", size=(_BOX_WIDTH/2, -1))
434        app_tip = "Generate the Data and send to Data Explorer."
435        self.bt_apply.SetToolTipString(app_tip)
436        self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply)
437       
438        self.bt_close = wx.Button(self, -1, 'Close', size=(_BOX_WIDTH/2, -1))
439        self.bt_close.Bind(wx.EVT_BUTTON, self.on_close)
440        self.bt_close.SetToolTipString("Close this panel.")
441       
442        self.button_sizer.AddMany([(PANEL_WIDTH/2, 25),
443                                   (self.bt_apply, 0, wx.RIGHT, 10),
444                                   (self.bt_close, 0, wx.RIGHT, 10)])
445       
446    def _do_layout(self):
447        """
448        Draw the current panel
449        """
450        self._define_structure()
451        self._layout_name()
452        self._layout_button()
453        self.main_sizer.AddMany([(self.name_sizer, 0, wx.EXPAND|wx.ALL, 10),
454                                (self.button_sizer, 0,
455                                          wx.EXPAND|wx.TOP|wx.BOTTOM, 5)])
456        self.SetSizer(self.main_sizer)
457        self.SetScrollbars(20, 20, 25, 65)
458        self.SetAutoLayout(True)
459   
460    def set_panel_on_focus(self, event):
461        """
462        On Focus at this window
463        """
464        if event != None:
465            event.Skip()
466        self._data = self.get_datalist()
467        children = self.GetChildren()
468        # update the list only when it is on the top
469        if self.FindFocus() in children:
470            self.fill_data_combox()
471         
472    def fill_oprator_combox(self):
473        """
474        fill the current combobox with the operator
475        """   
476        operator_list = [' +', ' -', ' *', " /", " |"]
477        for oper in operator_list:
478            pos = self.operator_cbox.Append(str(oper))
479            self.operator_cbox.SetClientData(pos, str(oper.strip()))
480        self.operator_cbox.SetSelection(0)
481       
482       
483    def fill_data_combox(self):
484        """
485        fill the current combobox with the available data
486        """
487        pos_pre1 = self.data1_cbox.GetCurrentSelection()
488        pos_pre2 = self.data2_cbox.GetCurrentSelection()
489        current1 = self.data1_cbox.GetLabel()
490        current2 = self.data2_cbox.GetLabel()
491        if pos_pre1 < 0:
492            pos_pre1 = 0
493        if pos_pre2 < 0:
494            pos_pre2 = 0
495        self.data1_cbox.Clear()
496        self.data2_cbox.Clear()
497
498        if not self._data:
499            pos = self.data1_cbox.Append('No Data Available')
500            self.data1_cbox.SetSelection(pos)
501            self.data1_cbox.SetClientData(pos, None)
502            pos2 = self.data2_cbox.Append('No Data Available')
503            self.data2_cbox.SetSelection(pos2)
504            self.data2_cbox.SetClientData(pos2, None)
505            return
506        pos1 = self.data1_cbox.Append('Select Data')
507        self.data1_cbox.SetSelection(pos1)
508        self.data1_cbox.SetClientData(pos1, None)
509        pos2 = self.data2_cbox.Append('Select Data')
510        self.data2_cbox.SetSelection(pos2)
511        self.data2_cbox.SetClientData(pos2, None)
512        pos3 = self.data2_cbox.Append('Number')
513        val = None
514        if (self.numberctr.IsShown() and self.numberctr.IsEnabled()):
515            try:
516                val = float(self.numberctr.GetValue())
517            except:
518                val = None
519        self.data2_cbox.SetClientData(pos3, val)
520        dnames = []
521        ids = self._data.keys()
522        for id in ids:
523            if id != None:
524                if self._data[id].data != None:
525                    dnames.append(self._data[id].data.name)
526                else:
527                    theory_list = self._data[id].get_theory()
528                    theory, _ = theory_list.values()[0]
529                    dnames.append(theory.name)
530        ind = numpy.argsort(dnames)
531        if len(ind) > 0:
532            val_list = numpy.array(self._data.values())[ind]
533            for datastate in val_list:
534                data = datastate.data
535                if data != None:
536                    name = data.name
537                    pos1 = self.data1_cbox.Append(str(name))
538                    self.data1_cbox.SetClientData(pos1, data)
539                    pos2 = self.data2_cbox.Append(str(name))
540                    self.data2_cbox.SetClientData(pos2, data)
541                    if str(current1) == str(name):
542                      pos_pre1 = pos1
543                    if str(current2) == str(name):
544                      pos_pre2 = pos2
545                try:
546                    theory_list = datastate.get_theory()
547                    for theory, _ in theory_list.values():
548                        th_name = theory.name
549                        posth1 = self.data1_cbox.Append(str(th_name))
550                        self.data1_cbox.SetClientData(posth1, theory)
551                        posth2 = self.data2_cbox.Append(str(th_name))
552                        self.data2_cbox.SetClientData(posth2, theory)
553                        if str(current1) == str(th_name):
554                            pos_pre1 = posth1
555                        if str(current2) == str(th_name):
556                            pos_pre2 = posth2
557                except:
558                    continue 
559        self.data1_cbox.SetSelection(pos_pre1)
560        self.data2_cbox.SetSelection(pos_pre2)
561   
562    def get_datalist(self):
563        """
564        """
565        data_manager = self.parent.parent.get_data_manager()
566        if data_manager != None:
567            return  data_manager.get_all_data()
568        else:
569            return {}
570           
571    def on_click_apply(self, event):
572        """   
573        changes are saved in data object imported to edit
574        """
575        self.send_warnings('')
576        self.data_namectr.SetBackgroundColour('white')
577        state_list = self.get_datalist().values()
578        name = self.data_namectr.GetValue().strip()
579        name_list = []
580        for state in state_list:
581            if state.data == None:
582                theory_list = state.get_theory()
583                theory, _ = theory_list.values()[0]
584                d_name = str(theory.name)
585            else:
586                d_name = str(state.data.name)
587            name_list.append(d_name)
588        if name in name_list:
589            self._set_textctrl_color(self.data_namectr, 'pink')
590            msg = "The Output Data Name already exists...   "
591            wx.MessageBox(msg, 'Error')
592            return
593        if name == '':
594            self._set_textctrl_color(self.data_namectr, 'pink')
595            msg = "Please type the output data name first...   "
596            wx.MessageBox(msg, 'Error')
597            return
598        if self.output == None:
599            msg = "No Output Data has been generated...   "
600            wx.MessageBox(msg, 'Error')
601            return
602        # send data to data manager
603        self.output.name = name
604        self.output.run = "Data Operation"
605        self.output.instrument = "SansView"
606        self.output.id = str(name) + str(time.time())
607        data = {self.output.id :self.output}
608        self.parent.parent.add_data(data)
609        self.name_sizer.Layout()
610        self.Refresh()
611        #must post event here
612        event.Skip()
613   
614    def on_close(self, event):
615        """
616        leave data as it is and close
617        """
618        self.parent.OnClose()
619       
620    def set_plot_unfocus(self):
621        """
622        Unfocus on right click
623        """
624   
625    def send_warnings(self, msg='', info='info'):
626        """
627        Send warning to status bar
628        """
629        wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info=info))
630         
631class SmallPanel(PlotPanel):
632    """
633    PlotPanel for Quick plot and masking plot
634    """
635    def __init__(self, parent, id=-1, is_number=False, content='?', **kwargs):
636        """
637        """ 
638        PlotPanel.__init__(self, parent, id=id, **kwargs)
639        self.is_number = is_number
640        self.content = content
641        self.point = None
642        self.position = (0.4, 0.5)
643        self.scale = 'linear'
644        self.subplot.set_xticks([])
645        self.subplot.set_yticks([])
646        self.add_text()
647        self.figure.subplots_adjust(left=0.1, bottom=0.1)
648       
649    def set_content(self, content=''):
650        """
651        Set text content
652        """
653        self.content = str(content)
654         
655    def add_toolbar(self):
656        """
657        Add toolbar
658        """
659        # Not implemented
660        pass
661   
662    def on_set_focus(self, event):
663        """
664        send to the parenet the current panel on focus
665        """
666        pass
667
668    def add_image(self, plot):
669        """
670        Add Image
671        """
672        self.content = ''
673        self.textList = []
674        self.plots = {}
675        self.clear()
676        self.point = plot
677        try:
678            self.figure.delaxes(self.figure.axes[0])
679            self.subplot = self.figure.add_subplot(111)
680            #self.figure.delaxes(self.figure.axes[1])
681        except:
682            pass
683        try:
684            name = plot.name
685        except:
686            name = plot.filename
687        self.plots[name] = plot
688
689        #init graph
690        self.graph = Graph()
691
692        #add plot
693        self.graph.add(plot)
694        #draw       
695        self.graph.render(self)
696       
697        try:
698            self.figure.delaxes(self.figure.axes[1])
699        except:
700            pass
701        self.subplot.figure.canvas.resizing = False
702        self.subplot.tick_params(axis='both', labelsize=9)
703        # Draw zero axis lines
704        self.subplot.axhline(linewidth = 1, color='r') 
705        self.subplot.axvline(linewidth = 1, color='r')       
706
707        self.erase_legend()
708        try:
709            # mpl >= 1.1.0
710            self.figure.tight_layout()
711        except:
712            self.figure.subplots_adjust(left=0.1, bottom=0.1)
713        self.subplot.figure.canvas.draw()
714
715    def add_text(self):
716        """
717        Text in the plot
718        """
719        if not self.is_number:
720            return
721
722        self.clear()
723        try:
724            self.figure.delaxes(self.figure.axes[0])
725            self.subplot = self.figure.add_subplot(111)
726            self.figure.delaxes(self.figure.axes[1])
727        except:
728            pass
729        self.subplot.set_xticks([])
730        self.subplot.set_yticks([])
731        label = self.content
732        FONT = FontProperties()
733        xpos, ypos = (0.4, 0.5)
734        font = FONT.copy()
735        font.set_size(14)
736
737        self.textList = []
738        self.subplot.set_xlim((0, 1))
739        self.subplot.set_ylim((0, 1))
740       
741        try:
742            if self.content != '?':
743                float(label)
744        except:
745            self.subplot.set_frame_on(False)
746        try:
747            # mpl >= 1.1.0
748            self.figure.tight_layout()
749        except:
750            self.figure.subplots_adjust(left=0.1, bottom=0.1)
751        if len(label) > 0 and xpos > 0 and ypos > 0:
752            new_text = self.subplot.text(str(xpos), str(ypos), str(label),
753                                           fontproperties=font)
754            self.textList.append(new_text) 
755       
756    def erase_legend(self):
757        """
758        Remove Legend
759        """
760        #for ax in self.axes:
761        self.remove_legend(self.subplot)
762                     
763    def onMouseMotion(self, event):
764        """
765        Disable dragging 2D image
766        """
767   
768    def onWheel(self, event):
769        """
770        """
771     
772    def onLeftDown(self, event):
773        """
774        Disables LeftDown
775        """
776   
777    def onPick(self, event):
778        """
779        Remove Legend
780        """
781        for ax in self.axes:
782            self.remove_legend(ax)
783                       
784   
785    def draw(self):
786        """
787        Draw
788        """
789        if self.dimension == 3:
790            pass
791        else:
792            self.subplot.figure.canvas.resizing = False
793            self.subplot.tick_params(axis='both', labelsize=9)     
794            self.erase_legend()
795            self.subplot.figure.canvas.draw_idle() 
796            try:
797                self.figure.delaxes(self.figure.axes[1])
798            except:
799                pass
800           
801       
802    def onContextMenu(self, event):
803        """
804        Default context menu for a plot panel
805        """
806        id = wx.NewId()
807        slicerpop = wx.Menu()
808        data = self.point
809        if issubclass(data.__class__, Data1D):
810            slicerpop.Append(id, '&Change Scale')
811            wx.EVT_MENU(self, id, self._onProperties)
812        else:
813            slicerpop.Append(id, '&Toggle Linear/Log Scale')
814            wx.EVT_MENU(self, id, self.ontogglescale)     
815        try:
816            # mouse event
817            pos_evt = event.GetPosition()
818            pos = self.ScreenToClient(pos_evt)
819        except:
820            # toolbar event
821            pos_x, pos_y = self.toolbar.GetPositionTuple()
822            pos = (pos_x, pos_y + 5)
823        self.PopupMenu(slicerpop, pos)
824       
825    def ontogglescale(self, event): 
826        """
827        On toggle 2d scale
828        """
829        self._onToggleScale(event) 
830        try:
831            # mpl >= 1.1.0
832            self.figure.tight_layout()
833        except:
834            self.figure.subplots_adjust(left=0.1, bottom=0.1) 
835        try:
836            self.figure.delaxes(self.figure.axes[1])
837        except:
838            pass
839       
840    def _onProperties(self, event):
841        """
842        when clicking on Properties on context menu ,
843        The Property dialog is displayed
844        The user selects a transformation for x or y value and
845        a new plot is displayed
846        """
847        list = []
848        list = self.graph.returnPlottable()
849        if len(list.keys()) > 0:
850            first_item = list.keys()[0]
851            if first_item.x != []:
852                from danse.common.plottools.PropertyDialog import Properties
853                dial = Properties(self, -1, 'Change Scale')
854                # type of view or model used
855                dial.xvalue.Clear()
856                dial.yvalue.Clear()
857                dial.view.Clear()
858                dial.xvalue.Insert("x", 0)
859                dial.xvalue.Insert("log10(x)", 1)
860                dial.yvalue.Insert("y", 0)
861                dial.yvalue.Insert("log10(y)", 1)
862                dial.view.Insert("--", 0)
863                dial.view.Insert("Linear y vs x", 1)
864                dial.xvalue.SetValue("x")
865                dial.yvalue.SetValue("y")
866                dial.view.SetValue("--")
867                dial.Update()
868                if dial.ShowModal() == wx.ID_OK:
869                    self.xLabel, self.yLabel, self.viewModel = dial.getValues()
870                    if self.viewModel == "Linear y vs x":
871                        self.xLabel = "x"
872                        self.yLabel = "y"
873                        self.viewModel = "--"
874                        dial.setValues(self.xLabel, self.yLabel, self.viewModel)
875                    self._onEVT_FUNC_PROPERTY()
876                dial.Destroy()
877         
878    def _onEVT_FUNC_PROPERTY(self, remove_fit=True):
879        """
880        Receive the x and y transformation from myDialog,
881        Transforms x and y in View
882        and set the scale
883        """
884        list = []
885        list = self.graph.returnPlottable()
886        # Changing the scale might be incompatible with
887        # currently displayed data (for instance, going
888        # from ln to log when all plotted values have
889        # negative natural logs).
890        # Go linear and only change the scale at the end.
891        self.set_xscale("linear")
892        self.set_yscale("linear")
893        _xscale = 'linear'
894        _yscale = 'linear'
895        for item in list:
896            item.setLabel(self.xLabel, self.yLabel)
897            # control axis labels from the panel itself
898            yname, yunits = item.get_yaxis()
899            xname, xunits = item.get_xaxis()
900            # Goes through all possible scales
901            # Goes through all possible scales
902            if(self.xLabel == "x"):
903                item.transformX(transform.toX, transform.errToX)
904                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
905            if(self.xLabel == "log10(x)"):
906                item.transformX(transform.toX_pos, transform.errToX_pos)
907                _xscale = 'log'
908                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
909            if(self.yLabel == "y"):
910                item.transformY(transform.toX, transform.errToX)
911                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
912            if(self.yLabel == "log10(y)"):
913                item.transformY(transform.toX_pos, transform.errToX_pos)
914                _yscale = 'log'
915                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
916            item.transformView()
917        self.set_xscale(_xscale)
918        self.set_yscale(_yscale)
919        self.draw()
920       
921class DataOperatorWindow(wx.Frame):
922    def __init__(self, parent, *args, **kwds):
923        kwds["size"] = (PANEL_WIDTH, PANEL_HEIGTH)
924        wx.Frame.__init__(self, parent, *args, **kwds)
925        self.parent = parent
926        self.panel = DataOperPanel(parent=self)
927        wx.EVT_CLOSE(self, self.OnClose)
928        self.CenterOnParent()
929        self.Show()
930   
931    def OnClose(self, event=None): 
932        """
933        On close event
934        """
935        self.Show(False)
936
937       
938if __name__ == "__main__":
939   
940    app  = wx.App()
941    window = DataOperatorWindow(parent=None, data=[], title="Data Editor")
942    app.MainLoop()
943 
Note: See TracBrowser for help on using the repository browser.