DataTypeEditor.py
changeset 576 3f2024f30553
parent 566 6014ef82a98a
child 577 9dbb79722fbc
equal deleted inserted replaced
575:a7c706b9492e 576:3f2024f30553
    55         # The base class must be initialized *first*
    55         # The base class must be initialized *first*
    56         wx.grid.PyGridTableBase.__init__(self)
    56         wx.grid.PyGridTableBase.__init__(self)
    57         self.data = data
    57         self.data = data
    58         self.old_value = None
    58         self.old_value = None
    59         self.colnames = colnames
    59         self.colnames = colnames
    60         self.Errors = {}
    60         self.Highlights = {}
    61         self.Parent = parent
    61         self.Parent = parent
    62         # XXX
    62         # XXX
    63         # we need to store the row length and collength to
    63         # we need to store the row length and collength to
    64         # see if the table has changed size
    64         # see if the table has changed size
    65         self._rows = self.GetNumberRows()
    65         self._rows = self.GetNumberRows()
   146 
   146 
   147         Otherwise default to the default renderer.
   147         Otherwise default to the default renderer.
   148         """
   148         """
   149         
   149         
   150         for row in range(self.GetNumberRows()):
   150         for row in range(self.GetNumberRows()):
       
   151             row_highlights = self.Highlights.get(row, {})
   151             for col in range(self.GetNumberCols()):
   152             for col in range(self.GetNumberCols()):
   152                 editor = None
   153                 editor = None
   153                 renderer = None
   154                 renderer = None
   154                 colname = self.GetColLabelValue(col, False)
   155                 colname = self.GetColLabelValue(col, False)
   155                 if col != 0:
   156                 if col != 0:
   166                     grid.SetReadOnly(row, col, True)
   167                     grid.SetReadOnly(row, col, True)
   167                 
   168                 
   168                 grid.SetCellEditor(row, col, editor)
   169                 grid.SetCellEditor(row, col, editor)
   169                 grid.SetCellRenderer(row, col, renderer)
   170                 grid.SetCellRenderer(row, col, renderer)
   170                 
   171                 
   171                 if self.Errors.has_key(row) and self.Errors[row][0] == colname.lower():
   172                 highlight_colours = row_highlights.get(colname.lower(), [(wx.WHITE, wx.BLACK)])[-1]
   172                     grid.SetCellBackgroundColour(row, col, wx.Colour(255, 255, 0))
   173                 grid.SetCellBackgroundColour(row, col, highlight_colours[0])
   173                     grid.SetCellTextColour(row, col, wx.RED)
   174                 grid.SetCellTextColour(row, col, highlight_colours[1])
   174                     grid.MakeCellVisible(row, col)
   175             if wx.Platform == '__WXMSW__':
   175                 else:
   176                 grid.SetRowMinimalHeight(row, 20)
   176                     grid.SetCellTextColour(row, col, wx.BLACK)
   177             else:
   177                     grid.SetCellBackgroundColour(row, col, wx.WHITE)
   178                 grid.SetRowMinimalHeight(row, 28)
       
   179             grid.AutoSizeRow(row, False)
   178     
   180     
   179     def SetData(self, data):
   181     def SetData(self, data):
   180         self.data = data
   182         self.data = data
   181     
   183     
   182     def GetData(self):
   184     def GetData(self):
   200 
   202 
   201     def Empty(self):
   203     def Empty(self):
   202         self.data = []
   204         self.data = []
   203         self.editors = []
   205         self.editors = []
   204 
   206 
   205     def AddError(self, infos):
   207     def AddHighlight(self, infos, highlight_type):
   206         self.Errors[infos[0]] = infos[1:]
   208         row_highlights = self.Highlights.setdefault(infos[0], {})
   207 
   209         if infos[1] == "initial":
   208     def ClearErrors(self):
   210             col_highlights = row_highlights.setdefault("initial value", [])
   209         self.Errors = {}
   211         else:
       
   212             col_highlights = row_highlights.setdefault(infos[1], [])
       
   213         col_highlights.append(highlight_type)
       
   214 
       
   215     def ClearHighlights(self, highlight_type=None):
       
   216         if highlight_type is None:
       
   217             self.Highlights = {}
       
   218         else:
       
   219             for row, row_highlights in self.Highlights.iteritems():
       
   220                 row_items = row_highlights.items()
       
   221                 for col, col_highlights in row_items:
       
   222                     if highlight_type in col_highlights:
       
   223                         col_highlights.remove(highlight_type)
       
   224                     if len(col_highlights) == 0:
       
   225                         row_highlights.pop(col)
   210 
   226 
   211 #-------------------------------------------------------------------------------
   227 #-------------------------------------------------------------------------------
   212 #                          Datatype Editor class
   228 #                          Datatype Editor class
   213 #-------------------------------------------------------------------------------
   229 #-------------------------------------------------------------------------------
   214 
   230 
   381               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   397               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   382 
   398 
   383         self.DirectlyBaseType = wx.ComboBox(id=ID_DATATYPEEDITORDIRECTLYBASETYPE, 
   399         self.DirectlyBaseType = wx.ComboBox(id=ID_DATATYPEEDITORDIRECTLYBASETYPE, 
   384               name='DirectlyBaseType', parent=self.DirectlyPanel, pos=wx.Point(0, 0),
   400               name='DirectlyBaseType', parent=self.DirectlyPanel, pos=wx.Point(0, 0),
   385               size=wx.Size(0, 28), style=wx.CB_READONLY)
   401               size=wx.Size(0, 28), style=wx.CB_READONLY)
       
   402         self.DirectlyBaseType.SetBackgroundColour(wx.BLUE)
   386         self.Bind(wx.EVT_COMBOBOX, self.OnInfosChanged, id=ID_DATATYPEEDITORDIRECTLYBASETYPE)
   403         self.Bind(wx.EVT_COMBOBOX, self.OnInfosChanged, id=ID_DATATYPEEDITORDIRECTLYBASETYPE)
   387 
   404 
   388         self.staticText3 = wx.StaticText(id=ID_DATATYPEEDITORSTATICTEXT3,
   405         self.staticText3 = wx.StaticText(id=ID_DATATYPEEDITORSTATICTEXT3,
   389               label=_('Initial Value:'), name='staticText3', parent=self.DirectlyPanel,
   406               label=_('Initial Value:'), name='staticText3', parent=self.DirectlyPanel,
   390               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   407               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   405               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   422               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   406 
   423 
   407         self.SubrangeBaseType = wx.ComboBox(id=ID_DATATYPEEDITORSUBRANGEBASETYPE, 
   424         self.SubrangeBaseType = wx.ComboBox(id=ID_DATATYPEEDITORSUBRANGEBASETYPE, 
   408               name='SubrangeBaseType', parent=self.SubrangePanel, pos=wx.Point(0, 0),
   425               name='SubrangeBaseType', parent=self.SubrangePanel, pos=wx.Point(0, 0),
   409               size=wx.Size(0, 28), style=wx.CB_READONLY)
   426               size=wx.Size(0, 28), style=wx.CB_READONLY)
       
   427         self.SubrangeBaseType.SetBackgroundColour(wx.BLUE)
   410         self.Bind(wx.EVT_COMBOBOX, self.OnSubrangeBaseTypeChanged, id=ID_DATATYPEEDITORSUBRANGEBASETYPE)
   428         self.Bind(wx.EVT_COMBOBOX, self.OnSubrangeBaseTypeChanged, id=ID_DATATYPEEDITORSUBRANGEBASETYPE)
   411 
   429 
   412         self.staticText5 = wx.StaticText(id=ID_DATATYPEEDITORSTATICTEXT5,
   430         self.staticText5 = wx.StaticText(id=ID_DATATYPEEDITORSTATICTEXT5,
   413               label=_('Initial Value:'), name='staticText5', parent=self.SubrangePanel,
   431               label=_('Initial Value:'), name='staticText5', parent=self.SubrangePanel,
   414               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   432               pos=wx.Point(0, 0), size=wx.DefaultSize, style=0)
   587         self.Initializing = False
   605         self.Initializing = False
   588         
   606         
   589         self.ParentWindow = window
   607         self.ParentWindow = window
   590         self.Controler = controler
   608         self.Controler = controler
   591         self.TagName = tagname
   609         self.TagName = tagname
       
   610         
       
   611         self.HighlightControls = {
       
   612             ("Directly", "base"): self.DirectlyBaseType,
       
   613             ("Directly", "initial"): self.DirectlyInitialValue,
       
   614             ("Subrange", "base"): self.SubrangeBaseType,
       
   615             ("Subrange", "lower"): self.SubrangeMinimum,
       
   616             ("Subrange", "upper"): self.SubrangeMaximum,
       
   617             ("Subrange", "initial"): self.SubrangeInitialValue,
       
   618             ("Enumerated", "value"): self.EnumeratedValues,
       
   619             ("Enumerated", "initial"): self.EnumeratedInitialValue,
       
   620             ("Array", "initial"): self.ArrayInitialValue,
       
   621             ("Array", "base"): self.ArrayBaseType,
       
   622             ("Array", "range"): self.ArrayDimensions,
       
   623         }
   592         
   624         
   593         self.RefreshHighlightsTimer = wx.Timer(self, -1)
   625         self.RefreshHighlightsTimer = wx.Timer(self, -1)
   594         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
   626         self.Bind(wx.EVT_TIMER, self.OnRefreshHighlightsTimer, self.RefreshHighlightsTimer)
   595         
   627         
   596     def __del__(self):
   628     def __del__(self):
   653                 self.ArrayBaseType.SetStringSelection(type_infos["base_type"])
   685                 self.ArrayBaseType.SetStringSelection(type_infos["base_type"])
   654                 self.ArrayDimensions.SetStrings(map(lambda x : "..".join(x), type_infos["dimensions"]))
   686                 self.ArrayDimensions.SetStrings(map(lambda x : "..".join(x), type_infos["dimensions"]))
   655                 self.ArrayInitialValue.SetValue(type_infos["initial"])
   687                 self.ArrayInitialValue.SetValue(type_infos["initial"])
   656             elif type_infos["type"] == "Structure":
   688             elif type_infos["type"] == "Structure":
   657                 self.StructureElementsTable.SetData(type_infos["elements"])
   689                 self.StructureElementsTable.SetData(type_infos["elements"])
   658                 self.StructureElementsTable.ResetView(self.StructureElementsGrid)
       
   659             self.RefreshDisplayedInfos()
   690             self.RefreshDisplayedInfos()
   660         self.ShowErrors()
   691         self.ShowHighlights()
       
   692         self.StructureElementsTable.ResetView(self.StructureElementsGrid)
   661         self.Initializing = False
   693         self.Initializing = False
   662 
   694 
   663     def RefreshScaling(self, refresh=True):
   695     def RefreshScaling(self, refresh=True):
   664         pass
   696         pass
   665 
   697 
   938     def ClearHighlights(self, highlight_type=None):
   970     def ClearHighlights(self, highlight_type=None):
   939         if highlight_type is None:
   971         if highlight_type is None:
   940             self.Highlights = []
   972             self.Highlights = []
   941         else:
   973         else:
   942             self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type]
   974             self.Highlights = [(infos, start, end, highlight) for (infos, start, end, highlight) in self.Highlights if highlight != highlight_type]
       
   975         for control in self.HighlightControls.itervalues():
       
   976             if isinstance(control, (wx.ComboBox, wx.SpinCtrl)):
       
   977                 control.SetBackgroundColour(wx.NullColour)
       
   978                 control.SetForegroundColour(wx.NullColour)
       
   979             elif isinstance(control, wx.TextCtrl):
       
   980                 value = control.GetValue()
       
   981                 control.SetStyle(0, len(value), wx.TextAttr(wx.NullColour))
       
   982             elif isinstance(control, wx.gizmos.EditableListBox):
       
   983                 listctrl = control.GetListCtrl()
       
   984                 for i in xrange(listctrl.GetItemCount()):
       
   985                     listctrl.SetItemBackgroundColour(i, wx.NullColour)
       
   986                     listctrl.SetItemTextColour(i, wx.NullColour)
       
   987         self.StructureElementsTable.ClearHighlights(highlight_type)
   943         self.RefreshView()
   988         self.RefreshView()
   944 
   989 
   945     def AddHighlight(self, infos, start, end ,highlight_type):
   990     def AddHighlight(self, infos, start, end ,highlight_type):
   946         self.Highlights.append((infos, start, end, highlight_type))
   991         self.Highlights.append((infos, start, end, highlight_type))
   947         self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True)
   992         self.RefreshHighlightsTimer.Start(int(REFRESH_HIGHLIGHT_PERIOD * 1000), oneShot=True)
   948 
   993 
   949     def ShowErrors(self):
   994     def ShowHighlights(self):
   950         type_infos = self.Controler.GetDataTypeInfos(self.TagName)
   995         type_infos = self.Controler.GetDataTypeInfos(self.TagName)
   951         for infos, start, end, highlight_type in self.Highlights:
   996         for infos, start, end, highlight_type in self.Highlights:
   952             if infos[0] == "base":
   997             if infos[0] == "struct":
   953                 if type_infos["type"] == "Directly":
   998                 self.StructureElementsTable.AddHighlight(infos[1:], highlight_type)
   954                     self.DirectlyBaseType.SetBackgroundColour(highlight_type[0])
   999             else:
   955                     self.DirectlyBaseType.SetForegroundColour(highlight_type[1])
  1000                 control = self.HighlightControls.get((type_infos["type"], infos[0]), None)
   956                 elif type_infos["type"] == "Subrange":
  1001                 if control is not None:
   957                     self.SubrangeBaseType.SetBackgroundColour(highlight_type[0])
  1002                     if isinstance(control, (wx.ComboBox, wx.SpinCtrl)):
   958                     self.SubrangeBaseType.SetForegroundColour(highlight_type[1])
  1003                         control.SetBackgroundColour(highlight_type[0])
   959                 elif type_infos["type"] == "Array":
  1004                         control.SetForegroundColour(highlight_type[1])
   960                     self.ArrayBaseType.SetBackgroundColour(highlight_type[0])
  1005                     elif isinstance(control, wx.TextCtrl):
   961                     self.ArrayBaseType.SetForegroundColour(highlight_type[1])
  1006                         control.SetStyle(start[1], end[1] + 1, wx.TextAttr(highlight_type[1], highlight_type[0]))
   962             elif infos[0] == "lower":
  1007                     elif isinstance(control, wx.gizmos.EditableListBox):
   963                 self.SubrangeMinimum.SetBackgroundColour(highlight_type[0])
  1008                         listctrl = control.GetListCtrl()
   964                 self.SubrangeMaximum.SetForegroundColour(highlight_type[1])
  1009                         listctrl.SetItemBackgroundColour(infos[1], highlight_type[0])
   965             elif infos[0] == "upper":
  1010                         listctrl.SetItemTextColour(infos[1], highlight_type[1])
   966                 self.SubrangeMinimum.SetBackgroundColour(highlight_type[0])
  1011                         listctrl.Select(listctrl.FocusedItem, False)
   967                 self.SubrangeMaximum.SetForegroundColour(highlight_type[1])
  1012 
   968             elif infos[0] == "value":
       
   969                 listctrl = self.EnumeratedValues.GetListCtrl()
       
   970                 listctrl.SetItemBackgroundColour(infos[1], highlight_type[0])
       
   971                 listctrl.SetItemTextColour(infos[1], highlight_type[1])
       
   972                 listctrl.Select(listctrl.FocusedItem, False)
       
   973             elif infos[0] == "range":
       
   974                 listctrl = self.EnumeratedValues.GetListCtrl()
       
   975                 listctrl.SetItemBackgroundColour(infos[1], highlight_type[0])
       
   976                 listctrl.SetItemTextColour(infos[1], highlight_type[1])
       
   977                 listctrl.SetStringSelection("")
       
   978             elif infos[0] == "initial":
       
   979                 if type_infos["type"] == "Directly":
       
   980                     self.DirectlyInitialValue.SetStyle(start[1], end[1] + 1, wx.TextAttr(highlight_type[1], highlight_type[0]))
       
   981                 elif type_infos["type"] == "Subrange":
       
   982                     self.SubrangeInitialValue.SetBackgroundColour(highlight_type[0])
       
   983                     self.SubrangeInitialValue.SetForegroundColour(highlight_type[1])
       
   984                 elif type_infos["type"] == "Enumerated":
       
   985                     self.EnumeratedInitialValue.SetBackgroundColour(highlight_type[0])
       
   986                     self.EnumeratedInitialValue.SetForegroundColour(highlight_type[1])
       
   987                 elif type_infos["type"] == "Array":
       
   988                     self.ArrayInitialValue.SetStyle(start[1], end[1] + 1, wx.TextAttr(highlight_type[1], highlight_type[0]))
       
   989