File indexing completed on 2024-05-12 15:56:42

0001 /* This file is part of the KDE project
0002    SPDX-FileCopyrightText: 2006 Thorsten Zachmann <zachmann@kde.org>
0003    SPDX-FileCopyrightText: 2007, 2009 Thomas Zander <zander@kde.org>
0004 
0005    SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "KoParameterShape.h"
0009 #include "KoParameterShape_p.h"
0010 
0011 #include <KisHandlePainterHelper.h>
0012 
0013 #include <QPainter>
0014 #include <FlakeDebug.h>
0015 
0016 KoParameterShape::Private::Private()
0017     : QSharedData()
0018     , parametric(true)
0019 {
0020 }
0021 
0022 KoParameterShape::Private::Private(const Private &rhs)
0023     : QSharedData()
0024     , parametric(rhs.parametric)
0025     , handles(rhs.handles)
0026 {
0027 }
0028 
0029 KoParameterShape::KoParameterShape()
0030     : KoPathShape()
0031     , d(new Private)
0032 {
0033 }
0034 
0035 KoParameterShape::KoParameterShape(const KoParameterShape &rhs)
0036     : KoPathShape(rhs)
0037     , d(rhs.d)
0038 {
0039 }
0040 
0041 KoParameterShape::~KoParameterShape()
0042 {
0043 }
0044 
0045 void KoParameterShape::moveHandle(int handleId, const QPointF & point, Qt::KeyboardModifiers modifiers)
0046 {
0047 
0048     if (handleId >= d->handles.size()) {
0049         warnFlake << "handleId out of bounds";
0050         return;
0051     }
0052 
0053     update();
0054     // function to do special stuff
0055     moveHandleAction(handleId, documentToShape(point), modifiers);
0056 
0057     updatePath(size());
0058     update();
0059 }
0060 
0061 
0062 int KoParameterShape::handleIdAt(const QRectF & rect) const
0063 {
0064 
0065     int handle = -1;
0066 
0067     for (int i = 0; i < d->handles.size(); ++i) {
0068         if (rect.contains(d->handles.at(i))) {
0069             handle = i;
0070             break;
0071         }
0072     }
0073     return handle;
0074 }
0075 
0076 QPointF KoParameterShape::handlePosition(int handleId) const
0077 {
0078 
0079     return d->handles.value(handleId);
0080 }
0081 
0082 void KoParameterShape::paintHandles(KisHandlePainterHelper &handlesHelper)
0083 {
0084 
0085 
0086     QList<QPointF>::const_iterator it(d->handles.constBegin());
0087     for (; it != d->handles.constEnd(); ++it) {
0088         handlesHelper.drawGradientHandle(*it);
0089     }
0090 }
0091 
0092 void KoParameterShape::paintHandle(KisHandlePainterHelper &handlesHelper, int handleId)
0093 {
0094 
0095     handlesHelper.drawGradientHandle(d->handles[handleId]);
0096 }
0097 
0098 void KoParameterShape::setSize(const QSizeF &newSize)
0099 {
0100 
0101     QTransform matrix(resizeMatrix(newSize));
0102 
0103     for (int i = 0; i < d->handles.size(); ++i) {
0104         d->handles[i] = matrix.map(d->handles[i]);
0105     }
0106 
0107     KoPathShape::setSize(newSize);
0108 }
0109 
0110 QPointF KoParameterShape::normalize()
0111 {
0112 
0113     QPointF offset(KoPathShape::normalize());
0114     QTransform matrix;
0115     matrix.translate(-offset.x(), -offset.y());
0116 
0117     for (int i = 0; i < d->handles.size(); ++i) {
0118         d->handles[i] = matrix.map(d->handles[i]);
0119     }
0120 
0121     return offset;
0122 }
0123 
0124 bool KoParameterShape::isParametricShape() const
0125 {
0126 
0127     return d->parametric;
0128 }
0129 
0130 void KoParameterShape::setParametricShape(bool parametric)
0131 {
0132 
0133     d->parametric = parametric;
0134     update();
0135 }
0136 
0137 QList<QPointF> KoParameterShape::handles() const
0138 {
0139 
0140     return d->handles;
0141 }
0142 
0143 void KoParameterShape::setHandles(const QList<QPointF> &handles)
0144 {
0145 
0146     d->handles = handles;
0147 
0148     shapeChangedPriv(ParameterChanged);
0149 }
0150 
0151 int KoParameterShape::handleCount() const
0152 {
0153 
0154     return d->handles.count();
0155 }