26#include <svx/strings.hrc>
34#include <com/sun/star/beans/XPropertySet.hpp>
35#include <com/sun/star/beans/PropertyValue.hpp>
36#include <com/sun/star/container/NoSuchElementException.hpp>
37#include <com/sun/star/document/XExporter.hpp>
38#include <com/sun/star/drawing/GraphicExportFilter.hpp>
39#include <com/sun/star/drawing/XShape.hpp>
40#include <com/sun/star/lang/XMultiServiceFactory.hpp>
41#include <com/sun/star/lang/XComponent.hpp>
42#include <com/sun/star/io/XInputStream.hpp>
43#include <com/sun/star/ucb/SimpleFileAccess.hpp>
44#include <com/sun/star/ui/dialogs/XFilePicker3.hpp>
45#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
46#include <com/sun/star/beans/XPropertyAccess.hpp>
47#include <com/sun/star/task/ErrorCodeIOException.hpp>
48#include <com/sun/star/graphic/XGraphic.hpp>
49#include <com/sun/star/drawing/ShapeCollection.hpp>
55using namespace css::uno;
56using namespace css::lang;
57using namespace css::graphic;
58using namespace css::ucb;
59using namespace css::beans;
60using namespace css::io;
61using namespace css::document;
62using namespace css::ui::dialogs;
63using namespace css::container;
70void GraphicHelper::GetPreferredExtension( OUString& rExtension,
const Graphic& rGraphic )
72 OUString aExtension =
"png";
75 if (rVectorGraphicDataPtr && !rVectorGraphicDataPtr->getBinaryDataContainer().isEmpty())
77 switch (rVectorGraphicDataPtr->getType())
79 case VectorGraphicDataType::Wmf:
82 case VectorGraphicDataType::Emf:
90 rExtension = aExtension;
96 case GfxLinkType::NativeGif:
99 case GfxLinkType::NativeTif:
102 case GfxLinkType::NativeWmf:
105 case GfxLinkType::NativeMet:
108 case GfxLinkType::NativePct:
111 case GfxLinkType::NativeJpg:
114 case GfxLinkType::NativeBmp:
117 case GfxLinkType::NativeSvg:
120 case GfxLinkType::NativePdf:
123 case GfxLinkType::NativeWebp:
129 rExtension = aExtension;
132OUString GraphicHelper::GetImageType(
const Graphic& rGraphic)
134 OUString aGraphicTypeString =
SvxResId(STR_IMAGE_UNKNOWN);
138 switch (pGfxLink->GetType())
140 case GfxLinkType::NativeGif:
141 aGraphicTypeString =
SvxResId(STR_IMAGE_GIF);
143 case GfxLinkType::NativeJpg:
144 aGraphicTypeString =
SvxResId(STR_IMAGE_JPEG);
146 case GfxLinkType::NativePng:
147 aGraphicTypeString =
SvxResId(STR_IMAGE_PNG);
149 case GfxLinkType::NativeTif:
150 aGraphicTypeString =
SvxResId(STR_IMAGE_TIFF);
152 case GfxLinkType::NativeWmf:
153 aGraphicTypeString =
SvxResId(STR_IMAGE_WMF);
155 case GfxLinkType::NativeMet:
156 aGraphicTypeString =
SvxResId(STR_IMAGE_MET);
158 case GfxLinkType::NativePct:
159 aGraphicTypeString =
SvxResId(STR_IMAGE_PCT);
161 case GfxLinkType::NativeSvg:
162 aGraphicTypeString =
SvxResId(STR_IMAGE_SVG);
164 case GfxLinkType::NativeBmp:
165 aGraphicTypeString =
SvxResId(STR_IMAGE_BMP);
167 case GfxLinkType::NativeWebp:
168 aGraphicTypeString =
SvxResId(STR_IMAGE_WEBP);
174 return aGraphicTypeString;
179bool lcl_ExecuteFilterDialog(
const Sequence< PropertyValue >& rPropsForDialog,
180 Sequence< PropertyValue >& rFilterData )
182 bool bStatus =
false;
185 Reference< XExecutableDialog > xFilterDialog(
187 Reference< XPropertyAccess > xFilterProperties( xFilterDialog, UNO_QUERY );
189 if( xFilterDialog.is() && xFilterProperties.is() )
191 xFilterProperties->setPropertyValues( rPropsForDialog );
192 if( xFilterDialog->execute() )
195 const Sequence< PropertyValue > aPropsFromDialog = xFilterProperties->getPropertyValues();
196 for (
const auto& rProp : aPropsFromDialog )
198 if (rProp.Name ==
"FilterData")
200 rProp.Value >>= rFilterData;
206 catch(
const NoSuchElementException& e )
209 throw ErrorCodeIOException(
210 (
"lcl_ExecuteFilterDialog: NoSuchElementException"
211 " \"" + e.Message +
"\": ERRCODE_IO_ABORT"),
212 Reference< XInterface >(), sal_uInt32(ERRCODE_IO_INVALIDPARAMETER));
214 catch(
const ErrorCodeIOException& )
218 catch(
const Exception& )
227OUString GraphicHelper::ExportGraphic(
weld::Window* pParent,
const Graphic& rGraphic,
const OUString& rGraphicName)
229 FileDialogHelper aDialogHelper(TemplateDescription::FILESAVE_AUTOEXTENSION, FileDialogFlags::NONE, pParent);
230 Reference < XFilePicker3 > xFilePicker = aDialogHelper.GetFilePicker();
233 aDialogHelper.SetContext(FileDialogHelper::ExportImage);
234 aDialogHelper.SetTitle(
SvxResId(RID_SVXSTR_EXPORT_GRAPHIC_TITLE));
236 aURL.SetSmartURL( rGraphicName );
237 aDialogHelper.SetFileName(
aURL.GetLastName());
242 OUString aExtension(
aURL.GetFileExtension());
243 if( aExtension.isEmpty() )
245 GetPreferredExtension( aExtension, rGraphic );
248 aExtension = aExtension.toAsciiLowerCase();
249 sal_uInt16 nDefaultFilter = USHRT_MAX;
251 for ( sal_uInt16 i = 0;
i <
nCount;
i++ )
255 if ( aFormatShortName.equalsIgnoreAsciiCase( aExtension ) )
260 if ( USHRT_MAX == nDefaultFilter )
263 GetPreferredExtension( aExtension, rGraphic );
264 for ( sal_uInt16 i = 0;
i <
nCount; ++
i )
272 if( USHRT_MAX != nDefaultFilter )
276 if( aDialogHelper.Execute() == ERRCODE_NONE )
278 OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
279 if( !rGraphicName.isEmpty() &&
283 SfxMedium aIn( rGraphicName, StreamMode::READ | StreamMode::NOCREATE );
284 if( aIn.GetInStream() && !aIn.GetInStream()->GetError() )
286 SfxMedium aOut( sPath, StreamMode::WRITE | StreamMode::SHARE_DENYNONE);
287 if( aOut.GetOutStream() && !aOut.GetOutStream()->GetError())
289 aOut.GetOutStream()->WriteStream( *aIn.GetInStream() );
290 if ( ERRCODE_NONE == aIn.GetError() )
294 if ( ERRCODE_NONE == aOut.GetError() )
312 if ( rGraphic.
GetType() == GraphicType::Bitmap )
315 Reference<XGraphic> xGraphic = aGraphic.
GetXGraphic();
319 Sequence< PropertyValue > aPropsForDialog{
324 Sequence< PropertyValue > aFilterData;
325 bool bStatus = lcl_ExecuteFilterDialog(aPropsForDialog, aFilterData);
328 sal_Int32 nWidth = 0;
329 sal_Int32 nHeight = 0;
331 for (
const auto& rProp : std::as_const(aFilterData))
333 if (rProp.Name ==
"PixelWidth")
335 rProp.Value >>= nWidth;
337 else if (rProp.Name ==
"PixelHeight")
339 rProp.Value >>= nHeight;
346 if( nWidth && nHeight &&
347 ( ( nWidth != aSizePixel.Width() ) ||
348 ( nHeight != aSizePixel.Height() ) ) )
352 aBmpEx.Scale(
Size( nWidth, nHeight ), BmpScaleFlag::Lanczos );
360 nullptr, &aFilterData );
376void GraphicHelper::SaveShapeAsGraphicToPath(
377 const css::uno::Reference<css::lang::XComponent>& xComponent,
378 const css::uno::Reference<css::drawing::XShape>& xShape,
const OUString& aExportMimeType,
379 const OUString& sPath)
381 Reference<XComponentContext> xContext(::comphelper::getProcessComponentContext());
382 Reference<XInputStream> xGraphStream;
384 if (xGraphStream.is())
386 Reference<XSimpleFileAccess3> xFileAccess = SimpleFileAccess::create(xContext);
387 xFileAccess->writeFile(sPath, xGraphStream);
389 else if (xComponent.is() && aExportMimeType ==
"application/pdf")
391 css::uno::Reference<css::lang::XMultiServiceFactory> xMSF(xContext->getServiceManager(),
392 css::uno::UNO_QUERY);
393 css::uno::Reference<css::document::XExporter> xExporter(
394 xMSF->createInstance(
"com.sun.star.comp.PDF.PDFFilter"), css::uno::UNO_QUERY);
395 xExporter->setSourceDocument(xComponent);
397 css::uno::Reference<css::drawing::XShapes> xShapes
399 xShapes->add(xShape);
400 css::uno::Sequence<PropertyValue> aFilterData{
403 SvFileStream aStream(sPath, StreamMode::READWRITE | StreamMode::TRUNC);
405 css::uno::Sequence<PropertyValue> aDescriptor{
409 css::uno::Reference<css::document::XFilter> xFilter(xExporter, css::uno::UNO_QUERY);
410 xFilter->filter(aDescriptor);
414 Reference<css::drawing::XGraphicExportFilter> xGraphicExporter
415 = css::drawing::GraphicExportFilter::create(xContext);
421 Reference<XComponent> xSourceDocument(xShape, UNO_QUERY_THROW);
422 xGraphicExporter->setSourceDocument(xSourceDocument);
423 xGraphicExporter->filter(aDescriptor);
427void GraphicHelper::SaveShapeAsGraphic(
weld::Window* pParent,
428 const css::uno::Reference<css::lang::XComponent>& xComponent,
429 const Reference<drawing::XShape>& xShape)
433 Reference< XPropertySet > xShapeSet( xShape, UNO_QUERY_THROW );
435 FileDialogHelper aDialogHelper(TemplateDescription::FILESAVE_AUTOEXTENSION, FileDialogFlags::NONE, pParent);
436 Reference < XFilePicker3 > xFilePicker = aDialogHelper.GetFilePicker();
437 aDialogHelper.SetContext(FileDialogHelper::ExportImage);
438 aDialogHelper.SetTitle(
SvxResId(RID_SVXSTR_SAVEAS_IMAGE) );
443 static constexpr OUStringLiteral aDefaultMimeType(u
"image/png");
444 OUString aDefaultFormatName;
447 std::map< OUString, OUString > aMimeTypeMap;
449 for ( sal_uInt16 i = 0;
i <
nCount;
i++ )
453 xFilePicker->appendFilter( aExportFormatName, rGraphicFilter.
GetExportWildcard( i ) );
454 aMimeTypeMap[ aExportFormatName ] = aFilterMimeType;
455 if( aDefaultMimeType == aFilterMimeType )
456 aDefaultFormatName = aExportFormatName;
459 if( !aDefaultFormatName.isEmpty() )
460 xFilePicker->setCurrentFilter( aDefaultFormatName );
464 if( aDialogHelper.Execute() == ERRCODE_NONE )
466 OUString sPath( xFilePicker->getFiles().getConstArray()[0] );
467 OUString aExportMimeType( aMimeTypeMap[xFilePicker->getCurrentFilter()] );
469 GraphicHelper::SaveShapeAsGraphicToPath(xComponent, xShape, aExportMimeType, sPath);
477short GraphicHelper::HasToSaveTransformedImage(
weld::Widget* pWin)
479 OUString aMsg(
SvxResId(RID_SVXSTR_SAVE_MODIFIED_IMAGE));
481 VclMessageType::Question, VclButtonsType::YesNo, aMsg));
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
GfxLinkType GetType() const
OUString GetExportWildcard(sal_uInt16 nFormat)
static GraphicFilter & GetGraphicFilter()
sal_uInt16 GetExportFormatNumber(std::u16string_view rFormatName)
OUString GetExportFormatName(sal_uInt16 nFormat)
sal_uInt16 GetExportFormatCount() const
OUString GetExportFormatShortName(sal_uInt16 nFormat)
OUString GetExportFormatMediaType(sal_uInt16 nFormat)
OUString GetExportInternalFilterName(sal_uInt16 nFormat)
css::uno::Reference< css::graphic::XGraphic > GetXGraphic() const
const std::shared_ptr< GfxLink > & GetSharedGfxLink() const
GraphicType GetType() const
GfxLink GetGfxLink() const
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
const std::shared_ptr< VectorGraphicData > & getVectorGraphicData() const
static ErrCode WriteGraphic(const Graphic &rGraphic, OUString &rFileName, const OUString &rFilterName, const XOutFlags nFlags, const Size *pMtfSize_100TH_MM=nullptr, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr, OUString *pMediaType=nullptr)
#define TOOLS_WARN_EXCEPTION(area, stream)
OUString SvxResId(TranslateId aId)
#define GRFILTER_FORMAT_DONTKNOW
Reference< XMultiServiceFactory > getProcessServiceFactory()
Reference< XComponentContext > getProcessComponentContext()
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)