2018-11-26 21:56:14 -05:00
import cairo
2018-12-15 23:19:42 -05:00
import pytest
2018-11-26 21:56:14 -05:00
2010-01-15 16:24:00 +01:00
from gaphas . canvas import Canvas , ConnectionError
2007-07-26 19:11:32 +00:00
from gaphas . examples import Box
2018-11-26 21:56:14 -05:00
from gaphas . item import Line
2007-07-26 19:23:55 +00:00
2018-11-25 16:07:58 -05:00
2018-12-15 23:19:42 -05:00
def test_update_matrices ( ) :
""" Test updating of matrices """
c = Canvas ( )
i = Box ( )
ii = Box ( )
c . add ( i )
c . add ( ii , i )
i . matrix = ( 1.0 , 0.0 , 0.0 , 1.0 , 5.0 , 0.0 )
ii . matrix = ( 1.0 , 0.0 , 0.0 , 1.0 , 0.0 , 8.0 )
2007-07-26 19:23:55 +00:00
2018-12-15 23:19:42 -05:00
updated = c . update_matrices ( [ i ] )
2007-07-26 19:23:55 +00:00
2018-12-15 23:19:42 -05:00
assert i . _matrix_i2c == cairo . Matrix ( 1 , 0 , 0 , 1 , 5 , 0 )
assert ii . _matrix_i2c == cairo . Matrix ( 1 , 0 , 0 , 1 , 5 , 8 )
2007-07-26 19:23:55 +00:00
2007-07-26 19:11:32 +00:00
2018-12-15 23:19:42 -05:00
def test_reparent ( ) :
c = Canvas ( )
b1 = Box ( )
b2 = Box ( )
c . add ( b1 )
c . add ( b2 , b1 )
c . reparent ( b2 , None )
2009-10-11 02:37:36 +01:00
2018-11-25 16:07:58 -05:00
2007-07-26 19:11:32 +00:00
# fixme: what about multiple constraints for a handle?
# what about 1d projection?
2018-11-25 16:07:58 -05:00
2010-01-15 16:24:00 +01:00
def count ( i ) :
return len ( list ( i ) )
2018-12-15 23:19:42 -05:00
def test_connect_item ( ) :
b1 = Box ( )
b2 = Box ( )
line = Line ( )
c = Canvas ( )
c . add ( b1 )
c . add ( b2 )
c . add ( line )
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
c . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
# Add the same
with pytest . raises ( ConnectionError ) :
2018-11-26 21:56:14 -05:00
c . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] )
2018-12-15 23:19:42 -05:00
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
def test_disconnect_item_with_callback ( ) :
b1 = Box ( )
b2 = Box ( )
line = Line ( )
c = Canvas ( )
c . add ( b1 )
c . add ( b2 )
c . add ( line )
events = [ ]
def callback ( ) :
events . append ( " called " )
c . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , callback = callback )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
c . disconnect_item ( line , line . handles ( ) [ 0 ] )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
assert events == [ " called " ]
def test_disconnect_item_with_constraint ( ) :
b1 = Box ( )
b2 = Box ( )
line = Line ( )
c = Canvas ( )
c . add ( b1 )
c . add ( b2 )
c . add ( line )
cons = b1 . ports ( ) [ 0 ] . constraint ( c , line , line . handles ( ) [ 0 ] , b1 )
c . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , constraint = cons )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
ncons = len ( c . solver . constraints )
assert ncons == 5
c . disconnect_item ( line , line . handles ( ) [ 0 ] )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
assert len ( c . solver . constraints ) == 4
def test_disconnect_item_by_deleting_element ( ) :
b1 = Box ( )
b2 = Box ( )
line = Line ( )
c = Canvas ( )
c . add ( b1 )
c . add ( b2 )
c . add ( line )
events = [ ]
def callback ( ) :
events . append ( " called " )
c . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , callback = callback )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
c . remove ( b1 )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
assert events == [ " called " ]
def test_disconnect_item_with_constraint_by_deleting_element ( ) :
b1 = Box ( )
b2 = Box ( )
line = Line ( )
c = Canvas ( )
c . add ( b1 )
c . add ( b2 )
c . add ( line )
cons = b1 . ports ( ) [ 0 ] . constraint ( c , line , line . handles ( ) [ 0 ] , b1 )
c . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , constraint = cons )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
ncons = len ( c . solver . constraints )
assert ncons == 5
c . remove ( b1 )
assert count ( c . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
assert 2 == len ( c . solver . constraints )
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
def test_line_projection ( ) :
""" Test projection with line ' s handle on element ' s side.
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
"""
line = Line ( )
line . matrix . translate ( 15 , 50 )
h1 , h2 = line . handles ( )
2019-05-23 22:14:27 +02:00
h1 . pos = ( 0 , 0 )
h2 . pos = ( 20 , 20 )
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
box = Box ( )
box . matrix . translate ( 10 , 10 )
box . width = 40
box . height = 20
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
canvas = Canvas ( )
canvas . add ( line )
canvas . add ( box )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
# move line's second handle on box side
2019-05-23 22:14:27 +02:00
h2 . pos = ( 5 , - 20 )
2018-11-25 16:07:58 -05:00
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
def test_remove_connected_item ( ) :
""" Test adding canvas constraint.
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
"""
canvas = Canvas ( )
2018-01-11 22:23:57 -05:00
2018-12-15 23:19:42 -05:00
from gaphas . aspect import Connector , ConnectionSink
2018-11-25 16:07:58 -05:00
2018-12-15 23:19:42 -05:00
l1 = Line ( )
canvas . add ( l1 )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
b1 = Box ( )
canvas . add ( b1 )
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
number_cons1 = len ( canvas . solver . constraints )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
b2 = Box ( )
canvas . add ( b2 )
2018-01-11 22:23:57 -05:00
2018-12-15 23:19:42 -05:00
number_cons2 = len ( canvas . solver . constraints )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
conn = Connector ( l1 , l1 . handles ( ) [ 0 ] )
sink = ConnectionSink ( b1 , b1 . ports ( ) [ 0 ] )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
conn . connect ( sink )
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
assert canvas . get_connection ( l1 . handles ( ) [ 0 ] )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
conn = Connector ( l1 , l1 . handles ( ) [ 1 ] )
sink = ConnectionSink ( b2 , b2 . ports ( ) [ 0 ] )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
conn . connect ( sink )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
assert canvas . get_connection ( l1 . handles ( ) [ 1 ] )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
assert number_cons2 + 2 == len ( canvas . solver . constraints )
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
canvas . remove ( b1 )
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
# Expecting a class + line connected at one end only
assert number_cons1 + 1 == len ( canvas . solver . constraints )