Use o3tl::convert in Math
[LibreOffice.git] / slideshow / source / engine / slide / layer.cxx
blob01ce4efa4bcd517aa690f149f8904402a37c3d11
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 <basegfx/range/b2drange.hxx>
22 #include <basegfx/range/b1drange.hxx>
23 #include <basegfx/range/b2dpolyrange.hxx>
24 #include <basegfx/polygon/b2dpolypolygon.hxx>
25 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
26 #include <osl/diagnose.h>
28 #include "layer.hxx"
30 using namespace ::com::sun::star;
32 namespace slideshow::internal
34 Layer::Layer( Dummy ) :
35 maViewEntries(),
36 maBounds(),
37 maNewBounds(),
38 mbBoundsDirty(false),
39 mbBackgroundLayer(true),
40 mbClipSet(false)
44 Layer::Layer() :
45 maViewEntries(),
46 maBounds(),
47 maNewBounds(),
48 mbBoundsDirty(false),
49 mbBackgroundLayer(false),
50 mbClipSet(false)
54 ViewLayerSharedPtr Layer::addView( const ViewSharedPtr& rNewView )
56 OSL_ASSERT( rNewView );
58 ViewEntryVector::iterator aIter;
59 const ViewEntryVector::iterator aEnd( maViewEntries.end() );
60 if( (aIter=std::find_if( maViewEntries.begin(),
61 aEnd,
62 [&rNewView]( const ViewEntry& rViewEntry )
63 { return rViewEntry.getView() == rNewView; } ) ) != aEnd )
65 // already added - just return existing layer
66 return aIter->mpViewLayer;
70 // not yet added - create new view layer
71 ViewLayerSharedPtr pNewLayer;
72 if( mbBackgroundLayer )
73 pNewLayer = rNewView;
74 else
75 pNewLayer = rNewView->createViewLayer(maBounds);
77 // add to local list
78 maViewEntries.emplace_back( rNewView,
79 pNewLayer );
81 return maViewEntries.back().mpViewLayer;
84 ViewLayerSharedPtr Layer::removeView( const ViewSharedPtr& rView )
86 OSL_ASSERT( rView );
88 ViewEntryVector::iterator aIter;
89 const ViewEntryVector::iterator aEnd( maViewEntries.end() );
90 if( (aIter=std::find_if( maViewEntries.begin(),
91 aEnd,
92 [&rView]( const ViewEntry& rViewEntry )
93 { return rViewEntry.getView() == rView; } ) ) == aEnd )
95 // View was not added/is already removed
96 return ViewLayerSharedPtr();
99 OSL_ENSURE( std::count_if( maViewEntries.begin(),
100 aEnd,
101 [&rView]( const ViewEntry& rViewEntry )
102 { return rViewEntry.getView() == rView; } ) == 1,
103 "Layer::removeView(): view added multiple times" );
105 ViewLayerSharedPtr pRet( aIter->mpViewLayer );
106 maViewEntries.erase(aIter);
108 return pRet;
111 void Layer::setShapeViews( ShapeSharedPtr const& rShape ) const
113 rShape->clearAllViewLayers();
115 for( const auto& rViewEntry : maViewEntries )
116 rShape->addViewLayer( rViewEntry.getViewLayer(), false );
119 void Layer::setPriority( const ::basegfx::B1DRange& rPrioRange )
121 if( !mbBackgroundLayer )
123 for( const auto& rViewEntry : maViewEntries )
124 rViewEntry.getViewLayer()->setPriority( rPrioRange );
128 void Layer::addUpdateRange( ::basegfx::B2DRange const& rUpdateRange )
130 // TODO(Q1): move this to B2DMultiRange
131 if( !rUpdateRange.isEmpty() )
132 maUpdateAreas.appendElement( rUpdateRange,
133 basegfx::B2VectorOrientation::Positive );
136 void Layer::updateBounds( ShapeSharedPtr const& rShape )
138 if( !mbBackgroundLayer )
140 if( !mbBoundsDirty )
141 maNewBounds.reset();
143 maNewBounds.expand( rShape->getUpdateArea() );
146 mbBoundsDirty = true;
149 bool Layer::commitBounds()
151 mbBoundsDirty = false;
153 if( mbBackgroundLayer )
154 return false;
156 if( maNewBounds == maBounds )
157 return false;
159 maBounds = maNewBounds;
160 if( std::count_if( maViewEntries.begin(),
161 maViewEntries.end(),
162 [this]( const ViewEntry& rViewEntry )
163 { return rViewEntry.getViewLayer()->resize( this->maBounds ); }
164 ) == 0 )
166 return false;
169 // layer content invalid, update areas have wrong
170 // coordinates/not sensible anymore.
171 clearUpdateRanges();
173 return true;
176 void Layer::clearUpdateRanges()
178 maUpdateAreas.clear();
181 void Layer::clearContent()
183 // clear content on all view layers
184 for( const auto& rViewEntry : maViewEntries )
185 rViewEntry.getViewLayer()->clearAll();
187 // layer content cleared, update areas are not sensible
188 // anymore.
189 clearUpdateRanges();
192 class LayerEndUpdate
194 public:
195 LayerEndUpdate( const LayerEndUpdate& ) = delete;
196 LayerEndUpdate& operator=( const LayerEndUpdate& ) = delete;
197 explicit LayerEndUpdate( LayerSharedPtr const& rLayer ) :
198 mpLayer( rLayer )
201 ~LayerEndUpdate() { if(mpLayer) mpLayer->endUpdate(); }
203 private:
204 LayerSharedPtr mpLayer;
207 Layer::EndUpdater Layer::beginUpdate()
209 if( maUpdateAreas.count() )
211 // perform proper layer update. That means, setup proper
212 // clipping, and render each shape that intersects with
213 // the calculated update area
214 ::basegfx::B2DPolyPolygon aClip( maUpdateAreas.solveCrossovers() );
215 aClip = ::basegfx::utils::stripNeutralPolygons(aClip);
216 aClip = ::basegfx::utils::stripDispensablePolygons(aClip);
218 // actually, if there happen to be shapes with zero
219 // update area in the maUpdateAreas vector, the
220 // resulting clip polygon will be empty.
221 if( aClip.count() )
223 for( const auto& rViewEntry : maViewEntries )
225 const ViewLayerSharedPtr& pViewLayer = rViewEntry.getViewLayer();
227 // set clip to all view layers and
228 pViewLayer->setClip( aClip );
230 // clear update area on all view layers
231 pViewLayer->clear();
234 mbClipSet = true;
238 return std::make_shared<LayerEndUpdate>(shared_from_this());
241 void Layer::endUpdate()
243 if( mbClipSet )
245 mbClipSet = false;
247 basegfx::B2DPolyPolygon aEmptyClip;
248 for( const auto& rViewEntry : maViewEntries )
249 rViewEntry.getViewLayer()->setClip( aEmptyClip );
252 clearUpdateRanges();
255 bool Layer::isInsideUpdateArea( ShapeSharedPtr const& rShape ) const
257 return maUpdateAreas.overlaps( rShape->getUpdateArea() );
260 LayerSharedPtr Layer::createBackgroundLayer()
262 return LayerSharedPtr(new Layer( BackgroundLayer ));
265 LayerSharedPtr Layer::createLayer( )
267 return LayerSharedPtr( new Layer );
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */