File indexing completed on 2024-05-12 16:01:54

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #include "KisSaveGroupVisitor.h"
0007 
0008 #include <KisDocument.h>
0009 #include <kis_painter.h>
0010 #include <kis_paint_layer.h>
0011 #include <KisPart.h>
0012 
0013 KisSaveGroupVisitor::KisSaveGroupVisitor(KisImageWSP image,
0014                                          bool saveInvisible,
0015                                          bool saveTopLevelOnly,
0016                                          const QString &path,
0017                                          const QString &baseName,
0018                                          const QString &extension,
0019                                          const QString &mimeFilter)
0020     : m_image(image)
0021     , m_saveInvisible(saveInvisible)
0022     , m_saveTopLevelOnly(saveTopLevelOnly)
0023     , m_path(path)
0024     , m_baseName(baseName)
0025     , m_extension(extension)
0026     , m_mimeFilter(mimeFilter)
0027 {
0028 }
0029 
0030 KisSaveGroupVisitor::~KisSaveGroupVisitor()
0031 {
0032 }
0033 
0034 bool KisSaveGroupVisitor::visit(KisNode* ) {
0035     return true;
0036 }
0037 
0038 bool KisSaveGroupVisitor::visit(KisPaintLayer *) {
0039     return true;
0040 }
0041 
0042 bool KisSaveGroupVisitor::visit(KisAdjustmentLayer *) {
0043     return true;
0044 }
0045 
0046 bool KisSaveGroupVisitor::visit(KisExternalLayer *) {
0047     return true;
0048 }
0049 
0050 
0051 bool KisSaveGroupVisitor::visit(KisCloneLayer *) {
0052     return true;
0053 }
0054 
0055 
0056 bool KisSaveGroupVisitor::visit(KisFilterMask *) {
0057     return true;
0058 }
0059 
0060 bool KisSaveGroupVisitor::visit(KisTransformMask *) {
0061     return true;
0062 }
0063 
0064 bool KisSaveGroupVisitor::visit(KisTransparencyMask *) {
0065     return true;
0066 }
0067 
0068 bool KisSaveGroupVisitor::visit(KisGeneratorLayer * ) {
0069     return true;
0070 }
0071 
0072 bool KisSaveGroupVisitor::visit(KisSelectionMask* ) {
0073     return true;
0074 }
0075 
0076 bool KisSaveGroupVisitor::visit(KisColorizeMask* ) {
0077     return true;
0078 }
0079 
0080 bool KisSaveGroupVisitor::visit(KisGroupLayer *layer)
0081 {
0082     if (layer == m_image->rootLayer()) {
0083         KisLayerSP child = qobject_cast<KisLayer*>(layer->firstChild().data());
0084         while (child) {
0085             child->accept(*this);
0086             child = qobject_cast<KisLayer*>(child->nextSibling().data());
0087         }
0088 
0089     }
0090     else if (layer->visible() || m_saveInvisible) {
0091 
0092         QRect r = m_image->bounds();
0093 
0094         KisDocument *exportDocument = KisPart::instance()->createDocument();
0095         { // make sure dst is deleted before calling 'delete exportDocument',
0096           // since KisDocument checks that its image is properly deref()'d.
0097           KisImageSP dst = new KisImage(exportDocument->createUndoStore(), r.width(), r.height(), m_image->colorSpace(), layer->name());
0098           dst->setResolution(m_image->xRes(), m_image->yRes());
0099           exportDocument->setCurrentImage(dst);
0100           KisPaintLayer* paintLayer = new KisPaintLayer(dst, "projection", layer->opacity());
0101           KisPainter gc(paintLayer->paintDevice());
0102           gc.bitBlt(QPoint(0, 0), layer->projection(), r);
0103           dst->addNode(paintLayer, dst->rootLayer(), KisLayerSP(0));
0104 
0105           dst->refreshGraph();
0106         }
0107 
0108         QString path = m_path + "/" + m_baseName + "_" + layer->name().replace(' ', '_') + '.' + m_extension;
0109 
0110         exportDocument->setFileBatchMode(true);
0111         exportDocument->exportDocumentSync(path, m_mimeFilter.toLatin1());
0112 
0113         if (!m_saveTopLevelOnly) {
0114             KisGroupLayerSP child = dynamic_cast<KisGroupLayer*>(layer->firstChild().data());
0115             while (child) {
0116                 child->accept(*this);
0117                 child = dynamic_cast<KisGroupLayer*>(child->nextSibling().data());
0118             }
0119         }
0120         delete exportDocument;
0121     }
0122 
0123     return true;
0124 }
0125 
0126