File indexing completed on 2024-05-19 04:36:34

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 "ToArrow.h"
0021 
0022 #include <tikz/core/Style.h>
0023 
0024 #include <QPainter>
0025 
0026 #include <cmath>
0027 
0028 class ToArrowPrivate
0029 {
0030     public:
0031 };
0032 
0033 ToArrow::ToArrow(tikz::core::Style * style)
0034     : AbstractArrow(style)
0035     , d(new ToArrowPrivate())
0036 {
0037 }
0038 
0039 ToArrow::~ToArrow()
0040 {
0041     delete d;
0042 }
0043 
0044 tikz::Arrow ToArrow::type() const
0045 {
0046     return tikz::Arrow::ToArrow;
0047 }
0048 
0049 QString ToArrow::name() const
0050 {
0051     return QObject::tr("to");
0052 }
0053 
0054 qreal ToArrow::leftExtend() const
0055 {
0056     // see: pgfcorearrows.code.tex; 0.84pt
0057     return -0.84 - 1.3 * style()->penWidth().toPoint();
0058 }
0059 
0060 qreal ToArrow::rightExtend() const
0061 {
0062     // see: pgfcorearrows.code.tex; 0.21pt
0063     return 0.21 + 0.625 * style()->penWidth().toPoint();
0064 }
0065 
0066 void ToArrow::draw(QPainter* painter) const
0067 {
0068     // see: pgfcorearrows.code.tex
0069     QPainterPath p = path();
0070     painter->save();
0071     QPen pen = painter->pen();
0072     pen.setWidthF(0.8 * style()->penWidth().toPoint());
0073     pen.setColor(style()->penColor());
0074     pen.setCapStyle(Qt::RoundCap);
0075     pen.setJoinStyle(Qt::RoundJoin);
0076     painter->setPen(pen);
0077     painter->setBrush(Qt::NoBrush);
0078     painter->drawPath(p);
0079     painter->restore();
0080 }
0081 
0082 QPainterPath ToArrow::path() const
0083 {
0084     // see: pgfcorearrows.code.tex
0085     QPainterPath path;
0086 
0087     const qreal dima = tikz::Value(0.28).toPoint() + 0.3 * style()->penWidth().toPoint();
0088     path.moveTo(dima * QPointF(-3, 4));
0089     path.cubicTo(dima * QPointF(-2.75, 2.5),
0090                  dima * QPointF(0.0, 0.25),
0091                  dima * QPointF(0.75, 0.0));
0092     path.cubicTo(dima * QPointF(0.0, -0.25),
0093                  dima * QPointF(-2.75, -2.5),
0094                  dima * QPointF(-3, -4));
0095     return path;
0096 }
0097 
0098 QPainterPath ToArrow::contour(qreal width) const
0099 {
0100     QPainterPathStroker stroker;
0101     stroker.setJoinStyle(Qt::RoundJoin);
0102     stroker.setCapStyle(Qt::RoundCap);
0103     stroker.setWidth(width + style()->penWidth().toPoint());
0104 
0105     return stroker.createStroke(path());
0106 }
0107 
0108 
0109 
0110 
0111 class ReversedToArrowPrivate
0112 {
0113     public:
0114 };
0115 
0116 ReversedToArrow::ReversedToArrow(tikz::core::Style * style)
0117     : AbstractArrow(style)
0118     , d(new ReversedToArrowPrivate())
0119 {
0120 }
0121 
0122 ReversedToArrow::~ReversedToArrow()
0123 {
0124     delete d;
0125 }
0126 
0127 tikz::Arrow ReversedToArrow::type() const
0128 {
0129     return tikz::Arrow::ReversedToArrow;
0130 }
0131 
0132 QString ReversedToArrow::name() const
0133 {
0134     return QObject::tr("to reversed");
0135 }
0136 
0137 qreal ReversedToArrow::leftExtend() const
0138 {
0139     // see: pgfcorearrows.code.tex
0140     return -0.21 - 0.475 * style()->penWidth().toPoint();
0141 }
0142 
0143 qreal ReversedToArrow::rightExtend() const
0144 {
0145     // see: pgfcorearrows.code.tex
0146     return 0.98 + 1.45 * style()->penWidth().toPoint();
0147 }
0148 
0149 void ReversedToArrow::draw(QPainter* painter) const
0150 {
0151     // see: pgfcorearrows.code.tex
0152     QPainterPath p = path();
0153     painter->save();
0154     QPen pen = painter->pen();
0155     pen.setWidthF(0.8 * style()->penWidth().toPoint());
0156     pen.setColor(style()->penColor());
0157     pen.setCapStyle(Qt::RoundCap);
0158     pen.setJoinStyle(Qt::RoundJoin);
0159     painter->setPen(pen);
0160     painter->setBrush(Qt::NoBrush);
0161     painter->drawPath(p);
0162     painter->restore();
0163 }
0164 
0165 QPainterPath ReversedToArrow::path() const
0166 {
0167     // see: pgfcorearrows.code.tex
0168     QPainterPath path;
0169 
0170     const qreal dima = 0.28 + 0.3 * style()->penWidth().toPoint();
0171     path.moveTo(dima * QPointF(3.5, 4));
0172     path.cubicTo(dima * QPointF(3.25, 2.5),
0173                  dima * QPointF(0.5, 0.25),
0174                  dima * QPointF(-0.25, 0.0));
0175     path.cubicTo(dima * QPointF(0.5, -0.25),
0176                  dima * QPointF(3.25, -2.5),
0177                  dima * QPointF(3.5, -4));
0178     return path;
0179 }
0180 
0181 QPainterPath ReversedToArrow::contour(qreal width) const
0182 {
0183     QPainterPathStroker stroker;
0184     stroker.setJoinStyle(Qt::RoundJoin);
0185     stroker.setCapStyle(Qt::RoundCap);
0186     stroker.setWidth(width + style()->penWidth().toPoint());
0187 
0188     return stroker.createStroke(path());
0189 }
0190 
0191 // kate: indent-width 4; replace-tabs on;