filters

kis_pdf_import.cpp

00001 /*
00002  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
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,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kis_pdf_import.h"
00021 
00022 // poppler's headers
00023 #include <poppler-qt.h>
00024 
00025 // Qt's headers
00026 #include <qfile.h>
00027 #include <qimage.h> // TODO that too
00028 #include <qradiobutton.h>
00029 
00030 // KDE's headers
00031 #include <kapplication.h>
00032 #include <kdebug.h>
00033 #include <kdialogbase.h>
00034 #include <kgenericfactory.h>
00035 #include <knuminput.h>
00036 #include <kpassdlg.h>
00037 
00038 #include <kio/netaccess.h>
00039 
00040 // koffice's headers
00041 #include <KoFilterChain.h>
00042 
00043 // krita's headers
00044 #include <kis_doc.h>
00045 #include <kis_colorspace.h>
00046 #include <kis_colorspace_factory_registry.h>
00047 #include <kis_group_layer.h>
00048 #include <kis_image.h>
00049 #include <kis_meta_registry.h>
00050 #include <kis_paint_layer.h>
00051 
00052 // plugins's headers
00053 #include "kis_pdf_import_widget.h"
00054 
00055 typedef KGenericFactory<KisPDFImport, KoFilter> PDFImportFactory;
00056 K_EXPORT_COMPONENT_FACTORY(libkritapdfimport, PDFImportFactory("kofficefilters"))
00057 
00058 KisPDFImport::KisPDFImport(KoFilter *, const char *, const QStringList&) : KoFilter()
00059 {
00060 }
00061 
00062 KisPDFImport::~KisPDFImport()
00063 {
00064 }
00065 
00066 KisPDFImport::ConversionStatus KisPDFImport::convert(const QCString& , const QCString& )
00067 {
00068     QString filename = m_chain -> inputFile();
00069     kdDebug(41008) << "Importing using PDFImport!" << filename << endl;
00070     
00071     if (filename.isEmpty())
00072         return KoFilter::FileNotFound;
00073     
00074     
00075     KURL url;
00076     url.setPath(filename);
00077 
00078     if (!KIO::NetAccess::exists(url, false, qApp -> mainWidget())) {
00079         return KoFilter::FileNotFound;
00080     }
00081 
00082     // We're not set up to handle asynchronous loading at the moment.
00083     QString tmpFile;
00084     if (KIO::NetAccess::download(url, tmpFile, qApp -> mainWidget())) {
00085         url.setPath( tmpFile );
00086     }
00087     
00088     Poppler::Document* pdoc = Poppler::Document::load( QFile::encodeName(url.path() ) );
00089     
00090 
00091     if ( !pdoc)
00092     {
00093         kdDebug(41008) << "Error when reading the PDF" << endl;
00094         return KoFilter::StorageCreationError;
00095     }
00096 
00097 
00098     while( pdoc->isLocked() )
00099     {
00100         QCString password;
00101         int result = KPasswordDialog::getPassword(password, i18n("A password is required to read that pdf"));
00102         if (result == KPasswordDialog::Accepted)
00103         {
00104             pdoc->unlock(password);
00105         } else {
00106             kdDebug(41008) << "Password canceled" << endl;
00107             return KoFilter::StorageCreationError;
00108         }
00109     }
00110 
00111     KDialogBase* kdb = new KDialogBase(0, "", false, i18n("PDF Import Options"), KDialogBase::Ok | KDialogBase::Cancel);
00112     
00113     KisPDFImportWidget* wdg = new KisPDFImportWidget(pdoc, kdb);
00114     kdb->setMainWidget(wdg);
00115     kapp->restoreOverrideCursor();
00116     if(kdb->exec() == QDialog::Rejected)
00117     {
00118         delete pdoc;
00119         delete kdb;
00120         return KoFilter::StorageCreationError; // FIXME Cancel doesn't exist :(
00121     }
00122     
00123     // Init kis's doc
00124     KisDoc * doc = dynamic_cast<KisDoc*>(m_chain -> outputDocument());
00125     if (!doc)
00126     {
00127         delete pdoc;
00128         delete kdb;
00129         return KoFilter::CreationError;
00130     }
00131 
00132     doc -> prepareForImport();
00133     // Create the krita image
00134     KisColorSpace* cs = KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("RGBA"), "");
00135     int width = wdg->intWidth->value();
00136     int height = wdg->intHeight->value();
00137     KisImageSP img = new KisImage(doc->undoAdapter(), width, height, cs, "built image");
00138     img->blockSignals(true); // Don't send out signals while we're building the image
00139     
00140     // create a layer
00141     QValueList<int> pages = wdg->pages();
00142     for(QValueList<int>::const_iterator it = pages.begin(); it != pages.end(); ++it)
00143     {
00144         KisPaintLayer* layer = new KisPaintLayer(img, QString(i18n("Page %1")).arg( QString::number(*it) + 1), Q_UINT8_MAX);
00145         layer->paintDevice()->convertFromQImage( pdoc->getPage( *it )->renderToImage(wdg->intHorizontal->value(), wdg->intVertical->value() ), "");
00146         img->addLayer(layer, img->rootLayer(), 0);
00147     }
00148     
00149     img->blockSignals(false);
00150     doc -> setCurrentImage( img);
00151     
00152     KIO::NetAccess::removeTempFile(tmpFile);
00153     
00154     delete pdoc;
00155     delete kdb;
00156     return KoFilter::OK;
00157 }
00158 
KDE Home | KDE Accessibility Home | Description of Access Keys