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

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

m=modeler.ModelerWindow()
storage=m.openProject("Class_with_Attrs_and_Ops.xml")
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 #this should be the diagram with the two classes

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

m.processEvents()

aclass_di = None
bclass_di = None
for e in diagram.contained:
    if e.semanticModel.element.__type__() == UML14.Class and e.semanticModel.element.name == "NewClass_1":
        aclass_di = e

assert aclass_di


# Move the class once to let layours do their stuff
coral.getTransactionManager().beginModifyCmd()
try:
    aclass_di.position.y += 1
    aclass_di.position.x += 1
finally:
    coral.getTransactionManager().endModifyCmd()


oldXValues=[]
oldYValues=[]
print "x",aclass_di.widget.__getPyValue__().x()
print "y",aclass_di.widget.__getPyValue__().y()

# Collect old values
for gn in aclass_di.contained:
    oldXValues.append(gn.widget.__getPyValue__().x())
    oldYValues.append(gn.widget.__getPyValue__().y())

# Check original values
assert aclass_di.widget.__getPyValue__().x()==146
assert aclass_di.widget.__getPyValue__().y()==106

# Move the class
coral.getTransactionManager().beginModifyCmd()
try:
    aclass_di.position.y+=200
    aclass_di.position.x+=200
finally:
    coral.getTransactionManager().endModifyCmd()

print "x",aclass_di.widget.__getPyValue__().x()
print "y",aclass_di.widget.__getPyValue__().y()

newXValues=[]
newYValues=[]

# Collect new values
for gn in aclass_di.contained:
    newXValues.append(gn.widget.__getPyValue__().x())
    newYValues.append(gn.widget.__getPyValue__().y())

# Check that the widget of the class has moved correctly
assert aclass_di.widget.__getPyValue__().x()==346
assert aclass_di.widget.__getPyValue__().y()==306

# Check that all the child widgets have moved correctly
for i in range(len(oldXValues)):
    print
    print oldXValues[i]+200,newXValues[i]
    print oldYValues[i]+200,newYValues[i]
    assert oldXValues[i]+200==newXValues[i]
    assert oldYValues[i]+200==newYValues[i]

print newXValues
print oldXValues
print newYValues
print oldYValues
