guistate.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 from PyQt5.QtCore import Qt, pyqtSignal
21 from PyQt5.QtGui import QBrush, QPen
22 from PyQt5.QtWidgets import QGraphicsEllipseItem, QGraphicsObject, QGraphicsItem
23 
24 from visualstates.gui.state.idtextboxgraphicsitem import IdTextBoxGraphicsItem
25 
26 
27 class StateGraphicsItem(QGraphicsObject):
28  # constant values
29  NODE_WIDTH = 40
30  INIT_WIDTH = 30
31  PEN_NORMAL_WIDTH = 1
32  PEN_FOCUS_WIDTH = 3
33 
34  posChanged = pyqtSignal('QGraphicsItem')
35  stateNameChanged = pyqtSignal('QGraphicsItem')
36 
37  stateTextEditStarted = pyqtSignal()
38  stateTextEditFinished = pyqtSignal()
39  doubleClicked = pyqtSignal('QGraphicsItem')
40 
41  def __init__(self, data):
42  super(QGraphicsObject, self).__init__()
43  self.stateData = data
44  self.setAcceptHoverEvents(True)
45  self.setFlag(QGraphicsItem.ItemIsMovable)
46  self.setAcceptDrops(True)
47 
48  # position of the graphics item on the scene
49  self.setPos(self.stateData.x, self.stateData.y)
50 
51  self.dragging = False
52 
53  # create an ellipse
54  self.ellipse = QGraphicsEllipseItem(-StateGraphicsItem.NODE_WIDTH / 2,
55  -StateGraphicsItem.NODE_WIDTH / 2,
56  StateGraphicsItem.NODE_WIDTH,
57  StateGraphicsItem.NODE_WIDTH, self)
58  brush = QBrush(Qt.SolidPattern)
59  brush.setColor(Qt.blue)
60  self.ellipse.setBrush(brush)
61 
62  self.textGraphics = IdTextBoxGraphicsItem(self.stateData.name, self)
63  textWidth = self.textGraphics.boundingRect().width()
64  self.textGraphics.setPos(-textWidth / 2, StateGraphicsItem.NODE_WIDTH -
65  (StateGraphicsItem.NODE_WIDTH / 2) + 5)
66  self.textGraphics.textChanged.connect(self.nameChanged)
67  self.textGraphics.textEditStarted.connect(self.textEditStarted)
68  self.textGraphics.textEditFinished.connect(self.textEditFinished)
69 
70  self.initGraphics = None
71  self.setInitial(self.stateData.initial)
72 
73  def setInitial(self, initial):
74  if initial:
75  if self.initGraphics is None:
76  self.initGraphics = QGraphicsEllipseItem(-StateGraphicsItem.INIT_WIDTH / 2,
77  -StateGraphicsItem.INIT_WIDTH / 2,
78  StateGraphicsItem.INIT_WIDTH,
79  StateGraphicsItem.INIT_WIDTH, self)
80  else:
81  self.initGraphics.setParentItem(self)
82  else:
83  if self.initGraphics is not None:
84  self.initGraphics.setParentItem(None)
85 
86  def hoverEnterEvent(self, event):
87  myPen = QPen(Qt.SolidLine)
88  myPen.setWidth(StateGraphicsItem.PEN_FOCUS_WIDTH)
89  self.ellipse.setPen(myPen)
90  super(QGraphicsObject, self).hoverEnterEvent(event)
91 
92  def hoverLeaveEvent(self, event):
93  myPen = QPen(Qt.SolidLine)
94  myPen.setWidth(StateGraphicsItem.PEN_NORMAL_WIDTH)
95  self.ellipse.setPen(myPen)
96  super(QGraphicsObject, self).hoverLeaveEvent(event)
97 
98  def mousePressEvent(self, qGraphicsSceneMouseEvent):
99  if qGraphicsSceneMouseEvent.button() == Qt.LeftButton:
100  self.dragging = True
101  super(QGraphicsObject, self).mousePressEvent(qGraphicsSceneMouseEvent)
102 
103  def mouseReleaseEvent(self, qGraphicsSceneMouseEvent):
104  if qGraphicsSceneMouseEvent.button() == Qt.LeftButton:
105  self.dragging = False
106  super(QGraphicsObject, self).mouseReleaseEvent(qGraphicsSceneMouseEvent)
107 
108  def mouseMoveEvent(self, qGraphicsSceneMouseEvent):
109  if self.dragging:
110  self.posChanged.emit(self)
111  super(QGraphicsObject, self).mouseMoveEvent(qGraphicsSceneMouseEvent)
112 
113  def mouseDoubleClickEvent(self, qGraphicsSceneMouseEvent):
114  self.doubleClicked.emit(self)
115 
116  def boundingRect(self):
117  return self.ellipse.boundingRect()
118 
119  def nameChanged(self, newName):
120  self.stateData.name = newName
121  textWidth = self.textGraphics.boundingRect().width()
122  # reposition to center the text
123  self.textGraphics.setPos(-textWidth / 2, StateGraphicsItem.NODE_WIDTH -
124  (StateGraphicsItem.NODE_WIDTH / 2) + 5)
125  self.stateNameChanged.emit(self)
126 
127  def textEditStarted(self):
128  self.stateTextEditStarted.emit()
129 
130  def textEditFinished(self):
131  self.stateTextEditFinished.emit()
132 
133  def paint(self, QPainter, QStyleOptionGraphicsItem, QWidget_widget=None):
134  pass
135 
136  def setRunning(self, status):
137  brush = QBrush(Qt.SolidPattern)
138  if status:
139  brush.setColor(Qt.green)
140  else:
141  brush.setColor(Qt.blue)
142 
143  self.ellipse.setBrush(brush)
144 
146  if self.textGraphics is not None:
147  self.textGraphics.disableTextEditingInteraction()
def paint(self, QPainter, QStyleOptionGraphicsItem, QWidget_widget=None)
Definition: guistate.py:133
def mouseDoubleClickEvent(self, qGraphicsSceneMouseEvent)
Definition: guistate.py:113
def mouseMoveEvent(self, qGraphicsSceneMouseEvent)
Definition: guistate.py:108
def mouseReleaseEvent(self, qGraphicsSceneMouseEvent)
Definition: guistate.py:103
def mousePressEvent(self, qGraphicsSceneMouseEvent)
Definition: guistate.py:98


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