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

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 657e52c was 657e52c, checked in by Jae Cho <jhjcho@…>, 11 years ago

merging from the release 2.2.0

  • Property mode set to 100644
File size: 34.4 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        if ON_MAC:
468            self.fill_data_combox()
469        else:
470            children = self.GetChildren()
471            # update the list only when it is on the top
472            if self.FindFocus() in children:
473                self.fill_data_combox()
474         
475    def fill_oprator_combox(self):
476        """
477        fill the current combobox with the operator
478        """   
479        operator_list = [' +', ' -', ' *', " /", " |"]
480        for oper in operator_list:
481            pos = self.operator_cbox.Append(str(oper))
482            self.operator_cbox.SetClientData(pos, str(oper.strip()))
483        self.operator_cbox.SetSelection(0)
484       
485       
486    def fill_data_combox(self):
487        """
488        fill the current combobox with the available data
489        """
490        pos_pre1 = self.data1_cbox.GetCurrentSelection()
491        pos_pre2 = self.data2_cbox.GetCurrentSelection()
492        current1 = self.data1_cbox.GetLabel()
493        current2 = self.data2_cbox.GetLabel()
494        if pos_pre1 < 0:
495            pos_pre1 = 0
496        if pos_pre2 < 0:
497            pos_pre2 = 0
498        self.data1_cbox.Clear()
499        self.data2_cbox.Clear()
500
501        if not self._data:
502            pos = self.data1_cbox.Append('No Data Available')
503            self.data1_cbox.SetSelection(pos)
504            self.data1_cbox.SetClientData(pos, None)
505            pos2 = self.data2_cbox.Append('No Data Available')
506            self.data2_cbox.SetSelection(pos2)
507            self.data2_cbox.SetClientData(pos2, None)
508            return
509        pos1 = self.data1_cbox.Append('Select Data')
510        self.data1_cbox.SetSelection(pos1)
511        self.data1_cbox.SetClientData(pos1, None)
512        pos2 = self.data2_cbox.Append('Select Data')
513        self.data2_cbox.SetSelection(pos2)
514        self.data2_cbox.SetClientData(pos2, None)
515        pos3 = self.data2_cbox.Append('Number')
516        val = None
517        if (self.numberctr.IsShown() and self.numberctr.IsEnabled()):
518            try:
519                val = float(self.numberctr.GetValue())
520            except:
521                val = None
522        self.data2_cbox.SetClientData(pos3, val)
523        dnames = []
524        ids = self._data.keys()
525        for id in ids:
526            if id != None:
527                if self._data[id].data != None:
528                    dnames.append(self._data[id].data.name)
529                else:
530                    theory_list = self._data[id].get_theory()
531                    theory, _ = theory_list.values()[0]
532                    dnames.append(theory.name)
533        ind = numpy.argsort(dnames)
534        if len(ind) > 0:
535            val_list = numpy.array(self._data.values())[ind]
536            for datastate in val_list:
537                data = datastate.data
538                if data != None:
539                    name = data.name
540                    pos1 = self.data1_cbox.Append(str(name))
541                    self.data1_cbox.SetClientData(pos1, data)
542                    pos2 = self.data2_cbox.Append(str(name))
543                    self.data2_cbox.SetClientData(pos2, data)
544                    if str(current1) == str(name):
545                      pos_pre1 = pos1
546                    if str(current2) == str(name):
547                      pos_pre2 = pos2
548                try:
549                    theory_list = datastate.get_theory()
550                    for theory, _ in theory_list.values():
551                        th_name = theory.name
552                        posth1 = self.data1_cbox.Append(str(th_name))
553                        self.data1_cbox.SetClientData(posth1, theory)
554                        posth2 = self.data2_cbox.Append(str(th_name))
555                        self.data2_cbox.SetClientData(posth2, theory)
556                        if str(current1) == str(th_name):
557                            pos_pre1 = posth1
558                        if str(current2) == str(th_name):
559                            pos_pre2 = posth2
560                except:
561                    continue 
562        self.data1_cbox.SetSelection(pos_pre1)
563        self.data2_cbox.SetSelection(pos_pre2)
564   
565    def get_datalist(self):
566        """
567        """
568        data_manager = self.parent.parent.get_data_manager()
569        if data_manager != None:
570            return  data_manager.get_all_data()
571        else:
572            return {}
573           
574    def on_click_apply(self, event):
575        """   
576        changes are saved in data object imported to edit
577        """
578        self.send_warnings('')
579        self.data_namectr.SetBackgroundColour('white')
580        state_list = self.get_datalist().values()
581        name = self.data_namectr.GetValue().strip()
582        name_list = []
583        for state in state_list:
584            if state.data == None:
585                theory_list = state.get_theory()
586                theory, _ = theory_list.values()[0]
587                d_name = str(theory.name)
588            else:
589                d_name = str(state.data.name)
590            name_list.append(d_name)
591        if name in name_list:
592            self._set_textctrl_color(self.data_namectr, 'pink')
593            msg = "The Output Data Name already exists...   "
594            wx.MessageBox(msg, 'Error')
595            return
596        if name == '':
597            self._set_textctrl_color(self.data_namectr, 'pink')
598            msg = "Please type the output data name first...   "
599            wx.MessageBox(msg, 'Error')
600            return
601        if self.output == None:
602            msg = "No Output Data has been generated...   "
603            wx.MessageBox(msg, 'Error')
604            return
605        # send data to data manager
606        self.output.name = name
607        self.output.run = "Data Operation"
608        self.output.instrument = "SansView"
609        self.output.id = str(name) + str(time.time())
610        data = {self.output.id :self.output}
611        self.parent.parent.add_data(data)
612        self.name_sizer.Layout()
613        self.Refresh()
614        #must post event here
615        event.Skip()
616   
617    def on_close(self, event):
618        """
619        leave data as it is and close
620        """
621        self.parent.OnClose()
622       
623    def set_plot_unfocus(self):
624        """
625        Unfocus on right click
626        """
627   
628    def send_warnings(self, msg='', info='info'):
629        """
630        Send warning to status bar
631        """
632        wx.PostEvent(self.parent.parent, StatusEvent(status=msg, info=info))
633         
634class SmallPanel(PlotPanel):
635    """
636    PlotPanel for Quick plot and masking plot
637    """
638    def __init__(self, parent, id=-1, is_number=False, content='?', **kwargs):
639        """
640        """ 
641        PlotPanel.__init__(self, parent, id=id, **kwargs)
642        self.is_number = is_number
643        self.content = content
644        self.point = None
645        self.position = (0.4, 0.5)
646        self.scale = 'linear'
647        self.prevXtrans = "x"
648        self.prevYtrans = "y"
649        self.viewModel = "--"
650        self.subplot.set_xticks([])
651        self.subplot.set_yticks([])
652        self.add_text()
653        self.figure.subplots_adjust(left=0.1, bottom=0.1)
654       
655    def set_content(self, content=''):
656        """
657        Set text content
658        """
659        self.content = str(content)
660         
661    def add_toolbar(self):
662        """
663        Add toolbar
664        """
665        # Not implemented
666        pass
667   
668    def on_set_focus(self, event):
669        """
670        send to the parenet the current panel on focus
671        """
672        pass
673
674    def add_image(self, plot):
675        """
676        Add Image
677        """
678        self.content = ''
679        self.textList = []
680        self.plots = {}
681        self.clear()
682        self.point = plot
683        try:
684            self.figure.delaxes(self.figure.axes[0])
685            self.subplot = self.figure.add_subplot(111)
686            #self.figure.delaxes(self.figure.axes[1])
687        except:
688            pass
689        try:
690            name = plot.name
691        except:
692            name = plot.filename
693        self.plots[name] = plot
694
695        #init graph
696        self.graph = Graph()
697
698        #add plot
699        self.graph.add(plot)
700        #draw       
701        self.graph.render(self)
702       
703        try:
704            self.figure.delaxes(self.figure.axes[1])
705        except:
706            pass
707        self.subplot.figure.canvas.resizing = False
708        self.subplot.tick_params(axis='both', labelsize=9)
709        # Draw zero axis lines
710        self.subplot.axhline(linewidth = 1, color='r') 
711        self.subplot.axvline(linewidth = 1, color='r')       
712
713        self.erase_legend()
714        try:
715            # mpl >= 1.1.0
716            self.figure.tight_layout()
717        except:
718            self.figure.subplots_adjust(left=0.1, bottom=0.1)
719        self.subplot.figure.canvas.draw()
720
721    def add_text(self):
722        """
723        Text in the plot
724        """
725        if not self.is_number:
726            return
727
728        self.clear()
729        try:
730            self.figure.delaxes(self.figure.axes[0])
731            self.subplot = self.figure.add_subplot(111)
732            self.figure.delaxes(self.figure.axes[1])
733        except:
734            pass
735        self.subplot.set_xticks([])
736        self.subplot.set_yticks([])
737        label = self.content
738        FONT = FontProperties()
739        xpos, ypos = (0.4, 0.5)
740        font = FONT.copy()
741        font.set_size(14)
742
743        self.textList = []
744        self.subplot.set_xlim((0, 1))
745        self.subplot.set_ylim((0, 1))
746       
747        try:
748            if self.content != '?':
749                float(label)
750        except:
751            self.subplot.set_frame_on(False)
752        try:
753            # mpl >= 1.1.0
754            self.figure.tight_layout()
755        except:
756            self.figure.subplots_adjust(left=0.1, bottom=0.1)
757        if len(label) > 0 and xpos > 0 and ypos > 0:
758            new_text = self.subplot.text(str(xpos), str(ypos), str(label),
759                                           fontproperties=font)
760            self.textList.append(new_text) 
761       
762    def erase_legend(self):
763        """
764        Remove Legend
765        """
766        #for ax in self.axes:
767        self.remove_legend(self.subplot)
768                     
769    def onMouseMotion(self, event):
770        """
771        Disable dragging 2D image
772        """
773   
774    def onWheel(self, event):
775        """
776        """
777     
778    def onLeftDown(self, event):
779        """
780        Disables LeftDown
781        """
782   
783    def onPick(self, event):
784        """
785        Remove Legend
786        """
787        for ax in self.axes:
788            self.remove_legend(ax)
789                       
790   
791    def draw(self):
792        """
793        Draw
794        """
795        if self.dimension == 3:
796            pass
797        else:
798            self.subplot.figure.canvas.resizing = False
799            self.subplot.tick_params(axis='both', labelsize=9)     
800            self.erase_legend()
801            self.subplot.figure.canvas.draw_idle() 
802            try:
803                self.figure.delaxes(self.figure.axes[1])
804            except:
805                pass
806           
807       
808    def onContextMenu(self, event):
809        """
810        Default context menu for a plot panel
811        """
812        id = wx.NewId()
813        slicerpop = wx.Menu()
814        data = self.point
815        if issubclass(data.__class__, Data1D):
816            slicerpop.Append(id, '&Change Scale')
817            wx.EVT_MENU(self, id, self._onProperties)
818        else:
819            slicerpop.Append(id, '&Toggle Linear/Log Scale')
820            wx.EVT_MENU(self, id, self.ontogglescale)     
821        try:
822            # mouse event
823            pos_evt = event.GetPosition()
824            pos = self.ScreenToClient(pos_evt)
825        except:
826            # toolbar event
827            pos_x, pos_y = self.toolbar.GetPositionTuple()
828            pos = (pos_x, pos_y + 5)
829        self.PopupMenu(slicerpop, pos)
830       
831    def ontogglescale(self, event): 
832        """
833        On toggle 2d scale
834        """
835        self._onToggleScale(event) 
836        try:
837            # mpl >= 1.1.0
838            self.figure.tight_layout()
839        except:
840            self.figure.subplots_adjust(left=0.1, bottom=0.1) 
841        try:
842            self.figure.delaxes(self.figure.axes[1])
843        except:
844            pass
845       
846    def _onProperties(self, event):
847        """
848        when clicking on Properties on context menu ,
849        The Property dialog is displayed
850        The user selects a transformation for x or y value and
851        a new plot is displayed
852        """
853        list = []
854        list = self.graph.returnPlottable()
855        if len(list.keys()) > 0:
856            first_item = list.keys()[0]
857            if first_item.x != []:
858                from danse.common.plottools.PropertyDialog import Properties
859                dial = Properties(self, -1, 'Change Scale')
860                # type of view or model used
861                dial.xvalue.Clear()
862                dial.yvalue.Clear()
863                dial.view.Clear()
864                dial.xvalue.Insert("x", 0)
865                dial.xvalue.Insert("log10(x)", 1)
866                dial.yvalue.Insert("y", 0)
867                dial.yvalue.Insert("log10(y)", 1)
868                dial.view.Insert("--", 0)
869                dial.view.Insert("Linear y vs x", 1)
870                dial.setValues(self.prevXtrans, self.prevYtrans, self.viewModel)
871                dial.Update()
872                if dial.ShowModal() == wx.ID_OK:
873                    self.xLabel, self.yLabel, self.viewModel = dial.getValues()
874                    if self.viewModel == "Linear y vs x":
875                        self.xLabel = "x"
876                        self.yLabel = "y"
877                        self.viewModel = "--"
878                        dial.setValues(self.xLabel, self.yLabel, self.viewModel)
879                    self._onEVT_FUNC_PROPERTY()
880                dial.Destroy()
881         
882    def _onEVT_FUNC_PROPERTY(self, remove_fit=True):
883        """
884        Receive the x and y transformation from myDialog,
885        Transforms x and y in View
886        and set the scale
887        """
888        list = []
889        list = self.graph.returnPlottable()
890        # Changing the scale might be incompatible with
891        # currently displayed data (for instance, going
892        # from ln to log when all plotted values have
893        # negative natural logs).
894        # Go linear and only change the scale at the end.
895        self.set_xscale("linear")
896        self.set_yscale("linear")
897        _xscale = 'linear'
898        _yscale = 'linear'
899        for item in list:
900            item.setLabel(self.xLabel, self.yLabel)
901            # control axis labels from the panel itself
902            yname, yunits = item.get_yaxis()
903            xname, xunits = item.get_xaxis()
904            # Goes through all possible scales
905            # Goes through all possible scales
906            if(self.xLabel == "x"):
907                item.transformX(transform.toX, transform.errToX)
908                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
909            if(self.xLabel == "log10(x)"):
910                item.transformX(transform.toX_pos, transform.errToX_pos)
911                _xscale = 'log'
912                self.graph._xaxis_transformed("%s" % xname, "%s" % xunits)
913            if(self.yLabel == "y"):
914                item.transformY(transform.toX, transform.errToX)
915                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
916            if(self.yLabel == "log10(y)"):
917                item.transformY(transform.toX_pos, transform.errToX_pos)
918                _yscale = 'log'
919                self.graph._yaxis_transformed("%s" % yname, "%s" % yunits)
920            item.transformView()
921        self.prevXtrans = self.xLabel
922        self.prevYtrans = self.yLabel
923        self.set_xscale(_xscale)
924        self.set_yscale(_yscale)
925        self.draw()
926       
927class DataOperatorWindow(wx.Frame):
928    def __init__(self, parent, *args, **kwds):
929        kwds["size"] = (PANEL_WIDTH, PANEL_HEIGTH)
930        wx.Frame.__init__(self, parent, *args, **kwds)
931        self.parent = parent
932        self.panel = DataOperPanel(parent=self)
933        wx.EVT_CLOSE(self, self.OnClose)
934        self.CenterOnParent()
935        self.Show()
936   
937    def OnClose(self, event=None): 
938        """
939        On close event
940        """
941        self.Show(False)
942
943       
944if __name__ == "__main__":
945   
946    app  = wx.App()
947    window = DataOperatorWindow(parent=None, data=[], title="Data Editor")
948    app.MainLoop()
949 
Note: See TracBrowser for help on using the repository browser.