tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / slideshow / source / engine / shapes / mediashape.cxx
blob5a22682e63f44b8a6f7da5693b49d104d5af6e1b
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/drawing/XShape.hpp>
22 #include <o3tl/safeint.hxx>
23 #include <osl/diagnose.h>
25 #include "mediashape.hxx"
26 #include "viewmediashape.hxx"
27 #include "externalshapebase.hxx"
28 #include <slideshowcontext.hxx>
29 #include <shape.hxx>
30 #include <tools.hxx>
32 #include <algorithm>
35 using namespace ::com::sun::star;
38 namespace slideshow::internal
40 namespace {
42 /** Represents a media shape.
44 This implementation offers support for media shapes.
45 Such shapes need special treatment.
47 class MediaShape : public ExternalShapeBase
49 public:
50 /** Create a shape for the given XShape for a media object
52 @param xShape
53 The XShape to represent.
55 @param nPrio
56 Externally-determined shape priority (used e.g. for
57 paint ordering). This number _must be_ unique!
59 MediaShape( const css::uno::Reference< css::drawing::XShape >& xShape,
60 double nPrio,
61 const SlideShowContext& rContext ); // throw ShapeLoadFailedException;
63 private:
65 // View layer methods
68 virtual void addViewLayer( const ViewLayerSharedPtr& rNewLayer,
69 bool bRedrawLayer ) override;
70 virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer ) override;
71 virtual void clearAllViewLayers() override;
74 // ExternalShapeBase methods
77 virtual bool implRender( const ::basegfx::B2DRange& rCurrBounds ) const override;
78 virtual void implViewChanged( const UnoViewSharedPtr& rView ) override;
79 virtual void implViewsChanged() override;
80 virtual bool implStartIntrinsicAnimation() override;
81 virtual bool implEndIntrinsicAnimation() override;
82 virtual void implPauseIntrinsicAnimation() override;
83 virtual bool implIsIntrinsicAnimationPlaying() const override;
84 virtual void implSetIntrinsicAnimationTime(double) override;
85 void implSetLooping(bool bLooping) override;
87 /// the list of active view shapes (one for each registered view layer)
88 typedef ::std::vector< ViewMediaShapeSharedPtr > ViewMediaShapeVector;
89 ViewMediaShapeVector maViewMediaShapes;
90 bool mbIsPlaying;
91 OUString maFallbackDir;
96 MediaShape::MediaShape( const uno::Reference< drawing::XShape >& xShape,
97 double nPrio,
98 const SlideShowContext& rContext ) :
99 ExternalShapeBase( xShape, nPrio, rContext ),
100 maViewMediaShapes(),
101 mbIsPlaying(false),
102 maFallbackDir(rContext.maFallbackDir)
107 void MediaShape::implViewChanged( const UnoViewSharedPtr& rView )
109 const ::basegfx::B2DRectangle aBounds = getBounds();
110 // determine ViewMediaShape that needs update
111 for( const auto& pViewMediaShape : maViewMediaShapes )
112 if( pViewMediaShape->getViewLayer()->isOnView( rView ) )
113 pViewMediaShape->resize( aBounds );
117 void MediaShape::implViewsChanged()
119 const ::basegfx::B2DRectangle aBounds = getBounds();
120 // resize all ViewShapes
121 for( const auto& pViewMediaShape : maViewMediaShapes )
122 pViewMediaShape->resize( aBounds );
126 void MediaShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
127 bool bRedrawLayer )
129 maViewMediaShapes.push_back(
130 std::make_shared<ViewMediaShape>( rNewLayer,
131 getXShape(),
132 mxComponentContext,
133 maFallbackDir ));
135 // push new size to view shape
136 maViewMediaShapes.back()->resize( getBounds() );
138 // render the Shape on the newly added ViewLayer
139 if( bRedrawLayer )
140 maViewMediaShapes.back()->render( getBounds() );
144 bool MediaShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
146 const ViewMediaShapeVector::iterator aEnd( maViewMediaShapes.end() );
148 OSL_ENSURE( ::std::count_if(maViewMediaShapes.begin(),
149 aEnd,
150 [&rLayer]
151 ( const ViewMediaShapeSharedPtr& pShape )
152 { return rLayer == pShape->getViewLayer(); } ) < 2,
153 "MediaShape::removeViewLayer(): Duplicate ViewLayer entries!" );
155 // TODO : needed for the moment since ANDROID doesn't know size_t return from std::erase_if
156 #if defined ANDROID
157 ViewMediaShapeVector::iterator aIter;
159 if( (aIter=::std::remove_if( maViewMediaShapes.begin(),
160 aEnd,
161 [&rLayer]
162 ( const ViewMediaShapeSharedPtr& pShape )
163 { return rLayer == pShape->getViewLayer(); } ) ) == aEnd )
165 // view layer seemingly was not added, failed
166 return false;
169 // actually erase from container
170 maViewMediaShapes.erase( aIter, aEnd );
172 return true;
173 #else
174 size_t nb = std::erase_if(maViewMediaShapes,
175 [&rLayer]
176 ( const ViewMediaShapeSharedPtr& pShape )
177 { return rLayer == pShape->getViewLayer(); } );
178 // if nb == 0, it means view media shape seemingly was not added, failed
179 return (nb != 0);
180 #endif
184 void MediaShape::clearAllViewLayers()
186 maViewMediaShapes.clear();
190 bool MediaShape::implRender( const ::basegfx::B2DRange& rCurrBounds ) const
192 // redraw all view shapes, by calling their update() method
193 if( o3tl::make_unsigned(::std::count_if( maViewMediaShapes.begin(),
194 maViewMediaShapes.end(),
195 [&rCurrBounds]
196 ( const ViewMediaShapeSharedPtr& pShape )
197 { return pShape->render( rCurrBounds ); } ))
198 != maViewMediaShapes.size() )
200 // at least one of the ViewShape::update() calls did return
201 // false - update failed on at least one ViewLayer
202 return false;
205 return true;
209 bool MediaShape::implStartIntrinsicAnimation()
211 for( const auto& pViewMediaShape : maViewMediaShapes )
212 pViewMediaShape->startMedia();
214 mbIsPlaying = true;
216 return true;
220 bool MediaShape::implEndIntrinsicAnimation()
222 for( const auto& pViewMediaShape : maViewMediaShapes )
223 pViewMediaShape->endMedia();
225 mbIsPlaying = false;
227 return true;
231 void MediaShape::implPauseIntrinsicAnimation()
233 for( const auto& pViewMediaShape : maViewMediaShapes )
234 pViewMediaShape->pauseMedia();
236 mbIsPlaying = false;
240 bool MediaShape::implIsIntrinsicAnimationPlaying() const
242 return mbIsPlaying;
246 void MediaShape::implSetIntrinsicAnimationTime(double fTime)
248 for( const auto& pViewMediaShape : maViewMediaShapes )
249 pViewMediaShape->setMediaTime( fTime );
252 void MediaShape::implSetLooping(bool bLooping)
254 for (const auto& pViewMediaShape : maViewMediaShapes)
256 pViewMediaShape->setLooping(bLooping);
260 ShapeSharedPtr createMediaShape(
261 const uno::Reference< drawing::XShape >& xShape,
262 double nPrio,
263 const SlideShowContext& rContext)
265 return std::make_shared<MediaShape>(xShape, nPrio, rContext);
270 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */