# eric4.Graphics.AssociationItem

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

### 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

E4ArrowItem

### 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.
__findIntersection | 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.
unassociate | Public method to unassociate from the widgets.
widgetMoved | Public method to recalculate the association after a widget was moved.

#### AssociationItem (Constructor)

AssociationItem(itemA, itemB, type = Normal, parent = None)

Constructor

itemA
  first widget of the association
itemB
  second widget of the association
type
  type of the association. This must be one of
  - Normal (default)
  - Generalisation
  - Imports
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.__findIntersection

__findIntersection(p1, p2, p3, p4)

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.unassociate

unassociate()

Public method to unassociate from the widgets.

#### AssociationItem.widgetMoved

widgetMoved()

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

Up