filters

pngexport.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002, The Karbon Developers
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 <qcstring.h>
00021 #include <qdom.h>
00022 #include <qfile.h>
00023 #include <qstring.h>
00024 #include <qvaluelist.h>
00025 #include <qimage.h>
00026 
00027 #include <kgenericfactory.h>
00028 #include <KoFilter.h>
00029 #include <KoFilterChain.h>
00030 #include <KoStore.h>
00031 
00032 #include "pngexport.h"
00033 #include "vdocument.h"
00034 #include "vselection.h"
00035 #include "vkopainter.h"
00036 #include "vlayer.h"
00037 
00038 #include <kdebug.h>
00039 
00040 
00041 typedef KGenericFactory<PngExport, KoFilter> PngExportFactory;
00042 K_EXPORT_COMPONENT_FACTORY( libkarbonpngexport, PngExportFactory( "kofficefilters" ) )
00043 
00044 
00045 PngExport::PngExport( KoFilter*, const char*, const QStringList& )
00046     : KoFilter()
00047 {
00048 }
00049 
00050 KoFilter::ConversionStatus
00051 PngExport::convert( const QCString& from, const QCString& to )
00052 {
00053     if ( to != "image/png" || from != "application/x-karbon" )
00054     {
00055         return KoFilter::NotImplemented;
00056     }
00057 
00058     KoStoreDevice* storeIn = m_chain->storageFile( "root", KoStore::Read );
00059     if( !storeIn )
00060         return KoFilter::StupidError;
00061 
00062     QDomDocument domIn;
00063     domIn.setContent( storeIn );
00064     QDomElement docNode = domIn.documentElement();
00065 
00066     // load the document and export it:
00067     VDocument doc;
00068     doc.load( docNode );
00069 
00070     VLayerListIterator layerItr( doc.layers() );
00071     VLayer *currentLayer;
00072 
00073     for( ; currentLayer = layerItr.current(); ++layerItr )
00074     {
00075         if( currentLayer->state() == VObject::normal || currentLayer->state() == VObject::normal_locked || currentLayer->state() == VObject::selected )
00076         {
00077             doc.selection()->append(currentLayer->objects());
00078         }
00079     }
00080 
00081     // get the bounding box of all selected objects:
00082     const KoRect& rect = doc.selection()->boundingBox();
00083 
00084     // create image with correct width and height
00085     QImage img( int( rect.width() ), int( rect.height() ), 32 );
00086     //img.setAlphaBuffer( true );
00087 
00088     // Create painter and set up objects to draw
00089     VKoPainter p( img.bits(), rect.width(), rect.height() );
00090     p.clear( qRgba( 0xFF, 0xFF, 0xFF, 0xFF ) );
00091     p.setWorldMatrix( QWMatrix().translate( -rect.x(), -rect.y() ) );
00092     VObjectList objects = doc.selection()->objects();
00093     VObjectListIterator itr = objects;
00094 
00095     // we dont need the selection anymore:
00096     doc.selection()->clear();
00097 
00098     // paint shapes over image
00099     for ( ; itr.current(); ++itr )
00100         itr.current()->draw( &p, &rect );
00101 
00102     QImage image = img.swapRGB();
00103     QImage mirrored = image.mirror( false, true );
00104     // save png
00105     mirrored.save( m_chain->outputFile(), "PNG" );
00106 
00107     return KoFilter::OK;
00108 }
00109 
00110 #include "pngexport.moc"
00111 
KDE Home | KDE Accessibility Home | Description of Access Keys