summaryrefslogtreecommitdiff
path: root/src/main/python/utils/graphics.py
blob: f80c6533f9ba2a04159a64e24bddd436c4e77b0d (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt, QRectF, QPointF
from PyQt5.QtCore import Qt, QPointF, pyqtSignal
from PyQt5.QtWidgets import QGraphicsScene, QApplication
from PyQt5.QtGui import QPen, QKeySequence, QTransform, QCursor
from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsProxyWidget, QGraphicsItem, QUndoStack, QAction, QUndoView

from .undo import *
from .dialogs import showUndoDialog

import shapes

class CustomView(QGraphicsView):
    """
    Defines custom QGraphicsView with zoom features and drag-drop accept event, overriding wheel event
    """
    
    def __init__(self, scene = None, parent=None):
        if scene is not None: #overloaded constructor
            super(CustomView, self).__init__(scene, parent)
        else:
            super(CustomView, self).__init__(parent)
        self._zoom = 1
        self.setDragMode(True) #sets pannable using mouse
        self.setAcceptDrops(True) #sets ability to accept drops
    
    #following four functions are required to be overridden for drag-drop functionality
    def dragEnterEvent(self, QDragEnterEvent):
        #defines acceptable drop items
        if QDragEnterEvent.mimeData().hasText():
            QDragEnterEvent.acceptProposedAction()
        
    def dragMoveEvent(self, QDragMoveEvent):
        #defines acceptable drop items
        if QDragMoveEvent.mimeData().hasText():
            QDragMoveEvent.acceptProposedAction()
            
    def dragLeaveEvent(self, QDragLeaveEvent):
        #accept any drag leave event, avoid unnecessary logging
        QDragLeaveEvent.accept()
    
    def dropEvent(self, QDropEvent):
        #defines item drop, fetches text, creates corresponding QGraphicItem and adds it to scene
        if QDropEvent.mimeData().hasText():
            #QDropEvent.mimeData().text() defines intended drop item, the pos values define position
            obj = QDropEvent.mimeData().text().replace(',', '').split('/')
            graphic = getattr(shapes, obj[0])(*map(lambda x: int(x) if x.isdigit() else x, obj[1:]))
            graphic.setPos(QDropEvent.pos().x(), QDropEvent.pos().y())
            self.scene().addItemPlus(graphic)
            graphic.setParent(self)
            QDropEvent.acceptProposedAction()
     
    def wheelEvent(self, QWheelEvent):
        #overload wheelevent, to zoom if control is pressed, else scroll normally
        if QWheelEvent.modifiers() & Qt.ControlModifier: #check if control is pressed
            if QWheelEvent.source() == Qt.MouseEventNotSynthesized: #check if precision mouse(mac)
                # angle delta is 1/8th of a degree per scroll unit
                if self.zoom + QWheelEvent.angleDelta().y()/2880 > 0.1: # hit and trial value (2880)
                    self.zoom += QWheelEvent.angleDelta().y()/2880
            else:
                # precision delta is exactly equal to amount to scroll
                if self.zoom + QWheelEvent.pixelDelta().y() > 0.1:
                    self.zoom += QWheelEvent.angleDelta().y()
            QWheelEvent.accept() # accept event so that scrolling doesnt happen simultaneously
        else:
            return super(CustomView, self).wheelEvent(QWheelEvent) # scroll if ctrl not pressed
    
    @property
    def zoom(self):
        # property for zoom
        return self._zoom
    
    @zoom.setter
    def zoom(self, value):
        # set scale according to zoom value being set
        temp = self.zoom
        self._zoom = value
        self.scale(self.zoom / temp, self.zoom / temp)

class CustomScene(QGraphicsScene):
    """
    Extends QGraphicsScene with undo-redo functionality
    """
    labelAdded = pyqtSignal(shapes.QGraphicsItem)
    itemMoved = QtCore.pyqtSignal(QtWidgets.QGraphicsItem, QtCore.QPointF)

    def __init__(self, *args, parent=None):
        super(CustomScene, self).__init__(*args,  parent=parent)
        self.movingItems = []  # List to store selected items for moving
        self.oldPositions = {}  # Dictionary to store old positions of moved items
        self.undoStack = QUndoStack(self) #Used to store undo-redo moves
        self.createActions() #creates necessary actions that need to be called for undo-redo

    def createActions(self):
        # helper function to create delete, undo and redo shortcuts
        self.deleteAction = QAction("Delete Item", self)
        self.deleteAction.setShortcut(Qt.Key_Delete)
        self.deleteAction.triggered.connect(self.deleteItem)

        self.undoAction = self.undoStack.createUndoAction(self, "Undo")
        self.undoAction.setShortcut(QKeySequence.Undo)
        self.redoAction = self.undoStack.createRedoAction(self, "Redo")
        self.redoAction.setShortcut(QKeySequence.Redo)

    def createUndoView(self, parent):
        # creates an undo stack view for current QGraphicsScene
        undoView = QUndoView(self.undoStack, parent)
        showUndoDialog(undoView, parent)

    def deleteItem(self):
        # (slot) used to delete all selected items, and add undo action for each of them
        if self.selectedItems():
            for item in self.selectedItems():
                if issubclass(item.__class__,shapes.NodeItem) or isinstance(item,shapes.Line):
                    itemToDelete = item
                    self.count = 0
                    if(issubclass(itemToDelete.__class__,shapes.NodeItem)):
                        for i in itemToDelete.lineGripItems:
                            for j in i.lines:
                                self.count+=1
                                self.undoStack.push(deleteCommand(j, self))
                    self.undoStack.push(deleteCommand(itemToDelete, self))

    def itemMoved(self, movedItem, lastPos):
        #item move event, checks if item is moved
        self.undoStack.push(moveCommand(movedItem, lastPos))
        self.advance()

    def addItemPlus(self, item):
        # extended add item method, so that a corresponding undo action is also pushed
        self.undoStack.push(addCommand(item, self))

    def mousePressEvent(self, event):
        bdsp = event.buttonDownScenePos(Qt.LeftButton)  # Get click position
        point = QPointF(bdsp.x(), bdsp.y())  # Create a QPointF from click position
        itemList = self.items(point)  # Get items at the specified point
        if itemList:
            item = itemList[0]  # Select the first item in the list
            if event.button() == Qt.LeftButton:
                modifiers = QApplication.keyboardModifiers()
                if modifiers == Qt.ControlModifier:
                    # Ctrl key is pressed, add item to the moving items list
                    if item not in self.movingItems:
                        self.movingItems.append(item)
                        self.oldPositions[item] = item.pos()
                else:
                    # Ctrl key is not pressed, clear the moving items list and selection
                    self.movingItems.clear()
                    self.clearSelection()
                    item.setSelected(True)

        return super(CustomScene, self).mousePressEvent(event)


    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            for item in self.movingItems:
                if self.oldPositions[item] != item.pos():
                    # Item position has changed, invoke the callback function
                    self.itemMovedCallback(item, self.oldPositions[item])
            self.movingItems.clear()  # Clear the moving items list
            self.oldPositions.clear()  # Clear the old positions dictionary

        return super(CustomScene, self).mouseReleaseEvent(event)


    def mouseMoveEvent(self, mouseEvent):
        if self.movingItems:
            # Move all selected items together
            for item in self.movingItems:
                newPos = item.pos() + mouseEvent.scenePos() - mouseEvent.lastScenePos()
                item.setPos(newPos)

        item = self.itemAt(mouseEvent.scenePos().x(), mouseEvent.scenePos().y(), QTransform())
        if isinstance(item, shapes.SizeGripItem):
            item.parentItem().showLineGripItem()

        return super(CustomScene, self).mouseMoveEvent(mouseEvent)

    # The above is the mouse events for moving multiple images at once.

    """def mousePressEvent(self, event):
        bdsp = event.buttonDownScenePos(Qt.LeftButton)  # get click pos
        point = QPointF(bdsp.x(), bdsp.y())  # create a QPointF from click pos
        itemList = self.items(point)  # get items at said point
        if event.button() == Qt.LeftButton:
            if itemList:
                self.movingItems = itemList
                self.initialPositions = {}
                for item in self.movingItems:
                    self.initialPositions[item] = item.pos()
            else:
                self.movingItems = []
        self.clearSelection()
        return super(CustomScene, self).mousePressEvent(event)

    def mouseMoveEvent(self, event):
        if event.buttons() == Qt.LeftButton and self.movingItems:
            modifiers = QApplication.keyboardModifiers()
            if modifiers == Qt.ControlModifier:
                for item in self.movingItems:
                    item.setPos(item.pos() + event.scenePos() - event.lastScenePos())
            else:
                super(CustomScene, self).mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        if event.button() == Qt.LeftButton and self.movingItems:
            for item in self.movingItems:
                if item.pos() != self.initialPositions[item]:
                    self.itemMoved(item, self.initialPositions[item])
            self.movingItems = []
            self.initialPositions = {}
        return super(CustomScene, self).mouseReleaseEvent(event)"""

    # The above mouse events are for moving items on top of each other.

    """def mousePressEvent(self, event):
        # overloaded mouse press event to check if an item was moved
        bdsp = event.buttonDownScenePos(Qt.LeftButton) #get click pos
        point = QPointF(bdsp.x(), bdsp.y()) #create a Qpoint from click pos
        itemList = self.items(point) #get items at said point
        self.movingItem = itemList[0] if itemList else None #set first item in list as moving item
        if self.movingItem and event.button() == Qt.LeftButton:
            self.oldPos = self.movingItem.pos() #if left click is held, then store old pos
        self.clearSelection() #clears selected items
        return super(CustomScene, self).mousePressEvent(event)
    
    def mouseReleaseEvent(self, event):
        # overloaded mouse release event to check if an item was moved
        if self.movingItem and event.button() == Qt.LeftButton:
            if self.oldPos != self.movingItem.pos():
                #if item pos had changed, when mouse was realeased, emit itemMoved signal
                self.itemMoved(self.movingItem, self.oldPos)
            self.movingItem = None #clear movingitem reference
        return super(CustomScene, self).mouseReleaseEvent(event)
    
    def mouseMoveEvent(self, mouseEvent):
        item = self.itemAt(mouseEvent.scenePos().x(), mouseEvent.scenePos().y(),
                                   QTransform())
        if isinstance(item,shapes.SizeGripItem):
            item.parentItem().showLineGripItem()
        super(CustomScene,self).mouseMoveEvent(mouseEvent)"""

    # The above is the original mouse events

    def reInsertLines(self):
        currentIndex = self.undoStack.index()
        i = 2
        skipper = 0   
        while i != self.count+2+skipper:
            currentCommand = self.undoStack.command(currentIndex-i)
            if not self.undoStack.text(currentIndex-i).__contains__('Move'):
                currentLine = currentCommand.diagramItem
                startGrip = currentCommand.startGripItem
                endGrip = currentCommand.endGripItem
                index_LineGripStart = currentCommand.indexLGS
                index_LineGripEnd = currentCommand.indexLGE
                startGrip.lineGripItems[index_LineGripStart].lines.append(currentLine)
                endGrip.lineGripItems[index_LineGripEnd].lines.append(currentLine)
            else:
                skipper+=1
            self.undoStack.setIndex(currentIndex-i)
            i+=1