From 44faceac60f9046e4827290a85f790a46ed1e227 Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Sun, 24 Feb 2013 13:45:47 +0100 Subject: Add "Compress Graphic Dialog" to Writer. Change-Id: Icff16a86d217b2648ce588ca2b90abefcd1634d9 --- svx/inc/svx/compressgraphicdialog.hxx | 5 ++ svx/source/dialog/compressgraphicdialog.cxx | 95 ++++++++++++++++++++--------- sw/sdi/_grfsh.sdi | 7 ++- sw/source/ui/app/mn.src | 1 + sw/source/ui/shells/grfsh.cxx | 45 ++++++++++++++ 5 files changed, 124 insertions(+), 29 deletions(-) diff --git a/svx/inc/svx/compressgraphicdialog.hxx b/svx/inc/svx/compressgraphicdialog.hxx index 20d15ab583b8..a733fa2b6298 100644 --- a/svx/inc/svx/compressgraphicdialog.hxx +++ b/svx/inc/svx/compressgraphicdialog.hxx @@ -80,9 +80,14 @@ private: public: CompressGraphicsDialog( Window* pParent, SdrGrafObj* pGraphicObj, SfxBindings& rBindings ); + CompressGraphicsDialog( Window* pParent, Graphic& rGraphic, Size rViewSize100mm, Rectangle& rCropRectangle, SfxBindings& rBindings ); + virtual ~CompressGraphicsDialog(); SdrGrafObj* GetCompressedSdrGrafObj(); + Graphic GetCompressedGraphic(); + + Rectangle GetScaledCropRectangle(); }; #endif diff --git a/svx/source/dialog/compressgraphicdialog.cxx b/svx/source/dialog/compressgraphicdialog.cxx index 4f86229daecc..92749aab4ccc 100644 --- a/svx/source/dialog/compressgraphicdialog.cxx +++ b/svx/source/dialog/compressgraphicdialog.cxx @@ -43,7 +43,21 @@ CompressGraphicsDialog::CompressGraphicsDialog( Window* pParent, SdrGrafObj* pGr m_pGraphicObj ( pGraphicObj ), m_aGraphic ( pGraphicObj->GetGraphicObject().GetGraphic() ), m_aViewSize100mm ( pGraphicObj->GetLogicRect().GetSize() ), - m_aCropRectangle ( Rectangle() ), + m_rBindings ( rBindings ), + m_dResolution ( 96.0 ) +{ + const SdrGrafCropItem& rCrop = (const SdrGrafCropItem&) m_pGraphicObj->GetMergedItem(SDRATTR_GRAFCROP); + m_aCropRectangle = Rectangle(rCrop.GetLeft(), rCrop.GetTop(), rCrop.GetRight(), rCrop.GetBottom()); + + Initialize(); +} + +CompressGraphicsDialog::CompressGraphicsDialog( Window* pParent, Graphic& rGraphic, Size rViewSize100mm, Rectangle& rCropRectangle, SfxBindings& rBindings ) : + ModalDialog ( pParent, "CompressGraphicDialog", "svx/ui/compressgraphicdialog.ui" ), + m_pGraphicObj ( NULL ), + m_aGraphic ( rGraphic ), + m_aViewSize100mm ( rViewSize100mm ), + m_aCropRectangle ( rCropRectangle ), m_rBindings ( rBindings ), m_dResolution ( 96.0 ) { @@ -184,15 +198,15 @@ sal_uLong CompressGraphicsDialog::GetSelectedInterpolationType() void CompressGraphicsDialog::Compress(SvStream& aStream) { - BitmapEx bitmap = m_aGraphic.GetBitmapEx(); + BitmapEx aBitmap = m_aGraphic.GetBitmapEx(); if ( m_pReduceResolutionCB->IsChecked() ) { long nPixelX = (long)( GetViewWidthInch() * m_dResolution ); long nPixelY = (long)( GetViewHeightInch() * m_dResolution ); - bitmap.Scale( Size( nPixelX, nPixelY ), GetSelectedInterpolationType() ); + aBitmap.Scale( Size( nPixelX, nPixelY ), GetSelectedInterpolationType() ); } - Graphic aScaledGraphic = Graphic( bitmap ); + Graphic aScaledGraphic( aBitmap ); GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); Sequence< PropertyValue > aFilterData( 3 ); @@ -206,7 +220,7 @@ void CompressGraphicsDialog::Compress(SvStream& aStream) String aGraphicFormatName = m_pLosslessRB->IsChecked() ? String( "png" ) : String( "jpg" ); sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aGraphicFormatName ); - rFilter.ExportGraphic( aScaledGraphic, String( "test" ), aStream, nFilterFormat, &aFilterData ); + rFilter.ExportGraphic( aScaledGraphic, OUString( "none" ), aStream, nFilterFormat, &aFilterData ); } IMPL_LINK_NOARG( CompressGraphicsDialog, NewWidthModifiedHdl ) @@ -266,7 +280,7 @@ IMPL_LINK_NOARG( CompressGraphicsDialog, CalculateClickHdl ) { sal_Int32 aSize = 0; - if ( m_dResolution > 0 ) + if ( m_dResolution > 0.0 ) { SvMemoryStream aMemStream; aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT ); @@ -286,38 +300,63 @@ IMPL_LINK_NOARG( CompressGraphicsDialog, CalculateClickHdl ) return 0L; } -SdrGrafObj* CompressGraphicsDialog::GetCompressedSdrGrafObj() +Rectangle CompressGraphicsDialog::GetScaledCropRectangle() { - if ( m_dResolution > 0 ) + if ( m_pReduceResolutionCB->IsChecked() ) { - SdrGrafObj* pNewObject = (SdrGrafObj*) m_pGraphicObj->Clone(); - - if ( m_pReduceResolutionCB->IsChecked() ) - { - const SdrGrafCropItem& rCrop = (const SdrGrafCropItem&) m_pGraphicObj->GetMergedItem(SDRATTR_GRAFCROP); - long nPixelX = (long)( GetViewWidthInch() * m_dResolution ); - long nPixelY = (long)( GetViewHeightInch() * m_dResolution ); - Size size = m_aGraphic.GetBitmapEx().GetSizePixel(); - double aScaleX = nPixelX / (double) size.Width(); - double aScaleY = nPixelY / (double) size.Height(); - - SdrGrafCropItem aNewCrop( - rCrop.GetLeft() * aScaleX, - rCrop.GetTop() * aScaleY, - rCrop.GetRight() * aScaleX, - rCrop.GetBottom()* aScaleY); - pNewObject->SetMergedItem(aNewCrop); - } + long nPixelX = (long)( GetViewWidthInch() * m_dResolution ); + long nPixelY = (long)( GetViewHeightInch() * m_dResolution ); + Size aSize = m_aGraphic.GetBitmapEx().GetSizePixel(); + double aScaleX = nPixelX / (double) aSize.Width(); + double aScaleY = nPixelY / (double) aSize.Height(); + + return Rectangle( + m_aCropRectangle.Left() * aScaleX, + m_aCropRectangle.Top() * aScaleY, + m_aCropRectangle.Right() * aScaleX, + m_aCropRectangle.Bottom()* aScaleY); + } + else + { + return m_aCropRectangle; + } +} +Graphic CompressGraphicsDialog::GetCompressedGraphic() +{ + if ( m_dResolution > 0.0 ) + { SvMemoryStream aMemStream; aMemStream.SetVersion( SOFFICE_FILEFORMAT_CURRENT ); Compress( aMemStream ); aMemStream.Seek( STREAM_SEEK_TO_BEGIN ); Graphic aResultGraphic; GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); - rFilter.ImportGraphic( aResultGraphic, String("import"), aMemStream ); + rFilter.ImportGraphic( aResultGraphic, OUString("import"), aMemStream ); - pNewObject->SetGraphic( aResultGraphic ); + return aResultGraphic; + } + return Graphic(); +} + +SdrGrafObj* CompressGraphicsDialog::GetCompressedSdrGrafObj() +{ + if ( m_dResolution > 0.0 ) + { + SdrGrafObj* pNewObject = (SdrGrafObj*) m_pGraphicObj->Clone(); + + if ( m_pReduceResolutionCB->IsChecked() ) + { + Rectangle aScaledCropedRectangle = GetScaledCropRectangle(); + SdrGrafCropItem aNewCrop( + aScaledCropedRectangle.Left(), + aScaledCropedRectangle.Top(), + aScaledCropedRectangle.Right(), + aScaledCropedRectangle.Bottom()); + + pNewObject->SetMergedItem(aNewCrop); + } + pNewObject->SetGraphic( GetCompressedGraphic() ); return pNewObject; } diff --git a/sw/sdi/_grfsh.sdi b/sw/sdi/_grfsh.sdi index 23afadd6f9ce..a76718431409 100644 --- a/sw/sdi/_grfsh.sdi +++ b/sw/sdi/_grfsh.sdi @@ -42,13 +42,18 @@ interface BaseTextGraphic DisableFlags="SW_DISABLE_ON_PROTECTED_CURSOR"; ] - SID_SAVE_GRAPHIC [ ExecMethod = Execute ; StateMethod = GetAttrState ; ] + SID_COMPRESS_GRAPHIC + [ + ExecMethod = Execute ; + StateMethod = GetAttrState ; + ] + SID_EXTERNAL_EDIT [ ExecMethod = Execute ; diff --git a/sw/source/ui/app/mn.src b/sw/source/ui/app/mn.src index 4dd44253774b..f95371d13902 100644 --- a/sw/source/ui/app/mn.src +++ b/sw/source/ui/app/mn.src @@ -1286,6 +1286,7 @@ Menu MN_GRF_POPUPMENU MN_FRM_CAPTION_ITEM SEPARATOR; MenuItem { ITEM_SAVE_GRAPHIC }; + MenuItem { ITEM_COMPRESS_GRAPHIC }; MenuItem { ITEM_EXTERNAL_EDIT }; SEPARATOR; MenuItem diff --git a/sw/source/ui/shells/grfsh.cxx b/sw/source/ui/shells/grfsh.cxx index bfee25b84501..f29eb7486221 100644 --- a/sw/source/ui/shells/grfsh.cxx +++ b/sw/source/ui/shells/grfsh.cxx @@ -41,6 +41,7 @@ #include #include #include +#include #include #include #include @@ -115,6 +116,50 @@ void SwGrfShell::Execute(SfxRequest &rReq) } } break; + case SID_COMPRESS_GRAPHIC: + { + const Graphic* pGraphic = rSh.GetGraphic(); + if( pGraphic ) + { + Size aSize ( + TWIP_TO_MM100(rSh.GetAnyCurRect(RECT_FLY_EMBEDDED).Width()), + TWIP_TO_MM100(rSh.GetAnyCurRect(RECT_FLY_EMBEDDED).Height())); + + SfxItemSet aSet( rSh.GetAttrPool(), RES_GRFATR_CROPGRF, RES_GRFATR_CROPGRF ); + rSh.GetCurAttr( aSet ); + SwCropGrf aCrop( (const SwCropGrf&) aSet.Get(RES_GRFATR_CROPGRF) ); + + Rectangle aCropRectangle( + TWIP_TO_MM100(aCrop.GetLeft()), + TWIP_TO_MM100(aCrop.GetTop()), + TWIP_TO_MM100(aCrop.GetRight()), + TWIP_TO_MM100(aCrop.GetBottom()) ); + + Graphic aGraphic = Graphic( *pGraphic ); + + CompressGraphicsDialog aDialog( GetView().GetWindow(), aGraphic, aSize, aCropRectangle, GetView().GetViewFrame()->GetBindings() ); + if( aDialog.Execute() == RET_OK ) + { + rSh.StartAllAction(); + rSh.StartUndo(UNDO_START); + Rectangle aScaledCropedRectangle = aDialog.GetScaledCropRectangle(); + + aCrop.SetLeft( MM100_TO_TWIP( aScaledCropedRectangle.Left() )); + aCrop.SetTop( MM100_TO_TWIP( aScaledCropedRectangle.Top() )); + aCrop.SetRight( MM100_TO_TWIP( aScaledCropedRectangle.Right() )); + aCrop.SetBottom( MM100_TO_TWIP( aScaledCropedRectangle.Bottom() )); + + Graphic aCompressedGraphic( aDialog.GetCompressedGraphic() ); + rSh.ReRead(aEmptyStr, aEmptyStr, (const Graphic*) &aCompressedGraphic); + + rSh.SetAttr(aCrop); + + rSh.EndUndo(UNDO_END); + rSh.EndAllAction(); + } + } + } + break; case SID_EXTERNAL_EDIT: { /* When the graphic is selected to be opened via some external tool -- cgit