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/overlayobjectlist.hxx>
27 #include <svx/sdr/overlay/overlaymanager.hxx>
28 #include <tools/debug.hxx>
29 #include <vcl/outdev.hxx>
30 #include <basegfx/matrix/b2dhommatrix.hxx>
31 
32 // for SOLARIS compiler include of algorithm part of _STL is necesary to
33 // get access to basic algos like ::std::find
34 #include <algorithm>
35 
36 #include <drawinglayer/processor2d/hittestprocessor2d.hxx>
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 namespace sdr
41 {
42 	namespace overlay
43 	{
~OverlayObjectList()44 		OverlayObjectList::~OverlayObjectList()
45 		{
46 			clear();
47 		}
48 
clear()49 		void OverlayObjectList::clear()
50 		{
51 			OverlayObjectVector::iterator aStart(maVector.begin());
52 
53 			for(; aStart != maVector.end(); aStart++)
54 			{
55 				::sdr::overlay::OverlayObject* pCandidate = *aStart;
56 
57 				if(pCandidate->getOverlayManager())
58 				{
59 					pCandidate->getOverlayManager()->remove(*pCandidate);
60 				}
61 
62 				delete pCandidate;
63 			}
64 
65 			maVector.clear();
66 		}
67 
remove(OverlayObject & rOverlayObject)68 		void OverlayObjectList::remove(OverlayObject& rOverlayObject)
69 		{
70 			const OverlayObjectVector::iterator aFindResult = ::std::find(maVector.begin(), maVector.end(), &rOverlayObject);
71 			const bool bFound(aFindResult != maVector.end());
72 			OSL_ENSURE(bFound, "Could not find given object in list (!)");
73 
74 			if(bFound)
75 			{
76 				maVector.erase(aFindResult);
77 			}
78 		}
79 
isHitLogic(const basegfx::B2DPoint & rLogicPosition,double fLogicTolerance) const80 		bool OverlayObjectList::isHitLogic(const basegfx::B2DPoint& rLogicPosition, double fLogicTolerance) const
81 		{
82 			if(!maVector.empty())
83 			{
84 				OverlayObjectVector::const_iterator aStart(maVector.begin());
85 				sdr::overlay::OverlayObject* pFirst = *aStart;
86 				OSL_ENSURE(pFirst, "Corrupt OverlayObjectList (!)");
87 				OverlayManager* pManager = pFirst->getOverlayManager();
88 
89 				if(pManager)
90 				{
91 					if(0.0 == fLogicTolerance)
92 					{
93 						const Size aSizeLogic(pManager->getOutputDevice().PixelToLogic(
94 							Size(DEFAULT_VALUE_FOR_HITTEST_PIXEL, DEFAULT_VALUE_FOR_HITTEST_PIXEL)));
95 						fLogicTolerance = aSizeLogic.Width();
96 					}
97 
98 					const drawinglayer::geometry::ViewInformation2D aViewInformation2D(pManager->getCurrentViewInformation2D());
99 					drawinglayer::processor2d::HitTestProcessor2D aHitTestProcessor2D(
100 						aViewInformation2D,
101 						rLogicPosition,
102 						fLogicTolerance,
103                         false);
104 
105 					for(; aStart != maVector.end(); aStart++)
106 					{
107 						sdr::overlay::OverlayObject* pCandidate = *aStart;
108 						OSL_ENSURE(pCandidate, "Corrupt OverlayObjectList (!)");
109 
110 						if(pCandidate->isHittable())
111 						{
112 							const drawinglayer::primitive2d::Primitive2DSequence& rSequence = pCandidate->getOverlayObjectPrimitive2DSequence();
113 
114 							if(rSequence.hasElements())
115 							{
116 								aHitTestProcessor2D.process(rSequence);
117 
118 								if(aHitTestProcessor2D.getHit())
119 								{
120 									return true;
121 								}
122 							}
123 						}
124 					}
125 				}
126 			}
127 
128 			return false;
129 		}
130 
isHitPixel(const Point & rDiscretePosition,sal_uInt32 nDiscreteTolerance) const131 		bool OverlayObjectList::isHitPixel(const Point& rDiscretePosition, sal_uInt32 nDiscreteTolerance) const
132 		{
133 			if(!maVector.empty())
134 			{
135 				OverlayObjectVector::const_iterator aStart(maVector.begin());
136 				sdr::overlay::OverlayObject* pCandidate = *aStart;
137 				OverlayManager* pManager = pCandidate->getOverlayManager();
138 
139 				if(pManager)
140 				{
141 					const Point aPosLogic(pManager->getOutputDevice().PixelToLogic(rDiscretePosition));
142 					const basegfx::B2DPoint aPosition(aPosLogic.X(), aPosLogic.Y());
143 
144 					if(nDiscreteTolerance)
145 					{
146 						const Size aSizeLogic(pManager->getOutputDevice().PixelToLogic(Size(nDiscreteTolerance, nDiscreteTolerance)));
147 						return isHitLogic(aPosition, (double)aSizeLogic.Width());
148 					}
149 					else
150 					{
151 						return isHitLogic(aPosition);
152 					}
153 				}
154 			}
155 
156 			return false;
157 		}
158 
getBaseRange() const159 		basegfx::B2DRange OverlayObjectList::getBaseRange() const
160 		{
161 			basegfx::B2DRange aRetval;
162 
163 			if(!maVector.empty())
164 			{
165 				OverlayObjectVector::const_iterator aStart(maVector.begin());
166 
167 				for(; aStart != maVector.end(); aStart++)
168 				{
169 					::sdr::overlay::OverlayObject* pCandidate = *aStart;
170 					aRetval.expand(pCandidate->getBaseRange());
171 				}
172 			}
173 
174 			return aRetval;
175 		}
176 	} // end of namespace overlay
177 } // end of namespace sdr
178 
179 //////////////////////////////////////////////////////////////////////////////
180 // eof
181