chalk

kis_import_catcher.cpp
1/*
2 * Copyright (c) 2006 Boudewijn Rempt <boud@valdyas.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18#include <kdebug.h>
19
20#include "kis_import_catcher.h"
21#include "kis_types.h"
22
23#include "kis_view.h"
24#include "kis_doc.h"
25#include "kis_image.h"
26#include "kis_layer.h"
27#include "kis_group_layer.h"
28
29KisImportCatcher::KisImportCatcher(KURL url, KisImageSP image)
30 : TQObject()
31 , m_doc( new KisDoc() )
32 , m_image( image )
33 , m_url( url )
34{
35 m_doc->openURL(url);
36 if ( !m_doc->isLoading() ) {
37 slotLoadingFinished();
38 }
39 else {
40 connect(m_doc, TQ_SIGNAL(loadingFinished()), this, TQ_SLOT(slotLoadingFinished()));
41 }
42}
43
44void KisImportCatcher::slotLoadingFinished()
45{
46 KisImageSP importedImage = m_doc->currentImage();
47
48 if (importedImage) {
49 KisLayerSP importedImageLayer = importedImage->rootLayer().data();
50
51 if (importedImageLayer != 0) {
52
53 if (importedImageLayer->numLayers() == 2) {
54 // Don't import the root if this is not a layered image (1 group layer
55 // plus 1 other).
56 importedImageLayer = importedImageLayer->firstChild();
57 importedImageLayer->parent()->removeLayer(importedImageLayer);
58 }
59
60 importedImageLayer->setName(m_url.prettyURL());
61
62 KisGroupLayerSP parent = 0;
63 KisLayerSP currentActiveLayer = m_image->activeLayer();
64
65 if (currentActiveLayer) {
66 parent = currentActiveLayer->parent();
67 }
68
69 if (parent == 0) {
70 parent = m_image->rootLayer();
71 }
72
73 m_image->addLayer(importedImageLayer.data(), parent, currentActiveLayer);
74 }
75 }
76 m_doc->deleteLater();
77 deleteLater();
78}
79
80