KisSaveGroupVisitor.cpp 4.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 *  Copyright (C) 2016 Boudewijn Rempt <boud@valdyas.org>
 *
 *  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
 *  the Free Software Foundation; either version 2 of the License, or
 *  (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 "KisSaveGroupVisitor.h"

#include <KisDocument.h>
#include <kis_painter.h>
#include <kis_paint_layer.h>
#include <KisPart.h>

KisSaveGroupVisitor::KisSaveGroupVisitor(KisImageWSP image,
                                         bool saveInvisible,
                                         bool saveTopLevelOnly,
28
                                         const QString &path,
29 30
                                         const QString &baseName,
                                         const QString &extension,
Boudewijn Rempt's avatar
Boudewijn Rempt committed
31
                                         const QString &mimeFilter)
32 33 34
    : m_image(image)
    , m_saveInvisible(saveInvisible)
    , m_saveTopLevelOnly(saveTopLevelOnly)
35
    , m_path(path)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    , m_baseName(baseName)
    , m_extension(extension)
    , m_mimeFilter(mimeFilter)
{
}

KisSaveGroupVisitor::~KisSaveGroupVisitor()
{
}

bool KisSaveGroupVisitor::visit(KisNode* ) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisPaintLayer *) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisAdjustmentLayer *) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisExternalLayer *) {
    return true;
}


bool KisSaveGroupVisitor::visit(KisCloneLayer *) {
    return true;
}


bool KisSaveGroupVisitor::visit(KisFilterMask *) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisTransformMask *) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisTransparencyMask *) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisGeneratorLayer * ) {
    return true;
}

bool KisSaveGroupVisitor::visit(KisSelectionMask* ) {
    return true;
}

88 89 90 91
bool KisSaveGroupVisitor::visit(KisColorizeMask* ) {
    return true;
}

92 93 94
bool KisSaveGroupVisitor::visit(KisGroupLayer *layer)
{
    if (layer == m_image->rootLayer()) {
95
        KisLayerSP child = qobject_cast<KisLayer*>(layer->firstChild().data());
96 97
        while (child) {
            child->accept(*this);
98
            child = qobject_cast<KisLayer*>(child->nextSibling().data());
99 100 101 102 103 104 105 106 107
        }

    }
    else if (layer->visible() || m_saveInvisible) {

        QRect r = m_image->bounds();

        KisDocument *exportDocument = KisPart::instance()->createDocument();

108
        KisImageSP dst = new KisImage(exportDocument->createUndoStore(), r.width(), r.height(), m_image->colorSpace(), layer->name());
109 110 111 112 113 114 115 116 117
        dst->setResolution(m_image->xRes(), m_image->yRes());
        exportDocument->setCurrentImage(dst);
        KisPaintLayer* paintLayer = new KisPaintLayer(dst, "projection", layer->opacity());
        KisPainter gc(paintLayer->paintDevice());
        gc.bitBlt(QPoint(0, 0), layer->projection(), r);
        dst->addNode(paintLayer, dst->rootLayer(), KisLayerSP(0));

        dst->refreshGraph();

118

119

120 121
        QString path = m_path + "/" + m_baseName + "_" + layer->name().replace(' ', '_') + '.' + m_extension;
        QUrl url = QUrl::fromLocalFile(path);
122 123 124

        exportDocument->setFileBatchMode(true);
        exportDocument->exportDocument(url, m_mimeFilter.toLatin1());
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

        if (!m_saveTopLevelOnly) {
            KisGroupLayerSP child = dynamic_cast<KisGroupLayer*>(layer->firstChild().data());
            while (child) {
                child->accept(*this);
                child = dynamic_cast<KisGroupLayer*>(child->nextSibling().data());
            }
        }
        delete exportDocument;
    }

    return true;
}