Use o3tl::convert in Math
[LibreOffice.git] / slideshow / source / engine / shapes / mediashape.cxx
blob33dffd06447cb387c7a38bcca8462e6adaed126c
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 <osl/diagnose.h>
24 #include "mediashape.hxx"
25 #include "viewmediashape.hxx"
26 #include "externalshapebase.hxx"
27 #include <slideshowcontext.hxx>
28 #include <shape.hxx>
29 #include <tools.hxx>
31 #include <algorithm>
34 using namespace ::com::sun::star;
37 namespace slideshow::internal
39 namespace {
41 /** Represents a media shape.
43 This implementation offers support for media shapes.
44 Such shapes need special treatment.
46 class MediaShape : public ExternalShapeBase
48 public:
49 /** Create a shape for the given XShape for a media object
51 @param xShape
52 The XShape to represent.
54 @param nPrio
55 Externally-determined shape priority (used e.g. for
56 paint ordering). This number _must be_ unique!
58 MediaShape( const css::uno::Reference< css::drawing::XShape >& xShape,
59 double nPrio,
60 const SlideShowContext& rContext ); // throw ShapeLoadFailedException;
62 private:
64 // View layer methods
67 virtual void addViewLayer( const ViewLayerSharedPtr& rNewLayer,
68 bool bRedrawLayer ) override;
69 virtual bool removeViewLayer( const ViewLayerSharedPtr& rNewLayer ) override;
70 virtual void clearAllViewLayers() override;
73 // ExternalShapeBase methods
76 virtual bool implRender( const ::basegfx::B2DRange& rCurrBounds ) const override;
77 virtual void implViewChanged( const UnoViewSharedPtr& rView ) override;
78 virtual void implViewsChanged() override;
79 virtual bool implStartIntrinsicAnimation() override;
80 virtual bool implEndIntrinsicAnimation() override;
81 virtual void implPauseIntrinsicAnimation() override;
82 virtual bool implIsIntrinsicAnimationPlaying() const override;
83 virtual void implSetIntrinsicAnimationTime(double) override;
84 void implSetLooping(bool bLooping) override;
86 /// the list of active view shapes (one for each registered view layer)
87 typedef ::std::vector< ViewMediaShapeSharedPtr > ViewMediaShapeVector;
88 ViewMediaShapeVector maViewMediaShapes;
89 bool mbIsPlaying;
94 MediaShape::MediaShape( const uno::Reference< drawing::XShape >& xShape,
95 double nPrio,
96 const SlideShowContext& rContext ) :
97 ExternalShapeBase( xShape, nPrio, rContext ),
98 maViewMediaShapes(),
99 mbIsPlaying(false)
104 void MediaShape::implViewChanged( const UnoViewSharedPtr& rView )
106 const ::basegfx::B2DRectangle& rBounds = getBounds();
107 // determine ViewMediaShape that needs update
108 for( const auto& pViewMediaShape : maViewMediaShapes )
109 if( pViewMediaShape->getViewLayer()->isOnView( rView ) )
110 pViewMediaShape->resize( rBounds );
114 void MediaShape::implViewsChanged()
116 const ::basegfx::B2DRectangle& rBounds = getBounds();
117 // resize all ViewShapes
118 for( const auto& pViewMediaShape : maViewMediaShapes )
119 pViewMediaShape->resize( rBounds );
123 void MediaShape::addViewLayer( const ViewLayerSharedPtr& rNewLayer,
124 bool bRedrawLayer )
126 maViewMediaShapes.push_back(
127 std::make_shared<ViewMediaShape>( rNewLayer,
128 getXShape(),
129 mxComponentContext ));
131 // push new size to view shape
132 maViewMediaShapes.back()->resize( getBounds() );
134 // render the Shape on the newly added ViewLayer
135 if( bRedrawLayer )
136 maViewMediaShapes.back()->render( getBounds() );
140 bool MediaShape::removeViewLayer( const ViewLayerSharedPtr& rLayer )
142 const ViewMediaShapeVector::iterator aEnd( maViewMediaShapes.end() );
144 OSL_ENSURE( ::std::count_if(maViewMediaShapes.begin(),
145 aEnd,
146 [&rLayer]
147 ( const ViewMediaShapeSharedPtr& pShape )
148 { return rLayer == pShape->getViewLayer(); } ) < 2,
149 "MediaShape::removeViewLayer(): Duplicate ViewLayer entries!" );
151 ViewMediaShapeVector::iterator aIter;
153 if( (aIter=::std::remove_if( maViewMediaShapes.begin(),
154 aEnd,
155 [&rLayer]
156 ( const ViewMediaShapeSharedPtr& pShape )
157 { return rLayer == pShape->getViewLayer(); } ) ) == aEnd )
159 // view layer seemingly was not added, failed
160 return false;
163 // actually erase from container
164 maViewMediaShapes.erase( aIter, aEnd );
166 return true;
170 void MediaShape::clearAllViewLayers()
172 maViewMediaShapes.clear();
176 bool MediaShape::implRender( const ::basegfx::B2DRange& rCurrBounds ) const
178 // redraw all view shapes, by calling their update() method
179 if( ::std::count_if( maViewMediaShapes.begin(),
180 maViewMediaShapes.end(),
181 [&rCurrBounds]
182 ( const ViewMediaShapeSharedPtr& pShape )
183 { return pShape->render( rCurrBounds ); } )
184 != static_cast<ViewMediaShapeVector::difference_type>(maViewMediaShapes.size()) )
186 // at least one of the ViewShape::update() calls did return
187 // false - update failed on at least one ViewLayer
188 return false;
191 return true;
195 bool MediaShape::implStartIntrinsicAnimation()
197 for( const auto& pViewMediaShape : maViewMediaShapes )
198 pViewMediaShape->startMedia();
200 mbIsPlaying = true;
202 return true;
206 bool MediaShape::implEndIntrinsicAnimation()
208 for( const auto& pViewMediaShape : maViewMediaShapes )
209 pViewMediaShape->endMedia();
211 mbIsPlaying = false;
213 return true;
217 void MediaShape::implPauseIntrinsicAnimation()
219 for( const auto& pViewMediaShape : maViewMediaShapes )
220 pViewMediaShape->pauseMedia();
222 mbIsPlaying = false;
226 bool MediaShape::implIsIntrinsicAnimationPlaying() const
228 return mbIsPlaying;
232 void MediaShape::implSetIntrinsicAnimationTime(double fTime)
234 for( const auto& pViewMediaShape : maViewMediaShapes )
235 pViewMediaShape->setMediaTime( fTime );
238 void MediaShape::implSetLooping(bool bLooping)
240 for (const auto& pViewMediaShape : maViewMediaShapes)
242 pViewMediaShape->setLooping(bLooping);
246 ShapeSharedPtr createMediaShape(
247 const uno::Reference< drawing::XShape >& xShape,
248 double nPrio,
249 const SlideShowContext& rContext)
251 return std::make_shared<MediaShape>(xShape, nPrio, rContext);
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */