filters

kivio_imageexport.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005-2006 Peter Simonsson <psn@linux.se>
00003    Copyright (C) 2006 Kåre Särs <kare.sars@kolumbus.fi>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kivio_imageexport.h"
00022 
00023 #include <qdom.h>
00024 #include <qcstring.h>
00025 #include <qpixmap.h>
00026 #include <qpainter.h>
00027 #include <qsize.h>
00028 #include <qstringlist.h>
00029 
00030 #include <kmessagebox.h>
00031 #include <klocale.h>
00032 #include <kgenericfactory.h>
00033 #include <kdebug.h>
00034 
00035 #include <KoStore.h>
00036 #include <KoFilterChain.h>
00037 #include <KoZoomHandler.h>
00038 
00039 #include "kivio_doc.h"
00040 #include "kivio_page.h"
00041 #include "kivio_map.h"
00042 #include "kivio_screen_painter.h"
00043 
00044 #include "kivio_imageexportdialog.h"
00045 
00046 typedef KGenericFactory<Kivio::ImageExport, KoFilter> KivioImageExportFactory;
00047 K_EXPORT_COMPONENT_FACTORY( libkivioimageexport, KivioImageExportFactory("KivioImageExport") )
00048 
00049 namespace Kivio
00050 {
00051 
00052 ImageExport::ImageExport(KoFilter *, const char *, const QStringList&)
00053   : KoFilter()
00054 {
00055   KGlobal::locale()->insertCatalogue("kofficefilters");
00056 }
00057 
00058 KoFilter::ConversionStatus ImageExport::convert(const QCString& from, const QCString& to)
00059 {
00060   if(from != "application/x-kivio") {
00061     return KoFilter::BadMimeType;
00062   }
00063 
00064   QString format;
00065 
00066   if(to == "image/png") {
00067     format = "PNG";
00068   } else if(to == "image/jpeg") {
00069     format = "JPEG";
00070   } else if(to == "image/x-bmp") {
00071     format = "BMP";
00072   } else if(to == "image/x-eps") {
00073     format = "EPS";
00074   } else if(to == "image/x-portable-bitmap") {
00075     format = "PBM";
00076   } else if(to == "image/x-pcx") {
00077     format = "PCX";
00078   } else if(to == "image/x-portable-pixmap") {
00079     format = "PPM";
00080   } else if(to == "image/x-rgb") {
00081     format = "RGB";
00082   } else if(to == "image/x-xpm") {
00083     format = "XPM";
00084   } else if(to == "image/jp2") {
00085     format = "JP2";
00086   } else {
00087     return KoFilter::BadMimeType;
00088   }
00089 
00090   KoStoreDevice* storeIn = m_chain->storageFile("root", KoStore::Read);
00091 
00092   if (!storeIn) {
00093     KMessageBox::error(0, i18n("Failed to read data."), i18n( "Export Error" ));
00094     return KoFilter::FileNotFound;
00095   }
00096 
00097   // Get the XML tree.
00098   QDomDocument domIn;
00099   domIn.setContent(storeIn);
00100 
00101   KivioDoc doc;
00102 
00103   if(!doc.loadXML(0, domIn)) {
00104     KMessageBox::error(0, i18n("Malformed XML data."), i18n("Export Error"));
00105     return KoFilter::WrongFormat;
00106   }
00107 
00108   ImageExportDialog dlg;
00109 
00110   QStringList pageNames;
00111   QPtrList<KivioPage> pageList = doc.map()->pageList();
00112   QPtrListIterator<KivioPage> it(pageList);
00113 
00114   for(; it.current() != 0; ++it) {
00115     pageNames.append(it.current()->pageName());
00116   }
00117 
00118   dlg.setPageList(pageNames);
00119 
00120   dlg.setInitialDPI(300);
00121   dlg.setInitialmargin(10);
00122 
00123   if(dlg.exec() != QDialog::Accepted) {
00124     return KoFilter::UserCancelled;
00125   }
00126 
00127   KivioPage* page = doc.map()->findPage(dlg.selectedPage());
00128 
00129   if(!page) {
00130     kdDebug() << "The page named " << dlg.selectedPage() << " wasn't found!!" << endl;
00131     return KoFilter::InternalError;
00132   }
00133 
00134   float z = (float)dlg.imageDPI()/(float)KoGlobal::dpiX();
00135   KoZoomHandler zoom;
00136   zoom.setZoomAndResolution(qRound(z * 100), KoGlobal::dpiX(), KoGlobal::dpiY());
00137 
00138   QSize size;
00139   if(dlg.usePageBorders()) {
00140     size = QSize(zoom.zoomItX(page->paperLayout().ptWidth), zoom.zoomItY(page->paperLayout().ptHeight));
00141   } else {
00142     size = zoom.zoomSize(page->getRectForAllStencils().size());
00143   }
00144 
00145   kdDebug() << "KoGlobal::dpiX() " << KoGlobal::dpiX() << " KoGlobal::dpiY() " << KoGlobal::dpiY() << endl;
00146 
00147   int border = dlg.margin();
00148 
00149   size.setWidth(size.width() + (border * 2));
00150   size.setHeight(size.height() + (border * 2));
00151 
00152   QPixmap pixmap = QPixmap(size);
00153   pixmap.fill(Qt::white);
00154   KivioScreenPainter kpainter;
00155   kpainter.start(&pixmap);
00156 
00157   float translationX = border;
00158   float translationY = border;
00159 
00160   if(!dlg.usePageBorders()) {
00161     QPoint point = zoom.zoomPoint(page->getRectForAllStencils().topLeft());
00162     translationX -= point.x();
00163     translationY -= point.y();
00164   }
00165 
00166   kpainter.setTranslation(translationX, translationY);
00167   page->printContent(kpainter, &zoom);
00168 
00169   if(!pixmap.save(m_chain->outputFile(), format.local8Bit())) {
00170     return KoFilter::CreationError;
00171   }
00172 
00173   return KoFilter::OK;
00174 }
00175 
00176 }
00177 
00178 #include "kivio_imageexport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys