File: node_painter.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 (334 lines) | stat: -rw-r--r-- 11,535 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
import math

from qtpy.QtCore import QPointF, QRectF, Qt
from qtpy.QtGui import QFontMetrics, QLinearGradient, QPainter, QPen

from .base import FlowSceneBase, NodeBase
from .enums import NodeValidationState, PortType
from .node_data import NodeDataModel
from .node_geometry import NodeGeometry
from .node_graphics_object import NodeGraphicsObject
from .node_state import NodeState
from .style import ConnectionStyle, NodeStyle


class NodePainterDelegate:
    def paint(self, painter: QPainter, geom: NodeGeometry, model: NodeDataModel):
        """
        Paint

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        model : NodeDataModel
        """
        ...


class NodePainter:
    @staticmethod
    def paint(painter: QPainter, node: NodeBase, scene: FlowSceneBase,
              node_style: NodeStyle, connection_style: ConnectionStyle):
        """
        Paint

        Parameters
        ----------
        painter : QPainter
        node : Node
        scene : FlowScene
        node_style : NodeStyle
        connection_style : ConnectionStyle
        """
        geom = node.geometry
        state = node.state
        graphics_object = node.graphics_object
        geom.recalculate_size(painter.font())

        model = node.model
        NodePainter.draw_node_rect(painter, geom, model, graphics_object,
                                   node_style)
        NodePainter.draw_connection_points(painter, geom, state, model, scene,
                                           node_style, connection_style)
        NodePainter.draw_filled_connection_points(painter, geom, state, model,
                                                  node_style, connection_style
                                                  )
        NodePainter.draw_model_name(painter, geom, state, model, node_style)
        NodePainter.draw_entry_labels(painter, geom, state, model, node_style)
        NodePainter.draw_resize_rect(painter, geom, model)
        NodePainter.draw_validation_rect(painter, geom, model, graphics_object,
                                         node_style)

        # call custom painter
        painter_delegate = model.painter_delegate()
        if painter_delegate:
            painter_delegate.paint(painter, geom, model)

    @staticmethod
    def draw_node_rect(painter: QPainter, geom: NodeGeometry,
                       model: NodeDataModel,
                       graphics_object: NodeGraphicsObject,
                       node_style: NodeStyle):
        """
        Draw node rect

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        model : NodeDataModel
        graphics_object : NodeGraphicsObject
        node_style : NodeStyle
        """
        color = (node_style.selected_boundary_color
                 if graphics_object.isSelected()
                 else node_style.normal_boundary_color
                 )
        p = QPen(color, (node_style.hovered_pen_width
                         if geom.hovered
                         else node_style.pen_width))
        painter.setPen(p)

        gradient = QLinearGradient(QPointF(0.0, 0.0),
                                   QPointF(2.0, geom.height))
        for at_, color in node_style.gradient_colors:
            gradient.setColorAt(at_, color)
        painter.setBrush(gradient)

        diam = node_style.connection_point_diameter
        boundary = QRectF(-diam,
                          -diam,
                          2.0 * diam + geom.width,
                          2.0 * diam + geom.height)
        radius = 3.0
        painter.drawRoundedRect(boundary, radius, radius)

    @staticmethod
    def draw_model_name(painter: QPainter, geom: NodeGeometry,
                        state: NodeState,
                        model: NodeDataModel,
                        node_style: NodeStyle):
        """
        Draw model name

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        state : NodeState
        model : NodeDataModel
        """
        if not model.caption_visible:
            return
        name = model.caption
        f = painter.font()
        f.setBold(True)
        metrics = QFontMetrics(f)
        rect = metrics.boundingRect(name)
        position = QPointF((geom.width - rect.width()) / 2.0,
                           (geom.spacing + geom.entry_height) / 3.0)
        painter.setFont(f)
        painter.setPen(node_style.font_color)
        painter.drawText(position, name)
        f.setBold(False)
        painter.setFont(f)

    @staticmethod
    def draw_entry_labels(painter: QPainter, geom: NodeGeometry,
                          state: NodeState, model: NodeDataModel,
                          node_style: NodeStyle):
        """
        Draw entry labels

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        state : NodeState
        model : NodeDataModel
        node_style : NodeStyle
        """
        metrics = painter.fontMetrics()

        for port in state.ports:
            scene_pos = port.scene_position
            if not port.connections:
                painter.setPen(node_style.font_color_faded)
            else:
                painter.setPen(node_style.font_color)

            display_text = port.display_text
            rect = metrics.boundingRect(display_text)
            scene_pos.setY(scene_pos.y() + rect.height() / 4.0)
            if port.port_type == PortType.input:
                scene_pos.setX(5.0)
            elif port.port_type == PortType.output:
                scene_pos.setX(geom.width - 5.0 - rect.width())

            painter.drawText(scene_pos, display_text)

    @staticmethod
    def draw_connection_points(painter: QPainter, geom: NodeGeometry,
                               state: NodeState, model: NodeDataModel,
                               scene: FlowSceneBase, node_style: NodeStyle,
                               connection_style: ConnectionStyle
                               ):
        """
        Draw connection points

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        state : NodeState
        model : NodeDataModel
        scene : FlowScene
        connection_style : ConnectionStyle
        """
        diameter = node_style.connection_point_diameter
        reduced_diameter = diameter * 0.6
        for port in state.ports:
            scene_pos = port.scene_position
            can_connect = port.can_connect
            port_type = port.port_type
            data_type = port.data_type

            r = 1.0
            if state.is_reacting and can_connect and port_type == state.reacting_port_type:
                diff = geom.dragging_pos - scene_pos
                dist = math.sqrt(QPointF.dotProduct(diff, diff))

                registry = scene.registry
                dtype1, dtype2 = state.reacting_data_type, data_type
                if port_type != PortType.input:
                    dtype2, dtype1 = dtype1, dtype2

                type_convertable = registry.get_type_converter(dtype1, dtype2) is not None
                if dtype1.id == dtype2.id or type_convertable:
                    thres = 40.0
                    r = ((2.0 - dist / thres)
                         if dist < thres
                         else 1.0)
                else:
                    thres = 80.0
                    r = ((dist / thres)
                         if dist < thres
                         else 1.0)

            if connection_style.use_data_defined_colors:
                brush = connection_style.get_normal_color(data_type.id)
            else:
                brush = node_style.connection_point_color

            painter.setBrush(brush)
            painter.drawEllipse(scene_pos, reduced_diameter * r, reduced_diameter * r)

    @staticmethod
    def draw_filled_connection_points(painter: QPainter, geom: NodeGeometry,
                                      state: NodeState, model: NodeDataModel,
                                      node_style: NodeStyle,
                                      connection_style: ConnectionStyle
                                      ):

        """
        Draw filled connection points

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        state : NodeState
        model : NodeDataModel
        node_style : NodeStyle
        connection_style : ConnectionStyle
        """
        diameter = node_style.connection_point_diameter
        for port in state.ports:
            if not port.connections:
                continue

            scene_pos = port.scene_position
            if connection_style.use_data_defined_colors:
                c = connection_style.get_normal_color(port.data_type.id)
            else:
                c = node_style.filled_connection_point_color
            painter.setPen(c)
            painter.setBrush(c)
            painter.drawEllipse(scene_pos, diameter * 0.4, diameter * 0.4)

    @staticmethod
    def draw_resize_rect(painter: QPainter, geom: NodeGeometry, model: NodeDataModel):
        """
        Draw resize rect

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        model : NodeDataModel
        """
        if model.resizable():
            painter.setBrush(Qt.gray)
            painter.drawEllipse(geom.resize_rect)

    @staticmethod
    def draw_validation_rect(painter: QPainter, geom: NodeGeometry,
                             model: NodeDataModel,
                             graphics_object: NodeGraphicsObject,
                             node_style: NodeStyle):
        """
        Draw validation rect

        Parameters
        ----------
        painter : QPainter
        geom : NodeGeometry
        model : NodeDataModel
        graphics_object : NodeGraphicsObject
        node_style : NodeStyle
        """
        model_validation_state = model.validation_state()
        if model_validation_state == NodeValidationState.valid:
            return

        color = (node_style.selected_boundary_color
                 if graphics_object.isSelected()
                 else node_style.normal_boundary_color)

        if geom.hovered:
            p = QPen(color, node_style.hovered_pen_width)
        else:
            p = QPen(color, node_style.pen_width)

        painter.setPen(p)

        # Drawing the validation message background
        if model_validation_state == NodeValidationState.error:
            painter.setBrush(node_style.error_color)
        else:
            painter.setBrush(node_style.warning_color)

        radius = 3.0
        diam = node_style.connection_point_diameter
        boundary = QRectF(
            -diam,
            -diam + geom.height - geom.validation_height,
            2.0 * diam + geom.width,
            2.0 * diam + geom.validation_height,
        )
        painter.drawRoundedRect(boundary, radius, radius)
        painter.setBrush(Qt.gray)

        # Drawing the validation message itself
        error_msg = model.validation_message()
        f = painter.font()
        metrics = QFontMetrics(f)
        rect = metrics.boundingRect(error_msg)
        position = QPointF(
            (geom.width - rect.width()) / 2.0,
            geom.height - (geom.validation_height - diam) / 2.0
        )
        painter.setFont(f)
        painter.setPen(node_style.font_color)
        painter.drawText(position, error_msg)