File indexing completed on 2024-05-19 04:36:33
0001 /* This file is part of the TikZKit project. 0002 * 0003 * Copyright (C) 2013 Dominik Haumann <dhaumann@kde.org> 0004 * 0005 * This library is free software; you can redistribute it and/or modify 0006 * it under the terms of the GNU Library General Public License as published 0007 * by the Free Software Foundation, either version 2 of the License, or 0008 * (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 * GNU Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, see 0017 * <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "StealthArrow.h" 0021 0022 #include <tikz/core/Style.h> 0023 0024 #include <QPainter> 0025 0026 #include <cmath> 0027 0028 class StealthArrowPrivate 0029 { 0030 public: 0031 }; 0032 0033 StealthArrow::StealthArrow(tikz::core::Style * style) 0034 : AbstractArrow(style) 0035 , d(new StealthArrowPrivate()) 0036 { 0037 } 0038 0039 StealthArrow::~StealthArrow() 0040 { 0041 delete d; 0042 } 0043 0044 tikz::Arrow StealthArrow::type() const 0045 { 0046 return tikz::Arrow::StealthArrow; 0047 } 0048 0049 QString StealthArrow::name() const 0050 { 0051 return QObject::tr("stealth"); 0052 } 0053 0054 qreal StealthArrow::leftExtend() const 0055 { 0056 // see: pgfcorearrows.code.tex 0057 tikz::Value dima(0.28); 0058 tikz::Value dimb = style()->penWidth(); 0059 if (style()->innerLineWidth() > 0.0) { 0060 dimb = 0.6 * dimb - 0.4 * style()->innerLineWidth(); 0061 } 0062 dima += 0.3 * dimb; 0063 return -3.0 * dima.toPoint(); 0064 } 0065 0066 qreal StealthArrow::rightExtend() const 0067 { 0068 // see: pgfcorearrows.code.tex 0069 qreal dima = 0.28; 0070 qreal dimb = style()->penWidth().toPoint(); 0071 if (style()->innerLineWidth().toPoint() > 0.0) { 0072 dimb = 0.6 * dimb - 0.4 * style()->innerLineWidth().toPoint(); 0073 } 0074 dima += 0.3 * dimb; 0075 return 5.0 * dima; 0076 } 0077 0078 void StealthArrow::draw(QPainter* painter) const 0079 { 0080 // see: pgfcorearrows.code.tex 0081 QPainterPath p = path(); 0082 painter->save(); 0083 painter->fillPath(p, style()->penColor()); 0084 painter->restore(); 0085 } 0086 0087 QPainterPath StealthArrow::path() const 0088 { 0089 // see: pgfcorearrows.code.tex 0090 qreal dima = 0.28; 0091 qreal dimb = style()->penWidth().toPoint(); 0092 if (style()->innerLineWidth().toPoint() > 0.0) { 0093 dimb = 0.6 * dimb - 0.4 * style()->innerLineWidth().toPoint(); 0094 } 0095 dima += 0.3 * dimb; 0096 0097 QPainterPath path; 0098 path.moveTo(dima * QPointF( 5, 0)); 0099 path.lineTo(dima * QPointF(-3, 4)); 0100 path.lineTo( QPointF( 0, 0)); 0101 path.lineTo(dima * QPointF(-3, -4)); 0102 path.closeSubpath(); 0103 0104 return path; 0105 } 0106 0107 QPainterPath StealthArrow::contour(qreal width) const 0108 { 0109 QPainterPathStroker stroker; 0110 stroker.setJoinStyle(Qt::RoundJoin); 0111 stroker.setCapStyle(Qt::RoundCap); 0112 stroker.setWidth(width); 0113 0114 return stroker.createStroke(path()); 0115 } 0116 0117 0118 0119 ReversedStealthArrow::ReversedStealthArrow(tikz::core::Style * style) 0120 : StealthArrow(style) 0121 { 0122 } 0123 0124 tikz::Arrow ReversedStealthArrow::type() const 0125 { 0126 return tikz::Arrow::ReversedStealthArrow; 0127 } 0128 0129 QString ReversedStealthArrow::name() const 0130 { 0131 return QObject::tr("stealth reversed"); 0132 } 0133 0134 qreal ReversedStealthArrow::leftExtend() const 0135 { 0136 return -StealthArrow::rightExtend(); 0137 } 0138 0139 qreal ReversedStealthArrow::rightExtend() const 0140 { 0141 return -StealthArrow::leftExtend(); 0142 } 0143 0144 QPainterPath ReversedStealthArrow::path() const 0145 { 0146 QTransform trans; 0147 trans.scale(-1, 1); 0148 return trans.map(StealthArrow::path()); 0149 } 0150 // kate: indent-width 4; replace-tabs on;