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_dbaccess.hxx"
26 #ifndef DBAUI_CONNECTIONLINE_HXX
27 #include "ConnectionLine.hxx"
28 #endif
29 #ifndef DBAUI_CONNECTIONLINEDATA_HXX
30 #include "ConnectionLineData.hxx"
31 #endif
32 #ifndef DBAUI_TABLEWINDOW_HXX
33 #include "TableWindow.hxx"
34 #endif
35 #ifndef DBAUI_TABLEWINDOWLISTBOX_HXX
36 #include "TableWindowListBox.hxx"
37 #endif
38 #ifndef DBAUI_TABLECONNECTION_HXX
39 #include "TableConnection.hxx"
40 #endif
41 #ifndef _SV_SVAPP_HXX
42 #include <vcl/svapp.hxx>
43 #endif
44 #ifndef _INC_MATH
45 #include <math.h>
46 #endif
47 #ifndef _TOOLS_DEBUG_HXX
48 #include <tools/debug.hxx>
49 #endif
50 #include <vcl/lineinfo.hxx>
51 
52 
53 using namespace dbaui;
54 const long DESCRIPT_LINE_WIDTH = 15;
55 const long HIT_SENSITIVE_RADIUS = 5;
56 
57 namespace
58 {
59 	/** calcRect creates a new rectangle with the given points
60 			@param	_rBase		the base point
61 			@param	_aVector	the vector which will be added
62 	*/
63 	inline Rectangle calcRect(const Point& _rBase,const Point& _aVector)
64 	{
65 		return Rectangle( _rBase - _aVector, _rBase + _aVector );
66 	}
67 	// -----------------------------------------------------------------------------
68 	/** GetTextPos calculate the rectangle for the connection to be drawn
69 			@param	_pWin			the table window where to draw it
70 			@param	_aConnPos		the connection point
71 			@param	_aDescrLinePos	the description line pos
72 	*/
73 	Rectangle GetTextPos(const OTableWindow* _pWin, const Point& _aConnPos,const Point& _aDescrLinePos)
74 	{
75 		OTableWindowListBox* pListBox = _pWin ? _pWin->GetListBox() : NULL;
76 		DBG_ASSERT(_pWin && pListBox, "OConnectionLine::GetSourceTextPos : invalid call !");
77 
78 		Rectangle aReturn;
79 		if ( pListBox )
80 		{
81 			const long nRowHeight = pListBox->GetEntryHeight();
82 			aReturn.Top() = _aConnPos.Y() - nRowHeight;
83 			aReturn.Bottom() = aReturn.Top() + nRowHeight;
84 			if (_aDescrLinePos.X() < _aConnPos.X())
85 			{
86 				aReturn.Left() = _aDescrLinePos.X();
87 				aReturn.Right() = aReturn.Left() + _aConnPos.X() - _aDescrLinePos.X();
88 			}
89 			else
90 			{
91 				aReturn.Left() = _aConnPos.X();
92 				aReturn.Right() = aReturn.Left() + _aDescrLinePos.X() - _aConnPos.X();
93 			}
94 		}
95 
96 		return aReturn;
97 	}
98 	// -----------------------------------------------------------------------------
99 	/** calcPointsYValue calculate the points Y value in relation to the listbox entry
100 			@param	_pWin			the corresponding window
101 			@param	_pEntry			the source or dest entry
102 			@param	_rNewConPos		(in/out) the connection pos
103 			@param	_rNewDescrPos	(in/out) the description pos
104 	*/
105 	void calcPointsYValue(const OTableWindow* _pWin,SvLBoxEntry* _pEntry,Point& _rNewConPos,Point& _rNewDescrPos)
106 	{
107 		const OTableWindowListBox* pListBox = _pWin->GetListBox();
108 		_rNewConPos.Y() = _pWin->GetPosPixel().Y();
109         if ( _pEntry )
110         {
111             const long nRowHeight = pListBox->GetEntryHeight();
112 		    _rNewConPos.Y() += pListBox->GetPosPixel().Y();
113 		    long nEntryPos = pListBox->GetEntryPosition( _pEntry ).Y();
114 
115 		    if( nEntryPos >= 0 )
116 		    {
117 			    _rNewConPos.Y() += nEntryPos;
118 			    _rNewConPos.Y() += (long)( 0.5 * nRowHeight );
119 		    }
120 		    else
121 			    _rNewConPos.Y() -= (long)( 0.5 * nRowHeight );
122 
123 		    long nListBoxBottom = _pWin->GetPosPixel().Y()
124 								    + pListBox->GetPosPixel().Y()
125 								    + pListBox->GetSizePixel().Height();
126 		    if( _rNewConPos.Y() > nListBoxBottom )
127 			    _rNewConPos.Y() = nListBoxBottom + 2;
128         }
129         else
130             _rNewConPos.Y() += static_cast<sal_Int32>(pListBox->GetPosPixel().Y()*0.5);
131 
132 		_rNewDescrPos.Y() = _rNewConPos.Y();
133 	}
134 	// -----------------------------------------------------------------------------
135 }
136 
137 //========================================================================
138 // class OConnectionLine
139 //========================================================================
140 DBG_NAME(OConnectionLine)
141 //------------------------------------------------------------------------
142 OConnectionLine::OConnectionLine( OTableConnection* _pConn, OConnectionLineDataRef _pLineData )
143 	: m_pTabConn( _pConn )
144 	 ,m_pData( _pLineData )
145 {
146 	DBG_CTOR(OConnectionLine,NULL);
147 }
148 
149 //------------------------------------------------------------------------
150 OConnectionLine::OConnectionLine( const OConnectionLine& _rLine )
151 {
152 	DBG_CTOR(OConnectionLine,NULL);
153 	m_pData = new OConnectionLineData( *_rLine.GetData() );
154 	*this = _rLine;
155 }
156 
157 //------------------------------------------------------------------------
158 OConnectionLine::~OConnectionLine()
159 {
160 	DBG_DTOR(OConnectionLine,NULL);
161 }
162 
163 //------------------------------------------------------------------------
164 OConnectionLine& OConnectionLine::operator=( const OConnectionLine& rLine )
165 {
166 	if( &rLine != this )
167 	{
168 		// da mir die Daten nicht gehoeren, loesche ich die alten nicht
169 		m_pData->CopyFrom(*rLine.GetData());
170 			// CopyFrom ist virtuell, damit ist es kein Problem, wenn m_pData von einem von OTableConnectionData abgeleiteten Typ ist
171 
172 		m_pTabConn = rLine.m_pTabConn;
173 		m_aSourceConnPos = rLine.m_aSourceConnPos;
174 		m_aDestConnPos = rLine.m_aDestConnPos;
175 		m_aSourceDescrLinePos = rLine.m_aSourceDescrLinePos;
176 		m_aDestDescrLinePos = rLine.m_aDestDescrLinePos;
177 	}
178 
179 	return *this;
180 }
181 
182 //------------------------------------------------------------------------
183 Rectangle OConnectionLine::GetBoundingRect()
184 {
185 	//////////////////////////////////////////////////////////////////////
186 	// Umgebendes Rechteck bestimmen
187 	Rectangle aBoundingRect( Point(0,0), Point(0,0) );
188 	if( !IsValid() )
189 		return aBoundingRect;
190 
191 	Point aTopLeft;
192 	Point aBottomRight;
193 
194 	if( m_aSourceDescrLinePos.Y() <= m_aDestDescrLinePos.Y() )
195 	{
196 		aTopLeft.Y() = m_aSourceDescrLinePos.Y();
197 		aBottomRight.Y() = m_aDestDescrLinePos.Y();
198 	}
199 	else
200 	{
201 		aTopLeft.Y() = m_aDestDescrLinePos.Y();
202 		aBottomRight.Y() = m_aSourceDescrLinePos.Y();
203 	}
204 
205 	if( m_aSourceDescrLinePos.X() <= m_aDestDescrLinePos.X() )
206 	{
207 		aTopLeft.X() = m_aSourceDescrLinePos.X();
208 		aBottomRight.X() = m_aDestDescrLinePos.X();
209 	}
210 	else
211 	{
212 		aTopLeft.X() = m_aDestDescrLinePos.X();
213 		aBottomRight.X() = m_aSourceDescrLinePos.X();
214 	}
215 
216     const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
217 	const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
218 	//////////////////////////////////////////////////////////////////////
219 	// Linie verlaeuft in z-Form
220 	if( pSourceWin == pDestWin || Abs(m_aSourceConnPos.X() - m_aDestConnPos.X()) > Abs(m_aSourceDescrLinePos.X() - m_aDestDescrLinePos.X()) )
221 	{
222 		aTopLeft.X() -= DESCRIPT_LINE_WIDTH;
223 		aBottomRight.X() += DESCRIPT_LINE_WIDTH;
224 	}
225 
226 	aBoundingRect = Rectangle( aTopLeft-Point(2,17), aBottomRight+Point(2,2) );
227 
228 	return aBoundingRect;
229 }
230 // -----------------------------------------------------------------------------
231 void calcPointX1(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
232 {
233 	_rNewConPos.X() = _pWin->GetPosPixel().X() + _pWin->GetSizePixel().Width();
234 	_rNewDescrPos.X() = _rNewConPos.X();
235 	_rNewConPos.X() += DESCRIPT_LINE_WIDTH;
236 }
237 // -----------------------------------------------------------------------------
238 void calcPointX2(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
239 {
240 	_rNewConPos.X() = _pWin->GetPosPixel().X();
241 	_rNewDescrPos.X() = _rNewConPos.X();
242 	_rNewConPos.X() -= DESCRIPT_LINE_WIDTH;
243 }
244 //------------------------------------------------------------------------
245 sal_Bool OConnectionLine::RecalcLine()
246 {
247 	//////////////////////////////////////////////////////////////////////
248 	// Fenster und Entries muessen gesetzt sein
249 	const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
250 	const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
251 
252 	if( !pSourceWin || !pDestWin )
253 		return sal_False;
254 
255 	SvLBoxEntry* pSourceEntry = pSourceWin->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
256 	SvLBoxEntry* pDestEntry = pDestWin->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
257 
258 	//////////////////////////////////////////////////////////////////////
259 	// X-Koordinaten bestimmen
260 	Point aSourceCenter( 0, 0 );
261 	Point aDestCenter( 0, 0 );
262 
263 	aSourceCenter.X() = pSourceWin->GetPosPixel().X() + (long)( 0.5*pSourceWin->GetSizePixel().Width() );
264 	aDestCenter.X() = pDestWin->GetPosPixel().X() + (long)( 0.5*pDestWin->GetSizePixel().Width() );
265 
266 	const OTableWindow* pFirstWin	= pDestWin;
267 	const OTableWindow* pSecondWin	= pSourceWin;
268 	Point* pFirstConPos				= &m_aDestConnPos;
269 	Point* pFirstDescrPos			= &m_aDestDescrLinePos;
270 	Point* pSecondConPos			= &m_aSourceConnPos;
271 	Point* pSecondDescrPos			= &m_aSourceDescrLinePos;
272 	if( aDestCenter.X() > aSourceCenter.X() )
273 	{
274 		pFirstWin		= pSourceWin;
275 		pSecondWin		= pDestWin;
276 		pFirstConPos	= &m_aSourceConnPos;
277 		pFirstDescrPos	= &m_aSourceDescrLinePos;
278 		pSecondConPos	= &m_aDestConnPos;
279 		pSecondDescrPos	= &m_aDestDescrLinePos;
280 	}
281 
282     if ( pFirstWin == pSecondWin && pSourceEntry != pDestEntry )
283         calcPointX2(pFirstWin,*pFirstConPos,*pFirstDescrPos);
284     else
285         calcPointX1(pFirstWin,*pFirstConPos,*pFirstDescrPos);
286 	calcPointX2(pSecondWin,*pSecondConPos,*pSecondDescrPos);
287 
288 	//////////////////////////////////////////////////////////////////////
289 	// aSourceConnPosY bestimmen
290     calcPointsYValue(pSourceWin,pSourceEntry,m_aSourceConnPos,m_aSourceDescrLinePos);
291 
292 	//////////////////////////////////////////////////////////////////////
293 	// aDestConnPosY bestimmen
294     calcPointsYValue(pDestWin,pDestEntry,m_aDestConnPos,m_aDestDescrLinePos);
295 
296 	return sal_True;
297 }
298 // -----------------------------------------------------------------------------
299 
300 //------------------------------------------------------------------------
301 void OConnectionLine::Draw( OutputDevice* pOutDev )
302 {
303 	const sal_uInt16 nRectSize = 3;
304 
305 	//////////////////////////////////////////////////////////////////////
306 	// Neue Dimensionen berechnen
307 	if( !RecalcLine() )
308 		return;
309 
310 	//////////////////////////////////////////////////////////////////////
311 	// Zeichnen der Linien
312 	if (m_pTabConn->IsSelected())
313 		pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
314 	else
315 		pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
316 
317     LineInfo aLineInfo;
318     if ( m_pTabConn->IsSelected() )
319         aLineInfo.SetWidth(3);
320     Polygon aPoly;
321     aPoly.Insert(0,m_aSourceDescrLinePos);
322     aPoly.Insert(1,m_aSourceConnPos);
323     aPoly.Insert(2,m_aDestConnPos);
324     aPoly.Insert(3,m_aDestDescrLinePos);
325     pOutDev->DrawPolyLine(aPoly,aLineInfo);
326 
327 	//////////////////////////////////////////////////////////////////////
328 	// draw the connection rectangles
329 	pOutDev->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
330 
331 	Point aVector(nRectSize,nRectSize);
332 	pOutDev->DrawRect( calcRect(m_aSourceDescrLinePos,aVector) );
333 	pOutDev->DrawRect( calcRect( m_aDestDescrLinePos,aVector) );
334 }
335 // -----------------------------------------------------------------------------
336 sal_Bool OConnectionLine::IsValid() const
337 {
338 	return m_pData.isValid();
339 }
340 //------------------------------------------------------------------------
341 double dist_Euklid(const Point &p1, const Point& p2,const Point& pM, Point& q)
342 {
343 	Point v(p2 - p1);
344 	Point w(pM - p1);
345 	double a = sqrt((double)(v.X()*v.X() + v.Y()*v.Y()));
346 	double l = (v.X() * w.Y() - v.Y() * w.X()) / a;
347 	double a2 = w.X()*v.X()+w.Y()*v.Y();
348 	a = a2 / (a * a);
349 	q.X() = long(p1.X() + a * v.X());
350 	q.Y() = long(p1.Y() + a * v.Y());
351 	return l;
352 }
353 //------------------------------------------------------------------------
354 bool OConnectionLine::CheckHit( const Point& rMousePos ) const
355 {
356 	//////////////////////////////////////////////////////////////////////
357 	/*
358 		Vorgehensweise beim HitTest:
359 		Es wird der Abstand nach Euklid berechnet.
360 	*/
361 	Point q;
362 	double l = fabs(dist_Euklid(m_aSourceConnPos,m_aDestConnPos,rMousePos,q));
363 	if( l < HIT_SENSITIVE_RADIUS)
364 	{
365 		if(::std::min(m_aSourceConnPos.X(),m_aDestConnPos.X()) <= q.X() && ::std::min(m_aSourceConnPos.Y(),m_aDestConnPos.Y()) <= q.Y()
366 			&& q.X() <= ::std::max(m_aDestConnPos.X(),m_aSourceConnPos.X())   && q.Y() <= ::std::max(m_aDestConnPos.Y(),m_aSourceConnPos.Y()))
367 			return true;
368 	}
369 
370 	return false;
371 }
372 // -----------------------------------------------------------------------------
373 Rectangle OConnectionLine::GetSourceTextPos() const
374 {
375 	return GetTextPos(m_pTabConn->GetSourceWin(),m_aSourceConnPos,m_aSourceDescrLinePos);
376 }
377 // -----------------------------------------------------------------------------
378 Rectangle OConnectionLine::GetDestTextPos() const
379 {
380 	return GetTextPos(m_pTabConn->GetDestWin(),m_aDestConnPos,m_aDestDescrLinePos);
381 }
382 // -----------------------------------------------------------------------------
383 Point OConnectionLine::getMidPoint() const
384 {
385 	Point aDest = m_aDestConnPos - m_aSourceConnPos;
386 	aDest.X() /= 2;
387 	aDest.Y() /= 2;
388 
389 	return m_aSourceConnPos + aDest;
390 }
391 // -----------------------------------------------------------------------------
392 
393 
394