From a5e22906c0b27019b61b41161ccc8cbc2cf40ade Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Tue, 26 Mar 2013 23:07:42 +0100 Subject: Export Chart as Graphic - allow vector graphic export Readd removed SaveShapeAsGraphic to GraphicHelper with which it is possible to export any shape to vector/raster graphic. Additionally cleanup GraphicHelper and ExternalEdit (convertsion to OUString, remove unused imports, comments). Change-Id: I1af45e2ed5d3b66dbd80a5f3a71c44530e46baef --- svx/source/core/graphichelper.cxx | 247 +++++++++++++++++++++++++++++--------- 1 file changed, 193 insertions(+), 54 deletions(-) (limited to 'svx/source/core/graphichelper.cxx') diff --git a/svx/source/core/graphichelper.cxx b/svx/source/core/graphichelper.cxx index 2cc0c7cc8cad..4671c912963a 100644 --- a/svx/source/core/graphichelper.cxx +++ b/svx/source/core/graphichelper.cxx @@ -18,119 +18,144 @@ */ #include -#include #include #include #include -#include #include +#include #include +#include #include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include +#include + +using namespace css::uno; +using namespace css::lang; +using namespace css::graphic; +using namespace css::ucb; +using namespace css::beans; +using namespace css::io; +using namespace css::document; +using namespace css::ui::dialogs; -using namespace ::com::sun::star::uno; -using namespace ::com::sun::star::ui::dialogs; -using namespace ::sfx2; +using namespace sfx2; -void GraphicHelper::GetPreferedExtension( String &rExtension, const Graphic &rGraphic ) +void GraphicHelper::GetPreferedExtension( OUString& rExtension, const Graphic& rGraphic ) { - // propose the "best" filter using the native-info, if applicable - const sal_Char* pExtension = "png"; + OUString aExtension = "png"; switch( const_cast( rGraphic ).GetLink().GetType() ) { case GFX_LINK_TYPE_NATIVE_GIF: - pExtension = "gif"; + aExtension = "gif"; break; case GFX_LINK_TYPE_NATIVE_TIF: - pExtension = "tif"; + aExtension = "tif"; break; case GFX_LINK_TYPE_NATIVE_WMF: - pExtension = "wmf"; + aExtension = "wmf"; break; case GFX_LINK_TYPE_NATIVE_MET: - pExtension = "met"; + aExtension = "met"; break; case GFX_LINK_TYPE_NATIVE_PCT: - pExtension = "pct"; + aExtension = "pct"; break; case GFX_LINK_TYPE_NATIVE_JPG: - pExtension = "jpg"; + aExtension = "jpg"; break; default: break; } - rExtension.AssignAscii( pExtension ); + rExtension = aExtension; } -String GraphicHelper::ExportGraphic( const Graphic &rGraphic, const String &rGrfName ) +OUString GraphicHelper::ExportGraphic( const Graphic& rGraphic, const OUString& rGraphicName ) { SvtPathOptions aPathOpt; - String sGrfPath( aPathOpt.GetGraphicPath() ); + OUString sGraphicsPath( aPathOpt.GetGraphicPath() ); - FileDialogHelper aDlgHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 ); - Reference < XFilePicker > xFP = aDlgHelper.GetFilePicker(); + FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 ); + Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker(); INetURLObject aPath; - aPath.SetSmartURL( sGrfPath ); + aPath.SetSmartURL( sGraphicsPath ); // fish out the graphic's name - String aName = rGrfName; + OUString aName = rGraphicName; - aDlgHelper.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE)); - aDlgHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) ); + aDialogHelper.SetTitle( SVX_RESSTR(RID_SVXSTR_EXPORT_GRAPHIC_TITLE)); + aDialogHelper.SetDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) ); INetURLObject aURL; aURL.SetSmartURL( aName ); - aDlgHelper.SetFileName( aURL.GetName() ); + aDialogHelper.SetFileName( aURL.GetName() ); - GraphicFilter& rGF = GraphicFilter::GetGraphicFilter(); - const sal_uInt16 nCount = rGF.GetExportFormatCount(); + GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter(); + const sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount(); - String aExt( aURL.GetExtension() ); - if( !aExt.Len() ) - GetPreferedExtension( aExt, rGraphic ); + OUString aExtension( aURL.GetExtension() ); + if( aExtension.isEmpty() ) + { + GetPreferedExtension( aExtension, rGraphic ); + } - aExt.ToLowerAscii(); - sal_uInt16 nDfltFilter = USHRT_MAX; + aExtension = aExtension.toAsciiLowerCase(); + sal_uInt16 nDefaultFilter = USHRT_MAX; - Reference xFltMgr(xFP, UNO_QUERY); + Reference xFilterManager(xFilePicker, UNO_QUERY); for ( sal_uInt16 i = 0; i < nCount; i++ ) { - xFltMgr->appendFilter( rGF.GetExportFormatName( i ), rGF.GetExportWildcard( i ) ); - if ( COMPARE_EQUAL == aExt.CompareIgnoreCaseToAscii(rGF.GetExportFormatShortName( i ).ToLowerAscii() )) - nDfltFilter = i; + xFilterManager->appendFilter( rGraphicFilter.GetExportFormatName( i ), rGraphicFilter.GetExportWildcard( i ) ); + OUString aFormatShortName = rGraphicFilter.GetExportFormatShortName( i ); + if ( aFormatShortName.equalsIgnoreAsciiCase( aExtension ) ) + { + nDefaultFilter = i; + } } - if ( USHRT_MAX == nDfltFilter ) + if ( USHRT_MAX == nDefaultFilter ) { // "wrong" extension? - GetPreferedExtension( aExt, rGraphic ); + GetPreferedExtension( aExtension, rGraphic ); for ( sal_uInt16 i = 0; i < nCount; ++i ) - if ( aExt == rGF.GetExportFormatShortName( i ).ToLowerAscii() ) + if ( aExtension == rGraphicFilter.GetExportFormatShortName( i ).ToLowerAscii() ) { - nDfltFilter = i; + nDefaultFilter = i; break; } } - if( USHRT_MAX != nDfltFilter ) + if( USHRT_MAX != nDefaultFilter ) { - xFltMgr->setCurrentFilter( rGF.GetExportFormatName( nDfltFilter ) ) ; + xFilterManager->setCurrentFilter( rGraphicFilter.GetExportFormatName( nDefaultFilter ) ) ; - if( aDlgHelper.Execute() == ERRCODE_NONE ) + if( aDialogHelper.Execute() == ERRCODE_NONE ) { - String sPath( xFP->getFiles().getConstArray()[0] ); + OUString sPath( xFilePicker->getFiles().getConstArray()[0] ); // remember used path - please don't optimize away! aPath.SetSmartURL( sPath); - sGrfPath = aPath.GetPath(); + sGraphicsPath = aPath.GetPath(); - if( rGrfName.Len() && - nDfltFilter == rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter())) + if( !rGraphicName.isEmpty() && + nDefaultFilter == rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter())) { // try to save the original graphic - SfxMedium aIn( rGrfName, STREAM_READ | STREAM_NOCREATE ); + SfxMedium aIn( rGraphicName, STREAM_READ | STREAM_NOCREATE ); if( aIn.GetInStream() && !aIn.GetInStream()->GetError() ) { SfxMedium aOut( sPath, STREAM_WRITE | STREAM_SHARE_DENYNONE); @@ -149,19 +174,133 @@ String GraphicHelper::ExportGraphic( const Graphic &rGraphic, const String &rGrf } sal_uInt16 nFilter; - if ( !xFltMgr->getCurrentFilter().isEmpty() && rGF.GetExportFormatCount() ) - nFilter = rGF.GetExportFormatNumber( xFltMgr->getCurrentFilter() ); + if ( !xFilterManager->getCurrentFilter().isEmpty() && rGraphicFilter.GetExportFormatCount() ) + { + nFilter = rGraphicFilter.GetExportFormatNumber( xFilterManager->getCurrentFilter() ); + } else + { nFilter = GRFILTER_FORMAT_DONTKNOW; - String aFilter( rGF.GetExportFormatShortName( nFilter ) ); - XOutBitmap::WriteGraphic( rGraphic, sPath, aFilter, - XOUTBMP_DONT_EXPAND_FILENAME| - XOUTBMP_DONT_ADD_EXTENSION| + } + OUString aFilter( rGraphicFilter.GetExportFormatShortName( nFilter ) ); + + String aFilterString( aFilter ); + String aPathString( sPath ); + XOutBitmap::WriteGraphic( rGraphic, aPathString, aFilterString, + XOUTBMP_DONT_EXPAND_FILENAME | + XOUTBMP_DONT_ADD_EXTENSION | XOUTBMP_USE_NATIVE_IF_POSSIBLE ); return sPath; } } - return String(); + return OUString(); +} + +void GraphicHelper::SaveShapeAsGraphic( const Reference< XShape >& xShape ) +{ + try + { + Reference< XMultiServiceFactory > xServiceFactory( ::comphelper::getProcessServiceFactory(), UNO_QUERY_THROW ); + Reference< XPropertySet > xShapeSet( xShape, UNO_QUERY_THROW ); + + OUString aMimeType; + OUString sGraphicURL; + + SvtPathOptions aPathOpt; + String sGraphicPath( aPathOpt.GetGraphicPath() ); + + FileDialogHelper aDialogHelper( TemplateDescription::FILESAVE_AUTOEXTENSION, 0 ); + Reference < XFilePicker > xFilePicker = aDialogHelper.GetFilePicker(); + + aDialogHelper.SetTitle( OUString( "Save as Picture" ) ); + + INetURLObject aPath; + aPath.SetSmartURL( sGraphicPath ); + xFilePicker->setDisplayDirectory( aPath.GetMainURL(INetURLObject::DECODE_TO_IURI) ); + + // populate filter dialog filter list and select default filter to match graphic mime type + + GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter(); + Reference xFilterManager( xFilePicker, UNO_QUERY ); + OUString aDefaultFormatName; + sal_uInt16 nCount = rGraphicFilter.GetExportFormatCount(); + + std::map< OUString, OUString > aMimeTypeMap; + + for ( sal_uInt16 i = 0; i < nCount; i++ ) + { + const OUString aExportFormatName( rGraphicFilter.GetExportFormatName( i ) ); + const OUString aFilterMimeType( rGraphicFilter.GetExportFormatMediaType( i ) ); + xFilterManager->appendFilter( aExportFormatName, rGraphicFilter.GetExportWildcard( i ) ); + aMimeTypeMap[ aExportFormatName ] = aFilterMimeType; + if( aMimeType == aFilterMimeType ) + aDefaultFormatName = aExportFormatName; + } + + if( aDefaultFormatName.getLength() == 0 ) + { + nCount = rGraphicFilter.GetImportFormatCount(); + for( sal_uInt16 i = 0; i < nCount; i++ ) + { + const OUString aFilterMimeType( rGraphicFilter.GetImportFormatMediaType( i ) ); + if( aMimeType == aFilterMimeType ) + { + aDefaultFormatName = rGraphicFilter.GetImportFormatName( i ); + xFilterManager->appendFilter( aDefaultFormatName, rGraphicFilter.GetImportWildcard( i ) ); + aMimeTypeMap[ aDefaultFormatName ] = aFilterMimeType; + break; + } + } + } + + if( aDefaultFormatName.getLength() == 0 ) + aDefaultFormatName = OUString( "PNG - Portable Network Graphic" ); + + xFilterManager->setCurrentFilter( aDefaultFormatName ); + + // execute dialog + + if( aDialogHelper.Execute() == ERRCODE_NONE ) + { + OUString sPath( xFilePicker->getFiles().getConstArray()[0] ); + OUString aExportMimeType( aMimeTypeMap[xFilterManager->getCurrentFilter()] ); + + Reference< XInputStream > xGraphStream; + if( aMimeType == aExportMimeType ) + { + xShapeSet->getPropertyValue( OUString( "GraphicStream" ) ) >>= xGraphStream; + } + + if( xGraphStream.is() ) + { + OUString aSimpleFileAccessFilter( "com.sun.star.ucb.SimpleFileAccess" ); + Reference xFileAccess( xServiceFactory->createInstance( aSimpleFileAccessFilter ), UNO_QUERY_THROW ); + xFileAccess->writeFile( sPath, xGraphStream ); + } + else + { + OUString aGraphicExportFilter("com.sun.star.drawing.GraphicExportFilter"); + Reference xGraphicExporter( xServiceFactory->createInstance( aGraphicExportFilter ), UNO_QUERY_THROW ); + Reference xFilter( xGraphicExporter, UNO_QUERY_THROW ); + + Sequence aDescriptor( 2 ); + aDescriptor[0].Name = OUString("MediaType"); + aDescriptor[0].Value <<= aExportMimeType; + aDescriptor[1].Name = OUString("URL"); + aDescriptor[1].Value <<= sPath; + + Reference< XComponent > xSourceDocument = Reference< XComponent >( xShape, UNO_QUERY_THROW ); + if ( xSourceDocument.is() ) + { + xGraphicExporter->setSourceDocument( xSourceDocument ); + xFilter->filter( aDescriptor ); + } + } + } + } + catch( Exception& ) + { + } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3