Warning, file /office/calligra/libs/flake/KoFilterEffect.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (c) 2009 Cyrille Berger <cberger@cberger.net>
0003  * Copyright (c) 2009 Jan Hambrecht <jaham@gmx.net>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (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 GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KoFilterEffect.h"
0022 #include "KoXmlWriter.h"
0023 
0024 #include <QImage>
0025 #include <QString>
0026 #include <QRectF>
0027 
0028 class Q_DECL_HIDDEN KoFilterEffect::Private
0029 {
0030 public:
0031     Private()
0032         : filterRect(0, 0, 1, 1)
0033         , requiredInputCount(1), maximalInputCount(1)
0034     {
0035         // add the default input
0036         inputs.append(QString());
0037     }
0038 
0039     QString id;
0040     QString name;
0041     QRectF filterRect;
0042     QList<QString> inputs;
0043     QString output;
0044     int requiredInputCount;
0045     int maximalInputCount;
0046 };
0047 
0048 KoFilterEffect::KoFilterEffect(const QString &id, const QString &name)
0049     : d(new Private)
0050 {
0051     d->id = id;
0052     d->name = name;
0053 }
0054 
0055 KoFilterEffect::~KoFilterEffect()
0056 {
0057     delete d;
0058 }
0059 
0060 QString KoFilterEffect::name() const
0061 {
0062     return d->name;
0063 }
0064 
0065 QString KoFilterEffect::id() const
0066 {
0067     return d->id;
0068 }
0069 
0070 void KoFilterEffect::setFilterRect(const QRectF &filterRect)
0071 {
0072     d->filterRect = filterRect;
0073 }
0074 
0075 QRectF KoFilterEffect::filterRect() const
0076 {
0077     return d->filterRect;
0078 }
0079 
0080 QRectF KoFilterEffect::filterRectForBoundingRect(const QRectF &boundingRect) const
0081 {
0082     qreal x = boundingRect.x() + d->filterRect.x() * boundingRect.width();
0083     qreal y = boundingRect.y() + d->filterRect.y() * boundingRect.height();
0084     qreal w = d->filterRect.width() * boundingRect.width();
0085     qreal h = d->filterRect.height() * boundingRect.height();
0086     return QRectF(x, y, w, h);
0087 }
0088 
0089 QList<QString> KoFilterEffect::inputs() const
0090 {
0091     return d->inputs;
0092 }
0093 
0094 void KoFilterEffect::addInput(const QString &input)
0095 {
0096     if (d->inputs.count() < d->maximalInputCount)
0097         d->inputs.append(input);
0098 }
0099 
0100 void KoFilterEffect::insertInput(int index, const QString &input)
0101 {
0102     if (d->inputs.count() < d->maximalInputCount)
0103         d->inputs.insert(index, input);
0104 }
0105 
0106 void KoFilterEffect::setInput(int index, const QString &input)
0107 {
0108     if (index < d->inputs.count())
0109         d->inputs[index] = input;
0110 }
0111 
0112 void KoFilterEffect::removeInput(int index)
0113 {
0114     if (d->inputs.count() > d->requiredInputCount)
0115         d->inputs.removeAt(index);
0116 }
0117 
0118 void KoFilterEffect::setOutput(const QString &output)
0119 {
0120     d->output = output;
0121 }
0122 
0123 QString KoFilterEffect::output() const
0124 {
0125     return d->output;
0126 }
0127 
0128 int KoFilterEffect::requiredInputCount() const
0129 {
0130     return d->requiredInputCount;
0131 }
0132 
0133 int KoFilterEffect::maximalInputCount() const
0134 {
0135     return qMax(d->maximalInputCount, d->requiredInputCount);
0136 }
0137 
0138 QImage KoFilterEffect::processImage(const QImage &image, const KoFilterEffectRenderContext &/*context*/) const
0139 {
0140     return image;
0141 }
0142 
0143 QImage KoFilterEffect::processImages(const QVector<QImage> &images, const KoFilterEffectRenderContext &/*context*/) const
0144 {
0145     Q_ASSERT(images.count());
0146     return images.first();
0147 }
0148 
0149 bool KoFilterEffect::load(const KoXmlElement &/*element*/, const KoFilterEffectLoadingContext &/*context*/)
0150 {
0151     return true;
0152 }
0153 
0154 void KoFilterEffect::save(KoXmlWriter &/*writer*/)
0155 {
0156 }
0157 
0158 void KoFilterEffect::setRequiredInputCount(int count)
0159 {
0160     d->requiredInputCount = qMax(0, count);
0161     for (int i = d->inputs.count(); i < d->requiredInputCount; ++i)
0162         d->inputs.append(QString());
0163 }
0164 
0165 void KoFilterEffect::setMaximalInputCount(int count)
0166 {
0167     d->maximalInputCount = qMax(0,count);
0168     if (d->inputs.count() > maximalInputCount()) {
0169         int removeCount = d->inputs.count()-maximalInputCount();
0170         for (int i = 0; i < removeCount; ++i)
0171             d->inputs.pop_back();
0172     }
0173 }
0174 
0175 void KoFilterEffect::saveCommonAttributes(KoXmlWriter &writer)
0176 {
0177     writer.addAttribute("result", output());
0178     if (requiredInputCount() == 1 && maximalInputCount() == 1 && d->inputs.count() == 1) {
0179         writer.addAttribute("in", d->inputs[0]);
0180     }
0181     writer.addAttribute("x", d->filterRect.x());
0182     writer.addAttribute("y", d->filterRect.y());
0183     writer.addAttribute("width", d->filterRect.width());
0184     writer.addAttribute("height", d->filterRect.height());
0185 }