File indexing completed on 2024-05-12 16:36:44
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2008 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 "KPrPlaceholderPictureStrategy.h" 0021 0022 #include <QString> 0023 #include <QFileDialog> 0024 #include <QUrl> 0025 0026 #include <KoImageCollection.h> 0027 #include <KoDocumentResourceManager.h> 0028 #include <KoImageData.h> 0029 #include <KoShape.h> 0030 #include <KoNetAccess.h> 0031 0032 #include "StageDebug.h" 0033 0034 0035 KPrPlaceholderPictureStrategy::KPrPlaceholderPictureStrategy() 0036 : KPrPlaceholderStrategy( "graphic" ) 0037 { 0038 } 0039 0040 KPrPlaceholderPictureStrategy::~KPrPlaceholderPictureStrategy() 0041 { 0042 } 0043 0044 KoShape *KPrPlaceholderPictureStrategy::createShape(KoDocumentResourceManager *rm) 0045 { 0046 KoShape * shape = 0; 0047 0048 QUrl url = QFileDialog::getOpenFileUrl(); 0049 if ( !url.isEmpty() ) { 0050 shape = KPrPlaceholderStrategy::createShape(rm); 0051 0052 KoImageCollection *collection = rm->imageCollection(); 0053 Q_ASSERT(collection); 0054 0055 QString tmpFile; 0056 if (KIO::NetAccess::download(url, tmpFile, 0)) { 0057 QImage image(tmpFile); 0058 if (!image.isNull()) { 0059 //setSuffix(url.prettyUrl()); 0060 KoImageData *data = collection->createImageData(image); 0061 if (data->isValid()) { 0062 shape->setUserData( data ); 0063 // TODO the pic should be fit into the space provided 0064 shape->setSize( data->imageSize() ); 0065 } 0066 } 0067 KIO::NetAccess::removeTempFile(tmpFile); 0068 } else { 0069 warnStage << "open image" << url << "failed"; 0070 } 0071 } 0072 return shape; 0073 }