kis_bmp_import.cpp 3.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/*
 *  Copyright (c) 2007 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_bmp_import.h"

#include <QCheckBox>
#include <QSlider>

#include <kio/netaccess.h>
#include <kio/deletejob.h>

#include <kapplication.h>
#include <kdialog.h>
#include <kpluginfactory.h>

#include <KoColorSpace.h>
#include <KoFilterChain.h>
#include <KoColorSpaceRegistry.h>

#include <kis_transaction.h>
#include <kis_paint_device.h>
#include <kis_doc2.h>
#include <kis_image.h>
#include <kis_paint_layer.h>
#include <kis_node.h>
#include <kis_group_layer.h>


K_PLUGIN_FACTORY(KisBMPImportFactory, registerPlugin<KisBMPImport>();)
Halla Rempt's avatar
Halla Rempt committed
46
K_EXPORT_PLUGIN(KisBMPImportFactory("calligrafilters"))
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

KisBMPImport::KisBMPImport(QObject *parent, const QVariantList &) : KoFilter(parent)
{
}

KisBMPImport::~KisBMPImport()
{
}

bool hasVisibleWidgets()
{
    QWidgetList wl = QApplication::allWidgets();
    foreach(QWidget* w, wl) {
        if (w->isVisible()) return true;
    }
    return false;
}

KoFilter::ConversionStatus KisBMPImport::convert(const QByteArray& from, const QByteArray& to)
{
67
    dbgFile << "BMP import! From:" << from << ", To:" << to << 0;
68

69
    if (!(from == "image/bmp" || from == "image/x-xpixmap" || from == "image/gif" || from == "image/x-xbitmap"))
70 71 72 73 74 75 76 77
        return KoFilter::NotImplemented;

    if (to != "application/x-krita")
        return KoFilter::BadMimeType;

        KisDoc2 * doc = dynamic_cast<KisDoc2*>(m_chain -> outputDocument());

    if (!doc)
78
        return KoFilter::NoDocumentCreated;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    QString filename = m_chain -> inputFile();

    doc->prepareForImport();

    if (!filename.isEmpty()) {
        KUrl url(filename);

        if (url.isEmpty())
            return KoFilter::FileNotFound;

        if (!KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, qApp -> activeWindow())) {
            return KoFilter::FileNotFound;
        }


        QString localFile = url.toLocalFile();
        QImage img(localFile);

        const KoColorSpace *colorSpace = KoColorSpaceRegistry::instance()->rgb8();
Dmitry Kazakov's avatar
Dmitry Kazakov committed
99
        KisImageSP image = new KisImage(doc->createUndoStore(), img.width(), img.height(), colorSpace, "imported from bmp");
100 101

        KisPaintLayerSP layer = new KisPaintLayer(image, image->nextLayerName(), 255);
102 103
        KisTransaction(0, layer->paintDevice());
        layer->paintDevice()->convertFromQImage(img, 0, 0, 0);
104 105 106 107 108 109 110 111 112 113 114
        image->addNode(layer.data(), image->rootLayer().data());

        doc->setCurrentImage(image);
        return KoFilter::OK;
    }
    return KoFilter::StorageCreationError;

}

#include "kis_bmp_import.moc"