File: flow_view.py

package info (click to toggle)
python-qtpynodeeditor 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 604 kB
  • sloc: python: 5,038; makefile: 14; sh: 1
file content (348 lines) | stat: -rw-r--r-- 11,015 bytes parent folder | download
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import logging
import math

from qtpy.QtCore import QLineF, QPoint, QRectF, Qt
from qtpy.QtGui import (QContextMenuEvent, QKeyEvent, QMouseEvent, QPainter,
                        QPen, QShowEvent, QWheelEvent, QKeySequence)
from qtpy.QtWidgets import (QAction, QGraphicsView, QLineEdit, QMenu,
                            QTreeWidget, QTreeWidgetItem, QWidgetAction)

from .connection_graphics_object import ConnectionGraphicsObject
from .flow_scene import FlowScene
from .node_graphics_object import NodeGraphicsObject

logger = logging.getLogger(__name__)


class FlowView(QGraphicsView):
    def __init__(self, scene, parent=None):
        super().__init__(parent=parent)

        self._clear_selection_action = None
        self._delete_selection_action = None
        self._scene = None
        self._click_pos = None

        self.setDragMode(QGraphicsView.ScrollHandDrag)
        self.setRenderHint(QPainter.Antialiasing)

        # setViewportUpdateMode(QGraphicsView.FullViewportUpdate)
        # setViewportUpdateMode(QGraphicsView.MinimalViewportUpdate)
        self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)

        self.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)

        self.setCacheMode(QGraphicsView.CacheBackground)
        # setViewport(new QGLWidget(QGLFormat(QGL.SampleBuffers)))
        if scene is not None:
            self.setScene(scene)

        self._style = self._scene.style_collection
        self.setBackgroundBrush(self._style.flow_view.background_color)

    def clear_selection_action(self) -> QAction:
        """
        Clear selection action

        Returns
        -------
        value : QAction
        """
        return self._clear_selection_action

    def delete_selection_action(self) -> QAction:
        """
        Delete selection action

        Returns
        -------
        value : QAction
        """
        return self._delete_selection_action

    def setScene(self, scene: FlowScene):
        """
        setScene

        Parameters
        ----------
        scene : FlowScene
        """
        self._scene = scene
        super().setScene(self._scene)

        # setup actions
        del self._clear_selection_action
        self._clear_selection_action = QAction("Clear Selection", self)
        self._clear_selection_action.setShortcut(QKeySequence.Cancel)
        self._clear_selection_action.triggered.connect(self._scene.clearSelection)

        self.addAction(self._clear_selection_action)
        del self._delete_selection_action
        self._delete_selection_action = QAction("Delete Selection", self)
        self._delete_selection_action.setShortcut(QKeySequence.Backspace)
        self._delete_selection_action.setShortcut(QKeySequence.Delete)
        self._delete_selection_action.triggered.connect(self.delete_selected)
        self.addAction(self._delete_selection_action)

    def scale_up(self):
        step = 1.2
        factor = step ** 1.0
        t = self.transform()
        if t.m11() <= 2.0:
            self.scale(factor, factor)

    def scale_down(self):
        step = 1.2
        factor = step ** -1.0
        self.scale(factor, factor)

    def delete_selected(self):
        # Delete the selected connections first, ensuring that they won't be
        # automatically deleted when selected nodes are deleted (deleting a node
        # deletes some connections as well)
        for item in self._scene.selectedItems():
            if isinstance(item, ConnectionGraphicsObject):
                self._scene.delete_connection(item.connection)

        if not self._scene.allow_node_deletion:
            return

        # Delete the nodes; self will delete many of the connections.
        # Selected connections were already deleted prior to self loop, otherwise
        # qgraphicsitem_cast<NodeGraphicsObject*>(item) could be a use-after-free
        # when a selected connection is deleted by deleting the node.
        for item in self._scene.selectedItems():
            if isinstance(item, NodeGraphicsObject):
                self._scene.remove_node(item.node)

    def generate_context_menu(self, pos: QPoint):
        """
        Generate a context menu for contextMenuEvent

        Parameters
        ----------
        pos : QPoint
            The point where the context menu was requested
        """
        model_menu = QMenu()
        skip_text = "skip me"

        # Add filterbox to the context menu
        txt_box = QLineEdit(model_menu)
        txt_box.setPlaceholderText("Filter")
        txt_box.setClearButtonEnabled(True)
        txt_box_action = QWidgetAction(model_menu)
        txt_box_action.setDefaultWidget(txt_box)
        model_menu.addAction(txt_box_action)

        # Add result treeview to the context menu
        tree_view = QTreeWidget(model_menu)
        tree_view.header().close()
        tree_view_action = QWidgetAction(model_menu)
        tree_view_action.setDefaultWidget(tree_view)
        model_menu.addAction(tree_view_action)

        top_level_items = {}
        for cat in self._scene.registry.categories():
            item = QTreeWidgetItem(tree_view)
            item.setText(0, cat)
            item.setData(0, Qt.UserRole, skip_text)
            top_level_items[cat] = item

        registry = self._scene.registry
        for model, category in registry.registered_models_category_association().items():
            self.parent = top_level_items[category]
            item = QTreeWidgetItem(self.parent)
            item.setText(0, model)
            item.setData(0, Qt.UserRole, model)

        tree_view.expandAll()

        def click_handler(item):
            model_name = item.data(0, Qt.UserRole)
            if model_name == skip_text:
                return

            try:
                model, _ = self._scene.registry.get_model_by_name(model_name)
            except ValueError:
                logger.error("Model not found: %s", model_name)
            else:
                node = self._scene.create_node(model)
                pos_view = self.mapToScene(pos)
                node.graphics_object.setPos(pos_view)
                self._scene.node_placed.emit(node)

            model_menu.close()

        tree_view.itemClicked.connect(click_handler)

        # Setup filtering
        def filter_handler(text):
            for name, top_lvl_item in top_level_items.items():
                for i in range(top_lvl_item.childCount()):
                    child = top_lvl_item.child(i)
                    model_name = child.data(0, Qt.UserRole)
                    child.setHidden(text not in model_name)

        txt_box.textChanged.connect(filter_handler)

        # make sure the text box gets focus so the user doesn't have to click on it
        txt_box.setFocus()
        return model_menu

    def contextMenuEvent(self, event: QContextMenuEvent):
        """
        contextMenuEvent

        Parameters
        ----------
        event : QContextMenuEvent
        """
        if self.itemAt(event.pos()):
            super().contextMenuEvent(event)
            return
        elif not self._scene.allow_node_creation:
            return

        menu = self.generate_context_menu(event.pos())
        menu.exec_(event.globalPos())

    def wheelEvent(self, event: QWheelEvent):
        """
        wheelEvent

        Parameters
        ----------
        event : QWheelEvent
        """
        delta = event.angleDelta()
        if delta.y() == 0:
            event.ignore()
            return

        d = delta.y() / abs(delta.y())
        if d > 0.0:
            self.scale_up()
        else:
            self.scale_down()

    def keyPressEvent(self, event: QKeyEvent):
        """
        keyPressEvent

        Parameters
        ----------
        event : QKeyEvent
        """
        if event.key() == Qt.Key_Shift:
            self.setDragMode(QGraphicsView.RubberBandDrag)

        super().keyPressEvent(event)

    def keyReleaseEvent(self, event: QKeyEvent):
        """
        keyReleaseEvent

        Parameters
        ----------
        event : QKeyEvent
        """
        if event.key() == Qt.Key_Shift:
            self.setDragMode(QGraphicsView.ScrollHandDrag)
        super().keyReleaseEvent(event)

    def mousePressEvent(self, event: QMouseEvent):
        """
        mousePressEvent

        Parameters
        ----------
        event : QMouseEvent
        """
        super().mousePressEvent(event)
        if event.button() == Qt.LeftButton:
            self._click_pos = self.mapToScene(event.pos())

    def mouseMoveEvent(self, event: QMouseEvent):
        """
        mouseMoveEvent

        Parameters
        ----------
        event : QMouseEvent
        """
        super().mouseMoveEvent(event)
        if self._scene.mouseGrabberItem() is None and event.buttons() == Qt.LeftButton:
            # Make sure shift is not being pressed
            if not (event.modifiers() & Qt.ShiftModifier):
                difference = self._click_pos - self.mapToScene(event.pos())
                self.setSceneRect(self.sceneRect().translated(difference.x(), difference.y()))

    def drawBackground(self, painter: QPainter, r: QRectF):
        """
        drawBackground

        Parameters
        ----------
        painter : QPainter
        r : QRectF
        """
        super().drawBackground(painter, r)

        def draw_grid(grid_step):
            window_rect = self.rect()
            tl = self.mapToScene(window_rect.topLeft())
            br = self.mapToScene(window_rect.bottomRight())
            left = math.floor(tl.x() / grid_step - 0.5)
            right = math.floor(br.x() / grid_step + 1.0)
            bottom = math.floor(tl.y() / grid_step - 0.5)
            top = math.floor(br.y() / grid_step + 1.0)

            # vertical lines
            lines = [
                QLineF(xi * grid_step, bottom * grid_step, xi * grid_step, top * grid_step)
                for xi in range(int(left), int(right) + 1)
            ]

            # horizontal lines
            lines.extend(
                [QLineF(left * grid_step, yi * grid_step, right * grid_step, yi * grid_step)
                 for yi in range(int(bottom), int(top) + 1)
                 ]
            )

            painter.drawLines(lines)

        style = self._style.flow_view
        # brush = self.backgroundBrush()
        pfine = QPen(style.fine_grid_color, 1.0)
        painter.setPen(pfine)
        draw_grid(15)
        p = QPen(style.coarse_grid_color, 1.0)
        painter.setPen(p)
        draw_grid(150)

    def showEvent(self, event: QShowEvent):
        """
        showEvent

        Parameters
        ----------
        event : QShowEvent
        """
        self._scene.setSceneRect(QRectF(self.rect()))
        super().showEvent(event)

    @property
    def scene(self) -> FlowScene:
        """
        Scene

        Returns
        -------
        value : FlowScene
        """
        return self._scene