#!/usr/bin/env python
#
# This test creates a class diagram with a package with a class,
# and then moves the class inside the package, once to the right edge
# of the package, once to the bottom edge of the package. The package
# should grow to make room accordingly
#
#
from coral.modeler.diagram.degen import *
from coral import modeler
from coral.modeler.modeler import *
DI = coral.lang.XMIDI20
from coral.modeler.xmidigraphbuilder import xmidibuilder
from coral.modeler.diagram import xmidi_diagrams
import time

def good_package(x_pkg):
    """Returns true if this package should look ok on screen. Basically
    it checks that the bodycompartment is as big as the package."""

    namecompartment, idx = searchForSimpleGraphNode(x_pkg.contained, "NameCompartment")
    assert namecompartment
    assert idx >= 0
    bodycompartment, idx = searchForSimpleGraphNode(x_pkg.contained, "BodyCompartment")
    assert bodycompartment
    assert idx >= 0
    
    assert x_pkg.size.width >= 0.0
    assert x_pkg.size.height >= 0.0
    assert bodycompartment.position.x == 0.0
    # The mysterious - 1.0 is because the top edge of the bodycompartment
    # and the bottom edge of the namecompartment are drawn together
    # at the same scanline
    assert bodycompartment.position.y == namecompartment.size.height - 1.0
    assert bodycompartment.size.width == x_pkg.size.width
    assert bodycompartment.size.height + namecompartment.size.height == x_pkg.size.height


L=coral.lang.UML14

m=ModelerWindow()
m.main(loop=0)

tm = coral.getTransactionManager()

tm.beginModifyCmd()
try:
    prj=coral.Project()
    
    m1=L.Model()
    m1.__setProject__(prj)
    p1 = L.Package()
    c1 = L.Class()
    c1.namespace = p1
    p1.namespace = m1
    p1.name = "ThePackage"
    c1.name = "TheClass"
    
    #create a Class Diagram for the model
    cd = xmidi_diagrams.createDiagram(prj,owner_elem=m1,typeinfo="ClassDiagram")
    assert cd
    xmidibuilder(m1, cd)
    
finally:
    tm.endModifyCmd()

m.getDiagramEditorManager().editorSet.notifyFocusOn(cd)
m.processEvents()

x_p1 = p1.xmidi_get_graphelements()[0]


tm.beginModifyCmd()
try:
    ## Create XMIDI for c1 into the package
    x_c1 = DiagramElementGenerator.getExtension(c1.__type__()).createDiagramElement(c1, xparent=x_p1.contained[1], container=x_p1.contained[1] )
finally:
    tm.endModifyCmd()


tm.beginModifyCmd()
try:
    x_p1.position.x = 100
    x_p1.position.y = 30
finally:
    tm.endModifyCmd()

m.processEvents()
good_package(x_p1)
time.sleep(2)

def inside(x_child, x_parent):
    """Returns true if XMI-DI element x_child is inside x_parent."""
    pcx = x_child.position.x
    pcy = x_child.position.y
    ppx = x_parent.position.x
    ppy = x_parent.position.y

    scx = x_child.size.width
    scy = x_child.size.height
    spx = x_parent.size.width
    spy = x_parent.size.height

    if not pcx >= 0.0: return False
    if not pcy >= 0.0: return False
    if not scx >= 0.0: return False
    if not scy >= 0.0: return False
    if not spx >= 0.0: return False
    if not spy >= 0.0: return False
    if not pcx + scx <= spx    : return False
    if not pcy + scy <= spy    : return False
    return True

assert inside(x_c1, x_p1)

tm.beginModifyCmd()
try:
    x_c1.position.x = x_p1.size.width - 20
    assert x_c1.position.x + x_c1.size.width > x_p1.size.width
    assert not inside(x_c1, x_p1)
finally:
    tm.endModifyCmd()

m.processEvents()
good_package(x_p1)
time.sleep(2)

assert inside(x_c1, x_p1)

tm.beginModifyCmd()
try:
    x_c1.position.y = x_p1.size.height - 20
    assert x_c1.position.y + x_c1.size.height > x_p1.size.height
    assert not inside(x_c1, x_p1)
finally:
    tm.endModifyCmd()

m.processEvents()
good_package(x_p1)
time.sleep(2)

assert inside(x_c1, x_p1)
