bump product version to 5.0.4.1
[LibreOffice.git] / slideshow / source / engine / slide / layer.cxx
blobf6e2eab66f1e6e3153d983ee0c155746956e2dcd
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 // must be first
22 #include <canvas/debug.hxx>
24 #include <basegfx/range/b2drange.hxx>
25 #include <basegfx/range/b1drange.hxx>
26 #include <basegfx/range/b2dpolyrange.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/polygon/b2dpolypolygon.hxx>
29 #include <basegfx/polygon/b2dpolypolygontools.hxx>
30 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
32 #include "layer.hxx"
34 #include <boost/bind.hpp>
37 using namespace ::com::sun::star;
39 namespace slideshow
41 namespace internal
43 Layer::Layer( const basegfx::B2DRange& rMaxLayerBounds,
44 Dummy ) :
45 maViewEntries(),
46 maBounds(),
47 maNewBounds(),
48 maMaxBounds( rMaxLayerBounds ),
49 mbBoundsDirty(false),
50 mbBackgroundLayer(true),
51 mbClipSet(false)
55 Layer::Layer( const basegfx::B2DRange& rMaxLayerBounds ) :
56 maViewEntries(),
57 maBounds(),
58 maNewBounds(),
59 maMaxBounds( rMaxLayerBounds ),
60 mbBoundsDirty(false),
61 mbBackgroundLayer(false),
62 mbClipSet(false)
66 ViewLayerSharedPtr Layer::addView( const ViewSharedPtr& rNewView )
68 OSL_ASSERT( rNewView );
70 ViewEntryVector::iterator aIter;
71 const ViewEntryVector::iterator aEnd( maViewEntries.end() );
72 if( (aIter=std::find_if( maViewEntries.begin(),
73 aEnd,
74 boost::bind<bool>(
75 std::equal_to< ViewSharedPtr >(),
76 boost::bind( &ViewEntry::getView, _1 ),
77 boost::cref( rNewView )))) != aEnd )
79 // already added - just return existing layer
80 return aIter->mpViewLayer;
84 // not yet added - create new view layer
85 ViewLayerSharedPtr pNewLayer;
86 if( mbBackgroundLayer )
87 pNewLayer = rNewView;
88 else
89 pNewLayer = rNewView->createViewLayer(maBounds);
91 // add to local list
92 maViewEntries.push_back(
93 ViewEntry( rNewView,
94 pNewLayer ));
96 return maViewEntries.back().mpViewLayer;
99 ViewLayerSharedPtr Layer::removeView( const ViewSharedPtr& rView )
101 OSL_ASSERT( rView );
103 ViewEntryVector::iterator aIter;
104 const ViewEntryVector::iterator aEnd( maViewEntries.end() );
105 if( (aIter=std::find_if( maViewEntries.begin(),
106 aEnd,
107 boost::bind<bool>(
108 std::equal_to< ViewSharedPtr >(),
109 boost::bind( &ViewEntry::getView, _1 ),
110 boost::cref( rView )))) == aEnd )
112 // View was not added/is already removed
113 return ViewLayerSharedPtr();
116 OSL_ENSURE( std::count_if( maViewEntries.begin(),
117 aEnd,
118 boost::bind<bool>(
119 std::equal_to< ViewSharedPtr >(),
120 boost::bind( &ViewEntry::getView, _1 ),
121 boost::cref( rView ))) == 1,
122 "Layer::removeView(): view added multiple times" );
124 ViewLayerSharedPtr pRet( aIter->mpViewLayer );
125 maViewEntries.erase(aIter);
127 return pRet;
130 void Layer::setShapeViews( ShapeSharedPtr const& rShape ) const
132 rShape->clearAllViewLayers();
134 std::for_each( maViewEntries.begin(),
135 maViewEntries.end(),
136 boost::bind(&Shape::addViewLayer,
137 boost::cref(rShape),
138 boost::bind(&ViewEntry::getViewLayer,
139 _1),
140 false ));
143 void Layer::setPriority( const ::basegfx::B1DRange& rPrioRange )
145 if( !mbBackgroundLayer )
147 std::for_each( maViewEntries.begin(),
148 maViewEntries.end(),
149 boost::bind( &ViewLayer::setPriority,
150 boost::bind( &ViewEntry::getViewLayer,
151 _1 ),
152 boost::cref(rPrioRange)));
156 void Layer::addUpdateRange( ::basegfx::B2DRange const& rUpdateRange )
158 // TODO(Q1): move this to B2DMultiRange
159 if( !rUpdateRange.isEmpty() )
160 maUpdateAreas.appendElement( rUpdateRange,
161 basegfx::ORIENTATION_POSITIVE );
164 void Layer::updateBounds( ShapeSharedPtr const& rShape )
166 if( !mbBackgroundLayer )
168 if( !mbBoundsDirty )
169 maNewBounds.reset();
171 maNewBounds.expand( rShape->getUpdateArea() );
174 mbBoundsDirty = true;
177 bool Layer::commitBounds()
179 mbBoundsDirty = false;
181 if( mbBackgroundLayer )
182 return false;
184 if( maNewBounds == maBounds )
185 return false;
187 maBounds = maNewBounds;
188 if( std::count_if( maViewEntries.begin(),
189 maViewEntries.end(),
190 boost::bind( &ViewLayer::resize,
191 boost::bind( &ViewEntry::getViewLayer,
192 _1 ),
193 boost::cref(maBounds)) ) == 0 )
195 return false;
198 // layer content invalid, update areas have wrong
199 // coordinates/not sensible anymore.
200 clearUpdateRanges();
202 return true;
205 void Layer::clearUpdateRanges()
207 maUpdateAreas.clear();
210 void Layer::clearContent()
212 // clear content on all view layers
213 std::for_each( maViewEntries.begin(),
214 maViewEntries.end(),
215 boost::bind(
216 &ViewLayer::clearAll,
217 boost::bind(
218 &ViewEntry::getViewLayer,
219 _1)));
221 // layer content cleared, update areas are not sensible
222 // anymore.
223 clearUpdateRanges();
226 class LayerEndUpdate : private boost::noncopyable
228 public:
229 LayerEndUpdate( LayerSharedPtr const& rLayer ) :
230 mpLayer( rLayer )
233 ~LayerEndUpdate() { if(mpLayer) mpLayer->endUpdate(); }
235 private:
236 LayerSharedPtr mpLayer;
239 Layer::EndUpdater Layer::beginUpdate()
241 if( maUpdateAreas.count() )
243 // perform proper layer update. That means, setup proper
244 // clipping, and render each shape that intersects with
245 // the calculated update area
246 ::basegfx::B2DPolyPolygon aClip( maUpdateAreas.solveCrossovers() );
247 aClip = ::basegfx::tools::stripNeutralPolygons(aClip);
248 aClip = ::basegfx::tools::stripDispensablePolygons(aClip, false);
250 // actually, if there happen to be shapes with zero
251 // update area in the maUpdateAreas vector, the
252 // resulting clip polygon will be empty.
253 if( aClip.count() )
255 // set clip to all view layers
256 std::for_each( maViewEntries.begin(),
257 maViewEntries.end(),
258 boost::bind(
259 &ViewLayer::setClip,
260 boost::bind(
261 &ViewEntry::getViewLayer,
262 _1),
263 boost::cref(aClip)));
265 // clear update area on all view layers
266 std::for_each( maViewEntries.begin(),
267 maViewEntries.end(),
268 boost::bind(
269 &ViewLayer::clear,
270 boost::bind(
271 &ViewEntry::getViewLayer,
272 _1)));
274 mbClipSet = true;
278 return EndUpdater(new LayerEndUpdate(shared_from_this()));
281 void Layer::endUpdate()
283 if( mbClipSet )
285 mbClipSet = false;
287 basegfx::B2DPolyPolygon aEmptyClip;
288 std::for_each( maViewEntries.begin(),
289 maViewEntries.end(),
290 boost::bind(
291 &ViewLayer::setClip,
292 boost::bind(
293 &ViewEntry::getViewLayer,
294 _1),
295 boost::cref(aEmptyClip)));
298 clearUpdateRanges();
301 bool Layer::isInsideUpdateArea( ShapeSharedPtr const& rShape ) const
303 return maUpdateAreas.overlaps( rShape->getUpdateArea() );
306 LayerSharedPtr Layer::createBackgroundLayer( const basegfx::B2DRange& rMaxLayerBounds )
308 return LayerSharedPtr(new Layer( rMaxLayerBounds,
309 BackgroundLayer ));
312 LayerSharedPtr Layer::createLayer( const basegfx::B2DRange& rMaxLayerBounds )
314 return LayerSharedPtr( new Layer( rMaxLayerBounds ) );
320 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */