1#!/usr/local/bin/python3.8
2# -*- coding: utf-8 -*-
3
4# PatchBay Canvas engine using QGraphicsView/Scene
5# Copyright (C) 2010-2019 Filipe Coelho <falktx@falktx.com>
6#
7# This program is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 2 of
10# the License, or any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# For a full copy of the GNU General Public License see the doc/GPL.txt file.
18
19# ------------------------------------------------------------------------------------------------------------
20# Imports (Global)
21
22from PyQt5.QtCore import qWarning, Qt, QPointF
23from PyQt5.QtGui import QPainter, QPainterPath, QPen
24from PyQt5.QtWidgets import QGraphicsPathItem
25
26# ------------------------------------------------------------------------------------------------------------
27# Imports (Custom)
28
29from . import (
30    canvas,
31    options,
32    port_mode2str,
33    port_type2str,
34    CanvasBezierLineMovType,
35    PORT_MODE_INPUT,
36    PORT_MODE_OUTPUT,
37    PORT_TYPE_AUDIO_JACK,
38    PORT_TYPE_MIDI_ALSA,
39    PORT_TYPE_MIDI_JACK,
40    PORT_TYPE_PARAMETER,
41)
42
43# ------------------------------------------------------------------------------------------------------------
44
45class CanvasBezierLineMov(QGraphicsPathItem):
46    def __init__(self, port_mode, port_type, parent):
47        QGraphicsPathItem.__init__(self)
48        self.setParentItem(parent)
49
50        self.m_port_mode = port_mode
51        self.m_port_type = port_type
52
53        # Port position doesn't change while moving around line
54        self.p_itemX = self.scenePos().x()
55        self.p_itemY = self.scenePos().y()
56        self.p_width = parent.getPortWidth()
57
58        if port_type == PORT_TYPE_AUDIO_JACK:
59            pen = QPen(canvas.theme.line_audio_jack, 2)
60        elif port_type == PORT_TYPE_MIDI_JACK:
61            pen = QPen(canvas.theme.line_midi_jack, 2)
62        elif port_type == PORT_TYPE_MIDI_ALSA:
63            pen = QPen(canvas.theme.line_midi_alsa, 2)
64        elif port_type == PORT_TYPE_PARAMETER:
65            pen = QPen(canvas.theme.line_parameter, 2)
66        else:
67            qWarning("PatchCanvas::CanvasBezierLineMov({}, {}, {}) - invalid port type".format(
68                     port_mode2str(port_mode), port_type2str(port_type), parent))
69            pen = QPen(Qt.black)
70
71        pen.setCapStyle(Qt.FlatCap)
72        pen.setWidthF(pen.widthF() + 0.00001)
73        self.setPen(pen)
74
75    def updateLinePos(self, scenePos):
76        if self.m_port_mode == PORT_MODE_INPUT:
77            old_x = 0
78            old_y = float(canvas.theme.port_height)/2
79            mid_x = abs(scenePos.x() - self.p_itemX) / 2
80            new_x = old_x - mid_x
81        elif self.m_port_mode == PORT_MODE_OUTPUT:
82            old_x = self.p_width + 12
83            old_y = float(canvas.theme.port_height)/2
84            mid_x = abs(scenePos.x() - (self.p_itemX + old_x)) / 2
85            new_x = old_x + mid_x
86        else:
87            return
88
89        final_x = scenePos.x() - self.p_itemX
90        final_y = scenePos.y() - self.p_itemY
91
92        path = QPainterPath(QPointF(old_x, old_y))
93        path.cubicTo(new_x, old_y, new_x, final_y, final_x, final_y)
94        self.setPath(path)
95
96    def type(self):
97        return CanvasBezierLineMovType
98
99    def paint(self, painter, option, widget):
100        painter.save()
101        painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
102        QGraphicsPathItem.paint(self, painter, option, widget)
103        painter.restore()
104
105# ------------------------------------------------------------------------------------------------------------
106