#!/usr/bin/env python
import coral
from qt import QFontMetrics
from coral import modeler
from coral.modeler.modeler import *
import time
XMIDI = coral.lang.XMIDI20

TM = coral.getTransactionManager()

m=ModelerWindow()
prj=m.openProject("testEdgeLabelSizes.xml")
m.main(loop=0)

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

assert diagram

m.getDiagramEditorManager().editorSet.notifyFocusOn(diagram)

m.processEvents()

class_diagram = prj.contents()[1]
model = prj.contents()[0]

L = coral.lang.UML14
DI = coral.lang.XMIDI20
assert model.oclIsKindOf(L.Model)
assert class_diagram.oclIsKindOf(DI.Diagram)

c1 = model.ownedElement.select(lambda x: x.name == "NewClass_1")[0]
c2 = model.ownedElement.select(lambda x: x.name == "NewClass_2")[0]
assoc = model.ownedElement.select(lambda x: x.oclIsKindOf(L.Association))[0]

assert c1
assert c2
assert assoc

xc1 = c1.xmidi_get_graphelements()[0]
xc2 = c2.xmidi_get_graphelements()[0]

graphedges = assoc.xmidi_get_graphelements()
assert len(graphedges) == 1

ge = graphedges[0]

## Find label nodes
assert len(ge.contained)==3

assert assoc.connection[0].name=="abcd"
assert assoc.connection[1].name=="abcd"

## Set start texts
TM.beginModifyCmd()
try:
    ## Fool to layout
    assoc.connection[0].name="foofoofoofoofoofoofoo"
    assoc.connection[1].name="foofoofoofoofoofoofoo"
    ## Put it back
    assoc.connection[0].name="abcd"
    assoc.connection[1].name="abcd"
finally:
    TM.endModifyCmd()

time.sleep(1)

# Association end name label
start_name_node = ge.contained[0].contained.select(lambda x: x.semanticModel.typeInfo=="Name")[0]
end_name_node = ge.contained[1].contained.select(lambda x: x.semanticModel.typeInfo=="Name")[0]

start_begin_width=start_name_node.size.width
end_begin_width=end_name_node.size.width

TM.beginModifyCmd()
try:
    assoc.connection[0].name="abcdabcdabcd"
    assoc.connection[1].name="a"
    # The problem is that the edge label box hasn't been layouted
    # properly, so we'll have to fool the system into layouting it
    assert assoc.name == "association"
    assoc.name = "foo"
    assoc.name = "association"
finally:
    TM.endModifyCmd()

time.sleep(1.5)

prj.setFilename("tmp/testLabels-output1.xml")
coral.saveDocument(prj)

from coral.modeler.diagram.presentation import defaultFont
font = defaultFont
fm = QFontMetrics(font)

print "FONT METRICS SAYS SIZE SHOULD BE: ", fm.width("abcdabcdabcd")+2
print "FONT METRICS SAYS SIZE SHOULD BE: ", fm.width("a")+2

print "*** Start widths ***", " Current size:", start_name_node.size.width," Original size:" , start_begin_width, " Should GROW"
print "*** End widths ***"," Current size:", end_name_node.size.width," Original size:" , end_begin_width, " Should SHRINK"

time.sleep(1)

assert start_name_node.size.width > 2 * start_begin_width
assert end_name_node.size.width < end_begin_width / 2

# Association name label
name_node = ge.contained[2]
begin_name_width = name_node.size.width

# Grow
TM.beginModifyCmd()
try:
    assoc.name="association__association"
finally:
    TM.endModifyCmd()
    
prj.setFilename("tmp/testLabels-output2.xml")
coral.saveDocument(prj)

assert 2*begin_name_width < name_node.size.width

# Shrink
TM.beginModifyCmd()
try:
    assoc.name="association"
finally:
    TM.endModifyCmd()
    
prj.setFilename("tmp/testLabels-output3.xml")
coral.saveDocument(prj)

assert begin_name_width == name_node.size.width

