krita

kis_load_visitor.h

00001 /*
00002  *  Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
00003  *  Copyright (c) 2005 Casper Boemann <cbr@boemann.dk>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 #ifndef KIS_LOAD_VISITOR_H_
00020 #define KIS_LOAD_VISITOR_H_
00021 
00022 #include <qrect.h>
00023 #include "kis_types.h"
00024 #include "kis_layer_visitor.h"
00025 #include "kis_image.h"
00026 #include "kis_selection.h"
00027 #include "kis_layer.h"
00028 #include "kis_paint_layer.h"
00029 #include "kis_group_layer.h"
00030 #include "kis_adjustment_layer.h"
00031 #include "kis_filter_configuration.h"
00032 
00033 #include "kis_datamanager.h"
00034 
00035 class KisLoadVisitor : public KisLayerVisitor {
00036 public:
00037     KisLoadVisitor(KisImageSP img, KoStore *store, QMap<KisLayerSP, QString> &layerFilenames) :
00038         KisLayerVisitor(),
00039         m_layerFilenames(layerFilenames)
00040     {
00041         m_external = false;
00042         m_img = img;
00043         m_store = store;
00044     }
00045 
00046 public:
00047     void setExternalUri(QString &uri)
00048     {
00049         m_external = true;
00050         m_uri = uri;
00051     }
00052 
00053     virtual bool visit(KisPaintLayer *layer)
00054     {        //connect(*layer->paintDevice(), SIGNAL(ioProgress(Q_INT8)), m_img, SLOT(slotIOProgress(Q_INT8)));
00055 
00056         QString location = m_external ? QString::null : m_uri;
00057         location += m_img->name() + "/layers/" + m_layerFilenames[layer];
00058 
00059         // Layer data
00060         if (m_store->open(location)) {
00061             if (!layer->paintDevice()->read(m_store)) {
00062                 layer->paintDevice()->disconnect();
00063                 m_store->close();
00064                 //IODone();
00065                 return false;
00066             }
00067 
00068             m_store->close();
00069         }
00070 
00071         // icc profile
00072         location = m_external ? QString::null : m_uri;
00073         location += m_img->name() + "/layers/" + m_layerFilenames[layer] + ".icc";
00074 
00075         if (m_store->hasFile(location)) {
00076             QByteArray data;
00077             m_store->open(location);
00078             data = m_store->read(m_store->size());
00079             m_store->close();
00080             // Create a colorspace with the embedded profile
00081             KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getColorSpace(layer->paintDevice()->colorSpace()->id(),
00082                                                                                             new KisProfile(data));
00083             // replace the old colorspace
00084             layer->paintDevice()->setData(layer->paintDevice()->dataManager(), cs);
00085             QRect rc = layer->paintDevice()->extent();
00086             kdDebug() << "After loading " << layer->name() << " extent is: " << rc.x() << ", " << rc.y() << ", " << rc.width() << ", " << rc.height() << endl;
00087             layer->setDirty(rc);
00088             kdDebug(DBG_AREA_FILE) << "Opened icc information, size is " << data.size() << endl;
00089         }
00090 
00091         return true;
00092 
00093     }
00094 
00095     virtual bool visit(KisGroupLayer *layer)
00096     {
00097         KisLoadVisitor visitor(m_img,m_store ,m_layerFilenames);
00098 
00099         if(m_external)
00100             visitor.setExternalUri(m_uri);
00101 
00102         KisLayerSP child = layer->firstChild();
00103 
00104         while(child)
00105         {
00106             child->accept(visitor);
00107             child = child->nextSibling();
00108         }
00109 
00110         layer->setDirty(m_img->bounds());
00111         return true;
00112     }
00113 
00114     virtual bool visit(KisPartLayer *)
00115     {
00116         return true;
00117     }
00118     
00119     virtual bool visit(KisAdjustmentLayer* layer)
00120     {
00121         //connect(*layer->paintDevice(), SIGNAL(ioProgress(Q_INT8)), m_img, SLOT(slotIOProgress(Q_INT8)));
00122 
00123         // The selection -- if present. If not, we simply cannot open the dratted thing.
00124         QString location = m_external ? QString::null : m_uri;
00125         location += m_img->name() + "/layers/" + m_layerFilenames[layer] + ".selection";
00126         if (m_store->hasFile(location)) {
00127             m_store->open(location);
00128             KisSelectionSP selection = new KisSelection();
00129             if (!selection->read(m_store)) {
00130                 selection->disconnect();
00131                 m_store->close();
00132             }
00133             else {
00134                 layer->setSelection( selection );
00135             }
00136             m_store->close();
00137         }
00138 
00139         // filter configuration
00140         location = m_external ? QString::null : m_uri;
00141         location += m_img->name() + "/layers/" + m_layerFilenames[layer] + ".filterconfig";
00142 
00143         if (m_store->hasFile(location) && layer->filter()) {
00144             QByteArray data;
00145             m_store->open(location);
00146             data = m_store->read(m_store->size());
00147             m_store->close();
00148             if (data) {
00149                 KisFilterConfiguration * kfc = layer->filter();
00150                 kfc->fromXML(QString(data));
00151             }
00152         }
00153 
00154         return true;
00155 
00156     }
00157     
00158 private:
00159     KisImageSP m_img;
00160     KoStore *m_store;
00161     bool m_external;
00162     QString m_uri;
00163     QMap<KisLayerSP, QString> m_layerFilenames;
00164 };
00165 
00166 #endif // KIS_LOAD_VISITOR_H_
00167 
KDE Home | KDE Accessibility Home | Description of Access Keys