krita

kis_import_catcher.cc

00001 /*
00002  *  Copyright (c) 2006 Boudewijn Rempt  <boud@valdyas.org>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 #include <kdebug.h>
00019 
00020 #include "kis_import_catcher.h"
00021 #include "kis_types.h"
00022 
00023 #include "kis_view.h"
00024 #include "kis_doc.h"
00025 #include "kis_image.h"
00026 #include "kis_layer.h"
00027 #include "kis_group_layer.h"
00028 
00029 KisImportCatcher::KisImportCatcher(KURL url, KisImageSP image)
00030     : QObject()
00031     , m_doc( new KisDoc() )
00032     , m_image( image )
00033     , m_url( url )
00034 {
00035     m_doc->openURL(url);
00036     if ( !m_doc->isLoading() ) {
00037         slotLoadingFinished();
00038     }
00039     else {
00040         connect(m_doc, SIGNAL(loadingFinished()), this, SLOT(slotLoadingFinished()));
00041     }
00042 }
00043 
00044 void KisImportCatcher::slotLoadingFinished()
00045 {
00046     KisImageSP importedImage = m_doc->currentImage();
00047 
00048     if (importedImage) {
00049         KisLayerSP importedImageLayer = importedImage->rootLayer().data();
00050 
00051         if (importedImageLayer != 0) {
00052 
00053             if (importedImageLayer->numLayers() == 2) {
00054                 // Don't import the root if this is not a layered image (1 group layer
00055                 // plus 1 other).
00056                 importedImageLayer = importedImageLayer->firstChild();
00057                 importedImageLayer->parent()->removeLayer(importedImageLayer);
00058             }
00059 
00060             importedImageLayer->setName(m_url.prettyURL());
00061 
00062             KisGroupLayerSP parent = 0;
00063             KisLayerSP currentActiveLayer = m_image->activeLayer();
00064 
00065             if (currentActiveLayer) {
00066                 parent = currentActiveLayer->parent();
00067             }
00068 
00069             if (parent == 0) {
00070                 parent = m_image->rootLayer();
00071             }
00072 
00073             m_image->addLayer(importedImageLayer.data(), parent, currentActiveLayer);
00074         }
00075     }
00076     m_doc->deleteLater();
00077     deleteLater();
00078 }
00079 
00080 
KDE Home | KDE Accessibility Home | Description of Access Keys