File indexing completed on 2024-05-12 16:34:29
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 "FloodEffect.h" 0021 #include "KoFilterEffectRenderContext.h" 0022 #include "KoViewConverter.h" 0023 #include "KoXmlWriter.h" 0024 #include "KoXmlReader.h" 0025 #include <klocalizedstring.h> 0026 #include <QPainter> 0027 0028 FloodEffect::FloodEffect() 0029 : KoFilterEffect(FloodEffectId, i18n("Flood fill")) 0030 , m_color(Qt::black) 0031 { 0032 } 0033 0034 QColor FloodEffect::floodColor() const 0035 { 0036 return m_color; 0037 } 0038 0039 void FloodEffect::setFloodColor(const QColor &color) 0040 { 0041 m_color = color; 0042 } 0043 0044 QImage FloodEffect::processImage(const QImage &image, const KoFilterEffectRenderContext &context) const 0045 { 0046 QImage result = image; 0047 QPainter painter(&result); 0048 painter.fillRect(context.filterRegion(), m_color); 0049 0050 return result; 0051 } 0052 0053 bool FloodEffect::load(const KoXmlElement &element, const KoFilterEffectLoadingContext &) 0054 { 0055 if (element.tagName() != id()) 0056 return false; 0057 0058 m_color = Qt::black; 0059 0060 if (element.hasAttribute("flood-color")) { 0061 QString colorStr = element.attribute("flood-color").trimmed(); 0062 if (colorStr.startsWith(QLatin1String("rgb("))) { 0063 QStringList channels = colorStr.mid(4, colorStr.length() - 5).split(','); 0064 float r = channels[0].toDouble(); 0065 if (channels[0].contains('%')) 0066 r /= 100.0; 0067 else 0068 r /= 255.0; 0069 float g = channels[1].toDouble(); 0070 if (channels[1].contains('%')) 0071 g /= 100.0; 0072 else 0073 g /= 255.0; 0074 float b = channels[2].toDouble(); 0075 if (channels[2].contains('%')) 0076 b /= 100.0; 0077 else 0078 b /= 255.0; 0079 m_color.setRgbF(r, g, b); 0080 0081 } else { 0082 m_color.setNamedColor(colorStr); 0083 } 0084 // TODO: add support for currentColor 0085 } 0086 0087 if (element.hasAttribute("flood-opacity")) 0088 m_color.setAlphaF(element.attribute("flood-opacity").toDouble()); 0089 0090 return true; 0091 } 0092 0093 void FloodEffect::save(KoXmlWriter &writer) 0094 { 0095 writer.startElement(FloodEffectId); 0096 0097 saveCommonAttributes(writer); 0098 0099 writer.addAttribute("flood-color", m_color.name()); 0100 if (m_color.alpha() < 255) 0101 writer.addAttribute("flood-opacity", QString("%1").arg(m_color.alphaF())); 0102 0103 writer.endElement(); 0104 }