bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / dialogs / cuigrfflt.cxx
blob89794f77870962be21a4aab260fbd1aa815e2750
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 .
20 #include <vcl/builderfactory.hxx>
21 #include <vcl/msgbox.hxx>
22 #include <sfx2/viewfrm.hxx>
23 #include <sfx2/viewsh.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <sfx2/request.hxx>
26 #include <dialmgr.hxx>
27 #include "cuigrfflt.hxx"
28 #include <cuires.hrc>
29 #include <svx/dialogs.hrc>
31 GraphicPreviewWindow::GraphicPreviewWindow(vcl::Window* pParent,
32 const WinBits nStyle)
33 : Control(pParent, nStyle)
34 , mpOrigGraphic(NULL)
35 , mfScaleX(0.0)
36 , mfScaleY(0.0)
40 VCL_BUILDER_DECL_FACTORY(GraphicPreviewWindow)
42 WinBits nWinBits = WB_TABSTOP;
44 OString sBorder = VclBuilder::extractCustomProperty(rMap);
45 if (!sBorder.isEmpty())
46 nWinBits |= WB_BORDER;
48 rRet = VclPtr<GraphicPreviewWindow>::Create(pParent, nWinBits);
51 Size GraphicPreviewWindow::GetOptimalSize() const
53 return LogicToPixel(Size(81, 73), MAP_APPFONT);
56 void GraphicPreviewWindow::Paint(vcl::RenderContext& rRenderContext, const Rectangle& rRect)
58 Control::Paint(rRenderContext, rRect);
60 const Size aOutputSize(GetOutputSizePixel());
62 if (maPreview.IsAnimated())
64 const Size aGraphicSize(rRenderContext.LogicToPixel(maPreview.GetPrefSize(), maPreview.GetPrefMapMode()));
65 const Point aGraphicPosition((aOutputSize.Width() - aGraphicSize.Width() ) >> 1,
66 (aOutputSize.Height() - aGraphicSize.Height() ) >> 1);
67 maPreview.StartAnimation(&rRenderContext, aGraphicPosition, aGraphicSize);
69 else
71 const Size aGraphicSize(maPreview.GetSizePixel());
72 const Point aGraphicPosition((aOutputSize.Width() - aGraphicSize.Width()) >> 1,
73 (aOutputSize.Height() - aGraphicSize.Height()) >> 1);
74 maPreview.Draw(&rRenderContext, aGraphicPosition, aGraphicSize);
78 void GraphicPreviewWindow::SetPreview(const Graphic& rGraphic)
80 maPreview = rGraphic;
81 Invalidate();
84 void GraphicPreviewWindow::ScaleImageToFit()
86 if (!mpOrigGraphic)
87 return;
89 maScaledOrig = *mpOrigGraphic;
91 const Size aPreviewSize( GetOutputSizePixel() );
92 Size aSizePixel(LogicToPixel(mpOrigGraphic->GetPrefSize(),
93 mpOrigGraphic->GetPrefMapMode()));
94 Size aGrfSize(aSizePixel);
96 if( mpOrigGraphic->GetType() == GRAPHIC_BITMAP &&
97 aPreviewSize.Width() && aPreviewSize.Height() &&
98 aGrfSize.Width() && aGrfSize.Height() )
100 const double fGrfWH = (double) aGrfSize.Width() / aGrfSize.Height();
101 const double fPreWH = (double) aPreviewSize.Width() / aPreviewSize.Height();
103 if( fGrfWH < fPreWH )
105 aGrfSize.Width() = (long) ( aPreviewSize.Height() * fGrfWH );
106 aGrfSize.Height() = aPreviewSize.Height();
108 else
110 aGrfSize.Width() = aPreviewSize.Width();
111 aGrfSize.Height() = (long) ( aPreviewSize.Width() / fGrfWH );
114 mfScaleX = (double) aGrfSize.Width() / aSizePixel.Width();
115 mfScaleY = (double) aGrfSize.Height() / aSizePixel.Height();
117 if( !mpOrigGraphic->IsAnimated() )
119 BitmapEx aBmpEx( mpOrigGraphic->GetBitmapEx() );
121 if( aBmpEx.Scale( aGrfSize, BmpScaleFlag::Default ) )
122 maScaledOrig = aBmpEx;
126 maModifyHdl.Call(this);
129 void GraphicPreviewWindow::Resize()
131 Control::Resize();
132 ScaleImageToFit();
135 GraphicFilterDialog::GraphicFilterDialog(vcl::Window* pParent,
136 const OUString& rID, const OUString& rUIXMLDescription,
137 const Graphic& rGraphic)
138 : ModalDialog(pParent, rID, rUIXMLDescription)
139 , maModifyHdl(LINK( this, GraphicFilterDialog, ImplModifyHdl))
140 , maSizePixel(LogicToPixel(rGraphic.GetPrefSize(),
141 rGraphic.GetPrefMapMode()))
143 bIsBitmap = rGraphic.GetType() == GRAPHIC_BITMAP;
145 maTimer.SetTimeoutHdl( LINK( this, GraphicFilterDialog, ImplPreviewTimeoutHdl ) );
146 maTimer.SetTimeout( 5 );
148 get(mpPreview, "preview");
149 mpPreview->init(&rGraphic, maModifyHdl);
152 GraphicFilterDialog::~GraphicFilterDialog()
154 disposeOnce();
157 void GraphicFilterDialog::dispose()
159 mpPreview.clear();
160 ModalDialog::dispose();
164 IMPL_LINK_NOARG_TYPED(GraphicFilterDialog, ImplPreviewTimeoutHdl, Timer *, void)
166 maTimer.Stop();
167 mpPreview->SetPreview(GetFilteredGraphic(mpPreview->GetScaledOriginal(),
168 mpPreview->GetScaleX(), mpPreview->GetScaleY()));
173 IMPL_LINK_NOARG(GraphicFilterDialog, ImplModifyHdl)
175 if (bIsBitmap)
177 maTimer.Stop();
178 maTimer.Start();
181 return 0;
185 // - FilterMosaic -
188 GraphicFilterMosaic::GraphicFilterMosaic( vcl::Window* pParent, const Graphic& rGraphic,
189 sal_uInt16 nTileWidth, sal_uInt16 nTileHeight, bool bEnhanceEdges )
190 : GraphicFilterDialog(pParent, "MosaicDialog",
191 "cui/ui/mosaicdialog.ui", rGraphic)
193 get(mpMtrWidth, "width");
194 get(mpMtrHeight, "height");
195 get(mpCbxEdges, "edges");
197 mpMtrWidth->SetValue( nTileWidth );
198 mpMtrWidth->SetLast( GetGraphicSizePixel().Width() );
199 mpMtrWidth->SetModifyHdl( GetModifyHdl() );
201 mpMtrHeight->SetValue( nTileHeight );
202 mpMtrHeight->SetLast( GetGraphicSizePixel().Height() );
203 mpMtrHeight->SetModifyHdl( GetModifyHdl() );
205 mpCbxEdges->Check( bEnhanceEdges );
206 mpCbxEdges->SetToggleHdl( GetModifyHdl() );
208 mpMtrWidth->GrabFocus();
211 GraphicFilterMosaic::~GraphicFilterMosaic()
213 disposeOnce();
216 void GraphicFilterMosaic::dispose()
218 mpMtrWidth.clear();
219 mpMtrHeight.clear();
220 mpCbxEdges.clear();
221 GraphicFilterDialog::dispose();
224 Graphic GraphicFilterMosaic::GetFilteredGraphic( const Graphic& rGraphic,
225 double fScaleX, double fScaleY )
227 Graphic aRet;
228 const Size aSize( std::max( FRound( GetTileWidth() * fScaleX ), 1L ),
229 std::max( FRound( GetTileHeight() * fScaleY ), 1L ) );
230 BmpFilterParam aParam( aSize );
232 if( rGraphic.IsAnimated() )
234 Animation aAnim( rGraphic.GetAnimation() );
236 if( aAnim.Filter( BMP_FILTER_MOSAIC, &aParam ) )
238 if( IsEnhanceEdges() )
239 aAnim.Filter( BMP_FILTER_SHARPEN );
241 aRet = aAnim;
244 else
246 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
248 if( aBmpEx.Filter( BMP_FILTER_MOSAIC, &aParam ) )
250 if( IsEnhanceEdges() )
251 (void)aBmpEx.Filter( BMP_FILTER_SHARPEN );
253 aRet = aBmpEx;
257 return aRet;
261 // - GraphicFilterSmooth -
264 GraphicFilterSmooth::GraphicFilterSmooth( vcl::Window* pParent, const Graphic& rGraphic, double nRadius)
265 : GraphicFilterDialog(pParent, "SmoothDialog",
266 "cui/ui/smoothdialog.ui", rGraphic)
268 get(mpMtrRadius, "radius");
270 mpMtrRadius->SetValue( nRadius* 10 );
271 mpMtrRadius->SetModifyHdl( GetModifyHdl() );
272 mpMtrRadius->GrabFocus();
275 GraphicFilterSmooth::~GraphicFilterSmooth()
277 disposeOnce();
280 void GraphicFilterSmooth::dispose()
282 mpMtrRadius.clear();
283 GraphicFilterDialog::dispose();
287 Graphic GraphicFilterSmooth::GetFilteredGraphic( const Graphic& rGraphic, double /*fScaleX*/, double /*fScaleY*/ )
289 Graphic aRet;
290 BmpFilterParam aParam( GetRadius() );
292 if( rGraphic.IsAnimated() )
294 Animation aAnim( rGraphic.GetAnimation() );
296 if( aAnim.Filter( BMP_FILTER_SMOOTH, &aParam ) )
298 aRet = aAnim;
301 else
303 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
305 if( aBmpEx.Filter( BMP_FILTER_SMOOTH, &aParam ) )
307 aRet = aBmpEx;
311 return aRet;
315 // - GraphicFilterSolarize -
318 GraphicFilterSolarize::GraphicFilterSolarize( vcl::Window* pParent, const Graphic& rGraphic,
319 sal_uInt8 cGreyThreshold, bool bInvert )
320 : GraphicFilterDialog(pParent, "SolarizeDialog",
321 "cui/ui/solarizedialog.ui", rGraphic)
323 get(mpMtrThreshold, "value");
324 get(mpCbxInvert, "invert");
326 mpMtrThreshold->SetValue( FRound( cGreyThreshold / 2.55 ) );
327 mpMtrThreshold->SetModifyHdl( GetModifyHdl() );
329 mpCbxInvert->Check( bInvert );
330 mpCbxInvert->SetToggleHdl( GetModifyHdl() );
333 GraphicFilterSolarize::~GraphicFilterSolarize()
335 disposeOnce();
338 void GraphicFilterSolarize::dispose()
340 mpMtrThreshold.clear();
341 mpCbxInvert.clear();
342 GraphicFilterDialog::dispose();
346 Graphic GraphicFilterSolarize::GetFilteredGraphic( const Graphic& rGraphic,
347 double /*fScaleX*/, double /*fScaleY*/ )
349 Graphic aRet;
350 BmpFilterParam aParam( GetGreyThreshold() );
352 if( rGraphic.IsAnimated() )
354 Animation aAnim( rGraphic.GetAnimation() );
356 if( aAnim.Filter( BMP_FILTER_SOLARIZE, &aParam ) )
358 if( IsInvert() )
359 aAnim.Invert();
361 aRet = aAnim;
364 else
366 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
368 if( aBmpEx.Filter( BMP_FILTER_SOLARIZE, &aParam ) )
370 if( IsInvert() )
371 aBmpEx.Invert();
373 aRet = aBmpEx;
377 return aRet;
381 // - GraphicFilterSepia -
384 GraphicFilterSepia::GraphicFilterSepia( vcl::Window* pParent, const Graphic& rGraphic,
385 sal_uInt16 nSepiaPercent )
386 : GraphicFilterDialog(pParent, "AgingDialog",
387 "cui/ui/agingdialog.ui", rGraphic)
389 get(mpMtrSepia, "value");
391 mpMtrSepia->SetValue( nSepiaPercent );
392 mpMtrSepia->SetModifyHdl( GetModifyHdl() );
395 GraphicFilterSepia::~GraphicFilterSepia()
397 disposeOnce();
400 void GraphicFilterSepia::dispose()
402 mpMtrSepia.clear();
403 GraphicFilterDialog::dispose();
407 Graphic GraphicFilterSepia::GetFilteredGraphic( const Graphic& rGraphic,
408 double /*fScaleX*/, double /*fScaleY*/ )
410 Graphic aRet;
411 BmpFilterParam aParam( GetSepiaPercent() );
413 if( rGraphic.IsAnimated() )
415 Animation aAnim( rGraphic.GetAnimation() );
417 if( aAnim.Filter( BMP_FILTER_SEPIA, &aParam ) )
418 aRet = aAnim;
420 else
422 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
424 if( aBmpEx.Filter( BMP_FILTER_SEPIA, &aParam ) )
425 aRet = aBmpEx;
428 return aRet;
432 // - GraphicFilterPoster -
435 GraphicFilterPoster::GraphicFilterPoster(vcl::Window* pParent, const Graphic& rGraphic,
436 sal_uInt16 nPosterCount)
437 : GraphicFilterDialog(pParent, "PosterDialog",
438 "cui/ui/posterdialog.ui", rGraphic)
440 get(mpNumPoster, "value");
442 mpNumPoster->SetFirst( 2 );
443 mpNumPoster->SetLast( rGraphic.GetBitmapEx().GetBitCount() );
444 mpNumPoster->SetValue( nPosterCount );
445 mpNumPoster->SetModifyHdl( GetModifyHdl() );
448 GraphicFilterPoster::~GraphicFilterPoster()
450 disposeOnce();
453 void GraphicFilterPoster::dispose()
455 mpNumPoster.clear();
456 GraphicFilterDialog::dispose();
460 Graphic GraphicFilterPoster::GetFilteredGraphic( const Graphic& rGraphic,
461 double /*fScaleX*/, double /*fScaleY*/ )
463 Graphic aRet;
464 const sal_uInt16 nPosterCount = GetPosterColorCount();
466 if( rGraphic.IsAnimated() )
468 Animation aAnim( rGraphic.GetAnimation() );
470 if( aAnim.ReduceColors( nPosterCount, BMP_REDUCE_POPULAR ) )
471 aRet = aAnim;
473 else
475 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
477 if( aBmpEx.ReduceColors( nPosterCount, BMP_REDUCE_POPULAR ) )
478 aRet = aBmpEx;
481 return aRet;
485 // - GraphicFilterEmboss -
488 void EmbossControl::MouseButtonDown( const MouseEvent& rEvt )
490 const RECT_POINT eOldRP = GetActualRP();
492 SvxRectCtl::MouseButtonDown( rEvt );
494 if( GetActualRP() != eOldRP )
495 maModifyHdl.Call( this );
498 Size EmbossControl::GetOptimalSize() const
500 return LogicToPixel(Size(77, 60), MAP_APPFONT);
503 VCL_BUILDER_FACTORY(EmbossControl)
505 GraphicFilterEmboss::GraphicFilterEmboss(vcl::Window* pParent,
506 const Graphic& rGraphic, RECT_POINT eLightSource)
507 : GraphicFilterDialog (pParent, "EmbossDialog",
508 "cui/ui/embossdialog.ui", rGraphic)
510 get(mpCtlLight, "lightsource");
511 mpCtlLight->SetActualRP(eLightSource);
512 mpCtlLight->SetModifyHdl( GetModifyHdl() );
513 mpCtlLight->GrabFocus();
516 GraphicFilterEmboss::~GraphicFilterEmboss()
518 disposeOnce();
521 void GraphicFilterEmboss::dispose()
523 mpCtlLight.clear();
524 GraphicFilterDialog::dispose();
528 Graphic GraphicFilterEmboss::GetFilteredGraphic( const Graphic& rGraphic,
529 double /*fScaleX*/, double /*fScaleY*/ )
531 Graphic aRet;
532 sal_uInt16 nAzim, nElev;
534 switch( mpCtlLight->GetActualRP() )
536 default: OSL_FAIL("svx::GraphicFilterEmboss::GetFilteredGraphic(), unknown Reference Point!" );
537 /* Fall through */
538 case( RP_LT ): nAzim = 4500, nElev = 4500; break;
539 case( RP_MT ): nAzim = 9000, nElev = 4500; break;
540 case( RP_RT ): nAzim = 13500, nElev = 4500; break;
541 case( RP_LM ): nAzim = 0, nElev = 4500; break;
542 case( RP_MM ): nAzim = 0, nElev = 9000; break;
543 case( RP_RM ): nAzim = 18000, nElev = 4500; break;
544 case( RP_LB ): nAzim = 31500, nElev = 4500; break;
545 case( RP_MB ): nAzim = 27000, nElev = 4500; break;
546 case( RP_RB ): nAzim = 22500, nElev = 4500; break;
549 BmpFilterParam aParam( nAzim, nElev );
551 if( rGraphic.IsAnimated() )
553 Animation aAnim( rGraphic.GetAnimation() );
555 if( aAnim.Filter( BMP_FILTER_EMBOSS_GREY, &aParam ) )
556 aRet = aAnim;
558 else
560 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
562 if( aBmpEx.Filter( BMP_FILTER_EMBOSS_GREY, &aParam ) )
563 aRet = aBmpEx;
566 return aRet;
569 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */