1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 #include <svx/sdr/overlay/overlaymanager.hxx>
27 #include <basegfx/point/b2dpoint.hxx>
28 #include <basegfx/range/b2drange.hxx>
29 #include <tools/gen.hxx>
30 #include <vcl/salbtype.hxx>
31 #include <vcl/outdev.hxx>
32 #include <vcl/window.hxx>
33 #include <svx/sdr/overlay/overlayobject.hxx>
34 #include <basegfx/matrix/b2dhommatrix.hxx>
35 #include <drawinglayer/processor2d/baseprocessor2d.hxx>
36 #include <svx/sdr/contact/objectcontacttools.hxx>
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 using namespace com::sun::star;
41 
42 //////////////////////////////////////////////////////////////////////////////
43 
44 namespace sdr
45 {
46 	namespace overlay
47 	{
48 		void OverlayManager::ImpDrawMembers(const basegfx::B2DRange& rRange, OutputDevice& rDestinationDevice) const
49 		{
50 			const sal_uInt32 nSize(maOverlayObjects.size());
51 
52 			if(nSize)
53 			{
54 				const sal_uInt16 nOriginalAA(rDestinationDevice.GetAntialiasing());
55 				const bool bIsAntiAliasing(getDrawinglayerOpt().IsAntiAliasing());
56 
57 				// create processor
58 				drawinglayer::processor2d::BaseProcessor2D* pProcessor = ::sdr::contact::createBaseProcessor2DFromOutputDevice(
59 					rDestinationDevice,
60 					getCurrentViewInformation2D());
61 
62 				if(pProcessor)
63 				{
64 					for(OverlayObjectVector::const_iterator aIter(maOverlayObjects.begin()); aIter != maOverlayObjects.end(); aIter++)
65 					{
66 						OSL_ENSURE(*aIter, "Corrupted OverlayObject List (!)");
67 						const OverlayObject& rCandidate = **aIter;
68 
69 						if(rCandidate.isVisible())
70 						{
71 							const drawinglayer::primitive2d::Primitive2DSequence& rSequence = rCandidate.getOverlayObjectPrimitive2DSequence();
72 
73 							if(rSequence.hasElements())
74 							{
75 								if(rRange.overlaps(rCandidate.getBaseRange()))
76 								{
77 									if(bIsAntiAliasing && rCandidate.allowsAntiAliase())
78 									{
79 										rDestinationDevice.SetAntialiasing(nOriginalAA | ANTIALIASING_ENABLE_B2DDRAW);
80 									}
81 									else
82 									{
83 										rDestinationDevice.SetAntialiasing(nOriginalAA & ~ANTIALIASING_ENABLE_B2DDRAW);
84 									}
85 
86 									pProcessor->process(rSequence);
87 								}
88 							}
89 						}
90 					}
91 
92 					delete pProcessor;
93 				}
94 
95 				// restore AA settings
96 				rDestinationDevice.SetAntialiasing(nOriginalAA);
97 			}
98 		}
99 
100 		void OverlayManager::ImpStripeDefinitionChanged()
101 		{
102 			const sal_uInt32 nSize(maOverlayObjects.size());
103 
104 			if(nSize)
105 			{
106 				for(OverlayObjectVector::iterator aIter(maOverlayObjects.begin()); aIter != maOverlayObjects.end(); aIter++)
107 				{
108 					OSL_ENSURE(*aIter, "Corrupted OverlayObject List (!)");
109 					OverlayObject& rCandidate = **aIter;
110 					rCandidate.stripeDefinitionHasChanged();
111 				}
112 			}
113 		}
114 
115         double OverlayManager::getDiscreteOne() const
116         {
117 			if(basegfx::fTools::equalZero(mfDiscreteOne))
118             {
119                 const basegfx::B2DVector aDiscreteInLogic(getOutputDevice().GetInverseViewTransformation() * basegfx::B2DVector(1.0, 0.0));
120                 const_cast< OverlayManager* >(this)->mfDiscreteOne = aDiscreteInLogic.getLength();
121             }
122 
123             return mfDiscreteOne;
124         }
125 
126 		OverlayManager::OverlayManager(OutputDevice& rOutputDevice)
127 		:	Scheduler(),
128 			rmOutputDevice(rOutputDevice),
129 			maOverlayObjects(),
130 			maStripeColorA(Color(COL_BLACK)),
131 			maStripeColorB(Color(COL_WHITE)),
132 			mnStripeLengthPixel(5),
133 			maDrawinglayerOpt(),
134             maViewTransformation(),
135 			maViewInformation2D(),
136             mfDiscreteOne(0.0)
137 		{
138             // set Property 'ReducedDisplayQuality' to true to allow simpler interaction
139             // visualisations
140             static bool bUseReducedDisplayQualityForDrag(true);
141 
142             if(bUseReducedDisplayQualityForDrag)
143             {
144 			    uno::Sequence< beans::PropertyValue > xProperties(1);
145 			    xProperties[0].Name = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("ReducedDisplayQuality"));
146 			    xProperties[0].Value <<= true;
147                 maViewInformation2D = drawinglayer::geometry::ViewInformation2D(xProperties);
148             }
149 		}
150 
151 		const drawinglayer::geometry::ViewInformation2D OverlayManager::getCurrentViewInformation2D() const
152 		{
153             if(getOutputDevice().GetViewTransformation() != maViewTransformation)
154             {
155 				basegfx::B2DRange aViewRange(maViewInformation2D.getViewport());
156 
157 				if(OUTDEV_WINDOW == getOutputDevice().GetOutDevType())
158 				{
159 					const Size aOutputSizePixel(getOutputDevice().GetOutputSizePixel());
160 
161                     // only set when we *have* a output size, else let aViewRange
162                     // stay on empty
163                     if(aOutputSizePixel.Width() && aOutputSizePixel.Height())
164                     {
165                         aViewRange = basegfx::B2DRange(0.0, 0.0, aOutputSizePixel.getWidth(), aOutputSizePixel.getHeight());
166                         aViewRange.transform(getOutputDevice().GetInverseViewTransformation());
167                     }
168 				}
169 
170                 OverlayManager* pThis = const_cast< OverlayManager* >(this);
171 
172 				pThis->maViewTransformation = getOutputDevice().GetViewTransformation();
173 				pThis->maViewInformation2D = drawinglayer::geometry::ViewInformation2D(
174 					maViewInformation2D.getObjectTransformation(),
175 					maViewTransformation,
176 					aViewRange,
177 					maViewInformation2D.getVisualizedPage(),
178 					maViewInformation2D.getViewTime(),
179 					maViewInformation2D.getExtendedInformationSequence());
180 				pThis->mfDiscreteOne = 0.0;
181             }
182 
183 			return maViewInformation2D;
184 		}
185 
186 		void OverlayManager::impApplyRemoveActions(OverlayObject& rTarget)
187 		{
188 			// handle evtl. animation
189 			if(rTarget.allowsAnimation())
190 			{
191 				// remove from event chain
192 				RemoveEvent(&rTarget);
193 			}
194 
195 			// make invisible
196 			invalidateRange(rTarget.getBaseRange());
197 
198 			// clear manager
199 			rTarget.mpOverlayManager = 0;
200 		}
201 
202 		void OverlayManager::impApplyAddActions(OverlayObject& rTarget)
203 		{
204 			// set manager
205 			rTarget.mpOverlayManager = this;
206 
207 			// make visible
208 			invalidateRange(rTarget.getBaseRange());
209 
210 			// handle evtl. animation
211 			if(rTarget.allowsAnimation())
212 			{
213 				// Trigger at current time to get alive. This will do the
214 				// object-specific next time calculation and hand over adding
215 				// again to the scheduler to the animated object, too. This works for
216 				// a paused or non-paused animator.
217 				rTarget.Trigger(GetTime());
218 			}
219 		}
220 
221 		OverlayManager::~OverlayManager()
222 		{
223 			// The OverlayManager is not the owner of the OverlayObjects
224 			// and thus will not delete them, but remove them. Profit here
225 			// from knowing that all will be removed
226 			const sal_uInt32 nSize(maOverlayObjects.size());
227 
228 			if(nSize)
229 			{
230 				for(OverlayObjectVector::iterator aIter(maOverlayObjects.begin()); aIter != maOverlayObjects.end(); aIter++)
231 				{
232 					OSL_ENSURE(*aIter, "Corrupted OverlayObject List (!)");
233 					OverlayObject& rCandidate = **aIter;
234 					impApplyRemoveActions(rCandidate);
235 				}
236 
237 				// erase vector
238 				maOverlayObjects.clear();
239 			}
240 		}
241 
242 		void OverlayManager::completeRedraw(const Region& rRegion, OutputDevice* pPreRenderDevice) const
243 		{
244 			if(!rRegion.IsEmpty() && maOverlayObjects.size())
245 			{
246 				// check for changed MapModes. That may influence the
247 				// logical size of pixel based OverlayObjects (like BitmapHandles)
248 				//ImpCheckMapModeChange();
249 
250 				// paint members
251 				const Rectangle aRegionBoundRect(rRegion.GetBoundRect());
252 				const basegfx::B2DRange aRegionRange(
253 					aRegionBoundRect.Left(), aRegionBoundRect.Top(),
254 					aRegionBoundRect.Right(), aRegionBoundRect.Bottom());
255 
256 				OutputDevice& rTarget = (pPreRenderDevice) ? *pPreRenderDevice : getOutputDevice();
257 				ImpDrawMembers(aRegionRange, rTarget);
258 			}
259 		}
260 
261 		void OverlayManager::flush()
262 		{
263 			// default has nothing to do
264 		}
265 
266 		// #i68597# part of content gets copied, react on it
267 		void OverlayManager::copyArea(const Point& /*rDestPt*/, const Point& /*rSrcPt*/, const Size& /*rSrcSize*/)
268 		{
269 			// unbuffered versions do nothing here
270 		}
271 
272 		void OverlayManager::restoreBackground(const Region& /*rRegion*/) const
273 		{
274 			// unbuffered versions do nothing here
275 		}
276 
277 		void OverlayManager::add(OverlayObject& rOverlayObject)
278 		{
279 			OSL_ENSURE(0 == rOverlayObject.mpOverlayManager, "OverlayObject is added twice to an OverlayManager (!)");
280 
281 			// add to the end of chain to preserve display order in paint
282 			maOverlayObjects.push_back(&rOverlayObject);
283 
284 			// execute add actions
285 			impApplyAddActions(rOverlayObject);
286 		}
287 
288 		void OverlayManager::remove(OverlayObject& rOverlayObject)
289 		{
290 			OSL_ENSURE(rOverlayObject.mpOverlayManager == this, "OverlayObject is removed from wrong OverlayManager (!)");
291 
292 			// execute remove actions
293 			impApplyRemoveActions(rOverlayObject);
294 
295 			// remove from vector
296 			const OverlayObjectVector::iterator aFindResult = ::std::find(maOverlayObjects.begin(), maOverlayObjects.end(), &rOverlayObject);
297 			const bool bFound(aFindResult != maOverlayObjects.end());
298 			OSL_ENSURE(bFound, "OverlayObject NOT found at OverlayManager (!)");
299 
300 			if(bFound)
301 			{
302 				maOverlayObjects.erase(aFindResult);
303 			}
304 		}
305 
306 		void OverlayManager::invalidateRange(const basegfx::B2DRange& rRange)
307 		{
308 			if(OUTDEV_WINDOW == getOutputDevice().GetOutDevType())
309 			{
310 				if(getDrawinglayerOpt().IsAntiAliasing())
311 				{
312 					// assume AA needs one pixel more and invalidate one pixel more
313                     const double fDiscreteOne(getDiscreteOne());
314 					const Rectangle aInvalidateRectangle(
315 						(sal_Int32)floor(rRange.getMinX() - fDiscreteOne),
316 						(sal_Int32)floor(rRange.getMinY() - fDiscreteOne),
317 						(sal_Int32)ceil(rRange.getMaxX() + fDiscreteOne),
318 						(sal_Int32)ceil(rRange.getMaxY() + fDiscreteOne));
319 
320 					// simply invalidate
321 					((Window&)getOutputDevice()).Invalidate(aInvalidateRectangle, INVALIDATE_NOERASE);
322 				}
323 				else
324 				{
325 					// #i77674# transform to rectangle. Use floor/ceil to get all covered
326 					// discrete pixels, see #i75163# and OverlayManagerBuffered::invalidateRange
327 					const Rectangle aInvalidateRectangle(
328 						(sal_Int32)floor(rRange.getMinX()), (sal_Int32)floor(rRange.getMinY()),
329 						(sal_Int32)ceil(rRange.getMaxX()), (sal_Int32)ceil(rRange.getMaxY()));
330 
331 					// simply invalidate
332 					((Window&)getOutputDevice()).Invalidate(aInvalidateRectangle, INVALIDATE_NOERASE);
333 				}
334 			}
335 		}
336 
337 		// stripe support ColA
338 		void OverlayManager::setStripeColorA(Color aNew)
339 		{
340 			if(aNew != maStripeColorA)
341 			{
342 				maStripeColorA = aNew;
343 				ImpStripeDefinitionChanged();
344 			}
345 		}
346 
347 		// stripe support ColB
348 		void OverlayManager::setStripeColorB(Color aNew)
349 		{
350 			if(aNew != maStripeColorB)
351 			{
352 				maStripeColorB = aNew;
353 				ImpStripeDefinitionChanged();
354 			}
355 		}
356 
357 		// stripe support StripeLengthPixel
358 		void OverlayManager::setStripeLengthPixel(sal_uInt32 nNew)
359 		{
360 			if(nNew != mnStripeLengthPixel)
361 			{
362 				mnStripeLengthPixel = nNew;
363 				ImpStripeDefinitionChanged();
364 			}
365 		}
366 	} // end of namespace overlay
367 } // end of namespace sdr
368 
369 //////////////////////////////////////////////////////////////////////////////
370 // eof
371