--- a/eric6/Graphics/AssociationItem.py Thu Apr 29 17:39:17 2021 +0200 +++ b/eric6/Graphics/AssociationItem.py Fri Apr 30 19:54:20 2021 +0200 @@ -7,6 +7,8 @@ Module implementing a graphics item for an association between two items. """ +import enum + from PyQt5.QtCore import QPointF, QRectF, QLineF from PyQt5.QtWidgets import QGraphicsItem @@ -15,22 +17,29 @@ import Utilities -# TODO: convert to Enum -Normal = 0 -Generalisation = 1 -Imports = 2 +class AssociationType(enum.Enum): + """ + Class defining the association types. + """ + NORMAL = 0 + GENERALISATION = 1 + IMPORTS = 2 + -# TODO: convert to Enum -NoRegion = 0 -West = 1 -North = 2 -East = 3 -South = 4 -NorthWest = 5 -NorthEast = 6 -SouthEast = 7 -SouthWest = 8 -Center = 9 +class AssociationPointRegion(enum.Enum): + """ + Class defining the regions for an association end point. + """ + NO_REGION = 0 + WEST = 1 + NORTH = 2 + EAST = 3 + SOUTH = 4 + NORTH_WEST = 5 + NORTH_EAST = 6 + SOUTH_EAST = 7 + SOUTH_WEST = 8 + CENTER = 9 class AssociationItem(E5ArrowItem): @@ -40,19 +49,17 @@ The association is drawn as an arrow starting at the first items and ending at the second. """ - def __init__(self, itemA, itemB, assocType=Normal, topToBottom=False, - colors=None, parent=None): + def __init__(self, itemA, itemB, assocType=AssociationType.NORMAL, + topToBottom=False, colors=None, parent=None): """ Constructor @param itemA first widget of the association + @type UMLItem @param itemB second widget of the association - @param assocType type of the association. This must be one of - <ul> - <li>Normal (default)</li> - <li>Generalisation</li> - <li>Imports</li> - </ul> + @type UMLItem + @param assocType type of the association + @type AssociationType @param topToBottom flag indicating to draw the association from item A top to item B bottom @type bool @@ -61,10 +68,10 @@ @param parent reference to the parent object @type QGraphicsItem """ - if assocType in (Normal, Imports): + if assocType in (AssociationType.NORMAL, AssociationType.IMPORTS): arrowType = E5ArrowType.NORMAL arrowFilled = True - elif assocType == Generalisation: + elif assocType == AssociationType.GENERALISATION: arrowType = E5ArrowType.WIDE arrowFilled = False @@ -87,8 +94,8 @@ self.assocType = assocType self.topToBottom = topToBottom - self.regionA = NoRegion - self.regionB = NoRegion + self.regionA = AssociationPointRegion.NO_REGION + self.regionB = AssociationPointRegion.NO_REGION self.calculateEndingPoints() @@ -216,14 +223,17 @@ rc = QRectF(xA, yA, rectA.width(), rectA.height()) self.regionA = self.__findPointRegion(rc, xB, yB) # move some regions to the standard ones - if self.regionA == NorthWest: - self.regionA = North - elif self.regionA == NorthEast: - self.regionA = East - elif self.regionA == SouthEast: - self.regionA = South - elif self.regionA in (SouthWest, Center): - self.regionA = West + if self.regionA == AssociationPointRegion.NORTH_WEST: + self.regionA = AssociationPointRegion.NORTH + elif self.regionA == AssociationPointRegion.NORTH_EAST: + self.regionA = AssociationPointRegion.EAST + elif self.regionA == AssociationPointRegion.SOUTH_EAST: + self.regionA = AssociationPointRegion.SOUTH + elif self.regionA in ( + AssociationPointRegion.SOUTH_WEST, + AssociationPointRegion.CENTER + ): + self.regionA = AssociationPointRegion.WEST self.__updateEndPoint(self.regionA, True) @@ -231,14 +241,17 @@ rc = QRectF(xB, yB, rectB.width(), rectB.height()) self.regionB = self.__findPointRegion(rc, xA, yA) # move some regions to the standard ones - if self.regionB == NorthWest: - self.regionB = North - elif self.regionB == NorthEast: - self.regionB = East - elif self.regionB == SouthEast: - self.regionB = South - elif self.regionB in (SouthWest, Center): - self.regionB = West + if self.regionB == AssociationPointRegion.NORTH_WEST: + self.regionB = AssociationPointRegion.NORTH + elif self.regionB == AssociationPointRegion.NORTH_EAST: + self.regionB = AssociationPointRegion.EAST + elif self.regionB == AssociationPointRegion.SOUTH_EAST: + self.regionB = AssociationPointRegion.SOUTH + elif self.regionB in ( + AssociationPointRegion.SOUTH_WEST, + AssociationPointRegion.CENTER + ): + self.regionB = AssociationPointRegion.WEST self.__updateEndPoint(self.regionB, False) @@ -273,43 +286,43 @@ eval1 = slope1 * posY + b1 eval2 = slope2 * posY + b2 - result = NoRegion + result = AssociationPointRegion.NO_REGION # inside region 1 if eval1 > posX and eval2 > posX: - result = West + result = AssociationPointRegion.WEST #inside region 2 elif eval1 > posX and eval2 < posX: - result = North + result = AssociationPointRegion.NORTH # inside region 3 elif eval1 < posX and eval2 < posX: - result = East + result = AssociationPointRegion.EAST # inside region 4 elif eval1 < posX and eval2 > posX: - result = South + result = AssociationPointRegion.SOUTH # inside region 5 elif eval1 == posX and eval2 < posX: - result = NorthWest + result = AssociationPointRegion.NORTH_WEST # inside region 6 elif eval1 < posX and eval2 == posX: - result = NorthEast + result = AssociationPointRegion.NORTH_EAST # inside region 7 elif eval1 == posX and eval2 > posX: - result = SouthEast + result = AssociationPointRegion.SOUTH_EAST # inside region 8 elif eval1 > posX and eval2 == posX: - result = SouthWest + result = AssociationPointRegion.SOUTH_WEST # inside region 9 elif eval1 == posX and eval2 == posX: - result = Center + result = AssociationPointRegion.CENTER return result @@ -320,7 +333,7 @@ @param region the region for the endpoint (integer) @param isWidgetA flag indicating update for itemA is done (boolean) """ - if region == NoRegion: + if region == AssociationPointRegion.NO_REGION: return rect = ( @@ -335,16 +348,19 @@ ch = wh / 2.0 cw = ww / 2.0 - if region == West: + if region == AssociationPointRegion.WEST: px = x py = y + ch - elif region == North: + elif region == AssociationPointRegion.NORTH: px = x + cw py = y - elif region == East: + elif region == AssociationPointRegion.EAST: px = x + ww py = y + ch - elif region in (South, Center): + elif region in ( + AssociationPointRegion.SOUTH, + AssociationPointRegion.CENTER + ): px = x + cw py = y + wh @@ -534,7 +550,7 @@ entries = [ "src={0}".format(self.itemA.getId()), "dst={0}".format(self.itemB.getId()), - "type={0}".format(self.assocType), + "type={0}".format(self.assocType.value), "topToBottom={0}".format(self.topToBottom) ] return ", ".join(entries) @@ -551,7 +567,7 @@ """ src = -1 dst = -1 - assocType = Normal + assocType = AssociationType.NORMAL topToBottom = False for entry in data.split(", "): if "=" in entry: @@ -561,7 +577,7 @@ elif key == "dst": dst = int(value) elif key == "type": - assocType = int(value) + assocType = AssociationType(int(value)) elif key == "topToBottom": topToBottom = Utilities.toBool(value)