-
Notifications
You must be signed in to change notification settings - Fork 13
/
qt_fig.py
1090 lines (990 loc) · 41 KB
/
qt_fig.py
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###########################################################################
#
# Copyright 2015-2018 Robert B. Lowrie (http://github.com/lowrie)
#
# This file is part of pyRouterJig.
#
# pyRouterJig is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# pyRouterJig is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# pyRouterJig; see the file LICENSE. If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################
'''
Contains the Qt functionality for drawing the template and boards.
'''
from __future__ import print_function
from __future__ import division
import time
from decimal import Decimal
from future.utils import lrange
from PyQt5 import QtCore, QtGui, QtWidgets, QtPrintSupport
import router
import utils
def paint_text(painter, text, coord, flags, shift=(0, 0), angle=0, fill_color=None):
'''
Puts text at coord with alignment flags. Returns a QRect of the bounding box
of the text that was painted.
painter: QPainter object
text: The text to print.
coord: The coordinate at which to place the text
flags: QtCore.Qt alignment flags
shift: Adjustments in coord, in pixels
angle: Rotation angle
fill_color: If not None, QColor that fills the bounding rectangle behind the text.
'''
# Save the current transform, then switch to transform that places the origin
# at coord and rotated by angle.
transform = painter.transform()
(x, y) = transform.map(coord[0], coord[1])
x += shift[0]
y += shift[1]
painter.resetTransform()
painter.translate(x, y)
painter.rotate(angle)
# Create a large rectangle and use it to find the bounding rectangle around the text.
# Find the origin of the rectangle based on the alignment.
big = 5000
xorg = 0
yorg = 0
if flags & QtCore.Qt.AlignRight:
xorg = -big
elif flags & QtCore.Qt.AlignHCenter:
xorg = -big // 2
if flags & QtCore.Qt.AlignBottom:
yorg = -big
elif flags & QtCore.Qt.AlignVCenter:
yorg = -big // 2
rect = QtCore.QRectF(xorg, yorg, big, big)
rect = painter.boundingRect(rect, flags, text)
# Draw the text
if fill_color is not None:
b = QtGui.QBrush(fill_color)
painter.fillRect(rect, b)
painter.drawText(rect, flags, text)
# Find the text rectangle in our original coordinate system
rect1 = painter.transform().mapRect(rect)
(inverted, dummy_invertable) = transform.inverted()
rect = inverted.mapRect(rect1)
# Restore the original transform
painter.setTransform(transform)
return rect
class Qt_Fig(QtWidgets.QWidget):
'''
Interface to the qt_driver, using Qt to draw the boards and template.
The attribute "canvas" is self, to mimic the old interface to matplotlib,
which this class replaced.
'''
def __init__(self, template, boards, config):
QtWidgets.QWidget.__init__(self)
self.canvas = self
self.config = config
self.colors = {}
self.woods = {}
self.description = ''
self.fig_width = -1
self.fig_height = -1
self.window_width = -1
self.window_height = -1
self.set_fig_dimensions(template, boards)
self.geom = None
self.labels = ['B', 'C', 'D', 'E', 'F']
# font sizes are in 1/32" of an inch
self.font_size = {'title': 4,
'fingers': 3,
'template': 3,
'boards': 4,
'template_labels': 3,
'watermark': 4}
self.transform = None
self.base_transform = None
self.mouse_pos = None
self.scaling = 1.0
self.translate = [0.0, 0.0]
self.zoom_mode = False
# Initialize keyboard modifiers
self.shift_key = False
self.transl = boards[0].units.transl
def minimumSizeHint(self):
'''
Minimum size for this widget
'''
return QtCore.QSize(100, 100)
def sizeHint(self):
'''
Size hint for this widget
'''
return QtCore.QSize(self.window_width, self.window_height)
def enable_zoom_mode(self, mode):
'''
Sets the zoom mode
'''
self.zoom_mode = mode
def set_fig_dimensions(self, template, boards):
'''
Computes the figure dimension attributes, fig_width and fig_height, in
increments.
Returns True if the dimensions changed.
'''
# Try default margins, but reset if the template is too small for margins
units = boards[0].units
top_margin = units.abstract_to_increments(self.config.top_margin)
bottom_margin = units.abstract_to_increments(self.config.bottom_margin)
left_margin = units.abstract_to_increments(self.config.left_margin)
right_margin = units.abstract_to_increments(self.config.right_margin)
separation = units.abstract_to_increments(self.config.separation)
self.margins = utils.Margins(separation, separation, left_margin, right_margin,
bottom_margin, top_margin)
# Set the figure dimensions
fig_width = template.length + self.margins.left + self.margins.right
fig_height = template.height + self.margins.bottom + self.margins.top
for i in lrange(4):
if boards[i].active:
fig_height += boards[i].height + self.margins.sep
if boards[3].active:
fig_height += template.height + self.margins.sep
if self.config.show_caul:
fig_height += template.height + self.margins.sep
min_width = 64
if fig_width < min_width:
fig_width = min_width
self.margins.left = (fig_width - template.length) // 2
self.margins.right = self.margins.left
fig_width = template.length + self.margins.left + self.margins.right
dimensions_changed = False
if fig_width != self.fig_width:
self.fig_width = fig_width
dimensions_changed = True
if fig_height != self.fig_height:
self.fig_height = fig_height
dimensions_changed = True
# The 1200 here is effectively a dpi for the screen
scale = 1200 * boards[0].units.increments_to_inches(1)
self.window_width = int(scale * fig_width)
self.window_height = int(scale * fig_height)
return dimensions_changed
def set_colors(self, do_color):
'''
Sets the colors to be used from the configuration.
'''
color_names = ['board_background',
'board_foreground',
'canvas_background',
'canvas_foreground',
'watermark_color',
'template_margin_background',
'template_margin_foreground',
'pass_color',
'pass_alt_color',
'center_color']
self.colors = {}
for c in color_names:
self.colors[c] = QtGui.QColor(*self.config.__dict__[c])
if not do_color:
for c in color_names:
g = self.colors[c].lightness()
self.colors[c].setRed(g)
self.colors[c].setGreen(g)
self.colors[c].setBlue(g)
def draw(self, template, boards, bit, spacing, woods, description):
'''
Draws the figure
'''
# Generate the new geometry layout
self.set_fig_dimensions(template, boards)
self.woods = woods
self.description = description
self.geom = router.Joint_Geometry(template, boards, bit, spacing, self.margins,
self.config)
self.update()
def print(self, template, boards, bit, spacing, woods, description):
'''
Prints the figure
'''
self.woods = woods
self.description = description
self.set_colors(self.config.print_color)
# Generate the new geometry layout
self.set_fig_dimensions(template, boards)
self.geom = router.Joint_Geometry(template, boards, bit, spacing, self.margins,
self.config)
# Print through the preview dialog
printer = QtPrintSupport.QPrinter(QtPrintSupport.QPrinter.HighResolution)
printer.setOrientation(QtPrintSupport.QPrinter.Landscape)
printer.setPageMargins(0, 0, 0, 0, QtPrintSupport.QPrinter.Inch)
pdialog = QtPrintSupport.QPrintPreviewDialog(printer)
pdialog.setModal(True)
pdialog.paintRequested.connect(self.preview_requested)
return pdialog.exec_()
def image(self, template, boards, bit, spacing, woods, description):
'''
Prints the figure to a QImage object
'''
self.woods = woods
self.description = description
self.set_fig_dimensions(template, boards)
self.geom = router.Joint_Geometry(template, boards, bit, spacing, self.margins,
self.config)
self.set_colors(True)
s = self.size()
window_ar = float(s.width()) / s.height()
fig_ar = float(self.fig_width) / self.fig_height
if window_ar < fig_ar:
w = s.width()
else:
w = int(s.height() * fig_ar)
w = max(self.config.min_image_width, min(self.config.max_image_width, w))
h = utils.my_round(w / fig_ar)
sNew = QtCore.QSize(w, h)
im = QtGui.QImage(sNew, QtGui.QImage.Format_RGB32)
painter = QtGui.QPainter()
painter.begin(im)
size = im.size()
if self.colors['canvas_background'] is not None:
b = QtGui.QBrush(self.colors['canvas_background'])
painter.fillRect(0, 0, size.width(), size.height(), b)
self.paint_all(painter)
painter.end()
return im
def preview_requested(self, printer):
'''
Handles the print preview action.
'''
dpi = printer.resolution() * self.config.print_scale_factor
painter = QtGui.QPainter()
painter.begin(printer)
self.set_colors(self.config.print_color)
self.paint_all(painter, dpi)
painter.end()
def paintEvent(self, dummy_event):
'''
Handles the paint event, which draws to the screen
'''
if self.geom is None:
return
self.set_colors(True)
painter = QtGui.QPainter(self)
size = self.size()
# on the screen, we add a background color:
if self.colors['canvas_background'] is not None:
b = QtGui.QBrush(self.colors['canvas_background'])
painter.fillRect(0, 0, size.width(), size.height(), b)
# paint all of the objects
self.window_width, self.window_height = self.paint_all(painter)
# on the screen, highlight the active cuts
self.draw_active_cuts(painter)
painter.end()
def set_font_size(self, painter, param):
'''
Sets the font size for font type param
'''
font_inches = self.font_size[param] / 32.0 * self.geom.bit.units.increments_per_inch
dx = self.transform.map(font_inches, 0)[0] - self.transform.dx()
font = painter.font()
font.setPixelSize(utils.my_round(dx))
painter.setFont(font)
def paint_all(self, painter, dpi=None):
'''
Paints all the objects.
painter: A QPainter object
dpi: The resolution of the painter, in dots-per-inch. If None, then
the image is maximized in the window, but maintaining aspect ratio.
'''
rw = painter.window()
window_width = rw.width()
window_height = rw.height()
units = self.geom.bit.units
if dpi is None:
# transform the painter to maintain the figure aspect ratio in the current
# window
window_ar = float(window_width) / window_height
fig_ar = float(self.fig_width) / self.fig_height
if fig_ar < window_ar:
w = utils.my_round(fig_ar * window_height)
painter.translate((window_width - w) // 2, window_height)
scale = float(window_height) / self.fig_height
else:
h = utils.my_round(window_width / fig_ar)
painter.translate(0, (window_height + h) // 2)
scale = float(window_width) / self.fig_width
else:
# Scale so that the image is the correct size on the page
painter.translate(0, window_height)
scale = float(dpi) / units.increments_per_inch
painter.scale(scale, -scale)
# Save the inverse of the un-zoomed transform
(self.base_transform, dummy_invertable) = painter.transform().inverted()
# Apply the zoom, zooming on the current figure center
painter.scale(self.scaling, self.scaling)
factor = 0.5 - 0.5 / self.scaling
x = self.translate[0] - self.fig_width * factor
y = self.translate[1] - self.fig_height * factor
painter.translate(x, y)
self.transform = painter.transform()
# draw the objects
self.draw_boards(painter)
self.draw_template(painter)
self.draw_title(painter)
# self.draw_finger_sizes(painter)
if self.config.show_finger_widths:
self.draw_finger_sizes(painter)
return (window_width, window_height)
def draw_passes(self, painter, blabel, cuts, y1, y2, flags, xMid,
is_template=True):
'''
Draws and labels the router passes on a template or board.
painter: The QPainter object
blabel: The board label (A, B, C, ...)
cuts: Array of router.Cut objects
y1: y-location where pass label is placed
y2: y-location where end of line is located
flags: Horizontal alignment for label
xMid: x-location of board center
is_template: If true, then a template
Returns the pass label if a pass matches xMid, None otherwise
'''
board_T = self.geom.board_T
shift = (0, 0) # for adjustments of text
passMid = None # location of board-center pass (return value)
font_type = 'template'
char_size = self.font_size[font_type]
self.set_font_size(painter, font_type)
# Collect the router pass locations in a single array by looping
# through each cut and each pass for each cut, right-to-left.
xp = []
for c in cuts[::-1]:
for p in lrange(len(c.passes) - 1, -1, -1):
xp.append(c.passes[p])
# Loop through the passes and do the labels
np = len(xp)
for i in lrange(np):
# Determine vertical alignment, by checking the separation from adjacent passes.
# If too close to an adjacent pass, then shift the alignment in the opposite
# direction.
flagsv = flags
diffm = xp[max(0, i - 1)] - xp[i]
diffp = xp[i] - xp[min(np - 1, i + 1)]
if i == 0:
if diffp < char_size:
flagsv |= QtCore.Qt.AlignTop
else:
flagsv |= QtCore.Qt.AlignVCenter
elif i == np - 1:
if diffm < char_size:
flagsv |= QtCore.Qt.AlignBottom
else:
flagsv |= QtCore.Qt.AlignVCenter
elif diffp < char_size:
if diffm < char_size:
flagsv |= QtCore.Qt.AlignVCenter
else:
flagsv |= QtCore.Qt.AlignTop
else:
if diffm < char_size:
flagsv |= QtCore.Qt.AlignBottom
else:
flagsv |= QtCore.Qt.AlignVCenter
xpShift = xp[i] + board_T.xL()
# Draw the text label for this pass
label = ''
this_is_midpoint = False
if is_template or self.config.show_router_pass_identifiers:
label = '%d%s' % (i + 1, blabel)
if xpShift == xMid:
passMid = label
this_is_midpoint = True
if not is_template and self.config.show_router_pass_locations:
if label:
label += ': '
loc = self.geom.bit.units.increments_to_string(board_T.xR() - xpShift)
label += loc
painter.save()
if this_is_midpoint and is_template:
pen = painter.pen()
pen.setColor(self.colors['center_color'])
painter.setPen(pen)
r = paint_text(painter, label, (xpShift, y1), flagsv, shift, -90)
# Determine the line starting point from the size of the text.
# Create a small margin so that the starting point is not too
# close to the text.
y1text = y1
if y1 > y2:
y1text = r.y()
if y1text > y2:
y1text += 0.05 * (y2 - y1text)
else:
y1text = r.y() + r.height()
if y1text < y2:
y1text += 0.05 * (y2 - y1text)
# If there is any room left, draw the line from the label to the base of cut
if (y1 - y2) * (y1text - y2) > 0:
p1 = QtCore.QPointF(xpShift, y1text)
p2 = QtCore.QPointF(xpShift, y2)
painter.drawLine(p1, p2)
painter.restore()
return passMid
def draw_alignment(self, painter):
'''
Draws the alignment lines on all templates
'''
board_T = self.geom.board_T
board_TDD = self.geom.board_TDD
board_caul = self.geom.board_caul
# draw the alignment lines on both templates
x = board_T.xR() + self.geom.bit.width // 2
pen = QtGui.QPen(QtCore.Qt.SolidLine)
pen.setColor(self.colors['template_margin_foreground'])
pen.setWidthF(0)
bg_pen = QtGui.QPen(QtCore.Qt.SolidLine)
bg_pen.setColor(QtGui.QColor('White'))
bg_pen.setWidthF(0)
self.set_font_size(painter, 'template')
label = 'ALIGN'
flags = QtCore.Qt.AlignTop | QtCore.Qt.AlignHCenter
for b in [board_T, board_TDD, board_caul]:
if b is not None:
y1 = b.yB()
y2 = b.yT()
painter.setPen(pen)
painter.drawLine(x, y1, x, y2)
paint_text(painter, label, (x, (y1 + y2) // 2), flags, (0, 0), -90)
painter.setPen(bg_pen)
painter.drawLine(QtCore.QPointF(x-0.5, y1+0.5), QtCore.QPointF(x-0.5, y2-0.5))
painter.drawLine(QtCore.QPointF(x+0.5, y1+0.5), QtCore.QPointF(x+0.5, y2-0.5))
def draw_template_rectangle(self, painter, r, b):
'''
Draws the geometry of a template
'''
# Fill the entire template as white
painter.fillRect(r.xL(), r.yB(), r.width, r.height, QtCore.Qt.white)
# Fill the template margins with a grayshade
brush = QtGui.QBrush(QtGui.QColor(self.colors['template_margin_background']))
painter.fillRect(r.xL(), r.yB(), b.xL() - r.xL(), r.height, brush)
painter.fillRect(b.xR(), r.yB(), r.xR() - b.xR(), r.height, brush)
# Draw the template bounding box
painter.drawRect(r.xL(), r.yB(), r.width, r.height)
# Label the template with a watermark
if self.description is not None:
painter.save()
self.set_font_size(painter, 'watermark')
painter.setPen(self.colors['watermark_color'])
flags = QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter
x = r.xL() + r.width // 2
y = r.yB() + r.height // 2
paint_text(painter, self.description, (x, y), flags)
painter.restore()
def draw_template(self, painter):
'''
Draws the Incra templates
'''
rect_T = self.geom.rect_T
board_T = self.geom.board_T
boards = self.geom.boards
xMid = board_T.xMid()
centerline = []
centerline_TDD = []
pen_canvas = QtGui.QPen(QtCore.Qt.SolidLine)
pen_canvas.setColor(self.colors['canvas_foreground'])
pen_canvas.setWidthF(0)
penA = QtGui.QPen(QtCore.Qt.SolidLine)
penA.setColor(self.colors['pass_color'])
penA.setWidthF(0)
penB = QtGui.QPen(QtCore.Qt.DashLine)
penB.setColor(self.colors['pass_alt_color'])
penB.setWidthF(0)
painter.setPen(pen_canvas)
self.draw_template_rectangle(painter, rect_T, board_T)
if boards[3].active:
rect_TDD = self.geom.rect_TDD
board_TDD = self.geom.board_TDD
self.draw_template_rectangle(painter, rect_TDD, board_TDD)
rect_top = rect_TDD
else:
rect_top = rect_T
flagsL = QtCore.Qt.AlignLeft
flagsR = QtCore.Qt.AlignRight
show_passes = self.config.show_router_pass_identifiers |\
self.config.show_router_pass_locations
frac_depth = 0.95 * self.geom.bit.depth
sepOver2 = 0.5 * self.geom.margins.sep
# Draw the router passes
# ... do the top board passes
y1 = boards[0].yB() - sepOver2
y2 = boards[0].yB() + frac_depth
painter.setPen(penA)
pm = self.draw_passes(painter, 'A', boards[0].bottom_cuts, rect_top.yMid(),
rect_top.yT(), flagsR, xMid)
if pm is not None:
if boards[3].active:
centerline_TDD.append(pm)
else:
centerline.append(pm)
if show_passes:
painter.setPen(pen_canvas)
self.draw_passes(painter, 'A', boards[0].bottom_cuts, y1, y2,
flagsL, xMid, False)
label_bottom = 'A,B'
label_top = None
i = 0
# Do double-double passes
if boards[3].active:
y1 = boards[3].yT() + sepOver2
y2 = boards[3].yT() - frac_depth
painter.setPen(penB)
pm = self.draw_passes(painter, self.labels[i], boards[3].top_cuts, rect_TDD.yMid(),
rect_TDD.yT(), flagsR, xMid)
if pm is not None:
centerline_TDD.append(pm)
if show_passes:
painter.setPen(pen_canvas)
self.draw_passes(painter, self.labels[i], boards[3].top_cuts, y1, y2,
flagsR, xMid, False)
y1 = boards[3].yB() - sepOver2
y2 = boards[3].yB() + frac_depth
painter.setPen(penA)
pm = self.draw_passes(painter, self.labels[i + 1], boards[3].bottom_cuts,
rect_TDD.yMid(), rect_TDD.yB(), flagsL, xMid)
if pm is not None:
centerline_TDD.append(pm)
if show_passes:
painter.setPen(pen_canvas)
self.draw_passes(painter, self.labels[i + 1], boards[3].bottom_cuts, y1, y2,
flagsL, xMid, False)
label_bottom = 'D,E,F'
label_top = 'A,B,C'
i += 2
# Do double passes
if boards[2].active:
y1 = boards[2].yT() + sepOver2
y2 = boards[2].yT() - frac_depth
if boards[3].active:
painter.setPen(penA)
else:
painter.setPen(penB)
pm = self.draw_passes(painter, self.labels[i], boards[2].top_cuts, rect_T.yMid(),
rect_T.yT(), flagsR, xMid)
if pm is not None:
centerline.append(pm)
if show_passes:
painter.setPen(pen_canvas)
self.draw_passes(painter, self.labels[i], boards[2].top_cuts, y1, y2,
flagsR, xMid, False)
y1 = boards[2].yB() - sepOver2
y2 = boards[2].yB() + frac_depth
painter.setPen(penA)
pm = self.draw_passes(painter, self.labels[i + 1], boards[2].bottom_cuts, rect_T.yMid(),
rect_T.yB(), flagsL, xMid)
if pm is not None:
centerline.append(pm)
if show_passes:
painter.setPen(pen_canvas)
self.draw_passes(painter, self.labels[i + 1], boards[2].bottom_cuts, y1, y2,
flagsL, xMid, False)
if not boards[3].active:
label_bottom = 'A,B,C,D'
i += 2
# ... do the bottom board passes
y1 = boards[1].yT() + sepOver2
y2 = boards[1].yT() - frac_depth
if boards[2].active or boards[3].active:
painter.setPen(penB)
else:
painter.setPen(penA)
pm = self.draw_passes(painter, self.labels[i], boards[1].top_cuts, rect_T.yMid(),
rect_T.yB(), flagsL, xMid)
if pm is not None:
centerline.append(pm)
if show_passes:
painter.setPen(pen_canvas)
self.draw_passes(painter, self.labels[i], boards[1].top_cuts, y1, y2,
flagsR, xMid, False)
flagsLC = QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter
flagsRC = QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter
# ... draw the caul template and do its passes. Draw events may be
# triggered before we have the ability to recreate the geom object,
# so we have to ensure the caul_top object actually exists.
datetime = time.strftime('\n%d %b %y %H:%M', time.localtime())
if self.config.show_caul and self.geom.caul_top is not None:
rect_caul = self.geom.rect_caul
board_caul = self.geom.board_caul
top = self.geom.caul_top
bottom = self.geom.caul_bottom
self.draw_template_rectangle(painter, rect_caul, board_caul)
centerline_caul = []
painter.setPen(penA)
pm = self.draw_passes(painter, 'A', top, rect_caul.yMid(), rect_caul.yT(), flagsR, xMid)
if pm is not None:
centerline_caul.append(pm)
pm = self.draw_passes(painter, self.labels[i], bottom, rect_caul.yMid(),
rect_caul.yB(), flagsL, xMid)
if pm is not None:
centerline_caul.append(pm)
self.set_font_size(painter, 'template_labels')
label = self.transl.tr('Cauls')
if centerline_caul:
label += self.transl.tr('\nCenter: ') + centerline_caul[0]
else:
pen = QtGui.QPen(QtCore.Qt.DashLine)
pen.setColor(self.colors['center_color'])
pen.setWidthF(0)
painter.setPen(pen)
painter.drawLine(xMid, rect_caul.yB(), xMid, rect_caul.yT())
painter.setPen(self.colors['template_margin_foreground'])
paint_text(painter, label + datetime, (rect_caul.xL(), rect_caul.yMid()),
flagsLC, (5, 0))
paint_text(painter, label, (rect_caul.xR(), rect_caul.yMid()), flagsRC, (-5, 0))
# Label the templates
pen = QtGui.QPen(QtCore.Qt.DashLine)
pen.setColor(self.colors['center_color'])
pen.setWidthF(0)
self.set_font_size(painter, 'template_labels')
if centerline:
label_bottom += self.transl.tr('\nCenter: ') + centerline[0]
else:
painter.setPen(pen)
painter.drawLine(xMid, rect_T.yB(), xMid, rect_T.yT())
painter.setPen(self.colors['template_margin_foreground'])
paint_text(painter, label_bottom + datetime, (rect_T.xL(), rect_T.yMid()), flagsLC, (5, 0))
paint_text(painter, label_bottom, (rect_T.xR(), rect_T.yMid()), flagsRC, (-5, 0))
if label_top is not None:
if centerline_TDD:
label_top += self.transl.tr('\nCenter: ') + centerline_TDD[0]
else:
painter.setPen(pen)
painter.drawLine(xMid, rect_TDD.yB(), xMid, rect_TDD.yT())
painter.setPen(self.colors['template_margin_foreground'])
paint_text(painter, label_top + datetime, (rect_TDD.xL(), rect_TDD.yMid()),
flagsLC, (5, 0))
paint_text(painter, label_top, (rect_TDD.xR(), rect_TDD.yMid()), flagsRC, (-5, 0))
self.draw_alignment(painter)
def draw_one_board(self, painter, board, bit, fill_color):
'''
Draws a single board
'''
if not board.active:
return
# form the polygon to draw
(x, y) = board.perimeter(bit)
n = len(x)
poly = QtGui.QPolygonF()
for i in lrange(n):
poly.append(QtCore.QPointF(x[i], y[i]))
# paint it
painter.save()
pen = QtGui.QPen(self.colors['board_foreground'])
pen.setWidthF(0)
painter.setPen(pen)
icon = self.woods[board.wood]
if icon is not None:
if isinstance(icon, str):
# then it's an image file
brush = QtGui.QBrush(QtGui.QPixmap(icon))
else:
# oterhwise, if must be a pattern fill
if icon == QtCore.Qt.SolidPattern:
color = fill_color
else:
# It's not a solid fill, so the polygon with the background color, first
color = self.colors['board_background']
brush = QtGui.QBrush(color)
painter.setBrush(brush)
painter.drawPolygon(poly)
color = self.colors['board_foreground']
brush = QtGui.QBrush(color, icon)
(inverted, dummy_invertable) = self.transform.inverted()
# setMatrix is not offered anymore
# brush.setMatrix(inverted.toAffine())
brush.setTransform(inverted)
painter.setBrush(brush)
painter.drawPolygon(poly)
painter.restore()
def draw_boards(self, painter):
'''
Draws all the boards
'''
# Draw all of the boards
for i in lrange(4):
self.draw_one_board(painter, self.geom.boards[i], self.geom.bit,
self.colors['board_background'])
# Label the boards
if self.config.show_router_pass_identifiers or self.config.show_router_pass_locations:
self.set_font_size(painter, 'boards')
pen = QtGui.QPen(QtCore.Qt.SolidLine)
pen.setColor(self.colors['canvas_foreground'])
pen.setWidthF(0)
painter.setPen(pen)
x1 = self.geom.boards[0].xL() - self.geom.bit.width // 2
x2 = self.geom.boards[0].xL() - self.geom.bit.width // 4
flags = QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter
y = self.geom.boards[0].yB()
p = (x1, y)
paint_text(painter, 'A', p, flags, (-3, 0))
painter.drawLine(x1, y, x2, y)
i = 0 # index in self.labels
if self.geom.boards[3].active:
y = self.geom.boards[3].yT()
p = (x1, y)
paint_text(painter, 'B', p, flags, (-3, 0))
painter.drawLine(x1, y, x2, y)
y = self.geom.boards[3].yB()
p = (x1, y)
paint_text(painter, 'C', p, flags, (-3, 0))
painter.drawLine(x1, y, x2, y)
i = 2
if self.geom.boards[2].active:
y = self.geom.boards[2].yT()
p = (x1, y)
paint_text(painter, self.labels[i], p, flags, (-3, 0))
painter.drawLine(x1, y, x2, y)
y = self.geom.boards[2].yB()
p = (x1, y)
paint_text(painter, self.labels[i + 1], p, flags, (-3, 0))
painter.drawLine(x1, y, x2, y)
i += 2
y = self.geom.boards[1].yT()
p = (x1, y)
paint_text(painter, self.labels[i], p, flags, (-3, 0))
painter.drawLine(x1, y, x2, y)
def cut_polygon(self, c):
'''
Forms the polygon for the cut corresponding to the cut c
'''
boards = self.geom.boards
xLT = boards[0].xL() + c.xmin
xRT = boards[0].xL() + c.xmax
xLB = xLT
xRB = xRT
if c.xmin > 0:
xLB += self.geom.bit.overhang
if c.xmax < self.geom.boards[0].width:
xRB -= self.geom.bit.overhang
yB = boards[0].yB()
yT = yB + self.geom.bit.depth
poly = QtGui.QPolygonF()
poly.append(QtCore.QPointF(xLT, yT))
poly.append(QtCore.QPointF(xRT, yT))
poly.append(QtCore.QPointF(xRB, yB))
poly.append(QtCore.QPointF(xLB, yB))
poly.append(QtCore.QPointF(xLT, yT))
return poly
def draw_active_cuts(self, painter):
'''
If the spacing supports it, highlight the active cuts and
draw their limits
'''
f = self.geom.spacing.cursor_cut
if f is None:
return
pen = QtGui.QPen()
pen.setWidthF(0)
# get the perimeter of the cursor
cursor_poly = self.cut_polygon(self.geom.boards[0].bottom_cuts[f])
# initialize limits
xminG = self.geom.boards[0].width
xmaxG = 0
# draw the active cuts filled, and track the limits
painter.save()
brush = QtGui.QBrush(QtGui.QColor(255, 0, 0, 75))
painter.setBrush(brush)
pen.setColor(QtCore.Qt.red)
painter.setPen(pen)
fcolor = QtGui.QColor(self.colors['board_background'])
fcolor.setAlphaF(1.0)
for f in self.geom.spacing.active_cuts:
poly = self.cut_polygon(self.geom.boards[0].bottom_cuts[f])
painter.drawPolygon(poly)
# keep track of the limits
(xmin, xmax) = self.geom.spacing.get_limits(f)
xminG = min(xminG, xmin)
xmaxG = max(xmaxG, xmax)
# label the polygon with its index
xText = 0.5 * (poly[0].x() + poly[1].x())
yText = 0.5 * (poly[0].y() + poly[1].y())
text = '%d' % f
flags = QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom
paint_text(painter, text, (xText, yText), flags, (0, -5),
fill_color=fcolor)
painter.restore()
# draw the limits
# using QPointF to avoid border marks overlap with cursor
painter.save()
xminG += self.geom.boards[0].xL()
xmaxG += self.geom.boards[0].xL()
yB = self.geom.boards[0].yB()
yT = self.geom.boards[0].yT()
pen.setColor(QtCore.Qt.green)
painter.setPen(pen)
half = Decimal(0.5)
painter.drawLine(QtCore.QPointF(xminG - half, yB), QtCore.QPointF(xminG - half, yT))
painter.drawLine(QtCore.QPointF(xmaxG + half, yB), QtCore.QPointF(xmaxG + half, yT))
painter.restore()
# draw the perimeter of the cursor cut at the end to get it always visible
painter.save()
pen.setColor(QtCore.Qt.blue)
painter.setPen(pen)
painter.drawPolyline(cursor_poly)
painter.restore()
def draw_title(self, painter):
'''
Draws the title
'''
self.set_font_size(painter, 'title')
painter.setPen(self.colors['canvas_foreground'])
title = router.create_title(self.geom.boards, self.geom.bit, self.geom.spacing)
flags = QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop
p = (self.geom.board_T.xMid(), self.margins.bottom)
paint_text(painter, title, p, flags, (0, 5))
def draw_finger_sizes(self, painter):
'''
Annotates the finger sizes on each board
with 3 digit precision
'''
units = self.geom.bit.units
self.set_font_size(painter, 'template')
painter.setPen(self.colors['board_foreground'])
fcolor = QtGui.QColor(self.colors['board_background'])
fcolor.setAlphaF(1.0)
# Determine the cuts that are adjacent to board-A and board-B
acuts = self.geom.boards[1].top_cuts
bcuts = self.geom.boards[0].bottom_cuts
if self.geom.boards[2].active:
bcuts = self.geom.boards[2].bottom_cuts
if self.geom.boards[3].active:
acuts = self.geom.boards[3].top_cuts
else:
acuts = self.geom.boards[2].top_cuts
# Draw the router passes
# ... do the B cuts
flags = QtCore.Qt.AlignHCenter | QtCore.Qt.AlignTop
shift = (0, 8)
for c in bcuts:
x = self.geom.boards[1].xL() + (c.xmin + c.xmax) // 2
y = self.geom.boards[1].yT()
label = units.increments_to_string(round(c.xmax - c.xmin, 3))
p = (x, y)
paint_text(painter, label, p, flags, shift, fill_color=fcolor)
# ... do the A cuts
flags = QtCore.Qt.AlignHCenter | QtCore.Qt.AlignBottom
shift = (0, -8)
for c in acuts:
x = self.geom.boards[0].xL() + (c.xmin + c.xmax) // 2
y = self.geom.boards[0].yB()
label = units.increments_to_string(round(c.xmax - c.xmin, 3))
p = (x, y)
paint_text(painter, label, p, flags, shift, fill_color=fcolor)
def keyPressEvent(self, event):
'''
Handles key press events. At this level, we handle zooming.
'''
if not self.zoom_mode:
event.ignore()
return
# Keyboard zooming mode works only if the shift key is pressed.
if event.key() == QtCore.Qt.Key_Shift:
self.shift_key = True
elif event.key() == QtCore.Qt.Key_Escape:
self.scaling = 1.0
self.translate = [0.0, 0.0]
self.update()
elif not self.shift_key:
event.ignore()
return
dx = 4.0 / self.scaling
scale_factor = 1.1
if event.key() == QtCore.Qt.Key_Left:
self.translate[0] -= dx
self.update()
elif event.key() == QtCore.Qt.Key_Right:
self.translate[0] += dx
self.update()
elif event.key() == QtCore.Qt.Key_Up:
self.translate[1] += dx
self.update()