Adding support for unifying grid table control elements
authorlaurent
Wed, 14 Dec 2011 15:17:36 +0100
changeset 651 cbeb769b0a56
parent 650 26236e691330
child 652 eb2d9f2b3567
Adding support for unifying grid table control elements
plugins/c_ext/CFileEditor.py
plugins/c_ext/c_ext.py
--- a/plugins/c_ext/CFileEditor.py	Fri Dec 09 10:32:06 2011 +0100
+++ b/plugins/c_ext/CFileEditor.py	Wed Dec 14 15:17:36 2011 +0100
@@ -5,7 +5,7 @@
 import wx.stc as stc
 import wx.lib.buttons
 
-from controls import CustomGrid, EditorPanel
+from controls import CustomGrid, CustomTable, EditorPanel
 
 if wx.Platform == '__WXMSW__':
     faces = { 'times': 'Times New Roman',
@@ -453,90 +453,15 @@
 #                         Helper for VariablesGrid values
 #-------------------------------------------------------------------------------
 
-class VariablesTable(wx.grid.PyGridTableBase):
-    
-    """
-    A custom wxGrid Table using user supplied data
-    """
-    def __init__(self, parent, data, colnames):
-        # The base class must be initialized *first*
-        wx.grid.PyGridTableBase.__init__(self)
-        self.data = data
-        self.colnames = colnames
-        self.Parent = parent
-        # XXX
-        # we need to store the row length and collength to
-        # see if the table has changed size
-        self._rows = self.GetNumberRows()
-        self._cols = self.GetNumberCols()
-    
-    def GetNumberCols(self):
-        return len(self.colnames)
-        
-    def GetNumberRows(self):
-        return len(self.data)
-
-    def GetColLabelValue(self, col):
-        if col < len(self.colnames):
-            return self.colnames[col]
-
-    def GetRowLabelValues(self, row):
-        return row
-
+class VariablesTable(CustomTable):
+    
     def GetValue(self, row, col):
         if row < self.GetNumberRows():
             if col == 0:
                 return row + 1
             else:
-                return str(self.data[row].get(self.GetColLabelValue(col), ""))
-    
-    def GetValueByName(self, row, colname):
-        if row < self.GetNumberRows():
-            return self.data[row].get(colname, None)
-        return None
-
-    def SetValue(self, row, col, value):
-        if col < len(self.colnames):
-            self.data[row][self.GetColLabelValue(col)] = value
-    
-    def SetValueByName(self, row, colname, value):
-        if row < self.GetNumberRows():
-            self.data[row][colname] = value
-        
-    def ResetView(self, grid):
-        """
-        (wxGrid) -> Reset the grid view.   Call this to
-        update the grid if rows and columns have been added or deleted
-        """
-        grid.BeginBatch()
-        for current, new, delmsg, addmsg in [
-            (self._rows, self.GetNumberRows(), wx.grid.GRIDTABLE_NOTIFY_ROWS_DELETED, wx.grid.GRIDTABLE_NOTIFY_ROWS_APPENDED),
-            (self._cols, self.GetNumberCols(), wx.grid.GRIDTABLE_NOTIFY_COLS_DELETED, wx.grid.GRIDTABLE_NOTIFY_COLS_APPENDED),
-        ]:
-            if new < current:
-                msg = wx.grid.GridTableMessage(self,delmsg,new,current-new)
-                grid.ProcessTableMessage(msg)
-            elif new > current:
-                msg = wx.grid.GridTableMessage(self,addmsg,new-current)
-                grid.ProcessTableMessage(msg)
-                self.UpdateValues(grid)
-        grid.EndBatch()
-
-        self._rows = self.GetNumberRows()
-        self._cols = self.GetNumberCols()
-        # update the column rendering scheme
-        self._updateColAttrs(grid)
-
-        # update the scrollbars and the displayed part of the grid
-        grid.AdjustScrollbars()
-        grid.ForceRefresh()
-
-    def UpdateValues(self, grid):
-        """Update all displayed values"""
-        # This sends an event to the grid table to update all of the values
-        msg = wx.grid.GridTableMessage(self, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
-        grid.ProcessTableMessage(msg)
-
+                return str(self.data[row].get(self.GetColLabelValue(col, False), ""))
+    
     def _updateColAttrs(self, grid):
         """
         wxGrid -> update the column attributes to add the
@@ -568,41 +493,8 @@
                 grid.SetCellRenderer(row, col, renderer)
                 
                 grid.SetCellBackgroundColour(row, col, wx.WHITE)
-    
-    def SetData(self, data):
-        self.data = data
-    
-    def GetData(self):
-        return self.data
-    
-    def GetCurrentIndex(self):
-        return self.CurrentIndex
-    
-    def SetCurrentIndex(self, index):
-        self.CurrentIndex = index
-    
-    def AppendRow(self, row_content):
-        self.data.append(row_content)
-
-    def InsertRow(self, row_index, row_content):
-        self.data.insert(row_index, row_content)
-
-    def RemoveRow(self, row_index):
-        self.data.pop(row_index)
-
-    def MoveRow(self, row_index, move):
-        new_index = max(0, min(row_index + move, len(self.data) - 1))
-        if new_index != row_index:
-            self.data.insert(new_index, self.data.pop(row_index))
-        return new_index
-    
-    def GetRow(self, row_index):
-        return self.data[row_index]
-
-    def Empty(self):
-        self.data = []
-        self.editors = []
-
+            self.ResizeRow(grid, row)
+    
 
 [ID_VARIABLESEDITOR, ID_VARIABLESEDITORVARIABLESGRID,
  ID_VARIABLESEDITORADDVARIABLEBUTTON, ID_VARIABLESEDITORDELETEVARIABLEBUTTON, 
--- a/plugins/c_ext/c_ext.py	Fri Dec 09 10:32:06 2011 +0100
+++ b/plugins/c_ext/c_ext.py	Wed Dec 14 15:17:36 2011 +0100
@@ -31,7 +31,6 @@
         
         self.Buffering = False
         self.CFile = CFileClasses["CFile"]()
-        self.CFileBuffer = UndoBuffer(self.Copy(self.CFile), False)
         if os.path.isfile(filepath):
             xmlfile = open(filepath, 'r')
             tree = minidom.parse(xmlfile)
@@ -42,11 +41,9 @@
                     self.CFile.loadXMLTree(child, ["xmlns", "xmlns:xsi", "xsi:schemaLocation"])
                     self.CFileBuffer = UndoBuffer(self.Copy(self.CFile), True)
         else:
+            self.CFileBuffer = UndoBuffer(self.Copy(self.CFile), False)
             self.OnPlugSave()
 
-    def GetIconPath(self, name):
-        return opjimg(name)
-
     def CFileName(self):
         return os.path.join(self.PlugPath(), "cfile.xml")
 
@@ -174,7 +171,7 @@
         xmlfile.write(text.encode("utf-8"))
         xmlfile.close()
         
-        self.CFileBuffer.CurrentSaved()
+        self.MarkCFileAsSaved()
         return True
 
     def PlugGenerate_C(self, buildpath, locations):
@@ -276,34 +273,41 @@
 #-------------------------------------------------------------------------------
 
     """
-    Return a copy of the project
+    Return a copy of the cfile model
     """
     def Copy(self, model):
         return cPickle.loads(cPickle.dumps(model))
 
+    def CreateConfigBuffer(self, saved):
+        self.CFileBuffer = UndoBuffer(cPickle.dumps(self.CFile), saved)
+
     def BufferCFile(self):
-        self.CFileBuffer.Buffering(self.Copy(self.CFile))
+        self.CFileBuffer.Buffering(cPickle.dumps(self.CFile))
     
     def StartBuffering(self):
-        self.CFileBuffer.Buffering(self.CFile)
         self.Buffering = True
         
     def EndBuffering(self):
         if self.Buffering:
-            self.CFile = self.Copy(self.CFile)
+            self.CFileBuffer.Buffering(cPickle.dumps(self.CFile))
             self.Buffering = False
     
+    def MarkCFileAsSaved(self):
+        self.EndBuffering()
+        self.CFileBuffer.CurrentSaved()
+    
     def CFileIsSaved(self):
         if self.CFileBuffer:
-            return self.CFileBuffer.IsCurrentSaved()
+            return self.CFileBuffer.IsCurrentSaved() and not self.Buffering
         else:
             return True
 
     def LoadPrevious(self):
-        self.CFile = self.Copy(self.CFileBuffer.Previous())
+        self.EndBuffering()
+        self.CFile = cPickle.loads(self.CFileBuffer.Previous())
     
     def LoadNext(self):
-        self.CFile = self.Copy(self.CFileBuffer.Next())
+        self.CFile = cPickle.loads(self.CFileBuffer.Next())
     
     def GetBufferState(self):
         first = self.CFileBuffer.IsFirst()