plugins/python/python.py
changeset 654 9f6c091c316c
parent 430 5981ad8547f5
child 657 340c0b9caeca
equal deleted inserted replaced
653:21a572d80bd7 654:9f6c091c316c
     1 import wx
     1 import wx
     2 import os
     2 import os
     3 import modules
     3 import modules
     4 from plugger import PlugTemplate, opjimg
     4 from plugger import PlugTemplate, opjimg
       
     5 from PLCControler import UndoBuffer
     5 from PythonEditor import PythonEditorFrame
     6 from PythonEditor import PythonEditorFrame
     6 
     7 
     7 from xml.dom import minidom
     8 from xml.dom import minidom
     8 from xmlclass import *
     9 from xmlclass import *
     9 import cPickle
    10 import cPickle
    10 
    11 
    11 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "python_xsd.xsd")) 
    12 PythonClasses = GenerateClassesFromXSD(os.path.join(os.path.dirname(__file__), "python_xsd.xsd")) 
    12 
       
    13 #-------------------------------------------------------------------------------
       
    14 #                         Undo Buffer for PythonCode
       
    15 #-------------------------------------------------------------------------------
       
    16 
       
    17 # Length of the buffer
       
    18 UNDO_BUFFER_LENGTH = 20
       
    19 
       
    20 """
       
    21 Class implementing a buffer of changes made on the current editing model
       
    22 """
       
    23 class UndoBuffer:
       
    24 
       
    25     # Constructor initialising buffer
       
    26     def __init__(self, currentstate, issaved = False):
       
    27         self.Buffer = []
       
    28         self.CurrentIndex = -1
       
    29         self.MinIndex = -1
       
    30         self.MaxIndex = -1
       
    31         # if current state is defined
       
    32         if currentstate:
       
    33             self.CurrentIndex = 0
       
    34             self.MinIndex = 0
       
    35             self.MaxIndex = 0
       
    36         # Initialising buffer with currentstate at the first place
       
    37         for i in xrange(UNDO_BUFFER_LENGTH):
       
    38             if i == 0:
       
    39                 self.Buffer.append(currentstate)
       
    40             else:
       
    41                 self.Buffer.append(None)
       
    42         # Initialising index of state saved
       
    43         if issaved:
       
    44             self.LastSave = 0
       
    45         else:
       
    46             self.LastSave = -1
       
    47     
       
    48     # Add a new state in buffer
       
    49     def Buffering(self, currentstate):
       
    50         self.CurrentIndex = (self.CurrentIndex + 1) % UNDO_BUFFER_LENGTH
       
    51         self.Buffer[self.CurrentIndex] = currentstate
       
    52         # Actualising buffer limits
       
    53         self.MaxIndex = self.CurrentIndex
       
    54         if self.MinIndex == self.CurrentIndex:
       
    55             # If the removed state was the state saved, there is no state saved in the buffer
       
    56             if self.LastSave == self.MinIndex:
       
    57                 self.LastSave = -1
       
    58             self.MinIndex = (self.MinIndex + 1) % UNDO_BUFFER_LENGTH
       
    59         self.MinIndex = max(self.MinIndex, 0)
       
    60     
       
    61     # Return current state of buffer
       
    62     def Current(self):
       
    63         return self.Buffer[self.CurrentIndex]
       
    64     
       
    65     # Change current state to previous in buffer and return new current state
       
    66     def Previous(self):
       
    67         if self.CurrentIndex != self.MinIndex:
       
    68             self.CurrentIndex = (self.CurrentIndex - 1) % UNDO_BUFFER_LENGTH
       
    69             return self.Buffer[self.CurrentIndex]
       
    70         return None
       
    71     
       
    72     # Change current state to next in buffer and return new current state
       
    73     def Next(self):
       
    74         if self.CurrentIndex != self.MaxIndex:
       
    75             self.CurrentIndex = (self.CurrentIndex + 1) % UNDO_BUFFER_LENGTH
       
    76             return self.Buffer[self.CurrentIndex]
       
    77         return None
       
    78     
       
    79     # Return True if current state is the first in buffer
       
    80     def IsFirst(self):
       
    81         return self.CurrentIndex == self.MinIndex
       
    82     
       
    83     # Return True if current state is the last in buffer
       
    84     def IsLast(self):
       
    85         return self.CurrentIndex == self.MaxIndex
       
    86 
       
    87     # Note that current state is saved
       
    88     def CurrentSaved(self):
       
    89         self.LastSave = self.CurrentIndex
       
    90         
       
    91     # Return True if current state is saved
       
    92     def IsCurrentSaved(self):
       
    93         return self.LastSave == self.CurrentIndex
       
    94 
    13 
    95 class PythonCodeTemplate:
    14 class PythonCodeTemplate:
    96     
    15     
    97     def __init__(self):
    16     def __init__(self):
    98         
    17