1 /*
2 * Patchbay Canvas engine using QGraphicsView/Scene
3 * Copyright (C) 2010-2012 Filipe Coelho <falktx@falktx.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * For a full copy of the GNU General Public License see the COPYING file
16 */
17
18 #include "canvasbezierlinemov.h"
19
20 #include <QtGui/QPainter>
21
22 #include "canvasport.h"
23
24 START_NAMESPACE_PATCHCANVAS
25
CanvasBezierLineMov(PortMode port_mode,PortType port_type,QGraphicsItem * parent)26 CanvasBezierLineMov::CanvasBezierLineMov(PortMode port_mode, PortType port_type, QGraphicsItem* parent) :
27 QGraphicsPathItem(parent, canvas.scene)
28 {
29 m_port_mode = port_mode;
30 m_port_type = port_type;
31
32 // Port position doesn't change while moving around line
33 p_itemX = scenePos().x();
34 p_itemY = scenePos().y();
35 p_width = ((CanvasPort*)parentItem())->getPortWidth();
36
37 QPen pen;
38
39 if (port_type == PORT_TYPE_AUDIO_JACK)
40 pen = QPen(canvas.theme->line_audio_jack, 2);
41 else if (port_type == PORT_TYPE_MIDI_JACK)
42 pen = QPen(canvas.theme->line_midi_jack, 2);
43 else if (port_type == PORT_TYPE_MIDI_A2J)
44 pen = QPen(canvas.theme->line_midi_a2j, 2);
45 else if (port_type == PORT_TYPE_MIDI_ALSA)
46 pen = QPen(canvas.theme->line_midi_alsa, 2);
47 else
48 {
49 qWarning("PatchCanvas::CanvasBezierLineMov(%s, %s, %p) - invalid port type", port_mode2str(port_mode), port_type2str(port_type), parent);
50 pen = QPen(Qt::black);
51 }
52
53 QColor color(0,0,0,0);
54 setBrush(color);
55 setPen(pen);
56 }
57
deleteFromScene()58 void CanvasBezierLineMov::deleteFromScene()
59 {
60 canvas.scene->removeItem(this);
61 delete this;
62 }
63
updateLinePos(QPointF scenePos)64 void CanvasBezierLineMov::updateLinePos(QPointF scenePos)
65 {
66 int old_x, old_y, mid_x, new_x, final_x, final_y;
67
68 if (m_port_mode == PORT_MODE_INPUT)
69 {
70 old_x = 0;
71 old_y = 7.5;
72 mid_x = abs(scenePos.x()-p_itemX)/2;
73 new_x = old_x-mid_x;
74 }
75 else if (m_port_mode == PORT_MODE_OUTPUT)
76 {
77 old_x = p_width+12;
78 old_y = 7.5;
79 mid_x = abs(scenePos.x()-(p_itemX+old_x))/2;
80 new_x = old_x+mid_x;
81 }
82 else
83 return;
84
85 final_x = scenePos.x()-p_itemX;
86 final_y = scenePos.y()-p_itemY;
87
88 QPainterPath path(QPointF(old_x, old_y));
89 path.cubicTo(new_x, old_y, new_x, final_y, final_x, final_y);
90 setPath(path);
91 }
92
type() const93 int CanvasBezierLineMov::type() const
94 {
95 return CanvasBezierLineMovType;
96 }
97
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)98 void CanvasBezierLineMov::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
99 {
100 painter->setRenderHint(QPainter::Antialiasing, bool(options.antialiasing));
101 QGraphicsPathItem::paint(painter, option, widget);
102 }
103
104 END_NAMESPACE_PATCHCANVAS
105