recthandlegraphicsitem.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.QtWidgets import QGraphicsRectItem
21 from PyQt5.QtGui import QPen, QBrush, QPolygonF
22 from PyQt5.QtCore import Qt
23 
24 SQUARE_SIDE = 10
25 PEN_FOCUS_WIDTH = 3
26 PEN_NORMAL_WIDTH = 1
27 
28 class RectHandleGraphicsItem(QGraphicsRectItem):
29  def __init__(self, width, parent=None):
30  super(QGraphicsRectItem, self).__init__(-SQUARE_SIDE / 2, -SQUARE_SIDE / 2, SQUARE_SIDE, SQUARE_SIDE, parent)
31  self.setAcceptHoverEvents(True)
32 
33  # set the color of the rectangle
34  brush = QBrush(Qt.SolidPattern)
35  brush.setColor(Qt.red)
36  self.setBrush(brush)
37 
38  self.dragging = False
39  self.interaction = True
40 
41  def hoverEnterEvent(self, event):
42  if self.interaction:
43  myPen = QPen(Qt.SolidLine)
44  myPen.setWidth(PEN_FOCUS_WIDTH)
45  self.setPen(myPen)
46 
47  def hoverLeaveEvent(self, event):
48  if self.interaction:
49  myPen = QPen(Qt.SolidLine)
50  myPen.setWidth(PEN_NORMAL_WIDTH)
51  self.setPen(myPen)
52 
53  def mousePressEvent(self, qGraphicsSceneMouseEvent):
54  if qGraphicsSceneMouseEvent.button() == Qt.LeftButton:
55  self.dragging = True
56  super(QGraphicsRectItem, self).mousePressEvent(qGraphicsSceneMouseEvent)
57 
58  def mouseReleaseEvent(self, qGraphicsSceneMouseEvent):
59  if qGraphicsSceneMouseEvent.button() == Qt.LeftButton:
60  self.dragging = False
61  super(QGraphicsRectItem, self).mouseReleaseEvent(qGraphicsSceneMouseEvent)
62 
63  def mouseMoveEvent(self, qGraphicsSceneMouseEvent):
64  if self.dragging:
65  self.parentItem().updateMiddlePoints(self.scenePos())
66  super(QGraphicsRectItem, self).mouseMoveEvent(qGraphicsSceneMouseEvent)
67 
68  def disableInteraction(self):
69  self.interaction = False


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