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 "canvasbezierline.h"
19 
20 #include <QtGui/QPainter>
21 
22 #include "canvasport.h"
23 #include "canvasportglow.h"
24 
25 START_NAMESPACE_PATCHCANVAS
26 
CanvasBezierLine(CanvasPort * item1_,CanvasPort * item2_,QGraphicsItem * parent)27 CanvasBezierLine::CanvasBezierLine(CanvasPort* item1_, CanvasPort* item2_, QGraphicsItem* parent) :
28     QGraphicsPathItem(parent, canvas.scene)
29 {
30     item1 = item1_;
31     item2 = item2_;
32 
33     m_locked = false;
34     m_lineSelected = false;
35 
36     setBrush(QColor(0,0,0,0));
37     setGraphicsEffect(0);
38     updateLinePos();
39 }
40 
~CanvasBezierLine()41 CanvasBezierLine::~CanvasBezierLine()
42 {
43     setGraphicsEffect(0);
44 }
45 
deleteFromScene()46 void CanvasBezierLine::deleteFromScene()
47 {
48     canvas.scene->removeItem(this);
49     delete this;
50 }
51 
isLocked() const52 bool CanvasBezierLine::isLocked() const
53 {
54     return m_locked;
55 }
56 
setLocked(bool yesno)57 void CanvasBezierLine::setLocked(bool yesno)
58 {
59     m_locked = yesno;
60 }
61 
isLineSelected() const62 bool CanvasBezierLine::isLineSelected() const
63 {
64     return m_lineSelected;
65 }
66 
setLineSelected(bool yesno)67 void CanvasBezierLine::setLineSelected(bool yesno)
68 {
69     if (m_locked)
70         return;
71 
72     if (options.eyecandy == EYECANDY_FULL)
73     {
74         if (yesno)
75             setGraphicsEffect(new CanvasPortGlow(item1->getPortType(), toGraphicsObject()));
76         else
77             setGraphicsEffect(0);
78     }
79 
80     m_lineSelected = yesno;
81     updateLineGradient();
82 }
83 
updateLinePos()84 void CanvasBezierLine::updateLinePos()
85 {
86     if (item1->getPortMode() == PORT_MODE_OUTPUT)
87     {
88         int item1_x = item1->scenePos().x() + item1->getPortWidth()+12;
89         int item1_y = item1->scenePos().y() + 7.5;
90 
91         int item2_x = item2->scenePos().x();
92         int item2_y = item2->scenePos().y()+7.5;
93 
94         int item1_mid_x = abs(item1_x-item2_x)/2;
95         int item1_new_x = item1_x+item1_mid_x;
96 
97         int item2_mid_x = abs(item1_x-item2_x)/2;
98         int item2_new_x = item2_x-item2_mid_x;
99 
100         QPainterPath path(QPointF(item1_x, item1_y));
101         path.cubicTo(item1_new_x, item1_y, item2_new_x, item2_y, item2_x, item2_y);
102         setPath(path);
103 
104         m_lineSelected = false;
105         updateLineGradient();
106     }
107 }
108 
type() const109 int CanvasBezierLine::type() const
110 {
111     return CanvasBezierLineType;
112 }
113 
updateLineGradient()114 void CanvasBezierLine::updateLineGradient()
115 {
116     short pos1, pos2;
117     int pos_top = boundingRect().top();
118     int pos_bot = boundingRect().bottom();
119 
120     if (item2->scenePos().y() >= item1->scenePos().y())
121     {
122         pos1 = 0;
123         pos2 = 1;
124     }
125     else
126     {
127         pos1 = 1;
128         pos2 = 0;
129     }
130 
131     PortType port_type1 = item1->getPortType();
132     PortType port_type2 = item2->getPortType();
133     QLinearGradient port_gradient(0, pos_top, 0, pos_bot);
134 
135     if (port_type1 == PORT_TYPE_AUDIO_JACK)
136         port_gradient.setColorAt(pos1, m_lineSelected ? canvas.theme->line_audio_jack_sel : canvas.theme->line_audio_jack);
137     else if (port_type1 == PORT_TYPE_MIDI_JACK)
138         port_gradient.setColorAt(pos1, m_lineSelected ? canvas.theme->line_midi_jack_sel : canvas.theme->line_midi_jack);
139     else if (port_type1 == PORT_TYPE_MIDI_A2J)
140         port_gradient.setColorAt(pos1, m_lineSelected ? canvas.theme->line_midi_a2j_sel : canvas.theme->line_midi_a2j);
141     else if (port_type1 == PORT_TYPE_MIDI_ALSA)
142         port_gradient.setColorAt(pos1, m_lineSelected ? canvas.theme->line_midi_alsa_sel : canvas.theme->line_midi_alsa);
143 
144     if (port_type2 == PORT_TYPE_AUDIO_JACK)
145         port_gradient.setColorAt(pos2, m_lineSelected ? canvas.theme->line_audio_jack_sel : canvas.theme->line_audio_jack);
146     else if (port_type2 == PORT_TYPE_MIDI_JACK)
147         port_gradient.setColorAt(pos2, m_lineSelected ? canvas.theme->line_midi_jack_sel : canvas.theme->line_midi_jack);
148     else if (port_type2 == PORT_TYPE_MIDI_A2J)
149         port_gradient.setColorAt(pos2, m_lineSelected ? canvas.theme->line_midi_a2j_sel : canvas.theme->line_midi_a2j);
150     else if (port_type2 == PORT_TYPE_MIDI_ALSA)
151         port_gradient.setColorAt(pos2, m_lineSelected ? canvas.theme->line_midi_alsa_sel : canvas.theme->line_midi_alsa);
152 
153     setPen(QPen(port_gradient, 2));
154 }
155 
paint(QPainter * painter,const QStyleOptionGraphicsItem * option,QWidget * widget)156 void CanvasBezierLine::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
157 {
158     painter->setRenderHint(QPainter::Antialiasing, bool(options.antialiasing));
159     QGraphicsPathItem::paint(painter, option, widget);
160 }
161 
162 END_NAMESPACE_PATCHCANVAS
163