filters

kivio_imageexport.cpp

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