LibreOffice Module svx (master)  1
compressgraphicdialog.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "dlgunit.hxx"
21 #include <vcl/graph.hxx>
22 #include <vcl/graphicfilter.hxx>
23 #include <vcl/virdev.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/settings.hxx>
26 #include <vcl/weld.hxx>
27 #include <svx/strings.hrc>
28 #include <svx/svdograf.hxx>
29 #include <svx/sdgcpitm.hxx>
30 #include <svx/dialmgr.hxx>
32 #include <sfx2/dispatch.hxx>
33 #include <sfx2/module.hxx>
34 #include <comphelper/fileformat.h>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <com/sun/star/beans/XPropertySet.hpp>
37 #include <tools/stream.hxx>
39 
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::beans;
42 
44  GenericDialogController( pParent, "svx/ui/compressgraphicdialog.ui", "CompressGraphicDialog" ),
45  m_xGraphicObj ( pGraphicObj ),
46  m_aGraphic ( pGraphicObj->GetGraphicObject().GetGraphic() ),
47  m_aViewSize100mm ( pGraphicObj->GetLogicRect().GetSize() ),
48  m_rBindings ( rBindings ),
49  m_dResolution ( 96.0 )
50 {
52  m_aCropRectangle = tools::Rectangle(rCrop.GetLeft(), rCrop.GetTop(), rCrop.GetRight(), rCrop.GetBottom());
53 
54  Initialize();
55 }
56 
57 CompressGraphicsDialog::CompressGraphicsDialog( weld::Window* pParent, Graphic const & rGraphic, Size rViewSize100mm, tools::Rectangle const & rCropRectangle, SfxBindings& rBindings ) :
58  GenericDialogController( pParent, "svx/ui/compressgraphicdialog.ui", "CompressGraphicDialog" ),
59  m_xGraphicObj ( nullptr ),
60  m_aGraphic ( rGraphic ),
61  m_aViewSize100mm ( rViewSize100mm ),
62  m_aCropRectangle ( rCropRectangle ),
63  m_rBindings ( rBindings ),
64  m_dResolution ( 96.0 )
65 {
66  Initialize();
67 }
68 
70 {
71 }
72 
74 {
75  m_xLabelGraphicType = m_xBuilder->weld_label("label-graphic-type");
76  m_xFixedText2 = m_xBuilder->weld_label("label-original-size");
77  m_xFixedText3 = m_xBuilder->weld_label("label-view-size");
78  m_xFixedText5 = m_xBuilder->weld_label("label-image-capacity");
79  m_xFixedText6 = m_xBuilder->weld_label("label-new-capacity");
80  m_xJpegCompRB = m_xBuilder->weld_radio_button("radio-jpeg");
81  m_xCompressionMF = m_xBuilder->weld_spin_button("spin-compression");
82  m_xCompressionSlider = m_xBuilder->weld_scale("scale-compression");
83  m_xLosslessRB = m_xBuilder->weld_radio_button("radio-lossless");
84  m_xQualityMF = m_xBuilder->weld_spin_button("spin-quality");
85  m_xQualitySlider = m_xBuilder->weld_scale("scale-quality");
86  m_xReduceResolutionCB = m_xBuilder->weld_check_button("checkbox-reduce-resolution");
87  m_xMFNewWidth = m_xBuilder->weld_spin_button("spin-new-width");
88  m_xMFNewHeight = m_xBuilder->weld_spin_button("spin-new-height");
89  m_xResolutionLB = m_xBuilder->weld_combo_box("combo-resolution");
90  m_xBtnCalculate = m_xBuilder->weld_button("calculate");
91  m_xInterpolationCombo = m_xBuilder->weld_combo_box("interpolation-method-combo");
92 
93  m_xInterpolationCombo->set_active_text("Lanczos");
94 
95  m_xInterpolationCombo->connect_changed(LINK(this, CompressGraphicsDialog, NewInterpolationModifiedHdl));
96 
97  m_xMFNewWidth->connect_changed( LINK( this, CompressGraphicsDialog, NewWidthModifiedHdl ));
98  m_xMFNewHeight->connect_changed( LINK( this, CompressGraphicsDialog, NewHeightModifiedHdl ));
99 
100  m_xResolutionLB->connect_changed( LINK( this, CompressGraphicsDialog, ResolutionModifiedHdl ));
101  m_xBtnCalculate->connect_clicked( LINK( this, CompressGraphicsDialog, CalculateClickHdl ) );
102 
103  m_xLosslessRB->connect_toggled( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
104  m_xJpegCompRB->connect_toggled( LINK( this, CompressGraphicsDialog, ToggleCompressionRB ) );
105 
106  m_xReduceResolutionCB->connect_toggled( LINK( this, CompressGraphicsDialog, ToggleReduceResolutionRB ) );
107 
108  m_xQualitySlider->connect_value_changed( LINK( this, CompressGraphicsDialog, SlideHdl ));
109  m_xCompressionSlider->connect_value_changed( LINK( this, CompressGraphicsDialog, SlideHdl ));
110  m_xQualityMF->connect_changed( LINK( this, CompressGraphicsDialog, NewQualityModifiedHdl ));
111  m_xCompressionMF->connect_changed( LINK( this, CompressGraphicsDialog, NewCompressionModifiedHdl ));
112 
113  m_xJpegCompRB->set_active(true);
114  m_xReduceResolutionCB->set_active(true);
115 
119  Update();
120 }
121 
123 {
124  GfxLinkType aLinkType = m_aGraphic.GetGfxLink().GetType();
125  OUString aGraphicTypeString;
126  switch(aLinkType)
127  {
128  case GfxLinkType::NativeGif:
129  aGraphicTypeString = SvxResId(STR_IMAGE_GIF);
130  break;
131  case GfxLinkType::NativeJpg:
132  aGraphicTypeString = SvxResId(STR_IMAGE_JPEG);
133  break;
134  case GfxLinkType::NativePng:
135  aGraphicTypeString = SvxResId(STR_IMAGE_PNG);
136  break;
137  case GfxLinkType::NativeTif:
138  aGraphicTypeString = SvxResId(STR_IMAGE_TIFF);
139  break;
140  case GfxLinkType::NativeWmf:
141  aGraphicTypeString = SvxResId(STR_IMAGE_WMF);
142  break;
143  case GfxLinkType::NativeMet:
144  aGraphicTypeString = SvxResId(STR_IMAGE_MET);
145  break;
146  case GfxLinkType::NativePct:
147  aGraphicTypeString = SvxResId(STR_IMAGE_PCT);
148  break;
149  case GfxLinkType::NativeSvg:
150  aGraphicTypeString = SvxResId(STR_IMAGE_SVG);
151  break;
152  case GfxLinkType::NativeBmp:
153  aGraphicTypeString = SvxResId(STR_IMAGE_BMP);
154  break;
155  default:
156  aGraphicTypeString = SvxResId(STR_IMAGE_UNKNOWN);
157  break;
158  }
159  m_xLabelGraphicType->set_label(aGraphicTypeString);
160 
161  const FieldUnit eFieldUnit = m_rBindings.GetDispatcher()->GetModule()->GetFieldUnit();
163  sal_Unicode cSeparator = rLocaleWrapper.getNumDecimalSep()[0];
164 
166  pDummyVDev->EnableOutput( false );
167  pDummyVDev->SetMapMode( m_aGraphic.GetPrefMapMode() );
168 
169  Size aPixelSize = m_aGraphic.GetSizePixel();
170  Size aOriginalSize100mm(pDummyVDev->PixelToLogic(m_aGraphic.GetSizePixel(), MapMode(MapUnit::Map100thMM)));
171 
172  OUString aBitmapSizeString = SvxResId(STR_IMAGE_ORIGINAL_SIZE);
173  OUString aWidthString = GetUnitString( aOriginalSize100mm.Width(), eFieldUnit, cSeparator );
174  OUString aHeightString = GetUnitString( aOriginalSize100mm.Height(), eFieldUnit, cSeparator );
175  aBitmapSizeString = aBitmapSizeString.replaceAll("$(WIDTH)", aWidthString);
176  aBitmapSizeString = aBitmapSizeString.replaceAll("$(HEIGHT)", aHeightString);
177  aBitmapSizeString = aBitmapSizeString.replaceAll("$(WIDTH_IN_PX)", OUString::number(aPixelSize.Width()));
178  aBitmapSizeString = aBitmapSizeString.replaceAll("$(HEIGHT_IN_PX)", OUString::number(aPixelSize.Height()));
179  m_xFixedText2->set_label(aBitmapSizeString);
180 
181  int aValX = static_cast<int>(aPixelSize.Width() / GetViewWidthInch());
182 
183  OUString aViewSizeString = SvxResId(STR_IMAGE_VIEW_SIZE);
184 
185  aWidthString = GetUnitString( m_aViewSize100mm.Width(), eFieldUnit, cSeparator );
186  aHeightString = GetUnitString( m_aViewSize100mm.Height(), eFieldUnit, cSeparator );
187  aViewSizeString = aViewSizeString.replaceAll("$(WIDTH)", aWidthString);
188  aViewSizeString = aViewSizeString.replaceAll("$(HEIGHT)", aHeightString);
189  aViewSizeString = aViewSizeString.replaceAll("$(DPI)", OUString::number(aValX));
190  m_xFixedText3->set_label(aViewSizeString);
191 
192  SvMemoryStream aMemStream;
194  m_aGraphic.ExportNative(aMemStream);
195  sal_Int32 aNativeSize = aMemStream.TellEnd();
196 
197  OUString aNativeSizeString = SvxResId(STR_IMAGE_CAPACITY);
198  aNativeSizeString = aNativeSizeString.replaceAll("$(CAPACITY)", OUString::number(aNativeSize / 1024));
199  m_xFixedText5->set_label(aNativeSizeString);
200 
201  m_xFixedText6->set_label("??");
202 }
203 
205 {
206  int nPixelX = static_cast<sal_Int32>( GetViewWidthInch() * m_dResolution );
207  m_xMFNewWidth->set_value(nPixelX);
208 }
209 
211 {
212  int nPixelY = static_cast<sal_Int32>( GetViewHeightInch() * m_dResolution );
213  m_xMFNewHeight->set_value(nPixelY);
214 }
215 
217 {
218  m_xResolutionLB->set_entry_text( OUString::number( static_cast<sal_Int32>(m_dResolution) ) );
219 }
220 
222 {
223  return static_cast<double>(MetricField::ConvertValue(m_aViewSize100mm.Width(), 2, MapUnit::Map100thMM, FieldUnit::INCH)) / 100.0;
224 }
225 
227 {
228  return static_cast<double>(MetricField::ConvertValue(m_aViewSize100mm.Height(), 2, MapUnit::Map100thMM, FieldUnit::INCH)) / 100.0;
229 }
230 
232 {
233  OUString aSelectionText = m_xInterpolationCombo->get_active_text();
234 
235  if( aSelectionText == "Lanczos" ) {
236  return BmpScaleFlag::Lanczos;
237  } else if( aSelectionText == "Bilinear" ) {
238  return BmpScaleFlag::BiLinear;
239  } else if( aSelectionText == "Bicubic" ) {
240  return BmpScaleFlag::BiCubic;
241  } else if ( aSelectionText == "None" ) {
242  return BmpScaleFlag::Fast;
243  }
244  return BmpScaleFlag::BestQuality;
245 }
246 
248 {
249  BitmapEx aBitmap = m_aGraphic.GetBitmapEx();
250  if ( m_xReduceResolutionCB->get_active() )
251  {
252  long nPixelX = static_cast<long>( GetViewWidthInch() * m_dResolution );
253  long nPixelY = static_cast<long>( GetViewHeightInch() * m_dResolution );
254 
255  aBitmap.Scale( Size( nPixelX, nPixelY ), GetSelectedInterpolationType() );
256  }
257  Graphic aScaledGraphic( aBitmap );
259 
260  Sequence< PropertyValue > aFilterData( 3 );
261  aFilterData[ 0 ].Name = "Interlaced";
262  aFilterData[ 0 ].Value <<= sal_Int32(0);
263  aFilterData[ 1 ].Name = "Compression";
264  aFilterData[ 1 ].Value <<= static_cast<sal_Int32>(m_xCompressionMF->get_value());
265  aFilterData[ 2 ].Name = "Quality";
266  aFilterData[ 2 ].Value <<= static_cast<sal_Int32>(m_xQualityMF->get_value());
267 
268  OUString aGraphicFormatName = m_xLosslessRB->get_active() ? OUString( "png" ) : OUString( "jpg" );
269 
270  sal_uInt16 nFilterFormat = rFilter.GetExportFormatNumberForShortName( aGraphicFormatName );
271  rFilter.ExportGraphic( aScaledGraphic, "none", aStream, nFilterFormat, &aFilterData );
272 }
273 
274 IMPL_LINK_NOARG( CompressGraphicsDialog, NewWidthModifiedHdl, weld::Entry&, void )
275 {
276  m_dResolution = m_xMFNewWidth->get_value() / GetViewWidthInch();
277 
278  UpdateNewHeightMF();
279  UpdateResolutionLB();
280  Update();
281 }
282 
283 IMPL_LINK( CompressGraphicsDialog, SlideHdl, weld::Scale&, rScale, void )
284 {
285  if (&rScale == m_xQualitySlider.get())
286  m_xQualityMF->set_value(m_xQualitySlider->get_value());
287  else
288  m_xCompressionMF->set_value(m_xCompressionSlider->get_value());
289  Update();
290 }
291 
292 IMPL_LINK_NOARG( CompressGraphicsDialog, NewInterpolationModifiedHdl, weld::ComboBox&, void )
293 {
294  Update();
295 }
296 
297 IMPL_LINK_NOARG( CompressGraphicsDialog, NewQualityModifiedHdl, weld::Entry&, void )
298 {
299  m_xQualitySlider->set_value(m_xQualityMF->get_value());
300  Update();
301 }
302 
303 IMPL_LINK_NOARG( CompressGraphicsDialog, NewCompressionModifiedHdl, weld::Entry&, void )
304 {
305  m_xCompressionSlider->set_value(m_xCompressionMF->get_value());
306  Update();
307 }
308 
309 IMPL_LINK_NOARG( CompressGraphicsDialog, NewHeightModifiedHdl, weld::Entry&, void )
310 {
311  m_dResolution = m_xMFNewHeight->get_value() / GetViewHeightInch();
312 
313  UpdateNewWidthMF();
314  UpdateResolutionLB();
315  Update();
316 }
317 
318 IMPL_LINK_NOARG( CompressGraphicsDialog, ResolutionModifiedHdl, weld::ComboBox&, void )
319 {
320  m_dResolution = static_cast<double>(m_xResolutionLB->get_active_text().toInt32());
321 
322  UpdateNewWidthMF();
323  UpdateNewHeightMF();
324  Update();
325 }
326 
328 {
329  bool choice = m_xLosslessRB->get_active();
330  m_xCompressionMF->set_sensitive(choice);
331  m_xCompressionSlider->set_sensitive(choice);
332  m_xQualityMF->set_sensitive(!choice);
333  m_xQualitySlider->set_sensitive(!choice);
334  Update();
335 }
336 
337 IMPL_LINK_NOARG( CompressGraphicsDialog, ToggleReduceResolutionRB, weld::ToggleButton&, void )
338 {
339  bool choice = m_xReduceResolutionCB->get_active();
340  m_xMFNewWidth->set_sensitive(choice);
341  m_xMFNewHeight->set_sensitive(choice);
342  m_xResolutionLB->set_sensitive(choice);
343  m_xInterpolationCombo->set_sensitive(choice);
344  Update();
345 }
346 
348 {
349  sal_Int32 aSize = 0;
350 
351  if ( m_dResolution > 0.0 )
352  {
353  SvMemoryStream aMemStream;
355  Compress( aMemStream );
356  aSize = aMemStream.TellEnd();
357  }
358 
359  if ( aSize > 0 )
360  {
361  OUString aSizeAsString = OUString::number(aSize / 1024);
362 
363  OUString aNewSizeString = SvxResId(STR_IMAGE_CAPACITY);
364  aNewSizeString = aNewSizeString.replaceAll("$(CAPACITY)", aSizeAsString);
365  m_xFixedText6->set_label(aNewSizeString);
366  }
367 }
368 
370 {
371  if ( m_xReduceResolutionCB->get_active() )
372  {
373  long nPixelX = static_cast<long>( GetViewWidthInch() * m_dResolution );
374  long nPixelY = static_cast<long>( GetViewHeightInch() * m_dResolution );
376  double aScaleX = nPixelX / static_cast<double>(aSize.Width());
377  double aScaleY = nPixelY / static_cast<double>(aSize.Height());
378 
379  return tools::Rectangle(
380  m_aCropRectangle.Left() * aScaleX,
381  m_aCropRectangle.Top() * aScaleY,
382  m_aCropRectangle.Right() * aScaleX,
383  m_aCropRectangle.Bottom()* aScaleY);
384  }
385  else
386  {
387  return m_aCropRectangle;
388  }
389 }
390 
392 {
393  if ( m_dResolution > 0.0 )
394  {
395  SvMemoryStream aMemStream;
397  Compress( aMemStream );
398  aMemStream.Seek( STREAM_SEEK_TO_BEGIN );
399  Graphic aResultGraphic;
401  rFilter.ImportGraphic( aResultGraphic, OUString("import"), aMemStream );
402 
403  return aResultGraphic;
404  }
405  return Graphic();
406 }
407 
409 {
410  if ( m_dResolution > 0.0 )
411  {
413 
414  if ( m_xReduceResolutionCB->get_active() )
415  {
416  tools::Rectangle aScaledCropedRectangle = GetScaledCropRectangle();
417  SdrGrafCropItem aNewCrop(
418  aScaledCropedRectangle.Left(),
419  aScaledCropedRectangle.Top(),
420  aScaledCropedRectangle.Right(),
421  aScaledCropedRectangle.Bottom());
422 
423  pNewObject->SetMergedItem(aNewCrop);
424  }
425  pNewObject->SetGraphic( GetCompressedGraphic() );
426 
427  return pNewObject;
428  }
429  return nullptr;
430 }
431 
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
OUString GetUnitString(long nVal_100, FieldUnit eFieldUnit, sal_Unicode cSep)
Definition: dlgunit.hxx:26
std::unique_ptr< weld::Label > m_xFixedText5
FieldUnit
sal_Int32 GetLeft() const
Definition: grfcrop.hxx:61
ErrCode ImportGraphic(Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat=GRFILTER_FORMAT_DONTKNOW, sal_uInt16 *pDeterminedFormat=nullptr, GraphicFilterImportFlags nImportFlags=GraphicFilterImportFlags::NONE)
std::unique_ptr< weld::Builder > m_xBuilder
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
long Height() const
bool ExportNative(SvStream &rOStream) const
bool Scale(const Size &rNewSize, BmpScaleFlag nScaleFlag=BmpScaleFlag::Default)
static const AllSettings & GetSettings()
std::unique_ptr< weld::SpinButton > m_xMFNewWidth
#define SOFFICE_FILEFORMAT_CURRENT
std::unique_ptr< weld::RadioButton > m_xLosslessRB
sal_uInt64 Seek(sal_uInt64 nPos)
virtual ~CompressGraphicsDialog() override
std::unique_ptr< weld::RadioButton > m_xJpegCompRB
virtual sal_uInt64 TellEnd() override
IMPL_LINK_NOARG(CompressGraphicsDialog, NewWidthModifiedHdl, weld::Entry &, void)
std::unique_ptr< weld::Scale > m_xQualitySlider
SfxModule * GetModule() const
sal_uInt16 sal_Unicode
long Right() const
std::unique_ptr< weld::SpinButton > m_xQualityMF
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
void SetGraphic(const Graphic &rGrf)
Definition: svdograf.cxx:324
std::unique_ptr< weld::Button > m_xBtnCalculate
sal_uInt16 GetExportFormatNumberForShortName(const OUString &rShortName)
std::unique_ptr< weld::ComboBox > m_xInterpolationCombo
void Compress(SvStream &aStream)
long Top() const
CompressGraphicsDialog(weld::Window *pParent, SdrGrafObj *pGraphicObj, SfxBindings &rBindings)
std::unique_ptr< weld::Label > m_xLabelGraphicType
const OUString & getNumDecimalSep() const
FieldUnit GetFieldUnit() const
tools::Rectangle GetScaledCropRectangle() const
#define STREAM_SEEK_TO_BEGIN
#define SDRATTR_GRAFCROP
Definition: svddef.hxx:317
long Bottom() const
SdrModel & getSdrModelFromSdrObject() const
Definition: svdobj.cxx:299
BmpScaleFlag
const LocaleDataWrapper & GetLocaleDataWrapper(LanguageType nLang)
void SetMergedItem(const SfxPoolItem &rItem)
Definition: svdobj.cxx:1921
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
const SfxPoolItem & GetMergedItem(const sal_uInt16 nWhich) const
Definition: svdobj.cxx:1946
void SetVersion(sal_Int32 n)
This class represents an embedded or linked bitmap graphic object.
Definition: svdograf.hxx:79
GfxLink GetGfxLink() const
std::unique_ptr< weld::Label > m_xFixedText3
ErrCode ExportGraphic(const Graphic &rGraphic, const INetURLObject &rPath, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue > *pFilterData=nullptr)
std::unique_ptr< weld::Label > m_xFixedText6
sal_Int32 GetBottom() const
Definition: grfcrop.hxx:64
IMPL_LINK(CompressGraphicsDialog, SlideHdl, weld::Scale &, rScale, void)
sal_Int32 GetTop() const
Definition: grfcrop.hxx:63
std::unique_ptr< weld::CheckButton > m_xReduceResolutionCB
MapMode GetPrefMapMode() const
static sal_Int64 ConvertValue(sal_Int64 nValue, sal_Int64 mnBaseValue, sal_uInt16 nDecDigits, FieldUnit eInUnit, FieldUnit eOutUnit)
virtual SdrGrafObj * CloneSdrObject(SdrModel &rTargetModel) const override
Definition: svdograf.cxx:721
BmpScaleFlag GetSelectedInterpolationType() const
std::unique_ptr< weld::Scale > m_xCompressionSlider
long Left() const
sal_Int32 GetRight() const
Definition: grfcrop.hxx:62
std::unique_ptr< weld::Label > m_xFixedText2
SfxDispatcher * GetDispatcher() const
static GraphicFilter & GetGraphicFilter()
std::unique_ptr< weld::SpinButton > m_xCompressionMF
const Size & GetSizePixel() const
std::unique_ptr< weld::ComboBox > m_xResolutionLB
std::unique_ptr< weld::SpinButton > m_xMFNewHeight