1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
//Copyright 2017 Ryan Wick
//This file is part of Bandage
//Bandage is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//Bandage is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with Bandage. If not, see <http://www.gnu.org/licenses/>.
#include "graphicsitemedge.h"
#include "debruijnedge.h"
#include <QPainterPathStroker>
#include <QPainter>
#include <QPen>
#include "../program/globals.h"
#include "../program/settings.h"
#include "debruijnnode.h"
#include "ogdfnode.h"
#include <QLineF>
#include "graphicsitemnode.h"
GraphicsItemEdge::GraphicsItemEdge(DeBruijnEdge * deBruijnEdge, QGraphicsItem * parent) :
QGraphicsPathItem(parent), m_deBruijnEdge(deBruijnEdge)
{
calculateAndSetPath();
}
QPointF GraphicsItemEdge::extendLine(QPointF start, QPointF end, double extensionLength)
{
double extensionRatio = extensionLength / QLineF(start, end).length();
QPointF difference = end - start;
difference *= extensionRatio;
return end + difference;
}
void GraphicsItemEdge::paint(QPainter * painter, const QStyleOptionGraphicsItem *, QWidget *)
{
double edgeWidth = g_settings->edgeWidth;
QColor penColour;
if (isSelected())
penColour = g_settings->selectionColour;
else
penColour = g_settings->edgeColour;
QPen edgePen(QBrush(penColour), edgeWidth, Qt::SolidLine, Qt::RoundCap);
painter->setPen(edgePen);
painter->drawPath(path());
}
QPainterPath GraphicsItemEdge::shape() const
{
QPainterPathStroker stroker;
stroker.setWidth(g_settings->edgeWidth);
stroker.setCapStyle(Qt::RoundCap);
stroker.setJoinStyle(Qt::RoundJoin);
return stroker.createStroke(path());
}
void GraphicsItemEdge::calculateAndSetPath()
{
setControlPointLocations();
double edgeDistance = QLineF(m_startingLocation, m_endingLocation).length();
double extensionLength = g_settings->edgeLength;
if (extensionLength > edgeDistance / 2.0)
extensionLength = edgeDistance / 2.0;
m_controlPoint1 = extendLine(m_beforeStartingLocation, m_startingLocation, extensionLength);
m_controlPoint2 = extendLine(m_afterEndingLocation, m_endingLocation, extensionLength);
//If this edge is connecting a node to itself, and that node
//is made of only one line segment, then a special path is
//required, otherwise the edge will be mostly hidden underneath
//the node.
DeBruijnNode * startingNode = m_deBruijnEdge->getStartingNode();
DeBruijnNode * endingNode = m_deBruijnEdge->getEndingNode();
if (startingNode == endingNode)
{
GraphicsItemNode * graphicsItemNode = startingNode->getGraphicsItemNode();
if (graphicsItemNode == 0)
graphicsItemNode = startingNode->getReverseComplement()->getGraphicsItemNode();
if (graphicsItemNode != 0 && graphicsItemNode->m_linePoints.size() == 2)
{
makeSpecialPathConnectingNodeToSelf();
return;
}
}
//If we are in single mode and the edge connects a node to its reverse
//complement, then we need a special path to make it visible.
if (startingNode == endingNode->getReverseComplement() &&
!g_settings->doubleMode)
{
makeSpecialPathConnectingNodeToReverseComplement();
return;
}
//Otherwise, the path is just a single cubic Bezier curve.
QPainterPath path;
path.moveTo(m_startingLocation);
path.cubicTo(m_controlPoint1, m_controlPoint2, m_endingLocation);
setPath(path);
}
void GraphicsItemEdge::setControlPointLocations()
{
DeBruijnNode * startingNode = m_deBruijnEdge->getStartingNode();
DeBruijnNode * endingNode = m_deBruijnEdge->getEndingNode();
if (startingNode->hasGraphicsItem())
{
m_startingLocation = startingNode->getGraphicsItemNode()->getLast();
m_beforeStartingLocation = startingNode->getGraphicsItemNode()->getSecondLast();
}
else if (startingNode->getReverseComplement()->hasGraphicsItem())
{
m_startingLocation = startingNode->getReverseComplement()->getGraphicsItemNode()->getFirst();
m_beforeStartingLocation = startingNode->getReverseComplement()->getGraphicsItemNode()->getSecond();
}
if (endingNode->hasGraphicsItem())
{
m_endingLocation = endingNode->getGraphicsItemNode()->getFirst();
m_afterEndingLocation = endingNode->getGraphicsItemNode()->getSecond();
}
else if (endingNode->getReverseComplement()->hasGraphicsItem())
{
m_endingLocation = endingNode->getReverseComplement()->getGraphicsItemNode()->getLast();
m_afterEndingLocation = endingNode->getReverseComplement()->getGraphicsItemNode()->getSecondLast();
}
}
//This function handles the special case of an edge that connects a node
//to itself where the node graphics item has only one line segment.
void GraphicsItemEdge::makeSpecialPathConnectingNodeToSelf()
{
double extensionLength = g_settings->edgeLength;
m_controlPoint1 = extendLine(m_beforeStartingLocation, m_startingLocation, extensionLength);
m_controlPoint2 = extendLine(m_afterEndingLocation, m_endingLocation, extensionLength);
QLineF nodeLine(m_startingLocation, m_endingLocation);
QLineF normalUnitLine = nodeLine.normalVector().unitVector();
QPointF perpendicularShift = (normalUnitLine.p2() - normalUnitLine.p1()) * g_settings->edgeLength;
QPointF nodeMidPoint = (m_startingLocation + m_endingLocation) / 2.0;
QPainterPath path;
path.moveTo(m_startingLocation);
path.cubicTo(m_controlPoint1, m_controlPoint1 + perpendicularShift, nodeMidPoint + perpendicularShift);
path.cubicTo(m_controlPoint2 + perpendicularShift, m_controlPoint2, m_endingLocation);
setPath(path);
}
//This function handles the special case of an edge that connects a node to its
//reverse complement and is displayed in single mode.
void GraphicsItemEdge::makeSpecialPathConnectingNodeToReverseComplement()
{
double extensionLength = g_settings->edgeLength / 2.0;
m_controlPoint1 = extendLine(m_beforeStartingLocation, m_startingLocation, extensionLength);
m_controlPoint2 = extendLine(m_afterEndingLocation, m_endingLocation, extensionLength);
QPointF startToControl = m_controlPoint1 - m_startingLocation;
QPointF pathMidPoint = m_startingLocation + startToControl * 3.0;
QLineF normalLine = QLineF(m_controlPoint1, m_startingLocation).normalVector();
QPointF perpendicularShift = (normalLine.p2() - normalLine.p1()) * 1.5;
QPainterPath path;
path.moveTo(m_startingLocation);
path.cubicTo(m_controlPoint1, pathMidPoint + perpendicularShift, pathMidPoint);
path.cubicTo(pathMidPoint - perpendicularShift, m_controlPoint2, m_endingLocation);
setPath(path);
}
|