Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sdext / source / minimizer / optimizerdialogcontrols.cxx
blob5ed2dd942b7e3cb2c1a8e3157f169c1aa8f09b71
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "optimizerdialog.hxx"
24 #include "pppoptimizer.hxx"
25 #include "graphiccollector.hxx"
26 #include "pagecollector.hxx"
27 #include <com/sun/star/awt/PushButtonType.hpp>
28 #include <com/sun/star/awt/XSpinField.hpp>
29 #include <com/sun/star/awt/XTextComponent.hpp>
30 #include <com/sun/star/presentation/XCustomPresentationSupplier.hpp>
31 #include <com/sun/star/drawing/XMasterPagesSupplier.hpp>
32 #include <com/sun/star/drawing/XDrawPagesSupplier.hpp>
33 #include <com/sun/star/awt/FontDescriptor.hpp>
34 #include <com/sun/star/awt/FontWeight.hpp>
35 #include <com/sun/star/frame/XStorable.hpp>
36 #include <rtl/ustrbuf.hxx>
37 #include <sal/macros.h>
38 #include <o3tl/string_view.hxx>
40 using namespace ::com::sun::star::awt;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::util;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::frame;
45 using namespace ::com::sun::star::beans;
46 using namespace ::com::sun::star::drawing;
47 using namespace ::com::sun::star::container;
48 using namespace ::com::sun::star::presentation;
50 void OptimizerDialog::InitNavigationBar()
52 m_xHelp->hide();
55 void OptimizerDialog::UpdateControlStatesPage0()
57 int nSelectedItem = -1;
58 std::vector<OUString> aItemList;
59 const std::vector< OptimizerSettings >& rList( GetOptimizerSettings() );
60 if ( rList.size() > 1 ) // the first session in the list is the actual one -> skipping first one
62 for ( std::vector<OptimizerSettings>::size_type i = 1; i < rList.size(); i++ )
64 aItemList.push_back(rList[i].maName);
65 if ( nSelectedItem < 0 )
67 if ( rList[ i ] == rList[ 0 ] )
68 nSelectedItem = static_cast< short >( i - 1 );
72 bool bRemoveButtonEnabled = false;
73 if ( nSelectedItem >= 0 )
75 if ( nSelectedItem > 2 ) // only allowing to delete custom themes, the first can|t be deleted
76 bRemoveButtonEnabled = true;
78 mpPage0->UpdateControlStates(aItemList, nSelectedItem, bRemoveButtonEnabled);
81 void OptimizerDialog::InitPage0()
83 UpdateControlStatesPage0();
86 void OptimizerDialog::UpdateControlStatesPage1()
88 bool bDeleteUnusedMasterPages( GetConfigProperty( TK_DeleteUnusedMasterPages, false ) );
89 bool bDeleteHiddenSlides( GetConfigProperty( TK_DeleteHiddenSlides, false ) );
90 bool bDeleteNotesPages( GetConfigProperty( TK_DeleteNotesPages, false ) );
91 mpPage1->UpdateControlStates(bDeleteUnusedMasterPages, bDeleteHiddenSlides, bDeleteNotesPages);
94 void OptimizerDialog::InitPage1()
96 Sequence< OUString > aCustomShowList;
97 Reference< XModel > xModel( mxController->getModel() );
98 if ( xModel.is() )
100 Reference< XCustomPresentationSupplier > aXCPSup( xModel, UNO_QUERY_THROW );
101 Reference< XNameContainer > aXCont( aXCPSup->getCustomPresentations() );
102 if ( aXCont.is() )
103 aCustomShowList = aXCont->getElementNames();
105 mpPage1->Init(aCustomShowList);
107 UpdateControlStatesPage1();
110 void OptimizerDialog::UpdateControlStatesPage2()
112 bool bJPEGCompression( GetConfigProperty( TK_JPEGCompression, false ) );
113 bool bRemoveCropArea( GetConfigProperty( TK_RemoveCropArea, false ) );
114 bool bEmbedLinkedGraphics( GetConfigProperty( TK_EmbedLinkedGraphics, true ) );
115 sal_Int32 nJPEGQuality( GetConfigProperty( TK_JPEGQuality, sal_Int32(90) ) );
116 sal_Int32 nImageResolution( GetConfigProperty( TK_ImageResolution, sal_Int32(0) ) );
118 mpPage2->UpdateControlStates(bJPEGCompression, nJPEGQuality, bRemoveCropArea, nImageResolution, bEmbedLinkedGraphics);
121 void OptimizerDialog::InitPage2()
123 UpdateControlStatesPage2();
126 void OptimizerDialog::UpdateControlStatesPage3()
128 bool bConvertOLEObjects( GetConfigProperty( TK_OLEOptimization, false ) );
129 sal_Int16 nOLEOptimizationType( GetConfigProperty( TK_OLEOptimizationType, sal_Int16(0) ) );
131 mpPage3->UpdateControlStates(bConvertOLEObjects, nOLEOptimizationType);
134 void OptimizerDialog::InitPage3()
136 int nOLECount = 0;
137 Reference< XModel > xModel( mxController->getModel() );
138 Reference< XDrawPagesSupplier > xDrawPagesSupplier( xModel, UNO_QUERY_THROW );
139 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
140 for ( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
142 Reference< XShapes > xShapes( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
143 for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
145 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
146 if ( xShape->getShapeType() == "com.sun.star.drawing.OLE2Shape" )
147 nOLECount++;
151 mpPage3->Init(nOLECount ? getString(STR_OLE_OBJECTS_DESC) : getString(STR_NO_OLE_OBJECTS_DESC));
153 UpdateControlStatesPage3();
156 static OUString ImpValueOfInMB( sal_Int64 rVal, sal_Unicode nSeparator )
158 double fVal( static_cast<double>( rVal ) );
159 fVal /= ( 1 << 20 );
160 fVal += 0.05;
161 OUStringBuffer aVal( OUString::number( fVal ) );
162 sal_Int32 nX( aVal.indexOf( '.' ) );
163 if ( nX >= 0 )
165 aVal.setLength( nX + 2 );
166 aVal[nX] = nSeparator;
168 aVal.append( " MB" );
169 return aVal.makeStringAndClear();
172 void OptimizerDialog::UpdateControlStatesPage4()
174 bool bSaveAs( GetConfigProperty( TK_SaveAs, true ) );
175 if ( mbIsReadonly )
176 bSaveAs = true;
178 std::vector<OUString> aItemList;
179 const std::vector< OptimizerSettings >& rList( GetOptimizerSettings() );
180 if ( rList.size() > 1 ) // the first session in the list is the actual one -> skipping first one
182 for ( std::vector<OptimizerSettings>::size_type w = 1; w < rList.size(); w++ )
183 aItemList.push_back(rList[ w ].maName);
186 // now check if it is sensible to enable the combo box
187 bool bSaveSettingsEnabled = true;
188 if ( rList.size() > 1 ) // the first session in the list is the actual one -> skipping first one
190 for ( std::vector<OptimizerSettings>::size_type w = 1; w < rList.size(); w++ )
192 if ( rList[ w ] == rList[ 0 ] )
194 bSaveSettingsEnabled = false;
195 break;
200 std::vector< OUString > aSummaryStrings;
202 // taking care of deleted slides
203 sal_Int32 nDeletedSlides = 0;
204 OUString sTKCustomShowName(mpPage1->Get_TK_CustomShowName());
205 if (!sTKCustomShowName.isEmpty())
206 SetConfigProperty(TK_CustomShowName, Any(sTKCustomShowName));
207 if ( GetConfigProperty( TK_DeleteHiddenSlides, false ) )
209 Reference< XDrawPagesSupplier > xDrawPagesSupplier( mxController->getModel(), UNO_QUERY_THROW );
210 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
211 for( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
213 Reference< XDrawPage > xDrawPage( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
214 Reference< XPropertySet > xPropSet( xDrawPage, UNO_QUERY_THROW );
216 bool bVisible = true;
217 if ( xPropSet->getPropertyValue( "Visible" ) >>= bVisible )
219 if (!bVisible )
220 nDeletedSlides++;
224 if ( GetConfigProperty( TK_DeleteUnusedMasterPages, false ) )
226 std::vector< PageCollector::MasterPageEntity > aMasterPageList;
227 PageCollector::CollectMasterPages( mxController->getModel(), aMasterPageList );
228 Reference< XMasterPagesSupplier > xMasterPagesSupplier( mxController->getModel(), UNO_QUERY_THROW );
229 Reference< XDrawPages > xMasterPages( xMasterPagesSupplier->getMasterPages(), UNO_SET_THROW );
230 nDeletedSlides += std::count_if(aMasterPageList.begin(), aMasterPageList.end(),
231 [](const PageCollector::MasterPageEntity& rEntity) { return !rEntity.bUsed; });
233 if ( nDeletedSlides > 1 )
235 OUString aStr( getString( STR_DELETE_SLIDES ) );
236 OUString aPlaceholder( "%SLIDES" );
237 sal_Int32 i = aStr.indexOf( aPlaceholder );
238 if ( i >= 0 )
239 aStr = aStr.replaceAt( i, aPlaceholder.getLength(), OUString::number( nDeletedSlides ) );
240 aSummaryStrings.push_back( aStr );
243 // generating graphic compression info
244 sal_Int32 nGraphics = 0;
245 bool bJPEGCompression( GetConfigProperty( TK_JPEGCompression, false ) );
246 sal_Int32 nJPEGQuality( GetConfigProperty( TK_JPEGQuality, sal_Int32(90) ) );
247 sal_Int32 nImageResolution( GetConfigProperty( TK_ImageResolution, sal_Int32(0) ) );
248 GraphicSettings aGraphicSettings( bJPEGCompression, nJPEGQuality, GetConfigProperty( TK_RemoveCropArea, false ),
249 nImageResolution, GetConfigProperty( TK_EmbedLinkedGraphics, true ) );
250 GraphicCollector::CountGraphics( mxContext, mxController->getModel(), aGraphicSettings, nGraphics );
251 if ( nGraphics > 1 )
253 OUString aStr( getString( STR_OPTIMIZE_IMAGES ) );
254 OUString aImagePlaceholder( "%IMAGES" );
255 OUString aQualityPlaceholder( "%QUALITY" );
256 OUString aResolutionPlaceholder( "%RESOLUTION" );
257 sal_Int32 i = aStr.indexOf( aImagePlaceholder );
258 if ( i >= 0 )
259 aStr = aStr.replaceAt( i, aImagePlaceholder.getLength(), OUString::number( nGraphics ) );
261 sal_Int32 j = aStr.indexOf( aQualityPlaceholder );
262 if ( j >= 0 )
263 aStr = aStr.replaceAt( j, aQualityPlaceholder.getLength(), OUString::number( nJPEGQuality ) );
265 sal_Int32 k = aStr.indexOf( aResolutionPlaceholder );
266 if ( k >= 0 )
267 aStr = aStr.replaceAt( k, aResolutionPlaceholder.getLength(), OUString::number( nImageResolution ) );
269 aSummaryStrings.push_back( aStr );
272 if ( GetConfigProperty( TK_OLEOptimization, false ) )
274 sal_Int32 nOLEReplacements = 0;
275 Reference< XDrawPagesSupplier > xDrawPagesSupplier( mxController->getModel(), UNO_QUERY_THROW );
276 Reference< XDrawPages > xDrawPages( xDrawPagesSupplier->getDrawPages(), UNO_SET_THROW );
277 for ( sal_Int32 i = 0; i < xDrawPages->getCount(); i++ )
279 Reference< XShapes > xShapes( xDrawPages->getByIndex( i ), UNO_QUERY_THROW );
280 for ( sal_Int32 j = 0; j < xShapes->getCount(); j++ )
282 Reference< XShape > xShape( xShapes->getByIndex( j ), UNO_QUERY_THROW );
283 if ( xShape->getShapeType() == "com.sun.star.drawing.OLE2Shape" )
284 nOLEReplacements++;
287 if ( nOLEReplacements > 1 )
289 OUString aStr( getString( STR_CREATE_REPLACEMENT ) );
290 OUString aPlaceholder( "%OLE" );
291 sal_Int32 i = aStr.indexOf( aPlaceholder );
292 if ( i >= 0 )
293 aStr = aStr.replaceAt( i, aPlaceholder.getLength(), OUString::number( nOLEReplacements ) );
294 aSummaryStrings.push_back( aStr );
297 while( aSummaryStrings.size() < 3 )
298 aSummaryStrings.emplace_back( );
300 sal_Int64 nCurrentFileSize = 0;
301 sal_Int64 nEstimatedFileSize = 0;
302 Reference< XStorable > xStorable( mxController->getModel(), UNO_QUERY );
303 if ( xStorable.is() && xStorable->hasLocation() )
304 nCurrentFileSize = PPPOptimizer::GetFileSize( xStorable->getLocation() );
306 if ( nCurrentFileSize )
308 double fE = static_cast< double >( nCurrentFileSize );
309 if ( nImageResolution )
311 double v = ( static_cast< double >( nImageResolution ) + 75.0 ) / 300.0;
312 if ( v < 1.0 )
313 fE *= v;
315 if ( bJPEGCompression )
317 double v = 0.75 - ( ( 100.0 - static_cast< double >( nJPEGQuality ) ) / 400.0 ) ;
318 fE *= v;
320 nEstimatedFileSize = static_cast< sal_Int64 >( fE );
322 sal_Unicode nSeparator = '.';
323 OUString aStr( getString( STR_FILESIZESEPARATOR ) );
324 if ( !aStr.isEmpty() )
325 nSeparator = aStr[ 0 ];
326 mpPage4->UpdateControlStates(bSaveAs, bSaveSettingsEnabled, aItemList,
327 aSummaryStrings,
328 ImpValueOfInMB(nCurrentFileSize, nSeparator),
329 ImpValueOfInMB(nEstimatedFileSize, nSeparator));
330 SetConfigProperty( TK_EstimatedFileSize, Any( nEstimatedFileSize ) );
333 void OptimizerDialog::InitPage4()
335 // creating a default session name that hasn't been used yet
336 OUString aSettingsName;
337 OUString aDefault( getString( STR_MY_SETTINGS ) );
338 sal_Int32 nSession = 1;
339 std::vector<OptimizerSettings>::size_type i;
340 const std::vector< OptimizerSettings >& rList( GetOptimizerSettings() );
343 OUString aTemp = aDefault + OUString::number( nSession++ );
344 for ( i = 1; i < rList.size(); i++ )
346 if ( rList[ i ].maName == aTemp )
347 break;
349 if ( i == rList.size() )
350 aSettingsName = aTemp;
352 while( aSettingsName.isEmpty() );
354 mpPage4->Init(aSettingsName, mbIsReadonly);
356 UpdateControlStatesPage4();
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */