Commit 0857b6c0 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Handle self undo stack in MSC handler

It is not possible to share the stack between SDL and MSC handlers, they
live in separate threads.
parent 9c620636
......@@ -321,6 +321,10 @@ def gui():
sdl = sdlHandler(myWidget)
sdl.dll = dll
sdl.msc.connect(msc.addToMsc)
sdl.msc_undo.connect(msc.undo)
sdl.msc_redo.connect(msc.redo)
sdl.msc_macro_start.connect(msc.start_undo_macro)
sdl.msc_macro_stop.connect(msc.stop_undo_macro)
sdl.allowed_messages.connect(myWidget.allowed_editors)
except IOError:
log.info('SDL viewer not available')
......
......@@ -6,7 +6,7 @@ from multiprocessing import Process, Pipe
from PySide.QtGui import(QFileDialog, QGraphicsView, QDockWidget, QMessageBox,
QErrorMessage, QTableWidget, QTableWidgetItem,
QUndoCommand)
QUndoCommand, QUndoStack)
from PySide.QtCore import Slot, Qt, QFile, QTimer
from PySide.QtUiTools import QUiLoader
......@@ -76,6 +76,7 @@ class AddToMsc(QUndoCommand):
self.handler.cnt_id -= 1
self.handler.msg.pop()
self.handler.next_y -= self.increase_factor
self.handler.msc_scene.visible_items.pop()
self.item.hide()
def redo(self):
......@@ -83,6 +84,7 @@ class AddToMsc(QUndoCommand):
self.handler.cnt_id += 1
self.handler.msg.append(self.txt)
self.handler.next_y += self.increase_factor
self.handler.msc_scene.visible_items.append(self.item)
self.item.show()
......@@ -163,6 +165,7 @@ inst {fv};'''.format(fv=fv_name)]
# Timer to periodically monitor a scenario
self.timer = QTimer()
self.timer.timeout.connect(self.monitor_scenario)
self.undo_stack = QUndoStack()
@Slot()
def startStop(self):
......@@ -174,63 +177,33 @@ inst {fv};'''.format(fv=fv_name)]
self.dock.hide()
@Slot()
def addToMsc(self, direction, message, undo_stack=None):
def undo(self):
''' Remove the last items from the MSC diagram using the undo stack '''
self.undo_stack.undo()
@Slot()
def redo(self):
''' Add back the last items on the MSC diagram from the undo stack '''
self.undo_stack.redo()
@Slot()
def start_undo_macro(self):
''' Allow a remote control of undo macros '''
self.undo_stack.beginMacro('Add Items')
@Slot()
def stop_undo_macro(self):
''' Allow a remote control of undo macros '''
self.undo_stack.endMacro()
@Slot()
def addToMsc(self, direction, message):
''' Add a message to the MSC '''
if not self.running:
return
self.msc_started = True
undo_cmd = AddToMsc(self, direction, message)
if undo_stack:
undo_stack.push(undo_cmd)
# TODO: check that the redo() is called anyway
# msg = MSG()
# msg.direction = direction
# # Add a counter (unique Id) incremented for each message
# # (eg. out MsgName,1(Param);) - to respect MSC syntax
# split = message.strip().split('(', 1)
# if len(split) > 1:
# msg.label = (',%d(' % self.cnt_id).join(split)
# else:
# msg.label = split[0] + ',%d' % self.cnt_id
# self.cnt_id += 1
# self.msg.append(msg)
# # Draw the message on the MSC Scene
# if direction in ('in', 'out'):
# if direction == 'out':
# start_item = self.gui_instance
# end_item = self.taste_instance
# elif direction == 'in':
# start_item = self.taste_instance
# end_item = self.gui_instance
# msg = self.msc_scene.addMessage(
# start_item, end_item, self.next_y, label=message)
# self.next_y += 30
# elif direction == 'set':
# msg = self.msc_scene.addSetTimer(self.taste_instance,
# self.next_y,
# message)
# self.next_y += 30
# elif direction == 'reset':
# msg = self.msc_scene.addResetTimer(self.taste_instance,
# self.next_y,
# message)
# self.next_y += 30
# elif direction == 'timeout':
# msg = self.msc_scene.addTimeout(self.taste_instance,
# self.next_y,
# message)
# self.next_y += 30
# elif direction == 'condition':
# # Condition: single word with no space
# condbox = self.msc_scene.addCondition(self.taste_instance,
# y=self.next_y,
# label=condition)
# self.next_y += 40
# # This is how to add a comment on the graph:
# # comment = self.msc_scene.addComment(msg)
# #comment.setCommentText("Hello!")
self.undo_stack.push(undo_cmd)
@Slot()
def stopMscRecording(self):
......
......@@ -130,7 +130,7 @@ class MscStreamingScene(MscGraphBasicMSCScene):
def streamingControl(self, element):
''' Scroll up in streaming mode '''
self.visible_items.append(element)
#self.visible_items.append(element)
if len(self.visible_items) > self.max_items:
item = self.visible_items.popleft()
firstY = item.pos().y()
......
......@@ -76,9 +76,20 @@ class sdlHandler(QObject):
'''
Class managing SDL models
'''
# signal sent whenever a paramless TM or TC event happens
# Qt signals:
# - msc is used to display an item on the MSC diagram
# - msc_macro_start/top are used to control the undo stack macros
# - allowed_messages is used to activate/deactivate GUI buttons
# depending on the context, driven by the SDL model internal state
# - msc undo/redo are used to request an undo stack action to the MSC
# handler, which lives in a different thread.
msc = Signal(unicode, unicode, QUndoStack)
msc_macro_start = Signal()
msc_macro_stop = Signal()
msc_undo = Signal()
msc_redo = Signal()
allowed_messages = Signal(list)
def __init__(self, parent):
''' Startup: check if there are some SDL files in
the directory and try to parse them and build the widget '''
......@@ -206,12 +217,14 @@ class sdlHandler(QObject):
self.undo_stack.undo()
self.check_state()
self.current_hash = self.on_event()
self.msc_undo.emit()
def redo(self):
''' Called when the redo button is pressed '''
self.undo_stack.redo()
self.check_state()
self.current_hash = self.on_event()
self.msc_redo.emit()
def restore_global_state(self, statehash):
''' From a hashcode, restore a global state in the DLL (state +
......@@ -322,6 +335,7 @@ class sdlHandler(QObject):
def send_tc(self, name, tc_func_ptr, param=None):
''' Send a TC - Used either locally (parameterless TCs) or via
a signal sent by the B-mapper-generated backends '''
self.msc_macro_start.emit()
with undo.UndoMacro(self.undo_stack, 'Send TC'):
if name in self.timers:
self.add_to_msc('timeout', name)
......@@ -338,6 +352,7 @@ class sdlHandler(QObject):
self.current_hash = self.on_event(tc_name=name,
param=param.GSER() if param
else None)
self.msc_macro_stop.emit()
def receive_tm(self, tm_name):
''' Callback function when a paramless TM is received '''
......
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