tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / slideshow / source / engine / shapes / backgroundshape.cxx
blob33c5b89bcb0a4cfdfa39ac150c56b931bb856680
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 <com/sun/star/beans/XPropertySet.hpp>
23 #include <sal/log.hxx>
24 #include <o3tl/safeint.hxx>
25 #include <osl/diagnose.h>
27 #include <algorithm>
29 #include "backgroundshape.hxx"
30 #include <slideshowexceptions.hxx>
31 #include <slideshowcontext.hxx>
32 #include "gdimtftools.hxx"
33 #include <shape.hxx>
34 #include "viewbackgroundshape.hxx"
37 using namespace ::com::sun::star;
40 namespace slideshow::internal
42 namespace {
44 /** Representation of a draw document's background shape.
46 This class implements the Shape interface for the
47 background shape. Since the background shape is neither
48 animatable nor attributable, those more specialized
49 derivations of the Shape interface are not implemented
50 here.
52 @attention this class is to be treated 'final', i.e. one
53 should not derive from it.
55 class BackgroundShape : public Shape
57 public:
58 /** Create the background shape.
60 This method creates a shape that handles the
61 peculiarities of the draw API regarding background
62 content.
64 BackgroundShape( const css::uno::Reference< css::drawing::XDrawPage >& xDrawPage,
65 const css::uno::Reference< css::drawing::XDrawPage >& xMasterPage,
66 const SlideShowContext& rContext ); // throw ShapeLoadFailedException;
68 virtual css::uno::Reference<
69 css::drawing::XShape > getXShape() const override;
71 // View layer methods
74 virtual void addViewLayer( const ViewLayerSharedPtr& rNewLayer,
75 bool bRedrawLayer ) override;
76 virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer ) override;
77 virtual void clearAllViewLayers() override;
80 // attribute methods
83 virtual ::basegfx::B2DRectangle getBounds() const override;
84 virtual ::basegfx::B2DRectangle getDomBounds() const override;
85 virtual ::basegfx::B2DRectangle getUpdateArea() const override;
86 virtual bool isVisible() const override;
87 virtual double getPriority() const override;
88 virtual bool isForeground() const override { return false; }
89 virtual bool isBackgroundDetached() const override;
92 // render methods
95 virtual bool update() const override;
96 virtual bool render() const override;
97 virtual bool isContentChanged() const override;
99 private:
100 /// The metafile actually representing the Shape
101 GDIMetaFileSharedPtr mpMtf;
103 // The attributes of this Shape
104 ::basegfx::B2DRectangle maBounds; // always needed for rendering
106 /// the list of active view shapes (one for each registered view layer)
107 typedef ::std::vector< ViewBackgroundShapeSharedPtr > ViewBackgroundShapeVector;
108 ViewBackgroundShapeVector maViewShapes;
113 BackgroundShape::BackgroundShape( const uno::Reference< drawing::XDrawPage >& xDrawPage,
114 const uno::Reference< drawing::XDrawPage >& xMasterPage,
115 const SlideShowContext& rContext ) :
116 mpMtf(),
117 maBounds(),
118 maViewShapes()
120 uno::Reference< beans::XPropertySet > xPropSet( xDrawPage,
121 uno::UNO_QUERY_THROW );
122 // first try the page background (overrides
123 // masterpage background), then try masterpage
124 GDIMetaFileSharedPtr xMtf = getMetaFile(uno::Reference<lang::XComponent>(xDrawPage, uno::UNO_QUERY),
125 xDrawPage, MTF_LOAD_BACKGROUND_ONLY,
126 rContext.mxComponentContext);
128 if (!xMtf)
130 xMtf = getMetaFile( uno::Reference<lang::XComponent>(xMasterPage, uno::UNO_QUERY),
131 xDrawPage, MTF_LOAD_BACKGROUND_ONLY,
132 rContext.mxComponentContext );
135 if (!xMtf)
137 throw ShapeLoadFailedException();
140 // there is a special background shape, add it
141 // as the first one
143 sal_Int32 nDocWidth=0;
144 sal_Int32 nDocHeight=0;
145 xPropSet->getPropertyValue(u"Width"_ustr) >>= nDocWidth;
146 xPropSet->getPropertyValue(u"Height"_ustr) >>= nDocHeight;
148 mpMtf = std::move(xMtf);
149 maBounds = ::basegfx::B2DRectangle( 0,0,nDocWidth, nDocHeight );
152 uno::Reference< drawing::XShape > BackgroundShape::getXShape() const
154 // no real XShape representative
155 return uno::Reference< drawing::XShape >();
158 void BackgroundShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
159 bool bRedrawLayer )
161 // already added?
162 if( ::std::any_of( maViewShapes.begin(),
163 maViewShapes.end(),
164 [&rNewLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
165 { return pBgShape->getViewLayer() == rNewLayer; } ) )
167 // yes, nothing to do
168 return;
171 maViewShapes.push_back(
172 std::make_shared<ViewBackgroundShape>(
173 rNewLayer, maBounds ) );
175 // render the Shape on the newly added ViewLayer
176 if( bRedrawLayer )
177 maViewShapes.back()->render( mpMtf );
180 bool BackgroundShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
182 const ViewBackgroundShapeVector::iterator aEnd( maViewShapes.end() );
184 OSL_ENSURE( ::std::count_if(maViewShapes.begin(),
185 aEnd,
186 [&rLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
187 { return pBgShape->getViewLayer() == rLayer; } ) < 2,
188 "BackgroundShape::removeViewLayer(): Duplicate ViewLayer entries!" );
190 // TODO : needed for the moment since ANDROID doesn't know size_t return from std::erase_if
191 #if defined ANDROID
192 ViewBackgroundShapeVector::iterator aIter;
194 if( (aIter=::std::remove_if( maViewShapes.begin(),
195 aEnd,
196 [&rLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
197 { return pBgShape->getViewLayer() == rLayer; } )) == aEnd )
199 // view layer seemingly was not added, failed
200 return false;
203 // actually erase from container
204 maViewShapes.erase( aIter, aEnd );
206 return true;
207 #else
209 size_t nb = std::erase_if(maViewShapes,
210 [&rLayer]( const ViewBackgroundShapeSharedPtr& pBgShape )
211 { return pBgShape->getViewLayer() == rLayer; } );
212 // if nb == 0, it means view shape seemingly was not added, failed
213 return (nb != 0);
214 #endif
217 void BackgroundShape::clearAllViewLayers()
219 maViewShapes.clear();
222 ::basegfx::B2DRectangle BackgroundShape::getBounds() const
224 return maBounds;
227 ::basegfx::B2DRectangle BackgroundShape::getDomBounds() const
229 return maBounds;
232 ::basegfx::B2DRectangle BackgroundShape::getUpdateArea() const
234 // TODO(F1): Need to expand background, too, when
235 // antialiasing?
237 // no transformation etc. possible for background shape
238 return maBounds;
241 bool BackgroundShape::isVisible() const
243 return true;
246 double BackgroundShape::getPriority() const
248 return 0.0; // lowest prio, we're the background
251 bool BackgroundShape::update() const
253 return render();
256 bool BackgroundShape::render() const
258 SAL_INFO( "slideshow", "::presentation::internal::BackgroundShape::render()" );
259 SAL_INFO( "slideshow", "::presentation::internal::BackgroundShape: 0x" << std::hex << this );
261 // gcc again...
262 const ::basegfx::B2DRectangle aCurrBounds( BackgroundShape::getBounds() );
264 if( aCurrBounds.getRange().equalZero() )
266 // zero-sized shapes are effectively invisible,
267 // thus, we save us the rendering...
268 return true;
271 // redraw all view shapes, by calling their render() method
272 if( o3tl::make_unsigned(::std::count_if( maViewShapes.begin(),
273 maViewShapes.end(),
274 [this]( const ViewBackgroundShapeSharedPtr& pBgShape )
275 { return pBgShape->render( this->mpMtf ); } ))
276 != maViewShapes.size() )
278 // at least one of the ViewBackgroundShape::render() calls did return
279 // false - update failed on at least one ViewLayer
280 return false;
283 return true;
286 bool BackgroundShape::isContentChanged() const
288 return false;
291 bool BackgroundShape::isBackgroundDetached() const
293 return false; // we're not animatable
297 ShapeSharedPtr createBackgroundShape(
298 const uno::Reference< drawing::XDrawPage >& xDrawPage,
299 const uno::Reference< drawing::XDrawPage >& xMasterPage,
300 const SlideShowContext& rContext )
302 return std::make_shared<BackgroundShape>(
303 xDrawPage,
304 xMasterPage,
305 rContext );
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */