# eric6.Graphics.AssociationItem

Module implementing a graphics item for an association between two items.

### Global Attributes

Center
East
Generalisation
Imports
NoRegion
Normal
North
NorthEast
NorthWest
South
SouthEast
SouthWest
West

### Classes

AssociationItem | Class implementing a graphics item for an association between two items.

### Functions

None

## AssociationItem

Class implementing a graphics item for an association between two items.

The association is drawn as an arrow starting at the first items and ending at the second.

### Derived from

E5ArrowItem

### Class Attributes

None

### Class Methods

parseAssociationItemDataString | Class method to parse the given persistence data.

### Methods

AssociationItem | Constructor
__calculateEndingPoints_center | Private method to calculate the ending points of the association item.
__calculateEndingPoints_rectangle | Private method to calculate the ending points of the association item.
__calculateEndingPoints_topToBottom | Private method to calculate the ending points of the association item.
__findIntersection | Private method to calculate the intersection point of two lines.
__findPointRegion | Private method to find out, which region of rectangle rect contains the point (PosX, PosY) and returns the region number.
__findRectIntersectionPoint | Private method to find the intersetion point of a line with a rectangle.
__mapRectFromItem | Private method to map item's rectangle to this item's coordinate system.
__updateEndPoint | Private method to update an endpoint.
buildAssociationItemDataString | Public method to build a string to persist the specific item data.
unassociate | Public method to unassociate from the widgets.
widgetMoved | Public method to recalculate the association after a widget was moved.

### Static Methods

None

#### AssociationItem.parseAssociationItemDataString (class method)

parseAssociationItemDataString(data)

Class method to parse the given persistence data.

data
  persisted data to be parsed (string)

Returns:
  tuple with the IDs of the source and destination items, the association type and a flag indicating to associate from top to bottom (integer, integer, integer, boolean)

#### AssociationItem (Constructor)

AssociationItem(itemA, itemB, assocType=Normal, topToBottom=False, parent=None)

Constructor

itemA
  first widget of the association
itemB
  second widget of the association
assocType
  type of the association. This must be one of
  - Normal (default)
  - Generalisation
  - Imports
topToBottom=
  flag indicating to draw the association from item A top to item B bottom (boolean)
parent=
  reference to the parent object (QGraphicsItem)

#### AssociationItem.__calculateEndingPoints_center

__calculateEndingPoints_center()

Private method to calculate the ending points of the association item.

The ending points are calculated from the centers of the two associated items.

#### AssociationItem.__calculateEndingPoints_rectangle

__calculateEndingPoints_rectangle()

Private method to calculate the ending points of the association item.

The ending points are calculated by the following method.

For each item the diagram is divided in four Regions by its diagonals as indicated below

                       \  Region 2  /
                        \          /
                         |--------|
                         | \    / |
                         |  \  /  |
                         |   \/   |
                Region 1 |   /\   | Region 3
                         |  /  \  |
                         | /    \ |
                         |--------|
                        /          \
                       /  Region 4  \

Each diagonal is defined by two corners of the bounding rectangle

To calculate the start point we have to find out in which region (defined by itemA's diagonals) is itemB's TopLeft corner (lets call it region M). After that the start point will be the middle point of rectangle's side contained in region M.

To calculate the end point we repeat the above but in the opposite direction (from itemB to itemA)

#### AssociationItem.__calculateEndingPoints_topToBottom

__calculateEndingPoints_topToBottom()

Private method to calculate the ending points of the association item.

The ending points are calculated from the top center of the lower item to the bottom center of the upper item.

#### AssociationItem.__findIntersection

__findIntersection(p1, p2, p3, p4)

Private method to calculate the intersection point of two lines.

The first line is determined by the points p1 and p2, the second line by p3 and p4. If the intersection point is not contained in the segment p1p2, then it returns (-1.0, -1.0).

For the function's internal calculations remember:
QT coordinates start with the point (0,0) as the topleft corner and x-values increase from left to right and y-values increase from top to bottom; it means the visible area is quadrant I in the regular XY coordinate system

                Quadrant II     |   Quadrant I
               -----------------|-----------------
                Quadrant III    |   Quadrant IV

In order for the linear function calculations to work in this method we must switch x and y values (x values become y values and viceversa)

p1
  first point of first line (QPointF)
p2
  second point of first line (QPointF)
p3
  first point of second line (QPointF)
p4
  second point of second line (QPointF)

Returns:
  the intersection point (QPointF)

#### AssociationItem.__findPointRegion

__findPointRegion(rect, posX, posY)

Private method to find out, which region of rectangle rect contains the point (PosX, PosY) and returns the region number.

rect
  rectangle to calculate the region for (QRectF)
posX
  x position of point (float)
posY
  y position of point (float)

Returns:
  the calculated region number
  West = Region 1
  North = Region 2
  East = Region 3
  South = Region 4
  NorthWest = On diagonal 2 between Region 1 and 2
  NorthEast = On diagonal 1 between Region 2 and 3
  SouthEast = On diagonal 2 between Region 3 and 4
  SouthWest = On diagonal 1 between Region4 and 1
  Center = On diagonal 1 and On diagonal 2 (the center)

#### AssociationItem.__findRectIntersectionPoint

__findRectIntersectionPoint(item, p1, p2)

Private method to find the intersetion point of a line with a rectangle.

item
  item to check against
p1
  first point of the line (QPointF)
p2
  second point of the line (QPointF)

Returns:
  the intersection point (QPointF)

#### AssociationItem.__mapRectFromItem

__mapRectFromItem(item)

Private method to map item's rectangle to this item's coordinate system.

item
  reference to the item to be mapped (QGraphicsRectItem)

Returns:
  item's rectangle in local coordinates (QRectF)

#### AssociationItem.__updateEndPoint

__updateEndPoint(region, isWidgetA)

Private method to update an endpoint.

region
  the region for the endpoint (integer)
isWidgetA
  flag indicating update for itemA is done (boolean)

#### AssociationItem.buildAssociationItemDataString

buildAssociationItemDataString()

Public method to build a string to persist the specific item data.

This string should be built like "attribute=value" with pairs separated by ", ". value must not contain ", " or newlines.

Returns:
  persistence data (string)

#### AssociationItem.unassociate

unassociate()

Public method to unassociate from the widgets.

#### AssociationItem.widgetMoved

widgetMoved()

Public method to recalculate the association after a widget was moved.

Up