bump product version to 6.3.0.0.beta1
[LibreOffice.git] / cui / source / dialogs / cuigrfflt.cxx
blob1972348523e4af60920ba85137b7c79389dded70
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/BitmapMosaicFilter.hxx>
21 #include <vcl/BitmapSharpenFilter.hxx>
22 #include <vcl/BitmapEmbossGreyFilter.hxx>
23 #include <vcl/BitmapSepiaFilter.hxx>
24 #include <vcl/BitmapSmoothenFilter.hxx>
25 #include <vcl/BitmapSolarizeFilter.hxx>
26 #include <vcl/BitmapColorQuantizationFilter.hxx>
27 #include <vcl/svapp.hxx>
28 #include <sfx2/viewfrm.hxx>
29 #include <sfx2/viewsh.hxx>
30 #include <sfx2/objsh.hxx>
31 #include <sfx2/request.hxx>
32 #include <cuigrfflt.hxx>
33 #include <svx/dialogs.hrc>
35 CuiGraphicPreviewWindow::CuiGraphicPreviewWindow()
36 : mpOrigGraphic(nullptr)
37 , mfScaleX(0.0)
38 , mfScaleY(0.0)
42 void CuiGraphicPreviewWindow::SetDrawingArea(weld::DrawingArea* pDrawingArea)
44 CustomWidgetController::SetDrawingArea(pDrawingArea);
45 OutputDevice &rDevice = pDrawingArea->get_ref_device();
46 maOutputSizePixel = rDevice.LogicToPixel(Size(81, 73), MapMode(MapUnit::MapAppFont));
47 pDrawingArea->set_size_request(maOutputSizePixel.Width(), maOutputSizePixel.Height());
50 void CuiGraphicPreviewWindow::Paint(vcl::RenderContext& rRenderContext, const ::tools::Rectangle&)
52 rRenderContext.SetBackground(Wallpaper(Application::GetSettings().GetStyleSettings().GetDialogColor()));
53 rRenderContext.Erase();
55 const Size aOutputSize(GetOutputSizePixel());
57 if (maPreview.IsAnimated())
59 const Size aGraphicSize(rRenderContext.LogicToPixel(maPreview.GetPrefSize(), maPreview.GetPrefMapMode()));
60 const Point aGraphicPosition((aOutputSize.Width() - aGraphicSize.Width() ) >> 1,
61 (aOutputSize.Height() - aGraphicSize.Height() ) >> 1);
62 maPreview.StartAnimation(&rRenderContext, aGraphicPosition, aGraphicSize);
64 else
66 const Size aGraphicSize(maPreview.GetSizePixel());
67 const Point aGraphicPosition((aOutputSize.Width() - aGraphicSize.Width()) >> 1,
68 (aOutputSize.Height() - aGraphicSize.Height()) >> 1);
69 maPreview.Draw(&rRenderContext, aGraphicPosition, aGraphicSize);
73 void CuiGraphicPreviewWindow::SetPreview(const Graphic& rGraphic)
75 maPreview = rGraphic;
76 Invalidate();
79 void CuiGraphicPreviewWindow::ScaleImageToFit()
81 if (!mpOrigGraphic)
82 return;
84 maScaledOrig = *mpOrigGraphic;
86 const Size aPreviewSize(GetOutputSizePixel());
87 Size aGrfSize(maOrigGraphicSizePixel);
89 if( mpOrigGraphic->GetType() == GraphicType::Bitmap &&
90 aPreviewSize.Width() && aPreviewSize.Height() &&
91 aGrfSize.Width() && aGrfSize.Height() )
93 const double fGrfWH = static_cast<double>(aGrfSize.Width()) / aGrfSize.Height();
94 const double fPreWH = static_cast<double>(aPreviewSize.Width()) / aPreviewSize.Height();
96 if( fGrfWH < fPreWH )
98 aGrfSize.setWidth( static_cast<long>( aPreviewSize.Height() * fGrfWH ) );
99 aGrfSize.setHeight( aPreviewSize.Height() );
101 else
103 aGrfSize.setWidth( aPreviewSize.Width() );
104 aGrfSize.setHeight( static_cast<long>( aPreviewSize.Width() / fGrfWH ) );
107 mfScaleX = static_cast<double>(aGrfSize.Width()) / maOrigGraphicSizePixel.Width();
108 mfScaleY = static_cast<double>(aGrfSize.Height()) / maOrigGraphicSizePixel.Height();
110 if( !mpOrigGraphic->IsAnimated() )
112 BitmapEx aBmpEx( mpOrigGraphic->GetBitmapEx() );
114 if( aBmpEx.Scale( aGrfSize ) )
115 maScaledOrig = aBmpEx;
119 maModifyHdl.Call(nullptr);
122 void CuiGraphicPreviewWindow::Resize()
124 maOutputSizePixel = GetOutputSizePixel();
125 ScaleImageToFit();
128 GraphicFilterDialog::GraphicFilterDialog(weld::Window* pParent,
129 const OUString& rUIXMLDescription, const OString& rID,
130 const Graphic& rGraphic)
131 : GenericDialogController(pParent, rUIXMLDescription, rID)
132 , maModifyHdl(LINK(this, GraphicFilterDialog, ImplModifyHdl))
133 , mxPreview(new weld::CustomWeld(*m_xBuilder, "preview", maPreview))
135 bIsBitmap = rGraphic.GetType() == GraphicType::Bitmap;
137 maTimer.SetInvokeHandler(LINK(this, GraphicFilterDialog, ImplPreviewTimeoutHdl));
138 maTimer.SetTimeout(5);
140 maPreview.init(&rGraphic, maModifyHdl);
143 IMPL_LINK_NOARG(GraphicFilterDialog, ImplPreviewTimeoutHdl, Timer *, void)
145 maTimer.Stop();
146 maPreview.SetPreview(GetFilteredGraphic(maPreview.GetScaledOriginal(),
147 maPreview.GetScaleX(), maPreview.GetScaleY()));
150 IMPL_LINK_NOARG(GraphicFilterDialog, ImplModifyHdl, LinkParamNone*, void)
152 if (bIsBitmap)
154 maTimer.Stop();
155 maTimer.Start();
159 GraphicFilterMosaic::GraphicFilterMosaic(weld::Window* pParent, const Graphic& rGraphic,
160 sal_uInt16 nTileWidth, sal_uInt16 nTileHeight, bool bEnhanceEdges)
161 : GraphicFilterDialog(pParent, "cui/ui/mosaicdialog.ui", "MosaicDialog", rGraphic)
162 , mxMtrWidth(m_xBuilder->weld_metric_spin_button("width", FieldUnit::PIXEL))
163 , mxMtrHeight(m_xBuilder->weld_metric_spin_button("height", FieldUnit::PIXEL))
164 , mxCbxEdges(m_xBuilder->weld_check_button("edges"))
166 mxMtrWidth->set_value(nTileWidth, FieldUnit::PIXEL);
167 mxMtrWidth->set_max(GetGraphicSizePixel().Width(), FieldUnit::PIXEL);
168 mxMtrWidth->connect_value_changed(LINK(this, GraphicFilterMosaic, EditModifyHdl));
170 mxMtrHeight->set_value(nTileHeight, FieldUnit::PIXEL);
171 mxMtrHeight->set_max(GetGraphicSizePixel().Height(), FieldUnit::PIXEL);
172 mxMtrHeight->connect_value_changed(LINK(this, GraphicFilterMosaic, EditModifyHdl));
174 mxCbxEdges->set_active(bEnhanceEdges);
175 mxCbxEdges->connect_toggled(LINK(this, GraphicFilterMosaic, CheckBoxModifyHdl));
177 mxMtrWidth->grab_focus();
180 IMPL_LINK_NOARG(GraphicFilterMosaic, CheckBoxModifyHdl, weld::ToggleButton&, void)
182 GetModifyHdl().Call(nullptr);
185 IMPL_LINK_NOARG(GraphicFilterMosaic, EditModifyHdl, weld::MetricSpinButton&, void)
187 GetModifyHdl().Call(nullptr);
190 Graphic GraphicFilterMosaic::GetFilteredGraphic( const Graphic& rGraphic,
191 double fScaleX, double fScaleY )
193 Graphic aRet;
194 long nTileWidth = static_cast<long>(mxMtrWidth->get_value(FieldUnit::PIXEL));
195 long nTileHeight = static_cast<long>(mxMtrHeight->get_value(FieldUnit::PIXEL));
196 const Size aSize( std::max( FRound( nTileWidth * fScaleX ), 1L ),
197 std::max( FRound( nTileHeight * fScaleY ), 1L ) );
199 if( rGraphic.IsAnimated() )
201 Animation aAnim( rGraphic.GetAnimation() );
203 if (BitmapFilter::Filter(aAnim, BitmapMosaicFilter(aSize.getWidth(), aSize.getHeight())))
205 if( IsEnhanceEdges() )
206 (void)BitmapFilter::Filter(aAnim, BitmapSharpenFilter());
208 aRet = aAnim;
211 else
213 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
215 if (BitmapFilter::Filter(aBmpEx, BitmapMosaicFilter(aSize.getWidth(), aSize.getHeight())))
217 if( IsEnhanceEdges() )
218 BitmapFilter::Filter(aBmpEx, BitmapSharpenFilter());
220 aRet = aBmpEx;
224 return aRet;
227 GraphicFilterSmooth::GraphicFilterSmooth(weld::Window* pParent, const Graphic& rGraphic, double nRadius)
228 : GraphicFilterDialog(pParent, "cui/ui/smoothdialog.ui", "SmoothDialog", rGraphic)
229 , mxMtrRadius(m_xBuilder->weld_spin_button("radius"))
231 mxMtrRadius->set_value(nRadius * 10);
232 mxMtrRadius->connect_value_changed(LINK(this, GraphicFilterSmooth, EditModifyHdl));
233 mxMtrRadius->grab_focus();
236 IMPL_LINK_NOARG(GraphicFilterSmooth, EditModifyHdl, weld::SpinButton&, void)
238 GetModifyHdl().Call(nullptr);
241 Graphic GraphicFilterSmooth::GetFilteredGraphic( const Graphic& rGraphic, double, double )
243 Graphic aRet;
244 double nRadius = mxMtrRadius->get_value() / 10.0;
246 if( rGraphic.IsAnimated() )
248 Animation aAnim( rGraphic.GetAnimation() );
250 if (BitmapFilter::Filter(aAnim, BitmapSmoothenFilter(nRadius)))
252 aRet = aAnim;
255 else
257 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
259 if (BitmapFilter::Filter(aBmpEx, BitmapSmoothenFilter(nRadius)))
261 aRet = aBmpEx;
265 return aRet;
268 GraphicFilterSolarize::GraphicFilterSolarize(weld::Window* pParent, const Graphic& rGraphic,
269 sal_uInt8 cGreyThreshold, bool bInvert)
270 : GraphicFilterDialog(pParent, "cui/ui/solarizedialog.ui", "SolarizeDialog", rGraphic)
271 , mxMtrThreshold(m_xBuilder->weld_metric_spin_button("value", FieldUnit::PERCENT))
272 , mxCbxInvert(m_xBuilder->weld_check_button("invert"))
274 mxMtrThreshold->set_value(FRound(cGreyThreshold / 2.55), FieldUnit::PERCENT);
275 mxMtrThreshold->connect_value_changed(LINK(this, GraphicFilterSolarize, EditModifyHdl));
277 mxCbxInvert->set_active(bInvert);
278 mxCbxInvert->connect_toggled(LINK(this, GraphicFilterSolarize, CheckBoxModifyHdl));
281 IMPL_LINK_NOARG(GraphicFilterSolarize, CheckBoxModifyHdl, weld::ToggleButton&, void)
283 GetModifyHdl().Call(nullptr);
286 IMPL_LINK_NOARG(GraphicFilterSolarize, EditModifyHdl, weld::MetricSpinButton&, void)
288 GetModifyHdl().Call(nullptr);
291 Graphic GraphicFilterSolarize::GetFilteredGraphic( const Graphic& rGraphic, double, double )
293 Graphic aRet;
294 sal_uInt8 nGreyThreshold = static_cast<sal_uInt8>(FRound(mxMtrThreshold->get_value(FieldUnit::PERCENT) * 2.55));
296 if( rGraphic.IsAnimated() )
298 Animation aAnim( rGraphic.GetAnimation() );
300 if (BitmapFilter::Filter(aAnim, BitmapSolarizeFilter(nGreyThreshold)))
302 if( IsInvert() )
303 aAnim.Invert();
305 aRet = aAnim;
308 else
310 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
312 if (BitmapFilter::Filter(aBmpEx, BitmapSolarizeFilter(nGreyThreshold)))
314 if( IsInvert() )
315 aBmpEx.Invert();
317 aRet = aBmpEx;
321 return aRet;
324 GraphicFilterSepia::GraphicFilterSepia(weld::Window* pParent, const Graphic& rGraphic,
325 sal_uInt16 nSepiaPercent)
326 : GraphicFilterDialog(pParent, "cui/ui/agingdialog.ui", "AgingDialog", rGraphic)
327 , mxMtrSepia(m_xBuilder->weld_metric_spin_button("value", FieldUnit::PERCENT))
329 mxMtrSepia->set_value(nSepiaPercent, FieldUnit::PERCENT);
330 mxMtrSepia->connect_value_changed(LINK(this, GraphicFilterSepia, EditModifyHdl));
333 IMPL_LINK_NOARG(GraphicFilterSepia, EditModifyHdl, weld::MetricSpinButton&, void)
335 GetModifyHdl().Call(nullptr);
338 Graphic GraphicFilterSepia::GetFilteredGraphic( const Graphic& rGraphic, double, double )
340 Graphic aRet;
341 sal_uInt16 nSepiaPct = sal::static_int_cast< sal_uInt16 >(mxMtrSepia->get_value(FieldUnit::PERCENT));
343 if( rGraphic.IsAnimated() )
345 Animation aAnim( rGraphic.GetAnimation() );
347 if (BitmapFilter::Filter(aAnim, BitmapSepiaFilter(nSepiaPct)))
348 aRet = aAnim;
350 else
352 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
354 if (BitmapFilter::Filter(aBmpEx, BitmapSepiaFilter(nSepiaPct)))
355 aRet = aBmpEx;
358 return aRet;
361 GraphicFilterPoster::GraphicFilterPoster(weld::Window* pParent, const Graphic& rGraphic,
362 sal_uInt16 nPosterCount)
363 : GraphicFilterDialog(pParent, "cui/ui/posterdialog.ui", "PosterDialog", rGraphic)
364 , mxNumPoster(m_xBuilder->weld_spin_button("value"))
366 mxNumPoster->set_range(2, rGraphic.GetBitmapEx().GetBitCount());
367 mxNumPoster->set_value(nPosterCount);
368 mxNumPoster->connect_value_changed(LINK(this, GraphicFilterPoster, EditModifyHdl));
371 IMPL_LINK_NOARG(GraphicFilterPoster, EditModifyHdl, weld::SpinButton&, void)
373 GetModifyHdl().Call(nullptr);
376 Graphic GraphicFilterPoster::GetFilteredGraphic( const Graphic& rGraphic, double, double )
378 Graphic aRet;
379 const sal_uInt16 nPosterCount = static_cast<sal_uInt16>(mxNumPoster->get_value());
381 if( rGraphic.IsAnimated() )
383 Animation aAnim( rGraphic.GetAnimation() );
385 if( aAnim.ReduceColors( nPosterCount ) )
386 aRet = aAnim;
388 else
390 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
392 if (BitmapFilter::Filter(aBmpEx, BitmapColorQuantizationFilter(nPosterCount)))
393 aRet = aBmpEx;
396 return aRet;
399 bool EmbossControl::MouseButtonDown( const MouseEvent& rEvt )
401 const RectPoint eOldRP = GetActualRP();
403 SvxRectCtl::MouseButtonDown( rEvt );
405 if( GetActualRP() != eOldRP )
406 maModifyHdl.Call( nullptr );
408 return true;
411 void EmbossControl::SetDrawingArea(weld::DrawingArea* pDrawingArea)
413 SvxRectCtl::SetDrawingArea(pDrawingArea);
414 Size aSize(pDrawingArea->get_ref_device().LogicToPixel(Size(77, 60), MapMode(MapUnit::MapAppFont)));
415 pDrawingArea->set_size_request(aSize.Width(), aSize.Height());
418 GraphicFilterEmboss::GraphicFilterEmboss(weld::Window* pParent,
419 const Graphic& rGraphic, RectPoint eLightSource)
420 : GraphicFilterDialog(pParent, "cui/ui/embossdialog.ui", "EmbossDialog", rGraphic)
421 , mxCtlLight(new weld::CustomWeld(*m_xBuilder, "lightsource", maCtlLight))
423 maCtlLight.SetActualRP(eLightSource);
424 maCtlLight.SetModifyHdl( GetModifyHdl() );
425 maCtlLight.GrabFocus();
428 GraphicFilterEmboss::~GraphicFilterEmboss()
432 Graphic GraphicFilterEmboss::GetFilteredGraphic( const Graphic& rGraphic, double, double )
434 Graphic aRet;
435 sal_uInt16 nAzim, nElev;
437 switch (maCtlLight.GetActualRP())
439 default: OSL_FAIL("svx::GraphicFilterEmboss::GetFilteredGraphic(), unknown Reference Point!" );
440 [[fallthrough]];
441 case RectPoint::LT: nAzim = 4500; nElev = 4500; break;
442 case RectPoint::MT: nAzim = 9000; nElev = 4500; break;
443 case RectPoint::RT: nAzim = 13500; nElev = 4500; break;
444 case RectPoint::LM: nAzim = 0; nElev = 4500; break;
445 case RectPoint::MM: nAzim = 0; nElev = 9000; break;
446 case RectPoint::RM: nAzim = 18000; nElev = 4500; break;
447 case RectPoint::LB: nAzim = 31500; nElev = 4500; break;
448 case RectPoint::MB: nAzim = 27000; nElev = 4500; break;
449 case RectPoint::RB: nAzim = 22500; nElev = 4500; break;
452 if( rGraphic.IsAnimated() )
454 Animation aAnim( rGraphic.GetAnimation() );
456 if (BitmapFilter::Filter(aAnim, BitmapEmbossGreyFilter(nAzim, nElev)))
457 aRet = aAnim;
459 else
461 BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
463 if (BitmapFilter::Filter(aBmpEx, BitmapEmbossGreyFilter(nAzim, nElev)))
464 aRet = aBmpEx;
467 return aRet;
470 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */