File indexing completed on 2024-05-12 16:34:30
0001 /* This file is part of the KDE project 0002 * Copyright (c) 2009 Jan Hambrecht <jaham@gmx.net> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Lesser General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2.1 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Lesser General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "OffsetEffect.h" 0021 #include "KoFilterEffectRenderContext.h" 0022 #include "KoFilterEffectLoadingContext.h" 0023 #include "KoViewConverter.h" 0024 #include "KoXmlWriter.h" 0025 #include "KoXmlReader.h" 0026 #include <klocalizedstring.h> 0027 #include <QPainter> 0028 0029 OffsetEffect::OffsetEffect() 0030 : KoFilterEffect(OffsetEffectId, i18n("Offset")) 0031 , m_offset(0, 0) 0032 { 0033 } 0034 0035 QPointF OffsetEffect::offset() const 0036 { 0037 return m_offset; 0038 } 0039 0040 void OffsetEffect::setOffset(const QPointF &offset) 0041 { 0042 m_offset = offset; 0043 } 0044 0045 QImage OffsetEffect::processImage(const QImage &image, const KoFilterEffectRenderContext &context) const 0046 { 0047 if (m_offset.x() == 0.0 && m_offset.y() == 0.0) 0048 return image; 0049 0050 // transform from bounding box coordinates 0051 QPointF offset = context.toUserSpace(m_offset); 0052 // transform to view coordinates 0053 offset = context.viewConverter()->documentToView(offset); 0054 0055 QImage result(image.size(), image.format()); 0056 result.fill(qRgba(0, 0, 0, 0)); 0057 0058 QPainter p(&result); 0059 p.drawImage(context.filterRegion().topLeft() + offset, image, context.filterRegion()); 0060 return result; 0061 } 0062 0063 bool OffsetEffect::load(const KoXmlElement &element, const KoFilterEffectLoadingContext &context) 0064 { 0065 if (element.tagName() != id()) 0066 return false; 0067 0068 if (element.hasAttribute("dx")) 0069 m_offset.rx() = element.attribute("dx").toDouble(); 0070 if (element.hasAttribute("dy")) 0071 m_offset.ry() = element.attribute("dy").toDouble(); 0072 0073 m_offset = context.convertFilterPrimitiveUnits(m_offset); 0074 0075 return true; 0076 } 0077 0078 void OffsetEffect::save(KoXmlWriter &writer) 0079 { 0080 writer.startElement(OffsetEffectId); 0081 0082 saveCommonAttributes(writer); 0083 0084 if (m_offset.x() != 0.0) 0085 writer.addAttribute("dx", m_offset.x()); 0086 if (m_offset.y() != 0.0) 0087 writer.addAttribute("dy", m_offset.x()); 0088 0089 writer.endElement(); 0090 }