source: sasview/sansguiframe/src/sans/guiframe/data_processor.py @ 656d65d

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 656d65d was 656d65d, checked in by Gervaise Alina <gervyh@…>, 13 years ago

working on data processor

  • Property mode set to 100644
File size: 31.4 KB
Line 
1"""
2Implement grid used to store data
3"""
4import wx
5import numpy
6import math
7import re
8import os
9import sys
10import copy
11from wx.lib.scrolledpanel import ScrolledPanel
12import  wx.grid as  Grid
13import wx.aui
14from wx.aui import AuiNotebook as nb
15from sans.guiframe.panel_base import PanelBase
16import wx.lib.sheet as sheet
17
18from sans.guiframe.events import NewPlotEvent
19from sans.guiframe.events import StatusEvent 
20from sans.guiframe.dataFitting import Data1D
21
22FUNC_DICT = {"sqrt": "math.sqrt",
23             "pow": "math.sqrt"}
24
25
26def parse_string(sentence, list):
27    """
28    Return a dictionary of column label and index or row selected
29    :param sentence: String to parse
30    :param list: list of columns label
31    """
32    toks = []
33    p2 = re.compile(r'\d+')
34    p = re.compile(r'[\+\-\*\%\/]')
35    labels = p.split(sentence)
36    col_dict = {}
37    for elt in labels:
38        rang = None
39        temp_arr = []
40        for label in  list:
41            label_pos =  elt.find(label)
42            if label_pos != -1:
43                if elt.count(',') > 0:
44                    new_temp = []
45                    temp = elt.split(label)
46                    for item in temp:
47                        range_pos = item.find(":")
48                        if range_pos != -1:
49                            rang = p2.findall(item)
50                            for i in xrange(int(rang[0]), int(rang[1])+1 ):
51                                new_temp.append(i)
52                    temp_arr += new_temp
53                else:
54                    temp = elt.split(label)
55                    for item in temp:
56                        range_pos = item.find(":")
57                        if range_pos != -1:
58                            rang = p2.findall(item)
59                            for i in xrange(int(rang[0]), int(rang[1])+1 ):
60                                temp_arr.append(i)
61                col_dict[elt] = (label, temp_arr)
62    return col_dict
63
64class GridPage(sheet.CSheet):
65    """
66    """
67    def __init__(self, parent, panel=None):
68        """
69        """
70        sheet.CSheet.__init__(self, parent)
71        self.SetLabelBackgroundColour('#DBD4D4')
72        self.AutoSize()
73        self.panel = panel
74        self.col_names = []
75        self.data_inputs = {}
76        self.data_outputs = {}
77        self.data = {}
78        self._cols = 50
79        self._rows = 51
80        col_with = 30
81        row_height = 20
82        self.max_row_touse = 0
83        self.axis_value = []
84        self.axis_label = ""
85        self.selected_cells = []
86        self.selected_cols = []
87        self.SetColMinimalAcceptableWidth(col_with)
88        self.SetRowMinimalAcceptableHeight(row_height)
89        self.SetNumberRows(self._cols)
90        self.SetNumberCols(self._rows)
91        self.Bind(wx.grid.EVT_GRID_LABEL_LEFT_CLICK, self.on_left_click)
92        self.Bind(wx.grid.EVT_GRID_LABEL_RIGHT_CLICK, self.on_right_click)
93        self.Bind(wx.grid.EVT_GRID_CELL_LEFT_CLICK, self.on_selected_cell)
94        self.Bind(wx.grid.EVT_GRID_CMD_CELL_CHANGE, self.on_edit_cell)
95       
96    def on_edit_cell(self, event):
97        """
98        """
99        row, col = event.GetRow(), event.GetCol()
100        if row > self.max_row_touse:
101            self.max_row_touse = row
102        event.Skip()
103       
104    def on_selected_cell(self, event):
105        """
106        Handler catching cell selection
107        """
108        flag = event.CmdDown() or event.ControlDown()
109        row, col = event.GetRow(), event.GetCol()
110        cell = (row, col)
111        if not flag:
112            self.selected_cells = []
113            self.axis_value = []
114            self.axis_label = ""
115        if cell in self.selected_cells:
116            self.selected_cells.remove(cell)
117        else:
118            self.selected_cells.append(cell)
119        if row > 1:
120            if (cell) in self.selected_cells:
121                self.axis_value.append(self.GetCellValue(row, col))
122        self.axis_label = self.GetCellValue(row, col)
123        event.Skip()
124     
125    def on_left_click(self, event):
126        """
127        Catch the left click on label mouse event
128        """
129        flag = event.CmdDown() or event.ControlDown()
130        col = event.GetCol()
131        event.Skip()
132       
133    def on_right_click(self, event):
134        """
135        Catch the right click mouse
136        """
137       
138        col = event.GetCol()
139        self.selected_cols = []
140        self.selected_cols.append(col)
141        # Slicer plot popup menu
142        slicerpop = wx.Menu()
143        col_label_menu  = wx.Menu()
144        slicerpop.AppendSubMenu(col_label_menu , 
145                                 '&Insert Column', 'Insert Column')
146        hint = 'Insert empty column before the selected column'
147        id = wx.NewId()
148        col_label_menu.Append(id, '&Empty', hint)
149        wx.EVT_MENU(self, id, self.on_insert_column)
150        col_name = [self.GetCellValue(0, col) 
151                        for col in range(self.GetNumberCols())]
152        for label in self.data.keys():
153            if label not in col_name:
154                id = wx.NewId()
155                hint = 'Insert %s column before the selected column' % str(label)
156                col_label_menu.Append(id, '&%s' % str(label), hint)
157                wx.EVT_MENU(self, id, self.on_insert_column)
158        id = wx.NewId()   
159        hint = 'Remove selected column %s'
160        slicerpop.Append(id, 
161                                 '&Remove Column', hint)
162        wx.EVT_MENU(self, id, self.on_remove_column)
163       
164        pos = wx.GetMousePosition()
165        pos = self.ScreenToClient(pos)
166        self.PopupMenu(slicerpop, pos)
167       
168    def on_remove_column(self, event):
169        """
170        """
171        if self.selected_cols is not None or len(self.selected_cols) > 0:
172            col = self.selected_cols[0]
173            # add data to the grid   
174            row = 0
175            id = event.GetId()
176            col_name = self.GetCellValue(row, col)
177            self.data[col_name] = []
178            for row in range(1, self.GetNumberRows()):
179                if row <= self.max_row_touse:
180                    value = self.GetCellValue(row, col)
181                    self.data[col_name].append(value)
182                    for k , value_list in self.data.iteritems():
183                        if k != col_name:
184                            length = len(value_list)
185                            if length < self.max_row_touse:
186                                diff = self.max_row_touse - length
187                                for i in range(diff):
188                                    self.data[k].append("")
189            self.DeleteCols(pos=col, numCols=1, updateLabels=True)
190            self.AutoSize()
191           
192    def on_insert_column(self, event):
193        """
194        """
195        if self.selected_cols is not None or len(self.selected_cols) > 0:
196            col = self.selected_cols[0]
197            # add data to the grid   
198            row = 0
199            id = event.GetId()
200            col_name = event.GetEventObject().GetLabelText(id)
201            self.InsertCols(pos=col, numCols=1, updateLabels=True)
202            if col_name.strip() != "Empty":
203                self.SetCellValue(row, col, str(col_name.strip()))
204            if col_name in self.data.keys():
205                value_list = self.data[col_name]
206                cell_row =  1
207                for value in value_list:
208                    self.SetCellValue(cell_row, col, str(value))
209                    cell_row += 1
210            self.AutoSize()
211               
212       
213    def on_set_x_axis(self, event):
214        """
215        """
216        self.panel.set_xaxis(x=self.axis_value, label=self.axis_label)
217   
218    def on_set_y_axis(self, event):
219        """
220        """
221        self.panel.set_yaxis(y=self.axis_value, label=self.axis_label)     
222           
223    def set_data(self, data_inputs, data_outputs):
224        """
225        Add data to the grid
226        """
227        if data_outputs is None:
228            data_outputs = {}
229        self.data_outputs = data_outputs
230        if self.data_inputs is None:
231            data_inputs = {}
232        self.data_inputs = data_inputs
233        self.data = {}
234        for item in (self.data_outputs, self.data_inputs):
235            self.data.update(item)
236        if  len(self.data_outputs) > 0:
237            self._cols = self.GetNumberCols()
238            self._rows = self.GetNumberRows()
239            self.col_names = self.data_outputs.keys()
240            self.col_names.sort() 
241            nbr_user_cols = len(self.col_names)
242            #Add more columns to the grid if necessary
243            if nbr_user_cols > self._cols:
244                new_col_nbr = nbr_user_cols -  self._cols
245                self.AppendCols(new_col_nbr, True)
246            #Add more rows to the grid if necessary 
247            nbr_user_row = len(self.data_outputs.values())
248            if nbr_user_row > self._rows + 1:
249                new_row_nbr =  nbr_user_row - self._rows
250                self.AppendRows(new_row_nbr, True)
251            # add data to the grid   
252            row = 0
253            col = 0
254            cell_col = 0
255            for col_name in  self.col_names:
256                # use the first row of the grid to add user defined labels
257                self.SetCellValue(row, col, str(col_name))
258                col += 1
259                cell_row =  1
260                value_list = self.data_outputs[col_name]
261               
262                for value in value_list:
263                    self.SetCellValue(cell_row, cell_col, str(value))
264                    cell_row += 1
265                cell_col += 1
266                if cell_row > self.max_row_touse:
267                    self.max_row_touse = cell_row
268            self.AutoSize()
269           
270
271class Notebook(nb, PanelBase):
272    """
273    ## Internal name for the AUI manager
274    window_name = "Fit panel"
275    ## Title to appear on top of the window
276    """
277    window_caption = "Notebook "
278   
279    def __init__(self, parent, manager=None, data=None, *args, **kwargs):
280        """
281        """
282        nb.__init__(self, parent, -1,
283                    style= wx.aui.AUI_BUTTON_DOWN|
284                    wx.aui.AUI_NB_WINDOWLIST_BUTTON|
285                    wx.aui.AUI_NB_DEFAULT_STYLE|
286                    wx.CLIP_CHILDREN)
287        PanelBase.__init__(self, parent)
288        self.enable_close_button()
289        self.parent = parent
290        self.manager = manager
291        self.data = data
292        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CLOSE, self.on_close_page)
293   
294    def enable_close_button(self):
295        """
296        display the close button on tab for more than 1 tabs else remove the
297        close button
298        """
299        if self.GetPageCount() <= 1:
300            style = self.GetWindowStyleFlag() 
301            flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
302            if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB == flag:
303                style = style & ~wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
304                self.SetWindowStyle(style)
305        else:
306            style = self.GetWindowStyleFlag()
307            flag = wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
308            if style & wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB != flag:
309                style |= wx.aui.AUI_NB_CLOSE_ON_ACTIVE_TAB
310                self.SetWindowStyle(style)
311             
312    def on_edit_axis(self):
313        """
314        Return the select cell of a given selected column
315        """
316        pos = self.GetSelection()
317        grid = self.GetPage(pos)
318        if len(grid.selected_cols) > 1:
319            msg = "Edit axis doesn't understand this selection.\n"
320            msg += "Please select only one column"
321            raise ValueError, msg
322        list_of_cells = []
323        if len(grid.selected_cols) == 1:
324            col = grid.selected_cols[0]
325            if len(grid.selected_cells) > 0:
326                cell_row, cell_col = grid.selected_cells[0]
327                if cell_col  != col:
328                    msg = "Edit axis doesn't understand this selection.\n"
329                    msg += "Please select element of the same col"
330                    raise ValueError, msg
331            for row in range(grid.GetNumberRows()):
332                list_of_cells.append((row + 1 , col))
333            for item in grid.selected_cells:
334                if item in list_of_cells:
335                    list_of_cells.remove(item)
336        elif len(grid.selected_cols) == 0:
337            list_of_cells = [(row + 1, col) for row, col in grid.selected_cells]
338        return list_of_cells
339   
340    def get_column_labels(self):
341        """
342        return dictionary of columns labels of the current page
343        """
344        pos = self.GetSelection()
345        grid = self.GetPage(pos)
346        labels = {}
347        row = 0
348        for col in range(grid.GetNumberCols()):
349            label = grid.GetCellValue(row, col)
350            if label.strip() != "" :
351                labels[label.strip()] = col
352        return labels
353       
354    def create_axis_label(self, cell_list):
355        """
356        Receive a list of cells and  create a string presenting the selected
357        cells.
358        :param cell_list: list of tuple
359       
360        """
361        pos = self.GetSelection()
362        grid = self.GetPage(pos)
363        label = ""
364        col_name = ""
365        if len(cell_list) > 0:
366            temp_list = copy.deepcopy(cell_list)
367            temp_list.sort()
368            temp = []
369            for item in temp_list:
370                if item[0] <= 1:
371                    temp.append(item)
372            for element in temp:
373                temp_list.remove(element)
374            row_min, col  = temp_list[0]   
375            row_max = row_min
376            col_name = grid.GetCellValue(0, col)
377            label += str(col_name) + "[" + str(row_min) + ":"
378            for index in xrange(len(temp_list)):
379                prev = index - 1
380                row, _ = temp_list[index]
381                if row > row_max + 1 :
382                    if prev > 0:
383                        row_max, _ = temp_list[prev]
384                        label += str(row_max) + "]" + ","
385                        row_min = row
386                        label  += "[" + str(row_min) + ":"
387                row_max = row
388                if (index == len(temp_list)- 1):
389                    label +=  str(row_max) + "]"     
390        return label, col_name
391   
392    def on_close_page(self, event):
393        """
394        close the page
395        """
396        if self.GetPageCount() == 1:
397            event.Veto()
398        self.enable_close_button()
399       
400    def set_data(self, data_inputs, data_outputs):
401        if data_outputs is None or data_outputs == {}:
402            return
403        grid = GridPage(self, panel=self.parent)
404        grid.set_data(data_inputs, data_outputs) 
405        self.AddPage(grid, "")
406        pos = self.GetPageIndex(grid)
407        title = "Batch " + str(self.GetPageCount())
408        self.SetPageText(pos, title)
409       
410    def add_column(self):
411        """
412        Append a new column to the grid
413        """
414        pos = self.GetSelection()
415        grid = self.GetPage(pos)
416        grid.AppendCols(1, True)
417       
418    def on_remove_column(self):
419        """
420        Remove column to the current grid
421        """
422        pos = self.GetSelection()
423        grid = self.GetPage(pos)
424        cols_pos = grid.GetSelectedCols() 
425        for cpos in cols_pos:
426            grid.DeleteCols(cpos)
427         
428         
429class SPanel(ScrolledPanel):
430    def __init__(self, parent, *args, **kwds):
431        ScrolledPanel.__init__(self, parent , *args, **kwds)
432        self.SetupScrolling() 
433
434
435class GridPanel(SPanel):
436    def __init__(self, parent, data_inputs=None,
437                 data_outputs=None, *args, **kwds):
438        SPanel.__init__(self, parent , *args, **kwds)
439        self.vbox = wx.BoxSizer(wx.VERTICAL)
440       
441        self.plotting_sizer = wx.FlexGridSizer(3, 7, 10, 5)
442        self.grid_sizer = wx.BoxSizer(wx.HORIZONTAL)
443        self.vbox.AddMany([(self.grid_sizer, 1, wx.EXPAND, 0),
444                           (wx.StaticLine(self, -1), 0, wx.EXPAND, 0),
445                           (self.plotting_sizer)])
446        self.parent = parent
447        self._data_inputs = data_inputs
448        self._data_outputs = data_outputs
449        self.x = []
450        self.= []
451        self.x_axis_label = None
452        self.y_axis_label = None
453        self.x_axis_title = None
454        self.y_axis_title = None
455        self.x_axis_unit = None
456        self.y_axis_unit = None
457        self.plot_button = None
458        self.notebook = None
459        self.layout_grid()
460        self.layout_plotting_area()
461        self.SetSizer(self.vbox)
462       
463    def set_data(self, data_inputs, data_outputs):
464        """
465        """
466        if self.notebook is not None:
467            self.notebook.set_data(data_inputs, data_outputs)
468       
469    def set_xaxis(self, label="", x=None):
470        """
471        """
472        if x is None:
473            x = []
474        self.x = x
475        self.x_axis_label.SetValue("%s[:]" % str(label))
476        self.x_axis_title.SetValue(str(label))
477       
478    def set_yaxis(self, label="", y=None):
479        """
480        """
481        if y is None:
482            y = []
483        self.y = y
484        self.y_axis_label.SetValue("%s[:]" % str(label))
485        self.y_axis_title.SetValue(str(label))
486       
487    def get_plot_axis(self, col, list):
488        """
489       
490        """
491        axis = []
492        pos = self.notebook.GetSelection()
493        grid = self.notebook.GetPage(pos)
494        for row in list:
495            label = grid.GetCellValue(0, col)
496            value = grid.GetCellValue(row - 1, col).strip()
497            if value != "":
498                if label.lower().strip() == "data":
499                    axis.append(float(row - 1))
500                else:
501                    axis.append(float(value))
502            else:
503                axis.append(None) 
504        return axis
505   
506    def on_save_column(self, parent):
507        """
508        """
509        pos = self.notebook.GetSelection()
510        grid = self.notebook.GetPage(pos)
511        if parent is not None and  self.data is not None:
512            parent.write_batch_tofile(data=grid.data, 
513                                               file_name=path,
514                                               details=self.details)
515       
516    def on_plot(self, event):
517        """
518        Evaluate the contains of textcrtl and plot result
519        """ 
520        pos = self.notebook.GetSelection()
521        grid = self.notebook.GetPage(pos)
522        column_names = {}
523        if grid is not None:
524            column_names = self.notebook.get_column_labels()
525        #evalue x
526        sentence = self.x_axis_label.GetValue()
527        if sentence.strip() == "":
528            msg = "select value for x axis"
529            raise ValueError, msg
530        dict = parse_string(sentence, column_names.keys())
531        for tok, (col_name, list) in dict.iteritems():
532            col = column_names[col_name]
533            xaxis = self.get_plot_axis(col, list)
534            sentence = sentence.replace(tok, 
535                                        "numpy.array(%s)" % str(xaxis))
536        for key, value in FUNC_DICT.iteritems():
537            sentence = sentence.replace(key.lower(), value)
538        x = eval(sentence)
539        #evaluate y
540        sentence = self.y_axis_label.GetValue()
541        if sentence.strip() == "":
542            msg = "select value for y axis"
543            raise ValueError, msg
544        dict = parse_string(sentence, column_names.keys())
545        for tok, (col_name, list) in dict.iteritems():
546            col = column_names[col_name]
547            yaxis = self.get_plot_axis(col, list)
548            sentence = sentence.replace(tok, 
549                                        "numpy.array(%s)" % str(yaxis))
550        for key, value in FUNC_DICT.iteritems():
551            sentence = sentence.replace(key, value)
552        y = eval(sentence)
553        #plotting
554        new_plot = Data1D(x=x, y=y)
555        new_plot.id =  wx.NewId()
556        new_plot.group_id = wx.NewId()
557        title = "%s vs %s" % (self.y_axis_title.GetValue(), 
558                              self.x_axis_title.GetValue())
559        new_plot.xaxis(self.x_axis_title.GetValue(), self.x_axis_unit.GetValue())
560        new_plot.yaxis(self.y_axis_title.GetValue(), self.y_axis_unit.GetValue())
561        try:
562            title = self.notebook.GetPageText(pos)
563            data.name = title
564            wx.PostEvent(self.parent.parent, 
565                             NewPlotEvent(plot=new_plot, 
566                        group_id=str(new_plot.group_id), title =title))   
567        except:
568            pass
569       
570    def layout_grid(self):
571        """
572        Draw the area related to the grid
573        """
574        self.notebook = Notebook(parent=self)
575        self.notebook.set_data(self._data_inputs, self._data_outputs)
576        self.grid_sizer.Add(self.notebook, 1, wx.EXPAND, 0)
577       
578    def layout_plotting_area(self):
579        """
580        Draw area containing options to plot
581        """
582        self.x_axis_title = wx.TextCtrl(self, -1)
583        self.y_axis_title = wx.TextCtrl(self, -1)
584        self.x_axis_label = wx.TextCtrl(self, -1, size=(200, -1))
585        self.y_axis_label = wx.TextCtrl(self, -1, size=(200, -1))
586        self.x_axis_add = wx.Button(self, -1, "Add")
587        self.x_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, 
588                            id=self.x_axis_add.GetId())
589        self.y_axis_add = wx.Button(self, -1, "Add")
590        self.y_axis_add.Bind(event=wx.EVT_BUTTON, handler=self.on_edit_axis, 
591                            id=self.y_axis_add.GetId())
592        self.x_axis_unit = wx.TextCtrl(self, -1)
593        self.y_axis_unit = wx.TextCtrl(self, -1)
594        self.plot_button = wx.Button(self, -1, "Plot")
595        wx.EVT_BUTTON(self, self.plot_button.GetId(), self.on_plot)
596        self.plotting_sizer.AddMany([
597                    (wx.StaticText(self, -1, "x-axis label"), 1,
598                      wx.TOP|wx.BOTTOM|wx.LEFT, 10),
599                    (self.x_axis_label, 1, wx.TOP|wx.BOTTOM, 10),
600                    (self.x_axis_add, 1, wx.TOP|wx.BOTTOM|wx.RIGHT, 10),
601                    (wx.StaticText(self, -1, "x-axis title"), 1, 
602                     wx.TOP|wx.BOTTOM|wx.LEFT, 10),
603                    (self.x_axis_title, 1, wx.TOP|wx.BOTTOM, 10),
604                    (wx.StaticText(self, -1 , "unit"), 1, 
605                     wx.TOP|wx.BOTTOM, 10),
606                    (self.x_axis_unit, 1, wx.TOP|wx.BOTTOM, 10),
607                    (wx.StaticText(self, -1, "y-axis label"), 1, 
608                     wx.BOTTOM|wx.LEFT, 10),
609                    (self.y_axis_label, wx.BOTTOM, 10),
610                    (self.y_axis_add, 1, wx.BOTTOM|wx.RIGHT, 10),
611                    (wx.StaticText(self, -1, "y-axis title"), 1, 
612                     wx.BOTTOM|wx.LEFT, 10),
613                    (self.y_axis_title,  wx.BOTTOM, 10),
614                    (wx.StaticText(self, -1 , "unit"), 1, wx.BOTTOM, 10),
615                    (self.y_axis_unit, 1, wx.BOTTOM, 10),
616                      (-1, -1),
617                      (-1, -1),
618                      (-1, -1),
619                      (-1, -1),
620                      (-1, -1),
621                      (-1, -1),
622                      (self.plot_button, 1, wx.LEFT|wx.BOTTOM, 10)])
623   
624    def on_edit_axis(self, event):
625        """
626        Get the selected column on  the visible grid and set values for axis
627        """
628        cell_list = self.notebook.on_edit_axis()
629        label, title = self.create_axis_label(cell_list)
630        tcrtl = event.GetEventObject()
631        if tcrtl == self.x_axis_add:
632            self.edit_axis_helper(self.x_axis_label, self.x_axis_title,
633                                   label, title)
634        elif tcrtl == self.y_axis_add:
635            self.edit_axis_helper(self.y_axis_label, self.y_axis_title,
636                                   label, title)
637           
638    def create_axis_label(self, cell_list):
639        """
640        Receive a list of cells and  create a string presenting the selected
641        cells.
642        :param cell_list: list of tuple
643       
644        """
645        if self.notebook is not None:
646            return self.notebook.create_axis_label(cell_list)
647   
648    def edit_axis_helper(self, tcrtl_label, tcrtl_title, label, title):
649        """
650        get controls to modify
651        """
652        tcrtl_label.SetValue(str(label))
653        tcrtl_title.SetValue(str(title))
654       
655    def add_column(self):
656        """
657        """
658        if self.notebook is not None:
659            self.notebook.add_column()
660       
661    def on_remove_column(self):
662        """
663        """
664        if self.notebook is not None:
665            self.notebook.on_remove_column()
666       
667       
668class GridFrame(wx.Frame):
669    def __init__(self, parent=None, data_inputs=None, data_outputs=None, id=-1, 
670                 title="Batch Results", size=(700, 400)):
671        wx.Frame.__init__(self, parent=parent, id=id, title=title, size=size)
672        self.parent = parent
673        self.panel = GridPanel(self, data_inputs, data_outputs)
674        menubar = wx.MenuBar()
675        self.SetMenuBar(menubar)
676        """
677        edit = wx.Menu()
678        menubar.Append(edit, "&File")
679        save_menu = edit.Append(wx.NewId(), 'Save As', 'Save into File')
680        wx.EVT_MENU(self, save_menu.GetId(), self.on_save_column)
681        """
682        self.Bind(wx.EVT_CLOSE, self.on_close)
683       
684    def on_save_column(self, event):
685        """
686        """
687        self.panel.on_save_column(self.parent)
688
689    def on_close(self, event):
690        """
691        """
692        self.Hide()
693       
694    def on_remove_column(self, event):
695        """
696        Remove the selected column to the grid
697        """
698        self.panel.on_remove_column()
699       
700    def on_insert_column(self, event):
701        """
702        Insert a new column to the grid
703        """
704        self.panel.insert_column()
705       
706    def on_append_column(self, event):
707        """
708        Append a new column to the grid
709        """
710        self.panel.add_column()
711       
712    def set_data(self, data_inputs, data_outputs):
713        """
714        """
715        self.panel.set_data(data_inputs, data_outputs)
716     
717     
718class BatchOutputFrame(wx.Frame):
719    """
720    Allow to select where the result of batch will be displayed or stored
721    """
722    def __init__(self, parent, data_inputs, data_outputs, file_name="",
723                 details="", *args, **kwds):
724        """
725        :param parent: Window instantiating this dialog
726        :param result: result to display in a grid or export to an external
727                application.
728        """
729        #kwds['style'] = wx.CAPTION|wx.SYSTEM_MENU
730        wx.Frame.__init__(self, parent, *args, **kwds)
731        self.parent = parent
732        self.panel = wx.Panel(self)
733        self.file_name = file_name
734        self.details = details
735        self.data_inputs = data_inputs
736        self.data_outputs = data_outputs
737        self.data = {}
738        for item in (self.data_outputs, self.data_inputs):
739            self.data.update(item)
740        self.flag = 1
741        self.SetSize((300, 200))
742        self.local_app_selected = None
743        self.external_app_selected = None
744        self.save_to_file = None
745        self._do_layout()
746   
747    def _do_layout(self):
748        """
749        Draw the content of the current dialog window
750        """
751        vbox = wx.BoxSizer(wx.VERTICAL)
752        box_description = wx.StaticBox(self.panel, -1, str("Batch Outputs"))
753        hint_sizer = wx.StaticBoxSizer(box_description, wx.VERTICAL)
754        selection_sizer = wx.GridBagSizer(5, 5)
755        button_sizer = wx.BoxSizer(wx.HORIZONTAL)
756        text = "Open with %s" % self.parent.application_name
757        self.local_app_selected = wx.RadioButton(self.panel, -1, text,
758                                                style=wx.RB_GROUP)
759        self.Bind(wx.EVT_RADIOBUTTON, self.onselect,
760                    id=self.local_app_selected.GetId())
761        text = "Open with Excel"
762        self.external_app_selected  = wx.RadioButton(self.panel, -1, text)
763        self.Bind(wx.EVT_RADIOBUTTON, self.onselect,
764                    id=self.external_app_selected.GetId())
765        text = "Save to File"
766        self.save_to_file = wx.CheckBox(self.panel, -1, text)
767        self.Bind(wx.EVT_CHECKBOX, self.onselect,
768                    id=self.save_to_file.GetId())
769        self.local_app_selected.SetValue(True)
770        self.external_app_selected.SetValue(False)
771        self.save_to_file.SetValue(False)
772        button_close = wx.Button(self.panel, -1, "Close")
773        button_close.Bind(wx.EVT_BUTTON, id=button_close.GetId(),
774                           handler=self.on_close)
775        button_apply = wx.Button(self.panel, -1, "Apply")
776        button_apply.Bind(wx.EVT_BUTTON, id=button_apply.GetId(),
777                        handler=self.on_apply)
778        button_apply.SetFocus()
779        hint = ""
780        hint_sizer.Add(wx.StaticText(self.panel, -1, hint))
781        hint_sizer.Add(selection_sizer)
782        #draw area containing radio buttons
783        ix = 0
784        iy = 0
785        selection_sizer.Add(self.local_app_selected, (iy, ix),
786                           (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
787        iy += 1
788        selection_sizer.Add(self.external_app_selected, (iy, ix),
789                           (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
790        iy += 1
791        selection_sizer.Add(self.save_to_file, (iy, ix),
792                           (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
793        #contruction the sizer contaning button
794        button_sizer.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
795
796        button_sizer.Add(button_close, 0,
797                        wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
798        button_sizer.Add(button_apply, 0,
799                                wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)
800        vbox.Add(hint_sizer,  0, wx.EXPAND|wx.ALL, 10)
801        vbox.Add(wx.StaticLine(self.panel, -1),  0, wx.EXPAND, 0)
802        vbox.Add(button_sizer, 0 , wx.TOP|wx.BOTTOM, 10)
803        self.SetSizer(vbox)
804       
805    def on_apply(self, event):
806        """
807        Get the user selection and display output to the selected application
808        """
809        if self.flag == 1:
810            self.parent.open_with_localapp(data_inputs=self.data_inputs,
811                                            data_outputs=self.data_outputs)
812        elif self.flag == 2:
813            self.parent.open_with_externalapp(data=self.data, 
814                                           file_name=self.file_name,
815                                           details=self.details)
816    def on_close(self, event):
817        """
818        close the Window
819        """
820        self.Close()
821       
822    def onselect(self, event=None):
823        """
824        Receive event and display data into third party application
825        or save data to file.
826       
827        """
828        if self.save_to_file.GetValue():
829            reader, ext = os.path.splitext(self.file_name)
830            path = None
831            location = os.getcwd()
832            if self.parent is not None: 
833                location = os.path.dirname(self.file_name)
834                dlg = wx.FileDialog(self, "Save Project file",
835                            location, self.file_name, ext, wx.SAVE)
836                path = None
837                if dlg.ShowModal() == wx.ID_OK:
838                    path = dlg.GetPath()
839                dlg.Destroy()
840                if path != None:
841                    if self.parent is not None and  self.data is not None:
842                        self.parent.write_batch_tofile(data=self.data, 
843                                               file_name=path,
844                                               details=self.details)
845        if self.local_app_selected.GetValue():
846            self.flag = 1
847        else:
848            self.flag = 2
849        return self.flag
850   
851 
852       
853if __name__ == "__main__":
854    app = wx.App()
855   
856    try:
857        data = {}
858        j = 0
859        for i in range(4):
860            j += 1
861            data["index"+str(i)] = [i/j, i*j, i, i+j]
862       
863        data_input =  copy.deepcopy(data)   
864        data_input["index5"] = [10,20,40, 50]
865        frame = GridFrame(data_outputs=data, data_inputs=data_input)
866        frame.Show(True)
867    except:
868        print sys.exc_value
869       
870    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.