1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
class PagePowerPlan(QGraphicsView):
def __init__(self, parent=None):
super(PagePowerPlan, self).__init__(parent)
# Widget for pagePowerPlan
self.scene = GraphicsScene(self)
self.scene.setSceneRect(0, 0, 500, 500)
self.setScene(self.scene)
# Variables
self.list_element_widget = []
self.list_arrows = []
self.widget_parent = 0
self.widget_child = 0
self.index_widget = 0
self.add_child_parent_connection = False
self.delete_element = False
# Variables for events
self.init_mouse_pos = QPoint()
self.rubber_band = QRubberBand(QRubberBand.Rectangle, self)
self.setDragMode(QGraphicsView.ScrollHandDrag)
self.item = None
self.move_scene_locked = True
self.move_item_locked = True
self.delta_mouse_item_pos = None
self.last_scene = None
def wheelEvent(self, event):
zoom = event.angleDelta().y()
if zoom > 0:
factor = 1.2
else:
factor = 0.8
self.scale(factor, factor)
def mousePressEvent(self, event):
self.init_mouse_pos = event.pos()
# Select an item
self.item = self.itemAt(event.pos())
print(self.item)
# Move an item on the scene
if event.button() == Qt.LeftButton:
# Left click --> Move an item on the scene or drag rubber band
self.move_scene_locked = True
if self.item is not None:
self.move_item_locked = False
self.item.item_clicked_from_scene()
self.delta_mouse_item_pos = self.item.scenePos() - self.mapToScene(self.init_mouse_pos)
else:
self.move_item_locked = True
# Rubber band
self.rubber_band.setGeometry(QRect(self.init_mouse_pos, QSize()))
self.rubber_band.show()
elif event.button() == Qt.RightButton:
# Right click --> Move the view on the scene
self.move_scene_locked = False
self.move_item_locked = True
QApplication.setOverrideCursor(Qt.OpenHandCursor)
def mouseMoveEvent(self, event):
update_mouse_pos = event.pos()
new_pos_map = self.mapToScene(event.pos())
# Move an item on the scene
if not self.move_item_locked:
updated_cursor_x = new_pos_map.x() + self.delta_mouse_item_pos.x()
updated_cursor_y = new_pos_map.y() + self.delta_mouse_item_pos.y()
self.item.setPos(QPointF(updated_cursor_x, updated_cursor_y))
self.item.item_moved_from_scene(updated_cursor_x, updated_cursor_y)
elif self.move_item_locked and self.move_scene_locked:
self.rubber_band.setGeometry(QRect(self.init_mouse_pos, update_mouse_pos).normalized())
# Move the view of the scene
if not self.move_scene_locked:
QApplication.changeOverrideCursor(Qt.ClosedHandCursor)
orig_scene_position = self.sceneRect()
print(orig_scene_position)
updated_cursor_x = (update_mouse_pos.x()) + orig_scene_position.x()
updated_cursor_y = (update_mouse_pos.y()) + orig_scene_position.y()
self.setSceneRect(updated_cursor_x, updated_cursor_y, 500, 500)
def mouseReleaseEvent(self, event):
self.item = None
self.move_item_locked = True
self.move_scene_locked = True
QApplication.changeOverrideCursor(Qt.ArrowCursor)
self.rubber_band.hide() |
Partager