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, QLineF
23from PyQt5.QtGui import QPainter, QPen
24from PyQt5.QtWidgets import QGraphicsLineItem
25
26# ------------------------------------------------------------------------------------------------------------
27# Imports (Custom)
28
29from . import (
30    canvas,
31    options,
32    port_mode2str,
33    port_type2str,
34    CanvasLineMovType,
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 CanvasLineMov(QGraphicsLineItem):
46    def __init__(self, port_mode, port_type, parent):
47        QGraphicsLineItem.__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_lineX = self.scenePos().x()
55        self.p_lineY = 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::CanvasLineMov({}, {}, {}) - invalid port type".format(
68                     port_mode2str(port_mode), port_type2str(port_type), parent))
69            pen = QPen(Qt.black)
70
71        pen.setCapStyle(Qt.RoundCap)
72        pen.setWidthF(pen.widthF() + 0.00001)
73        self.setPen(pen)
74
75    def updateLinePos(self, scenePos):
76        item_pos = [0, 0]
77
78        if self.m_port_mode == PORT_MODE_INPUT:
79            item_pos[0] = 0
80            item_pos[1] = float(canvas.theme.port_height)/2
81        elif self.m_port_mode == PORT_MODE_OUTPUT:
82            item_pos[0] = self.p_width + 12
83            item_pos[1] = float(canvas.theme.port_height)/2
84        else:
85            return
86
87        line = QLineF(item_pos[0], item_pos[1], scenePos.x() - self.p_lineX, scenePos.y() - self.p_lineY)
88        self.setLine(line)
89
90    def type(self):
91        return CanvasLineMovType
92
93    def paint(self, painter, option, widget):
94        painter.save()
95        painter.setRenderHint(QPainter.Antialiasing, bool(options.antialiasing))
96        QGraphicsLineItem.paint(self, painter, option, widget)
97        painter.restore()
98
99# ------------------------------------------------------------------------------------------------------------
100