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
|
#!/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-2023 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.
from typing import TYPE_CHECKING, Iterator, Optional, Callable
from enum import Enum, Flag, IntEnum, auto
import logging
from qtpy.QtCore import QPointF, QRectF, QSettings, QPoint
from qtpy.QtWidgets import QGraphicsItem
from patchbay.patchcanvas.proto_callbacker import ProtoCallbacker
from patshared import (
PortMode, PortType, PortSubType, BoxType, BoxPos, GroupPos)
if TYPE_CHECKING:
# all these classes are not importable normally because
# it would make a circular import
# they are imported only from IDE to get types
from .theme import Theme
from .theme_manager import ThemeManager
from .scene import PatchScene
from .box_widget import BoxWidget
from .port_widget import PortWidget
from .portgroup_widget import PortgroupWidget
from .patchcanvas import CanvasObject
from .hidden_conn_widget import HiddenConnWidget
_logger = logging.getLogger(__name__)
# Maximum Id for a plugin, treated as invalid/zero if above this value
MAX_PLUGIN_ID_ALLOWED = 0x7FF
class CanvasNeverInit(Exception):
def __init__(
self, message='patchcanvas.init() was probably never called!'):
super().__init__(message)
class CanvasSceneMissing(Exception):
def __init__(self, message='canvas.scene is needed here'):
super().__init__(message)
# inline display is not usable in RaySession or Patchance
# but this patchcanvas module has been forked from Carla
# and all about inline_display has been kept (we never know)
# but never tested.
class InlineDisplay(IntEnum):
DISABLED = 0
ENABLED = 1
CACHED = 2
class BoxHidding(Enum):
'Enum used for animations where there are hidding or restoring boxes'
NONE = auto()
HIDDING = auto()
RESTORING = auto()
# For Repulsive boxes
class Direction(Enum):
NONE = 0
LEFT = 1
RIGHT = 2
UP = 3
DOWN = 4
def __lt__(self, other: 'Direction'):
return self.value < other.value
class ConnectionThemeState(Enum):
NORMAL = 0
SELECTED = 1
DISCONNECTING = 2
class GridStyle(Enum):
NONE = 0
TECHNICAL_GRID = 1
GRID = 2
CHESSBOARD = 3
@classmethod
def _missing_(cls, value) -> 'GridStyle':
_logger.warning(
f'Attempt to access to an invalid GridWidgetStyle: {value}')
return GridStyle.NONE
class Zv(Enum):
'''enum for zValue for objects directly in the scene'''
GRID = auto()
RUBBERBAND = auto()
OPAC_LINE = auto() # line semi-hidden by filter
OPAC_BOX = auto() # box semi-hidden by filter
HIDDING_BOX = auto()
HIDDEN_CONN = auto()
LINE = auto()
BOX = auto()
NEW_BOX = auto()
SEL_BOX_LINE = auto() # line with a selected box
SEL_LINE = auto()
SEL_BOX = auto()
MOV_LINE = auto()
class ZvBox(Enum):
'''enum for zValue for objects directly in a box'''
PORTGRP = auto()
PORT = auto()
SEL_PORT = auto()
HIDDER = auto()
class AliasingReason(Flag):
NONE = 0x00
USER_MOVE = auto()
ANIMATION = auto()
VIEW_MOVE = auto()
SCROLL_BAR_MOVE = auto()
NAV_ON_BORDERS = auto()
class Joining(Enum):
NO_CHANGE = 0
NO = 1
YES = 2
class CanvasItemType(IntEnum):
'''this enum is still here if really needed
but never really used.
Prefer use isinstance(item, type) if possible
because IDE will know easier with which class
we are dealing.'''
BOX = QGraphicsItem.UserType + 1
ICON = QGraphicsItem.UserType + 2
PORT = QGraphicsItem.UserType + 3
PORTGROUP = QGraphicsItem.UserType + 4
BEZIER_LINE = QGraphicsItem.UserType + 5
BEZIER_LINE_MOV = QGraphicsItem.UserType + 6
HIDDEN_CONN = QGraphicsItem.UserType + 7
RUBBERBAND = QGraphicsItem.UserType + 8
# Canvas options
class CanvasOptionsObject:
theme_name = ''
auto_hide_groups = True
auto_select_items = False
show_shadows = False
grid_style = GridStyle.NONE
inline_displays = 0
elastic = True
borders_navigation = True
prevent_overlap = True
max_port_width = 180
semi_hide_opacity = 0.20
default_zoom = 100
box_grouped_auto_layout_ratio = 1.0
cell_width = 16
cell_height = 12
def set_from_settings(self, settings: QSettings):
for key, value in CanvasOptionsObject.__dict__.items():
if key.startswith('_') or isinstance(value, Callable):
continue
try:
if isinstance(value, Enum):
setg_value = settings.value(
f'Canvas/{key}', value.name, type=str)
self.__setattr__(key, type(value)[setg_value])
else:
setg_value = settings.value(
f'Canvas/{key}', value, type=type(value))
self.__setattr__(key, type(value)(setg_value))
except TypeError:
_logger.error(
f'wrong type in settings for Canvas/{key}, '
f'should be {type(value)}')
def save_to_settings(self, settings: QSettings):
for key, value in self.__dict__.items():
if key.startswith('_') or isinstance(value, Callable):
continue
try:
if isinstance(value, Enum):
settings.setValue(
f'Canvas/{key}', value.name)
elif isinstance(value, (bool, int, float, str)):
settings.setValue(
f'Canvas/{key}', value)
else:
_logger.warning(
f'unknown value type for {key}: {type(value)}')
except:
_logger.warning(
f'Failed to save value {value} for key {key}')
# Canvas features
class CanvasFeaturesObject:
group_info = False
group_rename = False
port_info = True
port_rename = False
handle_group_pos = False
# ------------------------
# object lists
class GroupObject:
group_id: int
group_name: str
splitted: bool
box_type: BoxType
icon_name: str
gpos: GroupPos
plugin_id: int
plugin_ui: int # to verify
plugin_inline: int # to verify
handle_client_gui: bool
gui_visible: bool
if TYPE_CHECKING:
widgets: list[BoxWidget]
else:
widgets: list
def current_port_mode(self) -> PortMode:
port_mode = PortMode.NULL
for box in self.widgets:
port_mode |= box._current_port_mode
return port_mode
class ConnectableObject:
group_id: int
port_mode: PortMode
port_type: PortType
port_subtype: PortSubType
portgrp_id: int
def get_port_ids(self) -> tuple[int]:
return tuple[int]()
class PortObject(ConnectableObject):
port_id: int
port_name: str
if TYPE_CHECKING:
widget: PortWidget
portgrp: Optional['PortgrpObject']
hidden_conn_widget: Optional[HiddenConnWidget]
else:
widget: object
portgrp: object
hidden_conn_widget: object
pg_pos = 0 # index in the portgroup (if any)
pg_len = 1 # length of the portgroup (if any)
def __repr__(self) -> str:
return f"PortObject({self.port_name} - {self.port_id} - {self.portgrp_id})"
def get_port_ids(self) -> tuple[int]:
return (self.port_id,)
def set_portgroup_id(self, pg_id: int, pg_pos: int, pg_len: int):
self.portgrp_id = pg_id
self.pg_pos = pg_pos
self.pg_len = pg_len
if self.widget is not None:
self.widget.set_portgroup_id(pg_id, pg_pos, pg_len)
class PortgrpObject(ConnectableObject):
port_id_list: list[int]
if TYPE_CHECKING:
widget: Optional[PortgroupWidget]
else:
widget: object
def __init__(self):
self.ports = list[PortObject]()
def get_port_ids(self) -> tuple[int]:
return tuple(self.port_id_list)
class ConnectionObject:
connection_id: int
group_in_id: int
port_in_id: int
group_out_id: int
port_out_id: int
port_type: PortType
ready_to_disc: bool
in_selected: bool
out_selected: bool
def copy(self) -> 'ConnectionObject':
conn_copy = ConnectionObject()
conn_copy.__dict__ = self.__dict__.copy()
return conn_copy
def matches(self, group_id_1: int, port_ids_list_1: list[int],
group_id_2: int, port_ids_list_2: list[int]) -> bool:
if (self.group_in_id == group_id_1
and self.port_in_id in port_ids_list_1
and self.group_out_id == group_id_2
and self.port_out_id in port_ids_list_2):
return True
elif (self.group_in_id == group_id_2
and self.port_in_id in port_ids_list_2
and self.group_out_id == group_id_1
and self.port_out_id in port_ids_list_1):
return True
else:
return False
def concerns(self, group_id: int, port_ids_list: set[int]) -> bool:
if (self.group_in_id == group_id
and self.port_in_id in port_ids_list):
return True
elif (self.group_out_id == group_id
and self.port_out_id in port_ids_list):
return True
else:
return False
def theme_state(self) -> ConnectionThemeState:
if self.ready_to_disc:
return ConnectionThemeState.DISCONNECTING
if self.in_selected or self.out_selected:
return ConnectionThemeState.SELECTED
return ConnectionThemeState.NORMAL
class ClipboardElement:
port_type: PortType
port_mode: PortMode
group_id: int
port_id: int
group_port_ids: list[int]
# Main Canvas object
class Canvas:
def __init__(self):
self._qobject: Optional['CanvasObject'] = None
self.settings: Optional[QSettings] = None
self._theme: Optional['Theme'] = None
self._cb: Optional[ProtoCallbacker] = None
self.initiated = False
self.theme_paths = ()
self.theme_manager: Optional['ThemeManager'] = None
self.group_list = list[GroupObject]()
self._groups_dict = dict[int, GroupObject]()
self._all_boxes = list['BoxWidget']()
self._ports_dict = dict[int, dict[int, PortObject]]()
self._portgrps_dict = dict[int, dict[int, PortgrpObject]]()
self._conns_dict = dict[int, ConnectionObject]()
self._conns_outin_dict = dict[int, dict[int, dict[int, ConnectionObject]]]()
self._conns_inout_dict = dict[int, dict[int, dict[int, ConnectionObject]]]()
self.groups_to_redraw_in = set[int]()
self.groups_to_redraw_out = set[int]()
self.clipboard = list[ClipboardElement]()
self.clipboard_cut = True
self.group_plugin_map = {}
self._scene: Optional['PatchScene'] = None
self.initial_pos = QPointF(0, 0)
self.size_rect = QRectF()
self.antialiasing = True
self.aliasing_reason = AliasingReason.NONE
self.is_line_mov = False
self.loading_items = False
self.menu_shown = False
self.menu_click_pos = QPoint(0, 0)
@property
def is_init(self) -> bool:
if self._scene is None:
return False
if self._theme is None:
return False
return True
@property
def scene(self) -> 'PatchScene':
'''The canvas PatchScene, used when it can not be None.
It can not be None once patchcanvas is init'''
return self._scene # type:ignore
@property
def theme(self) -> 'Theme':
'''The canvas Theme, used when it can not be None.
It can not be None once patchcanvas is init'''
return self._theme # type:ignore
@property
def qobject(self) -> 'CanvasObject':
'''The CanvasObject (a QObject), used when it can not be None.
It can not be None once patchcanvas is init'''
return self._qobject # type:ignore
@property
def cb(self) -> ProtoCallbacker:
'''The callbacker, used when it can not be None.
It can not be None once patchcanvas is init'''
return self._cb # type:ignore
def ensure_init(self):
'''Ensure patchcanvas.init() has been called,
otherwise raise CanvasNeverInit exception'''
if not self.initiated:
raise CanvasNeverInit
def clear_all(self):
if self._qobject is not None:
self._qobject.rm_all_groups_to_join()
self.group_list.clear()
self._groups_dict.clear()
self._all_boxes.clear()
self._ports_dict.clear()
self._portgrps_dict.clear()
self._conns_dict.clear()
self._conns_outin_dict.clear()
self._conns_inout_dict.clear()
self.clipboard.clear()
self.group_plugin_map.clear()
if self._scene is not None:
self._scene.clear()
def add_group(self, group: GroupObject):
self.group_list.append(group)
self._groups_dict[group.group_id] = group
for widget in group.widgets:
self._all_boxes.append(widget)
def add_box(self, box: 'BoxWidget'):
self._all_boxes.append(box)
def remove_box(self, box: 'BoxWidget'):
if box in self._all_boxes:
self._all_boxes.remove(box)
def add_port(self, port: PortObject):
gp_dict = self._ports_dict.get(port.group_id)
if gp_dict is None:
self._ports_dict[port.group_id] = {port.port_id: port}
else:
self._ports_dict[port.group_id][port.port_id] = port
def add_portgroup(self, portgrp: PortgrpObject):
gp_dict = self._portgrps_dict.get(portgrp.group_id)
if gp_dict is None:
self._portgrps_dict[portgrp.group_id] = {portgrp.portgrp_id: portgrp}
else:
self._portgrps_dict[portgrp.group_id][portgrp.portgrp_id] = portgrp
for port in self.list_ports(group_id=portgrp.group_id):
if port.port_id in portgrp.port_id_list:
portgrp.ports.append(port)
port.portgrp = portgrp
def add_connection(self, conn: ConnectionObject):
self._conns_dict[conn.connection_id] = conn
gp_outin_out_dict = self._conns_outin_dict.get(conn.group_out_id)
gp_inout_in_dict = self._conns_inout_dict.get(conn.group_in_id)
if gp_outin_out_dict is None:
self._conns_outin_dict[conn.group_out_id] = {
conn.group_in_id: {conn.connection_id: conn}}
else:
if conn.group_in_id in gp_outin_out_dict:
gp_outin_out_dict[conn.group_in_id][conn.connection_id] = conn
else:
gp_outin_out_dict[conn.group_in_id] = {conn.connection_id: conn}
if gp_inout_in_dict is None:
self._conns_inout_dict[conn.group_in_id] = {
conn.group_out_id: {conn.connection_id: conn}}
else:
if conn.group_out_id in gp_inout_in_dict:
gp_inout_in_dict[conn.group_out_id][conn.connection_id] = conn
else:
gp_inout_in_dict[conn.group_out_id] = {conn.connection_id: conn}
def remove_group(self, group: GroupObject):
self.group_list.remove(group)
if group.group_id in self._groups_dict.keys():
self._groups_dict.pop(group.group_id)
for widget in group.widgets:
if widget in self._all_boxes:
self._all_boxes.remove(widget)
if self._qobject is not None:
self._qobject.rm_group_to_join(group.group_id)
def remove_port(self, port: PortObject):
gp_dict = self._ports_dict.get(port.group_id)
if gp_dict and port.port_id in gp_dict.keys():
gp_dict.pop(port.port_id)
def remove_portgroup(self, portgrp: PortgrpObject):
gp_dict = self._portgrps_dict.get(portgrp.group_id)
if gp_dict and portgrp.portgrp_id in gp_dict.keys():
gp_dict.pop(portgrp.portgrp_id)
for port in self.list_ports(group_id=portgrp.group_id):
if port.port_id in portgrp.port_id_list:
port.portgrp = None
def remove_connection(self, conn: ConnectionObject):
try:
self._conns_dict.pop(conn.connection_id)
self._conns_outin_dict[conn.group_out_id][conn.group_in_id].pop(
conn.connection_id)
self._conns_inout_dict[conn.group_in_id][conn.group_out_id].pop(
conn.connection_id)
except:
pass
def get_group(self, group_id: int) -> Optional[GroupObject]:
return self._groups_dict.get(group_id)
def get_port(self, group_id: int, port_id: int) -> Optional[PortObject]:
gp = self._ports_dict.get(group_id)
if gp is None:
return None
return gp.get(port_id)
def get_portgroup(
self, group_id: int, portgrp_id: int) -> Optional[PortgrpObject]:
gp = self._portgrps_dict.get(group_id)
if gp is None:
return None
return gp.get(portgrp_id)
def get_connection(self, connection_id: int) -> Optional[ConnectionObject]:
if connection_id in self._conns_dict.keys():
return self._conns_dict[connection_id]
def list_boxes(self) -> list['BoxWidget']:
return self._all_boxes
def list_ports(
self, group_id: Optional[int]=None) -> Iterator[PortObject]:
if group_id is None:
for work_dict in self._ports_dict.values():
for port in work_dict.values():
yield port
return
work_dict = self._ports_dict.get(group_id)
if work_dict is None:
return
for port in work_dict.values():
yield port
def list_portgroups(
self, group_id: Optional[int]=None) -> Iterator[PortgrpObject]:
if group_id is None:
for work_dict in self._portgrps_dict.values():
for portgrp in work_dict.values():
yield portgrp
return
work_dict = self._portgrps_dict.get(group_id)
if work_dict is None:
return
for portgrp in work_dict.values():
yield portgrp
def list_connections(
self, *connectables: ConnectableObject,
group_in_id=None, group_out_id=None,
group_id=None) -> Iterator[ConnectionObject]:
if (not connectables
and group_id is None
and group_in_id is None
and group_out_id is None):
# no filter, list all connections
for conn in self._conns_dict.values():
yield conn
return
if len(connectables) > 2:
return
port_out_ids, port_in_ids = tuple[int](), tuple[int]()
# check infos from connectables (port/portgroup)
if len(connectables) == 2:
if (connectables[0].port_mode
is not connectables[1].port_mode.opposite()):
return
if connectables[0].port_mode is PortMode.OUTPUT:
connectable_out, connectable_in = connectables
else:
connectable_in, connectable_out = connectables
group_out_id = connectable_out.group_id
group_in_id = connectable_in.group_id
port_out_ids = connectable_out.get_port_ids()
port_in_ids = connectable_in.get_port_ids()
elif len(connectables) == 1:
if connectables[0].port_mode is PortMode.OUTPUT:
group_out_id = connectables[0].group_id
port_out_ids = connectables[0].get_port_ids()
else:
group_in_id = connectables[0].group_id
port_in_ids = connectables[0].get_port_ids()
# check if group_id should be group_out_id or group_in_id
if (group_id is not None
and (group_out_id is not None or group_in_id is not None)):
if group_out_id is None:
group_out_id = group_id
elif group_in_id is None:
group_in_id = group_id
group_id = None
# no precision for group_id
# we check first in the out dict, then in the in dict
if group_id is not None:
gp_out = self._conns_outin_dict.get(group_id)
if gp_out is not None:
for gp_out_in in gp_out.values():
for conn in gp_out_in.values():
yield conn
gp_in = self._conns_inout_dict.get(group_id)
if gp_in is not None:
for gp_id, gp_in_out in gp_in.items():
if gp_id == group_id:
# connections here have already been yielded
continue
for conn in gp_in_out.values():
yield conn
return
# here we are sure we have group_out_id or group_in_id filter(s)
if group_out_id is not None:
gp_out = self._conns_outin_dict.get(group_out_id)
if gp_out is None:
return
if group_in_id is not None:
gp_in = gp_out.get(group_in_id)
if gp_in is None:
return
for conn in gp_in.values():
if ((not port_out_ids or conn.port_out_id in port_out_ids)
and (not port_in_ids or conn.port_in_id in port_in_ids)):
yield conn
return
for gp_in in gp_out.values():
for conn in gp_in.values():
if not port_out_ids or conn.port_out_id in port_out_ids:
yield conn
return
# here we are sure we have no group_out_id filter
gp_in = self._conns_inout_dict.get(group_in_id)
if gp_in is None:
return
for gp_out in gp_in.values():
for conn in gp_out.values():
if not port_in_ids or conn.port_in_id in port_in_ids:
yield conn
def set_aliasing_reason(
self, aliasing_reason: AliasingReason, yesno: bool):
is_aliasing = bool(self.aliasing_reason)
if yesno:
self.aliasing_reason |= aliasing_reason
else:
self.aliasing_reason &= ~aliasing_reason
if bool(self.aliasing_reason) is is_aliasing:
return
if self._scene is not None:
self._scene.set_anti_aliasing(not bool(self.aliasing_reason))
# -----------------------------
# Global objects
canvas = Canvas()
options = CanvasOptionsObject()
features = CanvasFeaturesObject()
|