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

0001 /* This file is part of the KDE project
0002 * SPDX-FileCopyrightText: 2010 Jan Hambrecht <jaham@gmx.net>
0003 *
0004 * SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "KoFilterEffectLoadingContext.h"
0008 
0009 #include <QString>
0010 #include <QRectF>
0011 #include <QFileInfo>
0012 #include <QDir>
0013 
0014 class Q_DECL_HIDDEN KoFilterEffectLoadingContext::Private
0015 {
0016 public:
0017     Private()
0018         : convertFilterUnits(false), convertFilterPrimitiveUnits(false)
0019     {}
0020     QString basePath;
0021     QRectF shapeBound;
0022     bool convertFilterUnits;
0023     bool convertFilterPrimitiveUnits;
0024 };
0025 
0026 KoFilterEffectLoadingContext::KoFilterEffectLoadingContext(const QString &basePath)
0027     : d(new Private())
0028 {
0029     d->basePath = basePath;
0030 }
0031 
0032 KoFilterEffectLoadingContext::~KoFilterEffectLoadingContext()
0033 {
0034     delete d;
0035 }
0036 
0037 void KoFilterEffectLoadingContext::setShapeBoundingBox(const QRectF &shapeBound)
0038 {
0039     d->shapeBound = shapeBound;
0040 }
0041 
0042 void KoFilterEffectLoadingContext::enableFilterUnitsConversion(bool enable)
0043 {
0044     d->convertFilterUnits = enable;
0045 }
0046 
0047 void KoFilterEffectLoadingContext::enableFilterPrimitiveUnitsConversion(bool enable)
0048 {
0049     d->convertFilterPrimitiveUnits = enable;
0050 }
0051 
0052 QPointF KoFilterEffectLoadingContext::convertFilterUnits(const QPointF &value) const
0053 {
0054     if (!d->convertFilterUnits)
0055         return value;
0056 
0057     return QPointF(convertFilterUnitsX(value.x()), convertFilterUnitsY(value.y()));
0058 }
0059 
0060 qreal KoFilterEffectLoadingContext::convertFilterUnitsX(qreal value) const
0061 {
0062     if (!d->convertFilterUnits)
0063         return value;
0064 
0065     return value / d->shapeBound.width();
0066 }
0067 
0068 qreal KoFilterEffectLoadingContext::convertFilterUnitsY(qreal value) const
0069 {
0070     if (!d->convertFilterUnits)
0071         return value;
0072 
0073     return value / d->shapeBound.height();
0074 }
0075 
0076 QPointF KoFilterEffectLoadingContext::convertFilterPrimitiveUnits(const QPointF &value) const
0077 {
0078     if (!d->convertFilterPrimitiveUnits)
0079         return value;
0080 
0081     return QPointF(convertFilterPrimitiveUnitsX(value.x()), convertFilterPrimitiveUnitsY(value.y()));
0082 }
0083 
0084 qreal KoFilterEffectLoadingContext::convertFilterPrimitiveUnitsX(qreal value) const
0085 {
0086     if (!d->convertFilterPrimitiveUnits)
0087         return value;
0088 
0089     return value / d->shapeBound.width();
0090 }
0091 
0092 qreal KoFilterEffectLoadingContext::convertFilterPrimitiveUnitsY(qreal value) const
0093 {
0094     if (!d->convertFilterPrimitiveUnits)
0095         return value;
0096 
0097     return value / d->shapeBound.height();
0098 }
0099 
0100 QString KoFilterEffectLoadingContext::pathFromHref(const QString &href) const
0101 {
0102     QFileInfo info(href);
0103     if (! info.isRelative())
0104         return href;
0105 
0106     QFileInfo pathInfo(QFileInfo(d->basePath).filePath());
0107 
0108     QString relFile = href;
0109     while (relFile.startsWith(QLatin1String("../"))) {
0110         relFile.remove(0, 3);
0111         pathInfo.setFile(pathInfo.dir(), QString());
0112     }
0113 
0114     QString absFile = pathInfo.absolutePath() + '/' + relFile;
0115 
0116     return absFile;
0117 }