Commit d1ab49e9 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Create first undo command, to send TC

parent 684db23f
...@@ -19,7 +19,7 @@ import ctypes ...@@ -19,7 +19,7 @@ import ctypes
from functools import partial from functools import partial
from PySide.QtGui import (QDockWidget, QPushButton, QGridLayout, QListWidget, from PySide.QtGui import (QDockWidget, QPushButton, QGridLayout, QListWidget,
QUndoStack, QToolButton) QUndoStack, QUndoCommand, QToolButton)
from PySide.QtCore import QObject, Signal, Slot, Qt, QFile from PySide.QtCore import QObject, Signal, Slot, Qt, QFile
from PySide.QtUiTools import QUiLoader from PySide.QtUiTools import QUiLoader
...@@ -41,6 +41,37 @@ except ImportError: ...@@ -41,6 +41,37 @@ except ImportError:
ASN1 = None ASN1 = None
class SendTC(QUndoCommand):
''' Undo command to send a message to the running system '''
def __init__(self, handler, tc_ptr, param):
''' Init: save the current state and compute parameter pointer '''
super(SendTC, self).__init__()
self.handler = handler
self.recover_hash = handler.current_hash
self.tc_ptr = tc_ptr
self.param = param
if param:
# Cast the SWIG type (ASN.1 Native format) to a ctypes pointer
try:
swig_ptr = int(param._ptr)
except TypeError:
# when swig uses a proxy class, pointer is in _ptr.this
swig_ptr = int(param._ptr.this)
self.param_ptr = ctypes.cast(swig_ptr,
ctypes.POINTER(ctypes.c_uint32))
def undo(self):
''' Undo a send TC: Restore the system state as it was before the TC
was sent - setting back all global variables and internal state '''
self.handler.restore_global_state(self.recover_hash)
def redo(self):
''' Execute the send TC action: just call the function in the DLL '''
if self.param:
self.tc_ptr(self.param_ptr)
else:
self.tc_ptr()
class sdlHandler(QObject): class sdlHandler(QObject):
''' '''
Class managing SDL models Class managing SDL models
...@@ -86,6 +117,7 @@ class sdlHandler(QObject): ...@@ -86,6 +117,7 @@ class sdlHandler(QObject):
self.timers = {} self.timers = {}
# Keep a state graph to allow undo/redo and state exploration # Keep a state graph to allow undo/redo and state exploration
self.set_of_states = {} self.set_of_states = {}
self.current_hash = None
# Create a stack for handling undo/redo commands # Create a stack for handling undo/redo commands
self.undo_stack = QUndoStack(self) self.undo_stack = QUndoStack(self)
# Associate the Undo, Redo, Reset buttons with an action # Associate the Undo, Redo, Reset buttons with an action
...@@ -141,7 +173,7 @@ class sdlHandler(QObject): ...@@ -141,7 +173,7 @@ class sdlHandler(QObject):
# Initialization: set current state and internal variables # Initialization: set current state and internal variables
self.current_sdl_state = None self.current_sdl_state = None
self.check_state() self.check_state()
self.init_state = self.on_event() self.init_state = self.current_hash = self.on_event()
self.init_timers() self.init_timers()
@Slot() @Slot()
...@@ -195,7 +227,9 @@ class sdlHandler(QObject): ...@@ -195,7 +227,9 @@ class sdlHandler(QObject):
def on_event(self, tc_name=None, param=None): def on_event(self, tc_name=None, param=None):
''' Process signals indicating that a PI was called in the shared lib: ''' Process signals indicating that a PI was called in the shared lib:
update global state, manage undo/redo, manage active buttons ''' update global state, manage undo/redo, manage active buttons
This function does not trigger any undoable action, thus can
be called without restriction at any time, by anybody '''
if not ASN1: if not ASN1:
# The dataview must have been loaded to create ASN.1 native types # The dataview must have been loaded to create ASN.1 native types
return return
...@@ -281,19 +315,23 @@ class sdlHandler(QObject): ...@@ -281,19 +315,23 @@ class sdlHandler(QObject):
msg = '{tc}{arg}'.format(tc=name, msg = '{tc}{arg}'.format(tc=name,
arg='({})'.format(param.GSER()) if param else '') arg='({})'.format(param.GSER()) if param else '')
self.msc.emit('out', msg) self.msc.emit('out', msg)
if param: with undo.UndoMacro(self.undo_stack, 'Send TC'):
# Cast the SWIG type (ASN.1 Native format) to a ctypes pointer undo_cmd = SendTC(self, tc_func_ptr, param)
try: self.undo_stack.push(undo_cmd)
swig_ptr = int(param._ptr) self.check_state()
except TypeError: # if param:
# when swig uses a proxy class, pointer is in _ptr.this # # Cast the SWIG type (ASN.1 Native format) to a ctypes pointer
swig_ptr = int(param._ptr.this) # try:
param_ptr = ctypes.cast(swig_ptr, ctypes.POINTER(ctypes.c_uint32)) # swig_ptr = int(param._ptr)
tc_func_ptr(param_ptr) # except TypeError:
else: # # when swig uses a proxy class, pointer is in _ptr.this
tc_func_ptr() # swig_ptr = int(param._ptr.this)
# param_ptr = ctypes.cast(swig_ptr, ctypes.POINTER(ctypes.c_uint32))
# tc_func_ptr(param_ptr)
# else:
# tc_func_ptr()
# self.check_state()
self.check_state()
# Update windows, highlight state, enable/disable buttons, etc. # Update windows, highlight state, enable/disable buttons, etc.
self.on_event(tc_name=name, param=param.GSER() if param else None) self.on_event(tc_name=name, param=param.GSER() if param else None)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment