kis_import_catcher.cc 4.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  Copyright (c) 2006 Boudewijn Rempt  <boud@valdyas.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kis_import_catcher.h"
Halla Rempt's avatar
Halla Rempt committed
20 21
#include <kis_debug.h>

Halla Rempt's avatar
Halla Rempt committed
22 23 24 25 26 27 28 29 30 31 32 33
#include <kimageio.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <kmimetype.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kio/netaccess.h>
#include <kio/job.h>

#include <KoFilterManager.h>

#include "kis_node_manager.h"
34
#include "kis_types.h"
Halla Rempt's avatar
Halla Rempt committed
35
#include "kis_count_visitor.h"
36 37
#include "kis_view2.h"
#include "kis_doc2.h"
38 39
#include "kis_image.h"
#include "kis_layer.h"
40
#include "kis_painter.h"
Halla Rempt's avatar
Halla Rempt committed
41 42
#include "kis_selection.h"
#include "kis_node_commands_adapter.h"
43
#include "kis_group_layer.h"
Halla Rempt's avatar
Halla Rempt committed
44 45 46
#include "kis_statusbar.h"
#include "kis_progress_widget.h"

Halla Rempt's avatar
Halla Rempt committed
47
#include <QMessageBox>
Halla Rempt's avatar
Halla Rempt committed
48

49
struct KisImportCatcher::Private
Halla Rempt's avatar
Halla Rempt committed
50
{
Halla Rempt's avatar
Halla Rempt committed
51 52 53 54
public:
    KisDoc2* doc;
    KisView2* view;
    KUrl url;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
55
    bool importAsLayer;
56

57
    QString prettyLayerName() const;
58 59
    void importAsPaintLayer(KisPaintDeviceSP device);
    void importAsTransparencyMask(KisPaintDeviceSP device);
Halla Rempt's avatar
Halla Rempt committed
60 61
};

62 63 64 65 66 67
QString KisImportCatcher::Private::prettyLayerName() const
{
    QString name = url.fileName();
    return !name.isEmpty() ? name : url.prettyUrl();
}

68 69 70 71
void KisImportCatcher::Private::importAsPaintLayer(KisPaintDeviceSP device)
{
    KisLayerSP newLayer =
        new KisPaintLayer(view->image(),
72
                          prettyLayerName(),
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
                          OPACITY_OPAQUE_U8,
                          device);

    KisNodeSP parent = 0;
    KisLayerSP currentActiveLayer = view->activeLayer();

    if (currentActiveLayer) {
        parent = currentActiveLayer->parent();
    }

    if (parent.isNull()) {
        parent = view->image()->rootLayer();
    }

    KisNodeCommandsAdapter adapter(view);
    adapter.addNode(newLayer, parent, currentActiveLayer);
}

91
// NOTE: Unused currently
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
void KisImportCatcher::Private::importAsTransparencyMask(KisPaintDeviceSP device)
{
    KisLayerSP currentActiveLayer = view->activeLayer();

    if (!currentActiveLayer) {
        KisNodeSP node = view->activeNode();
        if (!node) return;

        do {
            currentActiveLayer = dynamic_cast<KisLayer*>(node.data());
        } while (!currentActiveLayer && (node = node->parent()));

        if (!currentActiveLayer) return;
    }

    KisTransparencyMaskSP mask = new KisTransparencyMask();
    mask->setSelection(new KisSelection(new KisDefaultBounds(currentActiveLayer->image())));
109
    mask->setName(prettyLayerName());
110 111 112 113 114 115 116 117 118 119 120 121

    QRect rc(device->exactBounds());
    KisPainter painter(mask->paintDevice());
    painter.bitBlt(rc.topLeft(), device, rc);

    KisNodeCommandsAdapter adapter(view);
    adapter.addNode(mask,
                    currentActiveLayer,
                    currentActiveLayer->lastChild());
}

KisImportCatcher::KisImportCatcher(const KUrl & url, KisView2 * view, bool importAsLayer)
Halla Rempt's avatar
Halla Rempt committed
122
        : m_d(new Private)
123
{
124
    m_d->doc = new KisDoc2();
125

Halla Rempt's avatar
Halla Rempt committed
126 127
    KoProgressProxy *progressProxy = view->statusBar()->progress()->progressProxy();
    m_d->doc->setProgressProxy(progressProxy);
Halla Rempt's avatar
Halla Rempt committed
128 129
    m_d->view = view;
    m_d->url = url;
Dmitry Kazakov's avatar
Dmitry Kazakov committed
130 131 132
    m_d->importAsLayer = importAsLayer;
    connect(m_d->doc, SIGNAL(sigLoadingFinished()), this, SLOT(slotLoadingFinished()));
    bool result = m_d->doc->openUrl(url);
Halla Rempt's avatar
Halla Rempt committed
133

Dmitry Kazakov's avatar
Dmitry Kazakov committed
134 135 136 137 138 139 140
    if (!result) {
        deleteMyself();
    }
}

void KisImportCatcher::slotLoadingFinished()
{
141
    KisImageWSP importedImage = m_d->doc->image();
Dmitry Kazakov's avatar
Dmitry Kazakov committed
142
    importedImage->waitForDone();
143

144
    if (importedImage && importedImage->projection()->exactBounds().isValid()) {
Dmitry Kazakov's avatar
Dmitry Kazakov committed
145
        if (m_d->importAsLayer) {
146 147 148
            m_d->importAsPaintLayer(importedImage->projection());
        } else {
            m_d->importAsTransparencyMask(importedImage->projection());
149 150
        }
    }
Halla Rempt's avatar
Halla Rempt committed
151

Dmitry Kazakov's avatar
Dmitry Kazakov committed
152 153 154 155 156
    deleteMyself();
}

void KisImportCatcher::deleteMyself()
{
Halla Rempt's avatar
Halla Rempt committed
157
    m_d->doc->deleteLater();
158 159 160
    deleteLater();
}

161 162 163 164 165
KisImportCatcher::~KisImportCatcher()
{
    delete m_d;
}

166
#include "kis_import_catcher.moc"