File: grid_widget.py

package info (click to toggle)
raysession 0.17.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,168 kB
  • sloc: python: 44,371; sh: 1,538; makefile: 208; xml: 86
file content (375 lines) | stat: -rw-r--r-- 14,188 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import math
from typing import TYPE_CHECKING, Optional
from qtpy.QtWidgets import QGraphicsPathItem
from qtpy.QtGui import QPen, QColor, QBrush, QPainter, QPainterPath
from qtpy.QtCore import QPointF, Qt, QRectF

from .init_values import canvas, options, GridStyle

if TYPE_CHECKING:
    from .scene_moth import PatchSceneMoth


def x_multiply(path: QPainterPath, n_times: int, width: int) -> QPainterPath:
    paths = [path]
        
    while n_times >= 2 ** len(paths):
        new_path = paths[-1].translated(0.0, 0.0)
        new_path.addPath(
            paths[-1].translated(
                width * 2 ** (len(paths) -1), 0.0))
        
        paths.append(new_path)
        
    x_done = 2 ** (len(paths) - 1)
    x_path = paths[-1]
    
    for i in range(len(paths) - 2, -1, -1):
        if 2 ** i <= n_times - x_done:
            x_path.addPath(
                paths[i].translated(x_done * width, 0.0))
            x_done += 2 ** i
    return x_path

def y_multiply(path: QPainterPath, n_times: int, height: int) -> QPainterPath:
    paths = [path]
        
    while n_times >= 2 ** len(paths):
        new_path = paths[-1].translated(0.0, 0.0)
        new_path.addPath(
            paths[-1].translated(
                0.0, height * 2 ** (len(paths) -1)))
        
        paths.append(new_path)
        
    y_done = 2 ** (len(paths) - 1)
    y_path = paths[-1]
    
    for i in range(len(paths) - 2, -1, -1):
        if 2 ** i <= n_times - y_done:
            y_path.addPath(
                paths[i].translated(0.0, y_done * height))
            y_done += 2 ** i
    return y_path


class GridWidget(QGraphicsPathItem):
    def __init__(self, scene: 'PatchSceneMoth', style=GridStyle.GRID):
        QGraphicsPathItem.__init__(self)
        self._scene = scene
        self.style = style
        self._rects = list[QRectF]()
        self.setPath(QPainterPath(QPointF()))
        self.left_path: Optional[QPainterPath] = None
        self.right_path: Optional[QPainterPath] = None
        self.left_path_width = 0.0
        self.right_path_width = 0.0
        
        self._pointt = QPointF(0.0, 0.0)
        
        self.global_path = QPainterPath()
        self._bounding_rect = QRectF()
        self.update_path()

    def grid_path(self):
        theme = canvas.theme.grid
        
        if self.style is GridStyle.TECHNICAL_GRID:
            theme = theme.technical_grid
        elif self.style is GridStyle.GRID:
            theme = theme.grid
        
        path = QPainterPath()
        x_path = QPainterPath()
        y_path = QPainterPath()
        cell_x = options.cell_width
        cell_y = options.cell_height
        
        if self.style is GridStyle.GRID:
            cell_x = cell_x * math.ceil(theme.grid_min_width / cell_x)
            cell_y = cell_y * math.ceil(theme.grid_min_height / cell_y)
        
        rect = self._scene.sceneRect()

        x = rect.left()
        x -= x % cell_x
        if x < rect.left():
            x += cell_x

        x_path.moveTo(QPointF(x, rect.top()))
        x_path.lineTo(QPointF(x, rect.bottom()))

        path.addPath(
            x_multiply(
                x_path, 1 + math.floor((rect.right() - x) / cell_x), cell_x))

        y = rect.top()
        y -= y % cell_y
        if y < rect.top():
            y += cell_y

        y_path.moveTo(QPointF(rect.left(), y))
        y_path.lineTo(QPointF(rect.right(), y))
        
        path.addPath(
            y_multiply(
                y_path, 1 + math.floor((rect.bottom() - y) / cell_y), cell_y))

        self.setPath(path)
        self.setPen(theme.fill_pen)
        self.setBrush(QBrush(Qt.BrushStyle.NoBrush))

    def update_path(self):
        self._bounding_rect = self._scene.sceneRect()
         
        if self.style is GridStyle.CHESSBOARD:
            self.chess_board()
        else:
            self.grid_path()
    
    def chess_board(self):
        theme = canvas.theme.grid.chessboard
        
        cell_x = (options.cell_width
                  * math.ceil(theme.grid_min_width / options.cell_width))
        cell_y = (options.cell_height
                  * math.ceil(theme.grid_min_height / options.cell_height))
        
        path = QPainterPath(QPointF(0.0, 0.0))
        rect = self._scene.sceneRect()
        
        x = rect.left()
        x -= x % (cell_x * 2)
        x += 2 * cell_x

        y = rect.top()
        y -= y % (cell_y * 2)        
        y += 2 * cell_y

        # make the normal pattern
        orig_path = QPainterPath()
        orig_path.moveTo(x + cell_x / 2, y)
        orig_path.lineTo(x + cell_x / 2, y + cell_y)
        orig_path.moveTo(x + cell_x * 1.5, y + cell_y)
        orig_path.lineTo(x + cell_x * 1.5, y + 2.0 * cell_y)

        total_x = math.floor((rect.right() - x) / (2 * cell_x)) 
        total_y = math.floor((rect.bottom() - y)/ (2 * cell_y))

        y_path = orig_path
        y_path = y_multiply(orig_path, total_y, 2 * cell_y)
        y_down = y + total_y * 2 * cell_y
        x_right = x + total_x * 2 * cell_x
        
        # construct top 
        if rect.top() < y:
            top_path = QPainterPath()
            if y - rect.top() > cell_y:
                top_path.moveTo(x + cell_x / 2, rect.top())
                top_path.lineTo(x + cell_x / 2, y - cell_y)
                top_path.moveTo(x + cell_x * 1.5, y - cell_y)
                top_path.lineTo(x + cell_x * 1.5, y)
            else:
                top_path.moveTo(x + cell_x * 1.5, rect.top())
                top_path.lineTo(x + cell_x * 1.5, y)

            y_path.addPath(top_path)
        
        # construct bottom
        if rect.bottom() > y + total_y * 2 * cell_y:
            bot_path = QPainterPath()

            if rect.bottom() - y_down > cell_y:
                bot_path.moveTo(x + cell_x / 2, y_down)
                bot_path.lineTo(x + cell_x / 2, y_down + cell_y)
                bot_path.moveTo(x + cell_x * 1.5, y_down + cell_y)
                bot_path.lineTo(x + cell_x * 1.5, rect.bottom())
            else:
                bot_path.moveTo(x + cell_x / 2, y_down)
                bot_path.lineTo(x + cell_x / 2, rect.bottom())

            y_path.addPath(bot_path)

        x_path = x_multiply(y_path, total_x, cell_x * 2)

        # construct left
        if rect.left() < x:
            if x - rect.left() > cell_x:
                left_path = QPainterPath()
                left_width = x - cell_x - rect.left()
                left_pos = rect.left() + left_width / 2
                left_path.moveTo(left_pos, y)
                left_path.lineTo(left_pos, y + cell_y)
                
                sec_left_path = QPainterPath()
                sec_left_path.moveTo(x - cell_x / 2, y + cell_y)
                sec_left_path.lineTo(x - cell_x / 2, y + 2 * cell_y)
                
                self.left_path = y_multiply(left_path, total_y, cell_y * 2)
                sec_lef_path = y_multiply(sec_left_path, total_y, cell_y * 2)

                if y > rect.top():
                    if y > rect.top() + cell_y:
                        self.left_path.moveTo(left_pos, rect.top())
                        self.left_path.lineTo(left_pos, y - cell_y)
                        sec_lef_path.moveTo(x - cell_x / 2, y - cell_y)
                        sec_lef_path.lineTo(x - cell_x / 2, y)
                    else:
                        sec_lef_path.moveTo(x - cell_x / 2, rect.top())
                        sec_lef_path.lineTo(x - cell_x / 2, y)
                
                if rect.bottom() > y_down:
                    if rect.bottom() > y_down + cell_y:
                        self.left_path.moveTo(left_pos, y_down)
                        self.left_path.lineTo(left_pos, y_down + cell_y)
                        sec_lef_path.moveTo(x - cell_x / 2, y_down + cell_y)
                        sec_lef_path.lineTo(x - cell_x / 2, rect.bottom())
                    else:
                        self.left_path.moveTo(left_pos, y_down)
                        self.left_path.lineTo(left_pos, rect.bottom())
                        
                x_path.addPath(sec_lef_path)
                
            else:
                left_path = QPainterPath()
                left_width = x - rect.left()
                left_pos = rect.left() + left_width / 2
                left_path.moveTo(rect.left() + left_width / 2, y + cell_y)
                left_path.lineTo(rect.left() + left_width / 2, y + 2 * cell_y)
                
                self.left_path = y_multiply(left_path, total_y, cell_y * 2)
                
                if y > rect.top():
                    if y > rect.top() + cell_y:
                        self.left_path.moveTo(left_pos, y - cell_y)
                        self.left_path.lineTo(left_pos, y)
                    else:
                        self.left_path.moveTo(left_pos, rect.top())
                        self.left_path.lineTo(left_pos, y)
                        
                if rect.bottom() > y_down:
                    if rect.bottom() > y_down + cell_y:
                        self.left_path.moveTo(left_pos, y_down + cell_y)
                        self.left_path.lineTo(left_pos, rect.bottom())
                                
            self.left_path_width = left_width
        else:
            self.left_path = None

        # construct right
        if rect.right() > x_right:
            if rect.right() >= x_right + cell_x:
                sec_right_path = QPainterPath()
                sec_right_path.moveTo(x_right + cell_x / 2, y)
                sec_right_path.lineTo(x_right + cell_x / 2, y + cell_y)                
                sec_rig_path = y_multiply(sec_right_path, total_y, cell_y * 2)
                
                right_path = QPainterPath()
                right_width = rect.right() - (x_right + cell_x)
                right_pos = rect.right() - right_width / 2

                right_path.moveTo(right_pos, y + cell_y)
                right_path.lineTo(right_pos, y + 2 * cell_y)
                
                self.right_path = y_multiply(right_path, total_y, cell_y * 2)

                if y > rect.top():
                    if y > rect.top() + cell_y:
                        sec_rig_path.moveTo(x_right + cell_x / 2, rect.top())
                        sec_rig_path.lineTo(x_right + cell_x / 2, y - cell_y)
                        self.right_path.moveTo(right_pos, y - cell_y)
                        self.right_path.lineTo(right_pos, y)
                    else:
                        self.right_path.moveTo(right_pos, rect.top())
                        self.right_path.lineTo(right_pos, y)
                
                if rect.bottom() > y_down:
                    if rect.bottom() > y_down + cell_y:
                        sec_rig_path.moveTo(x_right + cell_x / 2, y_down)
                        sec_rig_path.lineTo(
                            x_right + cell_x / 2, y_down + cell_y)
                        self.right_path.moveTo(right_pos, y_down + cell_y)
                        self.right_path.lineTo(right_pos, rect.bottom())
                    else:
                        sec_rig_path.moveTo(x_right + cell_x / 2, y_down)
                        sec_rig_path.lineTo(
                            x_right + cell_x / 2, rect.bottom())
                
                x_path.addPath(sec_rig_path)
                
            else:
                right_path = QPainterPath()
                right_width = rect.right() - x_right
                right_pos = rect.right() - right_width / 2
                right_path.moveTo(right_pos, y)
                right_path.lineTo(right_pos, y + cell_y)
                
                self.right_path = y_multiply(right_path, total_y, cell_y * 2)
                
                if y > rect.top():
                    if y > rect.top() + cell_y:
                        self.right_path.moveTo(right_pos, rect.top())
                        self.right_path.lineTo(right_pos, y - cell_y)
                        
                if rect.bottom() > y_down:
                    if rect.bottom() > y_down + cell_y:
                        self.right_path.moveTo(right_pos, y_down)
                        self.right_path.lineTo(right_pos, y_down + cell_y)
                    else:
                        self.right_path.moveTo(right_pos, y_down)
                        self.right_path.lineTo(right_pos, rect.bottom())
            
            self.right_path_width = right_width
        else:
            self.right_path = None

        path.setFillRule(Qt.FillRule.WindingFill)
        
        self.setPath(x_path)

        pen = QPen(theme.fill_pen)
        pen.setWidthF(cell_x)
        
        self.setPen(QPen(
            theme.fill_pen.color(), cell_x,
            Qt.PenStyle.SolidLine, Qt.PenCapStyle.FlatCap))
    
    def boundingRect(self) -> QRectF:
        # prevent the grid widget to make the scene Rect growing infinitely
        return self._bounding_rect
    
    def paint(self, painter: QPainter, option, widget):
        if canvas.loading_items:
            return
        
        painter.save()
        painter.setRenderHint(QPainter.RenderHint.Antialiasing, False)
        
        QGraphicsPathItem.paint(self, painter, option, widget)

        painter.drawPath(self.path())
        
        # for left and right paths, alpha channel is strangly too low
        # after changing the pen width.
        sides_color = self.pen().color() 
        sides_color.setAlphaF(min(sides_color.alphaF() * 1.707, 1.0))
        
        if self.left_path is not None:
            pen = QPen(self.pen())
            pen.setWidthF(self.left_path_width)
            pen.setColor(sides_color)
            painter.setPen(pen)
            painter.drawPath(self.left_path)
        
        if self.right_path is not None:
            pen = QPen(self.pen())
            pen.setWidthF(self.right_path_width)
            pen.setColor(sides_color)
            painter.setPen(pen)
            painter.drawPath(self.right_path)
        
        # pen = QPen(QColor(168, 120, 0), 1.0)
        # painter.setPen(pen)
        # painter.drawEllipse(self._pointt, 3.0, 3.0)

        painter.restore()