File indexing completed on 2024-05-12 16:36:44

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 Thorsten Zachmann <zachmann@kde.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "KPrPicturesImport.h"
0021 
0022 #include <KoShapeFactoryBase.h>
0023 #include <KoShapeRegistry.h>
0024 #include <KoShapeLayer.h>
0025 #include <KoImageData.h>
0026 #include <KoImageCollection.h>
0027 #include <KoPAPage.h>
0028 #include <KoPAMasterPage.h>
0029 #include <KoPAPageInsertCommand.h>
0030 
0031 #include "KPrDocument.h"
0032 #include "KPrView.h"
0033 #include "StageDebug.h"
0034 
0035 #include <kio/job.h>
0036 #include <kundo2command.h>
0037 #include <KoDocumentResourceManager.h>
0038 
0039 #include <QFileDialog>
0040 #include <QUrl>
0041 #include <QImageReader>
0042 
0043 
0044 KPrPicturesImport::KPrPicturesImport()
0045 {
0046 }
0047 
0048 void KPrPicturesImport::import(KPrView *view)
0049 {
0050     m_factory = KoShapeRegistry::instance()->value("PictureShape");
0051     Q_ASSERT(m_factory);
0052     if (m_factory) {
0053         // TODO: think about using KoFileDialog everywhere, after extending it to support remote urls
0054         QFileDialog *dialog = new QFileDialog();
0055         QStringList imageMimeTypes;
0056         foreach(const QByteArray &mimeType, QImageReader::supportedMimeTypes()) {
0057             imageMimeTypes << QLatin1String(mimeType);
0058         }
0059         dialog->setMimeTypeFilters(imageMimeTypes);
0060         dialog->setFileMode(QFileDialog::ExistingFiles);
0061         dialog->setAcceptMode(QFileDialog::AcceptOpen);
0062         if (dialog->exec() != QFileDialog::Accepted) {
0063             return;
0064         }
0065         m_urls = dialog->selectedUrls();
0066 
0067         // TODO there should be a progress bar
0068         // instead of the progress bar opening for each loaded picture
0069         m_currentPage = view->activePage();
0070         KoPAPage *activePage = dynamic_cast<KoPAPage*>(m_currentPage);
0071         if (activePage) {
0072             m_masterPage = activePage->masterPage();
0073 
0074             m_doc = view->kprDocument();
0075             m_cmd = new KUndo2Command(kundo2_i18n("Insert Pictures"));
0076             import();
0077         }
0078     }
0079     else {
0080         warnStage << "picture shape factory not found";
0081     }
0082 }
0083 
0084 void KPrPicturesImport::import()
0085 {
0086     if (m_urls.isEmpty()) {
0087         //  TODO check that a picture was added.
0088         m_doc->addCommand(m_cmd);
0089         // TODO activate first added page doUpdateActivePage(page);
0090     }
0091     else {
0092         QUrl url(m_urls.takeAt(0));
0093         // todo calculate the correct size so that the image is centered to
0094         KIO::StoredTransferJob *job(KIO::storedGet(url, KIO::NoReload, 0));
0095         connect(job, SIGNAL(result(KJob*)), this, SLOT(pictureImported(KJob*)));
0096         job->exec();
0097     }
0098 }
0099 
0100 void KPrPicturesImport::pictureImported(KJob *job)
0101 {
0102     KoShape *shape = m_factory->createDefaultShape();
0103     if (shape) {
0104         KIO::StoredTransferJob *transferJob = qobject_cast<KIO::StoredTransferJob*>(job);
0105         Q_ASSERT(transferJob);
0106         KoImageData *imageData = m_doc->resourceManager()->imageCollection()->createImageData(transferJob->data());
0107         if (imageData->isValid()) {
0108             shape->setUserData(imageData);
0109 
0110             // make sure the picture fits on the page
0111             QSizeF imageSize = imageData->imageSize();
0112             QSizeF pageSize = m_masterPage->size();
0113             qreal zoom = 1;
0114             if (imageSize.width() > pageSize.width() || imageSize.height() > pageSize.height()) {
0115                 zoom = pageSize.width() / imageSize.width();
0116                 zoom = qMin(zoom, pageSize.height() / imageSize.height());
0117             }
0118             imageSize *= zoom;
0119             shape->setSize(imageSize);
0120 
0121             // center the picture on the page
0122             QPointF pos( pageSize.width() / 2- imageSize.width() / 2, pageSize.height() / 2 - imageSize.height() / 2 );
0123             shape->setPosition(pos);
0124 
0125             KoPAPageBase *page = m_doc->newPage(m_masterPage);
0126             KoShapeLayer *layer = dynamic_cast<KoShapeLayer *>(page->shapes().first());
0127             if (layer) {
0128                 layer->addShape(shape);
0129                 new KoPAPageInsertCommand(m_doc, page, m_currentPage, m_cmd);
0130                 m_currentPage = page;
0131             }
0132             else {
0133                 delete page;
0134                 delete shape;
0135             }
0136         }
0137         else {
0138             warnStage << "imageData not valid";
0139             delete shape;
0140         }
0141     }
0142     else {
0143         warnStage << "shape not created";
0144     }
0145     import();
0146 }