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