bump product version to 4.1.6.2
[LibreOffice.git] / slideshow / source / engine / slide / layer.cxx
blob42af078a277abd565d1b1b8ccc0982050e1f0399
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 void dismiss() { mpLayer.reset(); }
237 private:
238 LayerSharedPtr mpLayer;
241 Layer::EndUpdater Layer::beginUpdate()
243 if( maUpdateAreas.count() )
245 // perform proper layer update. That means, setup proper
246 // clipping, and render each shape that intersects with
247 // the calculated update area
248 ::basegfx::B2DPolyPolygon aClip( maUpdateAreas.solveCrossovers() );
249 aClip = ::basegfx::tools::stripNeutralPolygons(aClip);
250 aClip = ::basegfx::tools::stripDispensablePolygons(aClip, false);
252 // actually, if there happen to be shapes with zero
253 // update area in the maUpdateAreas vector, the
254 // resulting clip polygon will be empty.
255 if( aClip.count() )
257 // set clip to all view layers
258 std::for_each( maViewEntries.begin(),
259 maViewEntries.end(),
260 boost::bind(
261 &ViewLayer::setClip,
262 boost::bind(
263 &ViewEntry::getViewLayer,
264 _1),
265 boost::cref(aClip)));
267 // clear update area on all view layers
268 std::for_each( maViewEntries.begin(),
269 maViewEntries.end(),
270 boost::bind(
271 &ViewLayer::clear,
272 boost::bind(
273 &ViewEntry::getViewLayer,
274 _1)));
276 mbClipSet = true;
280 return EndUpdater(new LayerEndUpdate(shared_from_this()));
283 void Layer::endUpdate()
285 if( mbClipSet )
287 mbClipSet = false;
289 basegfx::B2DPolyPolygon aEmptyClip;
290 std::for_each( maViewEntries.begin(),
291 maViewEntries.end(),
292 boost::bind(
293 &ViewLayer::setClip,
294 boost::bind(
295 &ViewEntry::getViewLayer,
296 _1),
297 boost::cref(aEmptyClip)));
300 clearUpdateRanges();
303 bool Layer::isInsideUpdateArea( ShapeSharedPtr const& rShape ) const
305 return maUpdateAreas.overlaps( rShape->getUpdateArea() );
308 LayerSharedPtr Layer::createBackgroundLayer( const basegfx::B2DRange& rMaxLayerBounds )
310 return LayerSharedPtr(new Layer( rMaxLayerBounds,
311 BackgroundLayer ));
314 LayerSharedPtr Layer::createLayer( const basegfx::B2DRange& rMaxLayerBounds )
316 return LayerSharedPtr( new Layer( rMaxLayerBounds ) );
322 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */