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         // mask
00092         if (layer->hasMask()) { // We set this in KisDoc::loadPaintLayer
00093             KisPaintDeviceSP mask = layer->getMask();
00094             location = m_external ? QString::null : m_uri;
00095             location += m_img->name() + "/layers/" + m_layerFilenames[layer] + ".mask";
00096 
00097             // Layer data
00098             if (m_store->open(location)) {
00099                 if (!mask->read(m_store)) {
00100                     mask->disconnect();
00101                     m_store->close();
00102                     //IODone();
00103                     return false;
00104                 }
00105 
00106                 m_store->close();
00107             }
00108             layer->setDirty(); // Update the entire layer
00109         }
00110 
00111         return true;
00112 
00113     }
00114 
00115     virtual bool visit(KisGroupLayer *layer)
00116     {
00117         KisLoadVisitor visitor(m_img,m_store ,m_layerFilenames);
00118 
00119         if(m_external)
00120             visitor.setExternalUri(m_uri);
00121 
00122         KisLayerSP child = layer->firstChild();
00123 
00124         while(child)
00125         {
00126             child->accept(visitor);
00127             child = child->nextSibling();
00128         }
00129 
00130         layer->setDirty(m_img->bounds());
00131         return true;
00132     }
00133 
00134     virtual bool visit(KisPartLayer *)
00135     {
00136         return true;
00137     }
00138     
00139     virtual bool visit(KisAdjustmentLayer* layer)
00140     {
00141         //connect(*layer->paintDevice(), SIGNAL(ioProgress(Q_INT8)), m_img, SLOT(slotIOProgress(Q_INT8)));
00142 
00143         // The selection -- if present. If not, we simply cannot open the dratted thing.
00144         QString location = m_external ? QString::null : m_uri;
00145         location += m_img->name() + "/layers/" + m_layerFilenames[layer] + ".selection";
00146         if (m_store->hasFile(location)) {
00147             m_store->open(location);
00148             KisSelectionSP selection = new KisSelection();
00149             if (!selection->read(m_store)) {
00150                 selection->disconnect();
00151                 m_store->close();
00152             }
00153             else {
00154                 layer->setSelection( selection );
00155             }
00156             m_store->close();
00157         }
00158 
00159         // filter configuration
00160         location = m_external ? QString::null : m_uri;
00161         location += m_img->name() + "/layers/" + m_layerFilenames[layer] + ".filterconfig";
00162 
00163         if (m_store->hasFile(location) && layer->filter()) {
00164             QByteArray data;
00165             m_store->open(location);
00166             data = m_store->read(m_store->size());
00167             m_store->close();
00168             if (data) {
00169                 KisFilterConfiguration * kfc = layer->filter();
00170                 kfc->fromXML(QString(data));
00171             }
00172         }
00173 
00174         return true;
00175 
00176     }
00177     
00178 private:
00179     KisImageSP m_img;
00180     KoStore *m_store;
00181     bool m_external;
00182     QString m_uri;
00183     QMap<KisLayerSP, QString> m_layerFilenames;
00184 };
00185 
00186 #endif // KIS_LOAD_VISITOR_H_
00187 
KDE Home | KDE Accessibility Home | Description of Access Keys