kis_vectoranimated_brush.cpp 4.52 KB
Newer Older
1 2
/*
 *  Copyright (c) 2019 Alberto Eleuterio Flores Guerrero <barbanegra+bugs@posteo.mx>
3 4 5
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
6
 *  the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_vectoranimated_brush.h"

#include <QDomElement>
#include <QFileInfo>
#include <QPainter>
#include <QImageReader>
#include <QSvgRenderer>

27 28 29
#include <SvgParser.h>
#include <KoDocumentResourceManager.h>
#include <KoXmlReader.h>
30
#include <KoShapePainter.h>
31
#include <kis_brushes_pipe.h>
32

33 34
#include <kis_vectorbrush_parasite.h>

35 36
#include <QDebug>

37
KisVectorAnimatedBrush::KisVectorAnimatedBrush(const QString& filename)
38
    : KisScalingSizeBrush(filename)   
39
{
40
    setBrushType(PIPE_MASK);
41 42
    setSpacing(0.25);
    setHasColor(false);
43

44 45 46 47
}

KisVectorAnimatedBrush::KisVectorAnimatedBrush(const KisVectorAnimatedBrush& rhs)
    : KisScalingSizeBrush(rhs)
48
    , m_svg(rhs.m_svg)   
49 50 51
{
}

52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
KisBrush* KisVectorAnimatedBrush::clone() const
{
    return new KisVectorAnimatedBrush(*this);
}

bool KisVectorAnimatedBrush::load()
{
    QFile f(filename());
    if (f.size() == 0) return false;
    if (!f.exists()) return false;
    if (!f.open(QIODevice::ReadOnly)) {
        warnKrita << "Can't open file " << filename();
        return false;
    }
    bool res = loadFromDevice(&f);
    f.close();

    return res;
}

73 74
KisVectorBrushParasite KisVectorAnimatedBrush::m_parasite;

75 76 77 78
bool KisVectorAnimatedBrush::loadFromDevice(QIODevice *dev)
{
    m_svg = dev->readAll();

79 80 81
    m_parasite.parseSvg(m_svg);
    m_parasite.setIndex(0);
    setBrushTipImage(m_parasite.nextBrushTip());
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100


    setValid(true);

    // Well for now, always true
    if (brushTipImage().isGrayscale()) {
        setBrushType(MASK);
        setHasColor(false);
    }
    else {
        setBrushType(IMAGE);
        setHasColor(true);
    }
    setWidth(brushTipImage().width());
    setHeight(brushTipImage().height());

    QFileInfo fi(filename());
    setName(fi.baseName());

101
    return !brushTipImage().isNull() && valid(); 
102 103
}

104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
bool KisVectorAnimatedBrush::save()
{
    QFile f(filename());
    if (!f.open(QFile::WriteOnly)) return false;
    bool res = saveToDevice(&f);
    f.close();
    return res;
}

bool KisVectorAnimatedBrush::saveToDevice(QIODevice *dev) const
{
    if((dev->write(m_svg.constData(), m_svg.size()) == m_svg.size())) {
        KoResource::saveToDevice(dev);
        return true;
    }
    return false;
}

QString KisVectorAnimatedBrush::defaultFileExtension() const
{
    return QString(".avb");
}

void KisVectorAnimatedBrush::toXML(QDomDocument& d, QDomElement& e) const
{
    predefinedBrushToXML("svg_brush", e);
    KisBrush::toXML(d, e);
}
133 134


135 136 137 138 139 140
void KisVectorAnimatedBrush::prepareForSeqNo(const KisPaintInformation &info, int seqNo)
{
    Q_UNUSED(info)
    Q_UNUSED(seqNo)
    m_parasite.nextIndex();
    newbrushTip();
141 142 143
}


144
void KisVectorAnimatedBrush::newbrushTip()
145
{
146
    QImage image = m_parasite.nextBrushTip();
147

148 149
    setBrushTipImage(image);
    setValid(true);
150

151 152 153 154 155 156 157 158 159
    // Well for now, always true
    if (brushTipImage().isGrayscale()) {
        setBrushType(MASK);
        setHasColor(false);
    }
    else {
        setBrushType(IMAGE);
        setHasColor(true);
    }
160

161 162
    setWidth(brushTipImage().width());
    setHeight(brushTipImage().height());
163 164 165 166 167 168



}


169 170 171 172 173
/*
KisFixedPaintDeviceSP KisVectorAnimatedBrush::paintDevice(const KoColorSpace *colorSpace,
                                            const KisDabShape &,
                                            const KisPaintInformation &info,
                                            double subPixelX, double subPixelY) const
174
{
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        Q_ASSERT(valid());
        Q_UNUSED(info)
        Q_UNUSED(subPixelX)
        Q_UNUSED(subPixelY)
        qDebug() << m_imageNumber;
        nextIndex();
        m_imageNumber++;
        QImage outputImage = m_brushTip;
        KisFixedPaintDeviceSP dab = new KisFixedPaintDevice(colorSpace);
        Q_CHECK_PTR(dab);
        dab->convertFromQImage(outputImage, "");
        return dab;

    }

190 191 192


*/