guitransition.py
Go to the documentation of this file.
1 '''
2  Copyright (C) 1997-2017 JDERobot Developers Team
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Library General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, see <http://www.gnu.org/licenses/>.
16 
17  Authors : Okan Asik (asik.okan@gmail.com)
18 
19  '''
20 import math
21 
22 from PyQt5.QtCore import Qt, QPointF, QLineF, pyqtSignal
23 from PyQt5.QtGui import QBrush, QPolygonF
24 from PyQt5.QtWidgets import QGraphicsObject, QGraphicsLineItem, QGraphicsPolygonItem, QGraphicsItem
25 
26 from visualstates.gui.state.idtextboxgraphicsitem import IdTextBoxGraphicsItem
27 from visualstates.gui.state.guistate import StateGraphicsItem
28 from visualstates.gui.transition.recthandlegraphicsitem import RectHandleGraphicsItem
29 
30 
31 class TransitionGraphicsItem(QGraphicsObject):
32  # constant values
33  SQUARE_SIDE = 10
34  ARROW_SIZE = 12
35  PEN_NORMAL_WIDTH = 1
36  PEN_FOCUS_WIDTH = 3
37 
38  posChanged = pyqtSignal('QGraphicsItem')
39 
40  def __init__(self, data):
41  super(QGraphicsObject, self).__init__()
42  self.transitionData = data
43 
44  self.originLine = None
45  self.destinationLine = None
46  self.arrow = None
47  self.textGraphics = None
48  self.middleHandle = None
49 
50  self.graphicsOrigin = self.transitionData.origin.getGraphicsItem()
51  self.graphicsDestination = self.transitionData.destination.getGraphicsItem()
52 
53  # connect position changed event
54  self.graphicsOrigin.posChanged.connect(self.statePosChanged)
55  self.graphicsDestination.posChanged.connect(self.statePosChanged)
56 
57  self.midPointX = (self.graphicsDestination.scenePos().x() + self.graphicsOrigin.scenePos().x()) / 2.0
58  self.midPointY = (self.graphicsDestination.scenePos().y() + self.graphicsOrigin.scenePos().y()) / 2.0
59 
60  self.createOriginLine()
62 
63  self.createArrow()
64  self.createMiddleHandle()
65  self.createIdTextBox()
66 
67  def statePosChanged(self, state):
68  if self.graphicsOrigin == state:
69  self.createOriginLine()
70  elif self.graphicsDestination == state:
72  self.createArrow()
73 
74  def createOriginLine(self):
75  if self.originLine == None:
76  self.originLine = QGraphicsLineItem(self.midPointX, self.midPointY, self.graphicsOrigin.scenePos().x(),
77  self.graphicsOrigin.scenePos().y(), self)
78  else:
79  self.originLine.setLine(QLineF(self.midPointX, self.midPointY, self.graphicsOrigin.scenePos().x(),
80  self.graphicsOrigin.scenePos().y()))
81  myLine = self.originLine.line()
82  myLine.setLength(myLine.length() - StateGraphicsItem.NODE_WIDTH / 2)
83  self.originLine.setLine(myLine)
84 
86  if self.destinationLine == None:
87  self.destinationLine = QGraphicsLineItem(self.midPointX, self.midPointY, self.graphicsDestination.scenePos().x(),
88  self.graphicsDestination.scenePos().y(), self)
89  else:
90  self.destinationLine.setLine(QLineF(self.midPointX, self.midPointY, self.graphicsDestination.scenePos().x(),
91  self.graphicsDestination.scenePos().y()))
92 
93  myLine = self.destinationLine.line()
94  myLine.setLength(myLine.length() - StateGraphicsItem.NODE_WIDTH / 2)
95  self.destinationLine.setLine(myLine)
96 
97  def createArrow(self):
98  # add an arrow to destination line
99  myLine = self.destinationLine.line()
100  myLine.setLength(myLine.length() - TransitionGraphicsItem.ARROW_SIZE)
101  rotatePoint = myLine.p2() - self.destinationLine.line().p2()
102 
103  rightPointX = rotatePoint.x() * math.cos(math.pi / 6) - rotatePoint.y() * math.sin(math.pi / 6)
104  rightPointY = rotatePoint.x() * math.sin(math.pi / 6) + rotatePoint.y() * math.cos(math.pi / 6)
105  rightPoint = QPointF(rightPointX + self.destinationLine.line().x2(),
106  rightPointY + self.destinationLine.line().y2())
107 
108  leftPointX = rotatePoint.x() * math.cos(-math.pi / 6) - rotatePoint.y() * math.sin(-math.pi / 6)
109  leftPointY = rotatePoint.x() * math.sin(-math.pi / 6) + rotatePoint.y() * math.cos(-math.pi / 6)
110  leftPoint = QPointF(leftPointX + self.destinationLine.line().x2(),
111  leftPointY + self.destinationLine.line().y2())
112 
113  polygon = QPolygonF()
114  polygon << rightPoint << leftPoint << self.destinationLine.line().p2() << rightPoint
115 
116  if self.arrow == None:
117  self.arrow = QGraphicsPolygonItem(polygon, self)
118  else:
119  self.arrow.setPolygon(polygon)
120 
121  brush = QBrush(Qt.SolidPattern)
122  brush.setColor(Qt.black)
123  self.arrow.setBrush(brush)
124 
126  # create middle handle
127  if self.middleHandle == None:
128  self.middleHandle = RectHandleGraphicsItem(TransitionGraphicsItem.SQUARE_SIDE, self)
129  self.middleHandle.setFlag(QGraphicsItem.ItemIsMovable)
130 
131  self.middleHandle.setPos(self.midPointX, self.midPointY)
132 
133  def createIdTextBox(self):
134  if self.textGraphics == None:
135  self.textGraphics = IdTextBoxGraphicsItem(self.transitionData.name, self)
136  self.textGraphics.textChanged.connect(self.nameChanged)
137  else:
138  self.textGraphics.setPlainText(self.transitionData.name)
139  textWidth = self.textGraphics.boundingRect().width()
140  self.textGraphics.setPos(self.midPointX - textWidth / 2, self.midPointY + TransitionGraphicsItem.SQUARE_SIDE -
141  (TransitionGraphicsItem.SQUARE_SIDE / 2) + 5)
142 
143  def updateMiddlePoints(self, newPosition):
144  self.midPointX = newPosition.x()
145  self.midPointY = newPosition.y()
146  self.createOriginLine()
147  self.createDestinationLine()
148  self.createArrow()
149  self.createIdTextBox()
150  self.posChanged.emit(self)
151 
152  def nameChanged(self, name):
153  self.transitionData.name = name
154  self.createIdTextBox()
155 
156  def boundingRect(self):
157  if self.middleHandle != None:
158  return self.middleHandle.boundingRect()
159  else:
160  return None
161 
163  if self.middleHandle is not None:
164  self.middleHandle.setFlag(QGraphicsItem.ItemIsMovable, False)
165  self.middleHandle.disableInteraction()
166  if self.textGraphics is not None:
167  self.textGraphics.disableTextEditingInteraction()


visualstates
Author(s):
autogenerated on Thu Apr 1 2021 02:42:20