#!/usr/bin/env python
import os
from coral.modeler import modeler,action
import coral
import time

XMIDI = coral.lang.XMIDI20
UML14 = coral.lang.UML14

m=modeler.ModelerWindow()
storage=m.openProject("../../../qa/models2import/Softsale.xmi")
m.main(loop=0)


diagram = None
for r in storage.contents():
    if r.oclIsKindOf(XMIDI.Diagram) and r.semanticModel.typeInfo == "ClassDiagram":
        diagram = r
        break

assert diagram

print "DIAGRAM", diagram
m.getDiagramEditorManager().editorSet.notifyFocusOn(diagram)

m.processEvents()

aclass_di = None
for e in diagram.contained:
    if e.semanticModel.element.__type__() == UML14.Class:
        aclass_di = e
        break

assert aclass_di

m.getDiagramEditorManager().focusOn(aclass_di)

aclass = aclass_di.semanticModel.element
assert aclass
m.getDiagramEditorManager().editorSet.notifyFocusOn(aclass_di)


the_widget = aclass_di.widget.__getPyValue__()
assert the_widget is not None
assert the_widget.canvas() is not None

print "X, Y:"
print "QT widget:", the_widget.height(), the_widget.width()
print "XMIDI elem:", aclass_di.size.height, aclass_di.size.width 

oldwidgetheight= the_widget.height()
oldwidgetwidth= the_widget.width()

oldxmi_diheight= aclass_di.size.height
oldxmi_diwidth=aclass_di.size.width

action.shellSetCurrentScript("self.size.height += 100")
    
import time
start_time = time.time()

coral.getTransactionManager().beginModifyCmd()
try:
    action.shellExecuteCurrentScript()
finally:
    coral.getTransactionManager().endModifyCmd()

end_time = time.time()
res = action.shellGetOutput()
print "RESULT", res
print "TIME", end_time - start_time

assert end_time > start_time

# Script should've run, we should be detached
print "X, Y:"
print "QT widget:", the_widget.height(), the_widget.width()
print "XMIDI elem:", aclass_di.size.height, aclass_di.size.width 

assert aclass_di.size.height == oldwidgetheight+100
assert the_widget.height() == oldxmi_diheight+100

########### UNDO
print "Undoing..."
coral.getTransactionManager().undo()
print "X, Y:"
print "QT widget:", the_widget.height(), the_widget.width()
print "XMIDI elem:", aclass_di.size.height, aclass_di.size.width 
assert aclass_di.size.height == oldwidgetheight
assert the_widget.height() == oldxmi_diheight


########### REDO
print "Redoing..."
coral.getTransactionManager().redo()
print "X, Y:"
print "QT widget:", the_widget.height(), the_widget.width()
print "XMIDI elem:", aclass_di.size.height, aclass_di.size.width 
assert aclass_di.size.height == oldwidgetheight+100
assert the_widget.height() == oldxmi_diheight+100


action.quit()
