Collection of tools useful for audio production
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.7KB

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