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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# PatchBay Canvas engine using QGraphicsView/Scene
# Copyright (C) 2010-2019 Filipe Coelho <falktx@falktx.com>
# Copyright (C) 2019-2024 Mathieu Picot <picotmathieu@gmail.com>
#
# This program 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 2 of
# the License, or any later version.
#
# This program 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.
#
# For a full copy of the GNU General Public License see the doc/GPL.txt file.
# Imports (Globals)
import logging
import time
from typing import Optional
from qtpy.QtCore import (
Signal, Slot, Qt, QPoint, QPointF, QRectF, QTimer) # type:ignore
from qtpy.QtGui import (
QCursor, QPixmap, QPolygonF, QBrush, QPainter, QTransform)
from qtpy.QtWidgets import (
QGraphicsRectItem, QGraphicsScene, QApplication,
QGraphicsItem, QGraphicsView)
from patshared import PortMode
from .init_values import (
AliasingReason,
BoxHidding,
CanvasItemType,
GridStyle,
Joining,
Direction,
canvas,
options,
Zv,
MAX_PLUGIN_ID_ALLOWED)
from .box_widget import BoxWidget
from .box_widget_moth import BoxWidgetMoth
from .connectable_widget import ConnectableWidget
from .line_widget import LineWidget
from .grouped_lines_widget import GroupedLinesWidget
from .grid_widget import GridWidget
from .scene_view import PatchGraphicsView
from .utils import boxes_in_dict
_logger = logging.getLogger(__name__)
class RubberbandRect(QGraphicsRectItem):
'''This class is used by rectangle selection when user
press mouse button and move to select boxes.'''
def __init__(self, scene: QGraphicsScene):
QGraphicsRectItem.__init__(self, QRectF(0, 0, 0, 0))
self.setZValue(Zv.RUBBERBAND.value)
self.hide()
scene.addItem(self)
def type(self) -> CanvasItemType:
return CanvasItemType.RUBBERBAND
class MovingBox:
widget: BoxWidgetMoth
from_pt: QPointF
to_pt: QPointF
final_rect: QRectF
start_time: float
is_joining: bool
is_wrapping: bool
hidding_state: BoxHidding
needs_move: bool
def __init__(self, widget: BoxWidgetMoth):
self.widget = widget
self.from_pt = QPointF(*widget.top_left())
self.to_pt = QPointF(*widget.top_left())
self.final_rect = widget.after_wrap_rect().translated(self.to_pt)
self.start_time = 0.0
self.is_joining = False
self.is_wrapping = False
self.hidding_state = BoxHidding.NONE
self.needs_move = False
def is_usefull(self) -> bool:
if self.needs_move or self.is_wrapping:
return True
return self.hidding_state is not BoxHidding.NONE
class SelectingBoxes:
'''Context for 'with' statment. Used when mutiple boxes are selected.'''
def __init__(self, scene: 'PatchSceneMoth'):
self.scene = scene
def __enter__(self):
self.scene.selecting_boxes = True
def __exit__(self, *args, **kwargs):
GroupedLinesWidget.reset_z_values_with_selection(
self.scene.get_selected_boxes())
self.scene.selecting_boxes = False
class PatchSceneMoth(QGraphicsScene):
" This class is used for the scene. "
" The child class in scene.py has all things to manage"
" repulsives boxes."
scale_changed = Signal(float)
scene_group_moved = Signal(int, int, QPointF)
plugin_selected = Signal(list)
def __init__(self, view: PatchGraphicsView):
QGraphicsScene.__init__(self)
self._scale_area = False
self._mouse_down_init = False
self._mouse_rubberband = False
self._mid_button_down = False
self._pointer_border = QRectF(0.0, 0.0, 1.0, 1.0)
self._scale_min = 0.1
self._scale_max = 4.0
self._rubberband = RubberbandRect(self)
self._rubberband_selection = False
self._rubberband_orig_point = QPointF(0, 0)
self._press_point = QPointF(0, 0)
self._view = view
if not self._view:
_logger.critical("Invalid view")
return
self._cursor_cut = None
self._cursor_zoom_area = None
self.prevent_box_user_move = False
'''During view change, this attr must be set to True
to prevent user to take and move a box.'''
self.move_boxes = dict[BoxWidgetMoth, MovingBox]()
self._MOVE_DURATION = 0.300 # 300ms
self._MOVE_TIMER_INTERVAL = 20 # 20 ms step animation (50 Hz)
self._move_timer_start_at = 0.0
self._move_timer_last_time = 0.0
self._move_box_timer = QTimer()
self._move_box_timer.setInterval(self._MOVE_TIMER_INTERVAL)
self._move_box_timer.timeout.connect(self.move_boxes_animation)
self.resizing_scene = False
self.translating_view = False
self._last_border_translate = QPointF(0.0, 0.0)
self._borders_nav_timer = QTimer()
self._borders_nav_timer.setInterval(50)
self._borders_nav_timer.timeout.connect(self._cursor_view_navigation)
self._last_view_cpos = QPointF()
self._allowed_nav_directions = set[Direction]()
self.flying_connectable = None
self.selecting_boxes = False
'''While selecting multiple boxes, this could take a quite long time
if there are many box selected, because of itemChange
in BoxWidgetMoth. If this attribute is True, we can prevent
actions for each box.
Should be never directly set,
use 'with SelectingBoxes(self):' instead.'''
self._selected_boxes = list[BoxWidgetMoth]()
'''Selected boxes saved here between mouse press event
and context menu callback. By default with Qt, all items are
unselected at right click elsewhere.'''
self._grid_widget: Optional[GridWidget] = None
self.sceneRectChanged.connect(self.update_grid_widget)
# self.selectionChanged.connect(self._slot_selection_changed)
def deplace_boxes_from_repulsers(self, repulser_boxes: list[BoxWidget],
wanted_direction=Direction.NONE,
new_scene_rect=None):
'''Change the place of boxes in order to have no
box overlapping other boxes.'''
# just for easier syntax, this method is overloaded in scene.py
# but executed in this file too.
pass
def clear(self):
# reimplement Qt function and fix missing rubberband after clear
self.move_boxes.clear()
QGraphicsScene.clear(self)
self._rubberband = RubberbandRect(self)
self._grid_widget = None
self.update_theme()
def clear_selection(self):
with SelectingBoxes(self):
self.clearSelection()
def set_anti_aliasing(self, yesno: bool):
if self._view is not None:
self._view.setRenderHint(QPainter.RenderHint.Antialiasing, yesno)
def screen_position(self, point: QPointF) -> QPoint:
return self._view.mapToGlobal(self._view.mapFromScene(point))
def get_device_pixel_ratio_f(self):
return self._view.devicePixelRatioF()
def get_scale_factor(self):
return self._view.transform().m11()
def fix_scale_factor(self, transform: Optional[QTransform]=None):
fix, set_view, view = False, False, None
if not transform:
set_view = True
view = self._view
transform = view.transform()
scale = transform.m11()
if scale > self._scale_max:
fix = True
transform.reset()
transform.scale(self._scale_max, self._scale_max)
elif scale < self._scale_min:
fix = True
transform.reset()
transform.scale(self._scale_min, self._scale_min)
if set_view:
if fix and view is not None:
view.setTransform(transform)
self.scale_changed.emit(transform.m11())
return fix
def _cursor_view_navigation(self):
''' This function is called every 50 ms when mouse
left button is pressed. It moves the view if the mouse cursor
is near a border of the view (in the limits of the scene size). '''
# max speed of the drag when mouse is at a side pixel of the view
SPEED = 0.8
# Acceleration, doesn't affect max speed
POWER = 14
view_width = self._view.width()
view_height = self._view.height()
if self._view.verticalScrollBar().isVisible():
view_width -= self._view.verticalScrollBar().width()
if self._view.horizontalScrollBar().isVisible():
view_height -= self._view.horizontalScrollBar().height()
view_cpos = self._view.mapFromGlobal(QCursor.pos())
scene_cpos = self._view.mapToScene(view_cpos)
# The scene relative area we want to be visible in the view
ensure_rect = QRectF(scene_cpos.x() - 1.0,
scene_cpos.y() - 1.0,
2.0, 2.0)
# the scene relative area currently visible in the view
vs_rect = QRectF(
QPointF(self._view.mapToScene(0, 0)),
QPointF(self._view.mapToScene(view_width - 1, view_height -1)))
# The speed of the move depends on the scene size
# to allow fast moves from one scene corner to another one.
speed_hor = (SPEED * (self.sceneRect().width() - vs_rect.width())
/ vs_rect.width())
speed_ver = (SPEED * (self.sceneRect().height() - vs_rect.height())
/ vs_rect.height())
interval_hor = vs_rect.width() / 2
interval_ver = vs_rect.height() / 2
# Navigation is allowed in a direction only if mouse cursor has
# already been moved in this direction, in order to prevent unintended
# moves when user just pressed the mouse button.
if self._last_view_cpos.isNull():
self._allowed_nav_directions.clear()
elif len(self._allowed_nav_directions) < 4:
if view_cpos.x() < self._last_view_cpos.x():
self._allowed_nav_directions.add(Direction.LEFT)
elif view_cpos.x() > self._last_view_cpos.x():
self._allowed_nav_directions.add(Direction.RIGHT)
if view_cpos.y() < self._last_view_cpos.y():
self._allowed_nav_directions.add(Direction.UP)
elif view_cpos.y() > self._last_view_cpos.y():
self._allowed_nav_directions.add(Direction.DOWN)
self._last_view_cpos = view_cpos
# Define the limits we want to see in the view
# Note that the zone where there is no move is defined by the fact
# the move in a direction is converted from float to int.
# This way, a move lower than 1.0 pixel will be ignored.
# By chance, the lower possible speed is good
# 1.0 pixel * (1s / 0.050s) = 20 pixels/second.
apply = False
if (Direction.LEFT in self._allowed_nav_directions
and scene_cpos.x() < vs_rect.center().x()):
offset = vs_rect.center().x() - max(scene_cpos.x(), vs_rect.left())
ratio_x = offset / interval_hor
move_x = - speed_hor * ((ratio_x ** POWER) * interval_hor)
left = vs_rect.left() + int(move_x)
ensure_rect.moveLeft(max(left, self.sceneRect().left()))
if int(move_x):
apply = True
elif (Direction.RIGHT in self._allowed_nav_directions
and scene_cpos.x() > vs_rect.center().x()):
offset = min(scene_cpos.x(), vs_rect.right()) - vs_rect.center().x()
ratio_x = offset / interval_hor
move_x = speed_hor * ((ratio_x ** POWER) * interval_hor)
right = vs_rect.right() + int(move_x)
ensure_rect.moveRight(min(right, self.sceneRect().right()))
if int(move_x):
apply = True
if (Direction.UP in self._allowed_nav_directions
and scene_cpos.y() < vs_rect.center().y()):
offset = vs_rect.center().y() - max(scene_cpos.y(), vs_rect.top())
ratio_y = offset / interval_ver
move_y = - speed_ver * ((ratio_y ** POWER) * interval_ver)
top = vs_rect.top() + int(move_y)
ensure_rect.moveTop(max(top, self.sceneRect().top()))
if int(move_y):
apply = True
elif (Direction.DOWN in self._allowed_nav_directions
and scene_cpos.y() > vs_rect.center().y()):
offset = min(scene_cpos.y(), vs_rect.bottom()) - vs_rect.center().y()
ratio_y = offset / interval_ver
move_y = speed_ver * ((ratio_y ** POWER) * interval_ver)
bottom = vs_rect.bottom() + int(move_y)
ensure_rect.moveBottom(min(bottom, self.sceneRect().bottom()))
if int(move_y):
apply = True
if apply:
canvas.qobject.start_aliasing_check(
AliasingReason.NAV_ON_BORDERS)
self._view.ensureVisible(ensure_rect, 0, 0)
def _start_navigation_on_borders(self):
if (options.borders_navigation
and not self._borders_nav_timer.isActive()):
self._last_view_cpos = QPointF()
self._borders_nav_timer.start()
def fix_temporary_scroll_bars(self):
if self._view is None:
return
if self._view.horizontalScrollBar().isVisible():
self._view.setHorizontalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
else:
self._view.setHorizontalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
if self._view.verticalScrollBar().isVisible():
self._view.setVerticalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
else:
self._view.setVerticalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
def reset_scroll_bars(self):
self._view.setHorizontalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self._view.setVerticalScrollBarPolicy(
Qt.ScrollBarPolicy.ScrollBarAsNeeded)
def list_boxes_at(self, rect: QRectF):
return [item for item in self.items(rect)
if isinstance(item, BoxWidget)]
def _start_move_timer(self):
if self._move_box_timer.isActive():
return
self._move_timer_start_at = time.time()
self._move_timer_last_time = self._move_timer_start_at
self._move_box_timer.start()
def move_boxes_animation(self):
# Animation is nice but not the priority.
# Do not ensure all steps are played
# but just move the box where it has to go now.
move_time = time.time()
time_since_start = move_time - self._move_timer_start_at
ratio = min(1.0, time_since_start / self._MOVE_DURATION)
if self._move_timer_last_time == self._move_timer_start_at:
# this is the first animation step
if time_since_start > 0.33 * self._MOVE_DURATION:
# this seems to be a big patch,
# animation won't be pretty anyway,
# let's finish it now.
ratio = 1.0
else:
# this is not the first animation step.
# If the timer called this method two times too late,
# i.e. >40ms instead of 20ms after the previous step,
# anti-aliasing is de-activated for a smoother animation.
if (move_time - self._move_timer_last_time
> 0.002 * self._MOVE_TIMER_INTERVAL):
canvas.set_aliasing_reason(AliasingReason.ANIMATION, True)
self._move_timer_last_time = move_time
lws = set[GroupedLinesWidget]()
usefull = False
for box, moving_box in self.move_boxes.items():
if not usefull:
usefull = moving_box.is_usefull()
if moving_box.needs_move:
x = (moving_box.from_pt.x()
+ ((moving_box.to_pt.x() - moving_box.from_pt.x())
* (ratio ** 0.6)))
y = (moving_box.from_pt.y()
+ ((moving_box.to_pt.y() - moving_box.from_pt.y())
* (ratio ** 0.6)))
box.set_top_left((x, y))
box.repaint_lines(fast_move=True)
if moving_box.is_wrapping:
box.animate_wrapping(ratio)
if moving_box.hidding_state in (BoxHidding.HIDDING,
BoxHidding.RESTORING):
if moving_box.hidding_state is BoxHidding.HIDDING:
box.animate_hidding(ratio)
else:
box.animate_restoring(ratio)
for lw in GroupedLinesWidget.widgets_for_box(
box._group_id, box._port_mode):
if lw not in lws:
lw.animate_hidding(ratio)
lws.add(lw)
if not usefull:
# stop animation now if all moving boxes have no change to make
ratio = 1.0
self.resize_the_scene(ratio)
if ratio >= 1.0:
# Animation is finished
self._move_box_timer.stop()
canvas.set_aliasing_reason(AliasingReason.ANIMATION, False)
self.prevent_box_user_move = False
GroupedLinesWidget.animation_finished()
# box update positions is forbidden while widget is in self.move_boxes,
# so we copy the list before to clear it,
# then we can ask update_positions on widgets
boxes = [b for b, mb in self.move_boxes.items()
if not (mb.is_joining or mb.hidding_state is BoxHidding.HIDDING)]
self.move_boxes.clear()
for box in boxes:
if box.update_positions_pending:
box.update_positions()
canvas.qobject.move_boxes_finished.emit()
def add_box_to_animation(
self, box_widget: BoxWidgetMoth, to_x: int, to_y: int,
joining=Joining.NO_CHANGE, joined_rect=QRectF()):
'''add a box to the move animation, to_x and to_y refer
to the top left of the box at the end of animation.
if joining is set to Joining.YES, joined_rect must be set'''
moving_box = self.move_boxes.get(box_widget)
if moving_box is None:
# box is not already moving, create a MovingBox instance
moving_box = MovingBox(box_widget)
if joining is Joining.YES:
moving_box.is_joining = True
self.move_boxes[box_widget] = moving_box
else:
# box is already moving, check joining state and change it
# if needed.
if moving_box.is_joining and joining is Joining.NO:
moving_box.is_joining = False
canvas.qobject.rm_group_to_join(
box_widget.get_group_id())
moving_box.from_pt = QPointF(*box_widget.top_left())
moving_box.to_pt = QPointF(to_x, to_y)
moving_box.needs_move = bool(moving_box.from_pt != moving_box.to_pt)
if joining is Joining.YES or not box_widget.isVisible():
moving_box.final_rect = joined_rect
elif joining is Joining.NO_CHANGE and moving_box.is_joining:
final_rect = QRectF(
0.0, 0.0,
moving_box.final_rect.width(),
moving_box.final_rect.height())
final_rect.translate(moving_box.to_pt)
moving_box.final_rect = final_rect
elif not moving_box.is_joining:
aft_wrap_rect = box_widget.after_wrap_rect()
final_rect = QRectF(
0.0, 0.0, aft_wrap_rect.width(), aft_wrap_rect.height())
final_rect.translate(moving_box.to_pt)
moving_box.final_rect = final_rect
else:
# can not happens
# would means moving_box.is_joining and joining is JOINING.NO,
# It is prevented
moving_box.final_rect = joined_rect
if joining is not Joining.NO_CHANGE:
moving_box.is_joining = True if joining is Joining.YES else False
# save the group position
group = canvas.get_group(box_widget._group_id)
if group is not None:
if moving_box.is_joining:
if not joined_rect.isNull():
group.gpos.boxes[PortMode.BOTH].pos = (to_x, to_y)
else:
group.gpos.boxes[box_widget._port_mode].pos = (to_x, to_y)
canvas.cb.group_pos_modified(group.group_id)
moving_box.start_time = time.time() - self._move_timer_start_at
if not self._move_box_timer.isActive():
moving_box.start_time = 0.0
self._start_move_timer()
if canvas.aliasing_reason:
# if antialiasing is already prevented
# we need to keep it prevented at animation start
canvas.set_aliasing_reason(AliasingReason.ANIMATION, True)
def remove_box_from_animation(self, box_widget: BoxWidgetMoth):
if self.prevent_box_user_move:
# should not happens.
# For now we can remove a box from animation
# only by moving box manually,
# and this is prevented by this attr in box_widget_moth.
return
if box_widget in self.move_boxes:
self.move_boxes.pop(box_widget)
def add_box_to_animation_wrapping(self, box_widget: BoxWidgetMoth, wrap: bool):
moving_box = self.move_boxes.get(box_widget)
if moving_box is None:
moving_box = MovingBox(box_widget)
self.move_boxes[box_widget] = moving_box
moving_box.start_time = time.time() - self._move_timer_start_at
aft_wrap_rect = box_widget.after_wrap_rect()
final_rect = QRectF(0.0, 0.0, aft_wrap_rect.width(), aft_wrap_rect.height())
moving_box.final_rect = \
final_rect.translated(moving_box.to_pt)
moving_box.is_wrapping = True
self._start_move_timer()
def add_box_to_animation_hidding(self, box_widget: BoxWidget):
moving_box = self.move_boxes.get(box_widget)
if moving_box is None:
moving_box = MovingBox(box_widget)
self.move_boxes[box_widget] = moving_box
moving_box.start_time = time.time() - self._move_timer_start_at
moving_box.final_rect = QRectF()
moving_box.hidding_state = BoxHidding.HIDDING
for port_mode in PortMode.OUTPUT, PortMode.INPUT:
if port_mode not in box_widget.get_port_mode():
continue
for lw in GroupedLinesWidget.widgets_for_box(
box_widget.get_group_id(), port_mode):
lw.set_mode_hidding(port_mode, BoxHidding.HIDDING)
self._start_move_timer()
def add_box_to_animation_restore(self, box_widget: BoxWidget):
moving_box = self.move_boxes.get(box_widget)
if moving_box is None:
moving_box = MovingBox(box_widget)
self.move_boxes[box_widget] = moving_box
moving_box.start_time = time.time() - self._move_timer_start_at
moving_box.from_pt = moving_box.to_pt
aft_wrap_rect = box_widget.after_wrap_rect()
final_rect = QRectF(
0.0, 0.0, aft_wrap_rect.width(), aft_wrap_rect.height())
moving_box.final_rect = \
final_rect.translated(moving_box.to_pt)
if moving_box.hidding_state is BoxHidding.NONE:
box_widget.animate_restoring(0.0)
moving_box.hidding_state = BoxHidding.RESTORING
for port_mode in PortMode.OUTPUT, PortMode.INPUT:
if port_mode not in box_widget._port_mode:
continue
for lw in GroupedLinesWidget.widgets_for_box(
box_widget._group_id, port_mode):
lw.set_mode_hidding(port_mode, BoxHidding.RESTORING)
self._start_move_timer()
def remove_box(self, box_widget: BoxWidget):
if box_widget in self.move_boxes:
self.move_boxes.pop(box_widget)
self.removeItem(box_widget)
def center_view_on(self, widget):
self._view.centerOn(widget)
def get_connectable_item_at(
self, pos: QPointF, origin: ConnectableWidget) \
-> Optional[ConnectableWidget]:
for item in self.items(pos, Qt.ItemSelectionMode.ContainsItemShape,
Qt.SortOrder.AscendingOrder):
if isinstance(item, ConnectableWidget) and item is not origin:
return item
def get_box_at(self, pos: QPointF) -> Optional[BoxWidget]:
for item in self.items(pos, Qt.ItemSelectionMode.ContainsItemShape,
Qt.SortOrder.AscendingOrder):
if isinstance(item, BoxWidget):
return item
def get_selected_boxes(self) -> list[BoxWidget]:
return [i for i in self.selectedItems() if isinstance(i, BoxWidget)]
def removeItem(self, item: QGraphicsItem):
for child_item in item.childItems():
QGraphicsScene.removeItem(self, child_item)
QGraphicsScene.removeItem(self, item)
def update_limits(self):
w0 = canvas.size_rect.width()
h0 = canvas.size_rect.height()
w1 = self._view.width()
h1 = self._view.height()
self._scale_min = w1/w0 if w0/h0 > w1/h1 else h1/h0
def update_theme(self):
if canvas.theme.scene_background_image is not None:
bg_brush = QBrush()
bg_brush.setTextureImage(canvas.theme.scene_background_image)
self.setBackgroundBrush(bg_brush)
else:
self.setBackgroundBrush(canvas.theme.scene_background_color)
self._rubberband.setPen(canvas.theme.rubberband.fill_pen)
self._rubberband.setBrush(canvas.theme.rubberband.background_color)
cur_color = ("black" if canvas.theme.scene_background_color.blackF() < 0.5
else "white")
self._cursor_cut = QCursor(QPixmap(f":/cursors/cut-{cur_color}.png"), 1, 1)
self._cursor_zoom_area = QCursor(
QPixmap(f":/cursors/zoom-area-{cur_color}.png"), 8, 7)
self.update_grid_style()
def drawBackground(self, painter, rect):
painter.save()
painter.setPen(Qt.PenStyle.NoPen)
if (canvas.theme.scene_background_image is not None
and not canvas.theme.scene_background_image.isNull()):
canvas.theme.scene_background_image.setDevicePixelRatio(3.0)
bg_brush = QBrush()
bg_brush.setTextureImage(canvas.theme.scene_background_image)
painter.setBrush(bg_brush)
painter.drawRect(rect)
painter.setBrush(canvas.theme.scene_background_color)
painter.drawRect(rect)
painter.restore()
def get_new_scene_rect(self, anim_ratio: Optional[float]=None) -> QRectF:
full_rect = QRectF()
if anim_ratio is None or anim_ratio >= 1.0:
for widget in canvas.list_boxes():
full_rect |= widget.rect_needed_in_scene()
return full_rect
futur_rect = QRectF()
for widget in canvas.list_boxes():
move_box = self.move_boxes.get(widget)
if move_box is None:
full_rect |= widget.rect_needed_in_scene()
else:
if move_box.hidding_state is BoxHidding.RESTORING:
continue
full_rect |= widget.rect_needed_in_scene()
for widget in canvas.list_boxes():
futur_rect |= widget.rect_needed_in_scene(futur=True)
if futur_rect is None or full_rect is None:
return full_rect
rect = QRectF(full_rect)
rect.setLeft(full_rect.left() * (1.0 - anim_ratio)
+ futur_rect.left() * anim_ratio)
rect.setRight(full_rect.right() * (1.0 - anim_ratio)
+ futur_rect.right() * anim_ratio)
rect.setTop(full_rect.top() * (1.0 - anim_ratio)
+ futur_rect.top() * anim_ratio)
rect.setBottom(full_rect.bottom() * (1.0 - anim_ratio)
+ futur_rect.bottom() * anim_ratio)
return rect
def resize_the_scene(self, anim_ratio: Optional[float]=None):
if not options.elastic:
return
scene_rect = self.get_new_scene_rect(anim_ratio)
if not scene_rect.isNull():
self.resizing_scene = True
self.setSceneRect(scene_rect)
self.resizing_scene = False
def set_elastic(self, yesno: bool):
options.elastic = True
self.resize_the_scene()
options.elastic = yesno
if not yesno:
# resize the scene to a null QRectF to auto set sceneRect
# always growing with items
self.setSceneRect(QRectF())
# add a fake item with the current canvas scene size
# (calculated with items), and remove it.
fake_item = QGraphicsRectItem(self.get_new_scene_rect())
self.addItem(fake_item)
self.update()
self.removeItem(fake_item)
def set_cursor(self, cursor: QCursor):
if self._view is None:
return
self._view.viewport().setCursor(cursor)
def unset_cursor(self):
if self._view is None:
return
self._view.viewport().unsetCursor()
def zoom_ratio(self, percent: float, force=False):
ratio = percent / 100.0
transform = self._view.transform()
if not force and ratio == transform.m11():
return
transform.reset()
transform.scale(ratio, ratio)
self._view.setTransform(transform)
for box in canvas.list_boxes():
if box.top_icon:
box.top_icon.update_zoom(ratio)
self.scale_changed.emit(transform.m11())
def zoom_fit(self):
if self._view is None:
return
full_rect = QRectF()
for item in self.items():
if isinstance(item, BoxWidget) and item.isVisible():
rect = item.sceneBoundingRect()
if full_rect.isNull():
full_rect = rect
continue
full_rect.setLeft(min(full_rect.left(), rect.left()))
full_rect.setRight(max(full_rect.right(), rect.right()))
full_rect.setTop(min(full_rect.top(), rect.top()))
full_rect.setBottom(max(full_rect.bottom(), rect.bottom()))
if full_rect.isNull():
return
self._view.fitInView(full_rect, Qt.AspectRatioMode.KeepAspectRatio)
self.fix_scale_factor()
self.scale_changed.emit(self._view.transform().m11())
def zoom_in(self):
view = self._view
transform = view.transform()
if transform.m11() < self._scale_max:
transform.scale(1.2, 1.2)
if transform.m11() > self._scale_max:
transform.reset()
transform.scale(self._scale_max, self._scale_max)
view.setTransform(transform)
self.scale_changed.emit(transform.m11())
def zoom_out(self):
view = self._view
transform = view.transform()
if transform.m11() > self._scale_min:
transform.scale(0.833333333333333, 0.833333333333333)
if transform.m11() < self._scale_min:
transform.reset()
transform.scale(self._scale_min, self._scale_min)
view.setTransform(transform)
self.scale_changed.emit(transform.m11())
def zoom_reset(self):
transform = self._view.transform()
transform.reset()
default_scale = options.default_zoom / 100
transform.scale(default_scale, default_scale)
self._view.setTransform(transform)
self.scale_changed.emit(default_scale)
@Slot()
def _slot_selection_changed(self):
items_list = self.selectedItems()
if len(items_list) == 0:
self.plugin_selected.emit([])
return
plugin_list = []
for item in items_list:
if item and item.isVisible():
group_item = None
if isinstance(item, BoxWidget):
group_item = item
elif isinstance(item, ConnectableWidget):
group_item = item.parentItem()
if group_item is not None and group_item._plugin_id >= 0:
plugin_id = group_item._plugin_id
if plugin_id > MAX_PLUGIN_ID_ALLOWED:
plugin_id = 0
plugin_list.append(plugin_id)
self.plugin_selected.emit(plugin_list)
def _trigger_rubberband_scale(self):
# TODO, should enable an auto-zoom on
# Ctrl+Right clic + drag (rubberband)
self._scale_area = True
if self._cursor_zoom_area:
self.set_cursor(self._cursor_zoom_area)
def get_zoom_scale(self):
return self._view.transform().m11()
def keyPressEvent(self, event):
if not self._view:
event.ignore()
return
if event.key() == Qt.Key.Key_Control:
if self._mid_button_down:
self._start_connection_cut()
elif event.key() == Qt.Key.Key_Home:
event.accept()
self.zoom_fit()
return
elif (QApplication.keyboardModifiers()
& Qt.KeyboardModifier.ControlModifier):
if event.key() == Qt.Key.Key_Plus:
event.accept()
self.zoom_in()
return
if event.key() == Qt.Key.Key_Minus:
event.accept()
self.zoom_out()
return
if event.key() == Qt.Key.Key_1:
event.accept()
self.zoom_reset()
return
QGraphicsScene.keyPressEvent(self, event)
def keyReleaseEvent(self, event):
if event.key() == Qt.Key.Key_Control:
# Connection cut mode off
if self._mid_button_down:
self.unset_cursor()
QGraphicsScene.keyReleaseEvent(self, event)
def _start_connection_cut(self):
if self._cursor_cut:
self.set_cursor(self._cursor_cut)
def zoom_wheel(self, delta: int):
transform = self._view.transform()
scale = transform.m11()
if ((delta > 0 and scale < self._scale_max)
or (delta < 0 and scale > self._scale_min)):
# prevent too large unzoom
if delta < 0:
rect = self.sceneRect()
top_left_vw = self._view.mapFromScene(rect.topLeft())
if (top_left_vw.x() > self._view.width() / 4
and top_left_vw.y() > self._view.height() / 4):
return
# Apply scale
factor = 1.4142135623730951 ** (delta / 240.0)
transform.scale(factor, factor)
self.fix_scale_factor(transform)
self._view.setTransform(transform)
self.scale_changed.emit(transform.m11())
# Update box icons especially when they are not scalable
# eg. coming from system theme
for box in canvas.list_boxes():
if box.top_icon:
box.top_icon.update_zoom(scale * factor)
def update_grid_widget(self):
if self._view.transforming:
return
if self._grid_widget is not None:
self._grid_widget.update_path()
def update_grid_style(self):
if self._grid_widget is not None:
self.removeItem(self._grid_widget)
if options.grid_style is GridStyle.NONE:
self._grid_widget = None
else:
self._grid_widget = GridWidget(self, style=options.grid_style)
self._grid_widget.update_path()
self._grid_widget.setZValue(Zv.GRID.value)
self.addItem(self._grid_widget)
self.update()
def invert_boxes_selection(self):
selected_boxes = set(self.get_selected_boxes())
with SelectingBoxes(self):
for box in canvas.list_boxes():
if box in selected_boxes:
box.setSelected(False)
elif box.isVisible():
box.setSelected(True)
def mouseDoubleClickEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton and not canvas.menu_shown:
# parse items under mouse to prevent CallbackAct.DOUBLE_CLICK
# if mouse is on a box
has_box = False
items = self.items(
event.scenePos(), Qt.ItemSelectionMode.ContainsItemShape,
Qt.SortOrder.AscendingOrder)
for item in items:
if isinstance(item, ConnectableWidget):
# start a flying connection with mouse button not pressed,
# here we just change the cursor
# and define the ConnectableWidget.
self.flying_connectable = item
self.set_cursor(QCursor(Qt.CursorShape.CrossCursor))
self._start_navigation_on_borders()
return
if not has_box:
has_box = isinstance(item, BoxWidget)
if not has_box:
canvas.cb.bg_double_click()
QGraphicsScene.mouseDoubleClickEvent(self, event)
def mousePressEvent(self, event):
if self.flying_connectable:
if event.button() == Qt.MouseButton.LeftButton:
self.flying_connectable.mouseReleaseEvent(event)
self.flying_connectable = None
self.set_cursor(Qt.CursorShape.ArrowCursor) # type:ignore
return
if event.button() == Qt.MouseButton.RightButton:
self.flying_connectable.mousePressEvent(event)
return
ctrl_pressed = bool(
QApplication.keyboardModifiers()
& Qt.KeyboardModifier.ControlModifier)
alt_or_meta_pressed = bool(
QApplication.keyboardModifiers()
& (Qt.KeyboardModifier.AltModifier
| Qt.KeyboardModifier.MetaModifier))
self._mouse_down_init = bool(
(event.button() == Qt.MouseButton.LeftButton
and not alt_or_meta_pressed)
or (event.button() == Qt.MouseButton.RightButton and ctrl_pressed))
self._press_point = event.scenePos()
self._mouse_rubberband = False
# There is no more possibility to use trigger disconnect
# with Ctrl + Middle click
# because connection objects are now grouped.
# we keep the code here,
# it may be possible to re-implement it,
# if really wanted by some users.
# if event.button() == Qt.MidButton:
# if ctrl_pressed:
# self._mid_button_down = True
# self._start_connection_cut()
# pos = event.scenePos()
# self._pointer_border.moveTo(floor(pos.x()), floor(pos.y()))
# for item in self.items(self._pointer_border):
# if isinstance(item, (ConnectableWidget, LineWidget)):
# item.trigger_disconnect()
self._selected_boxes = [
b for b in self.selectedItems()
if isinstance(b, BoxWidgetMoth) and b.isVisible()]
QGraphicsScene.mousePressEvent(self, event)
canvas.menu_shown = False
if (self._view.dragMode() != QGraphicsView.DragMode.ScrollHandDrag
and event.buttons() == Qt.MouseButton.LeftButton):
# Middle click and move changes the view drag mode
# and fake a left button.
# we don't want navigation on borders in case of middle click.
self._start_navigation_on_borders()
def mouseMoveEvent(self, event):
if self.flying_connectable is not None:
self.flying_connectable.mouseMoveEvent(event)
return
if self._mouse_down_init:
self._mouse_down_init = False
self._mouse_rubberband = False
for item in self.items(self._press_point):
if isinstance(item, (BoxWidget, ConnectableWidget)):
break
else:
if event.buttons() != 0:
self._mouse_rubberband = True
if self._mouse_rubberband:
event.accept()
pos = event.scenePos()
pos_x = pos.x()
pos_y = pos.y()
if not self._rubberband_selection:
self._rubberband.show()
self._rubberband_selection = True
self._rubberband_orig_point = self._press_point
rubb_orig_point = self._rubberband_orig_point
x = min(pos_x, rubb_orig_point.x())
y = min(pos_y, rubb_orig_point.y())
line_hinting = canvas.theme.rubberband.fill_pen.widthF() / 2.0
self._rubberband.setRect(
x + line_hinting, y + line_hinting,
abs(pos_x - rubb_orig_point.x()),
abs(pos_y - rubb_orig_point.y()))
if (self._mid_button_down
and (QApplication.keyboardModifiers()
& Qt.KeyboardModifier.ControlModifier)):
for item in self.items(
QPolygonF([event.scenePos(), event.lastScenePos(),
event.scenePos()])):
if isinstance(item, LineWidget):
item.trigger_disconnect()
QGraphicsScene.mouseMoveEvent(self, event)
def mouseReleaseEvent(self, event):
if self.flying_connectable:
QGraphicsScene.mouseReleaseEvent(self, event)
canvas.set_aliasing_reason(AliasingReason.NAV_ON_BORDERS, False)
return
if self._scale_area and not self._rubberband_selection:
self._scale_area = False
self.unset_cursor()
if self._rubberband_selection:
if self._scale_area:
self._scale_area = False
self.unset_cursor()
rect = self._rubberband.rect()
self._view.fitInView(
rect.x(), rect.y(),
rect.width(), rect.height(),
Qt.AspectRatioMode.KeepAspectRatio)
self.fix_scale_factor()
else:
with SelectingBoxes(self):
for item in self.items():
if isinstance(item, BoxWidget):
item_rect = item.sceneBoundingRect()
if self._rubberband.rect().contains(item_rect):
item.setSelected(True)
self._rubberband.hide()
self._rubberband.setRect(0, 0, 0, 0)
self._rubberband_selection = False
else:
for item in self.get_selected_boxes():
item.check_item_pos()
self.scene_group_moved.emit(
item._group_id, item._port_mode,
item.scenePos())
if len(self.selectedItems()) > 1:
self.update()
self._mouse_down_init = False
self._mouse_rubberband = False
if event.button() == Qt.MouseButton.LeftButton:
self._borders_nav_timer.stop()
canvas.set_aliasing_reason(AliasingReason.NAV_ON_BORDERS, False)
if event.button() == Qt.MouseButton.MiddleButton:
event.accept()
self._mid_button_down = False
# Connection cut mode off
if (QApplication.keyboardModifiers()
& Qt.KeyboardModifier.ControlModifier):
self.unset_cursor()
return
QGraphicsScene.mouseReleaseEvent(self, event)
def wheelEvent(self, event):
if not self._view:
event.ignore()
return
canvas.qobject.start_aliasing_check(AliasingReason.VIEW_MOVE)
if (QApplication.keyboardModifiers()
& Qt.KeyboardModifier.ControlModifier):
self.zoom_wheel(event.delta())
event.accept()
return
QGraphicsScene.wheelEvent(self, event)
def contextMenuEvent(self, event):
if canvas.is_line_mov:
event.ignore()
return
for item in self.items(event.scenePos()):
if isinstance(item, (BoxWidget, ConnectableWidget)):
break
else:
if (QApplication.keyboardModifiers()
& Qt.KeyboardModifier.ControlModifier):
event.accept()
self._trigger_rubberband_scale()
return
with SelectingBoxes(self):
for sel_box in self._selected_boxes:
sel_box.setSelected(True)
event.accept()
screen_pos = event.screenPos()
scene_pos = event.scenePos()
canvas.cb.bg_right_click(
screen_pos.x(), screen_pos.y(),
scene_pos.x(), scene_pos.y(),
boxes_in_dict(self._selected_boxes))
return
QGraphicsScene.contextMenuEvent(self, event)
|