update dev300-m58
[ooovba.git] / dbaccess / source / ui / querydesign / ConnectionLine.cxx
blob2e1d5d42cb09bc61c3b19647200ac0182d619240
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConnectionLine.cxx,v $
10 * $Revision: 1.14.68.2 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dbaccess.hxx"
33 #ifndef DBAUI_CONNECTIONLINE_HXX
34 #include "ConnectionLine.hxx"
35 #endif
36 #ifndef DBAUI_CONNECTIONLINEDATA_HXX
37 #include "ConnectionLineData.hxx"
38 #endif
39 #ifndef DBAUI_TABLEWINDOW_HXX
40 #include "TableWindow.hxx"
41 #endif
42 #ifndef DBAUI_TABLEWINDOWLISTBOX_HXX
43 #include "TableWindowListBox.hxx"
44 #endif
45 #ifndef DBAUI_TABLECONNECTION_HXX
46 #include "TableConnection.hxx"
47 #endif
48 #ifndef _SV_SVAPP_HXX
49 #include <vcl/svapp.hxx>
50 #endif
51 #ifndef _INC_MATH
52 #include <math.h>
53 #endif
54 #ifndef _TOOLS_DEBUG_HXX
55 #include <tools/debug.hxx>
56 #endif
57 #include <vcl/lineinfo.hxx>
60 using namespace dbaui;
61 const long DESCRIPT_LINE_WIDTH = 15;
62 const long HIT_SENSITIVE_RADIUS = 5;
64 namespace
66 /** calcRect creates a new rectangle with the given points
67 @param _rBase the base point
68 @param _aVector the vector which will be added
70 inline Rectangle calcRect(const Point& _rBase,const Point& _aVector)
72 return Rectangle( _rBase - _aVector, _rBase + _aVector );
74 // -----------------------------------------------------------------------------
75 /** GetTextPos calculate the rectangle for the connection to be drawn
76 @param _pWin the table window where to draw it
77 @param _aConnPos the connection point
78 @param _aDescrLinePos the description line pos
80 Rectangle GetTextPos(const OTableWindow* _pWin, const Point& _aConnPos,const Point& _aDescrLinePos)
82 OTableWindowListBox* pListBox = _pWin ? _pWin->GetListBox() : NULL;
83 DBG_ASSERT(_pWin && pListBox, "OConnectionLine::GetSourceTextPos : invalid call !");
85 Rectangle aReturn;
86 if ( pListBox )
88 const long nRowHeight = pListBox->GetEntryHeight();
89 aReturn.Top() = _aConnPos.Y() - nRowHeight;
90 aReturn.Bottom() = aReturn.Top() + nRowHeight;
91 if (_aDescrLinePos.X() < _aConnPos.X())
93 aReturn.Left() = _aDescrLinePos.X();
94 aReturn.Right() = aReturn.Left() + _aConnPos.X() - _aDescrLinePos.X();
96 else
98 aReturn.Left() = _aConnPos.X();
99 aReturn.Right() = aReturn.Left() + _aDescrLinePos.X() - _aConnPos.X();
103 return aReturn;
105 // -----------------------------------------------------------------------------
106 /** calcPointsYValue calculate the points Y value in relation to the listbox entry
107 @param _pWin the corresponding window
108 @param _pEntry the source or dest entry
109 @param _rNewConPos (in/out) the connection pos
110 @param _rNewDescrPos (in/out) the description pos
112 void calcPointsYValue(const OTableWindow* _pWin,SvLBoxEntry* _pEntry,Point& _rNewConPos,Point& _rNewDescrPos)
114 const OTableWindowListBox* pListBox = _pWin->GetListBox();
115 _rNewConPos.Y() = _pWin->GetPosPixel().Y();
116 if ( _pEntry )
118 const long nRowHeight = pListBox->GetEntryHeight();
119 _rNewConPos.Y() += pListBox->GetPosPixel().Y();
120 long nEntryPos = pListBox->GetEntryPosition( _pEntry ).Y();
122 if( nEntryPos >= 0 )
124 _rNewConPos.Y() += nEntryPos;
125 _rNewConPos.Y() += (long)( 0.5 * nRowHeight );
127 else
128 _rNewConPos.Y() -= (long)( 0.5 * nRowHeight );
130 long nListBoxBottom = _pWin->GetPosPixel().Y()
131 + pListBox->GetPosPixel().Y()
132 + pListBox->GetSizePixel().Height();
133 if( _rNewConPos.Y() > nListBoxBottom )
134 _rNewConPos.Y() = nListBoxBottom + 2;
136 else
137 _rNewConPos.Y() += static_cast<sal_Int32>(pListBox->GetPosPixel().Y()*0.5);
139 _rNewDescrPos.Y() = _rNewConPos.Y();
141 // -----------------------------------------------------------------------------
144 //========================================================================
145 // class OConnectionLine
146 //========================================================================
147 DBG_NAME(OConnectionLine)
148 //------------------------------------------------------------------------
149 OConnectionLine::OConnectionLine( OTableConnection* _pConn, OConnectionLineDataRef _pLineData )
150 : m_pTabConn( _pConn )
151 ,m_pData( _pLineData )
153 DBG_CTOR(OConnectionLine,NULL);
156 //------------------------------------------------------------------------
157 OConnectionLine::OConnectionLine( const OConnectionLine& _rLine )
159 DBG_CTOR(OConnectionLine,NULL);
160 m_pData = new OConnectionLineData( *_rLine.GetData() );
161 *this = _rLine;
164 //------------------------------------------------------------------------
165 OConnectionLine::~OConnectionLine()
167 DBG_DTOR(OConnectionLine,NULL);
170 //------------------------------------------------------------------------
171 OConnectionLine& OConnectionLine::operator=( const OConnectionLine& rLine )
173 if( &rLine != this )
175 // da mir die Daten nicht gehoeren, loesche ich die alten nicht
176 m_pData->CopyFrom(*rLine.GetData());
177 // CopyFrom ist virtuell, damit ist es kein Problem, wenn m_pData von einem von OTableConnectionData abgeleiteten Typ ist
179 m_pTabConn = rLine.m_pTabConn;
180 m_aSourceConnPos = rLine.m_aSourceConnPos;
181 m_aDestConnPos = rLine.m_aDestConnPos;
182 m_aSourceDescrLinePos = rLine.m_aSourceDescrLinePos;
183 m_aDestDescrLinePos = rLine.m_aDestDescrLinePos;
186 return *this;
189 //------------------------------------------------------------------------
190 Rectangle OConnectionLine::GetBoundingRect()
192 //////////////////////////////////////////////////////////////////////
193 // Umgebendes Rechteck bestimmen
194 Rectangle aBoundingRect( Point(0,0), Point(0,0) );
195 if( !IsValid() )
196 return aBoundingRect;
198 Point aTopLeft;
199 Point aBottomRight;
201 if( m_aSourceDescrLinePos.Y() <= m_aDestDescrLinePos.Y() )
203 aTopLeft.Y() = m_aSourceDescrLinePos.Y();
204 aBottomRight.Y() = m_aDestDescrLinePos.Y();
206 else
208 aTopLeft.Y() = m_aDestDescrLinePos.Y();
209 aBottomRight.Y() = m_aSourceDescrLinePos.Y();
212 if( m_aSourceDescrLinePos.X() <= m_aDestDescrLinePos.X() )
214 aTopLeft.X() = m_aSourceDescrLinePos.X();
215 aBottomRight.X() = m_aDestDescrLinePos.X();
217 else
219 aTopLeft.X() = m_aDestDescrLinePos.X();
220 aBottomRight.X() = m_aSourceDescrLinePos.X();
223 const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
224 const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
225 //////////////////////////////////////////////////////////////////////
226 // Linie verlaeuft in z-Form
227 if( pSourceWin == pDestWin || Abs(m_aSourceConnPos.X() - m_aDestConnPos.X()) > Abs(m_aSourceDescrLinePos.X() - m_aDestDescrLinePos.X()) )
229 aTopLeft.X() -= DESCRIPT_LINE_WIDTH;
230 aBottomRight.X() += DESCRIPT_LINE_WIDTH;
233 aBoundingRect = Rectangle( aTopLeft-Point(2,17), aBottomRight+Point(2,2) );
235 return aBoundingRect;
237 // -----------------------------------------------------------------------------
238 void calcPointX1(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
240 _rNewConPos.X() = _pWin->GetPosPixel().X() + _pWin->GetSizePixel().Width();
241 _rNewDescrPos.X() = _rNewConPos.X();
242 _rNewConPos.X() += DESCRIPT_LINE_WIDTH;
244 // -----------------------------------------------------------------------------
245 void calcPointX2(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
247 _rNewConPos.X() = _pWin->GetPosPixel().X();
248 _rNewDescrPos.X() = _rNewConPos.X();
249 _rNewConPos.X() -= DESCRIPT_LINE_WIDTH;
251 //------------------------------------------------------------------------
252 BOOL OConnectionLine::RecalcLine()
254 //////////////////////////////////////////////////////////////////////
255 // Fenster und Entries muessen gesetzt sein
256 const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
257 const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
259 if( !pSourceWin || !pDestWin )
260 return FALSE;
262 SvLBoxEntry* pSourceEntry = pSourceWin->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
263 SvLBoxEntry* pDestEntry = pDestWin->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
265 //////////////////////////////////////////////////////////////////////
266 // X-Koordinaten bestimmen
267 Point aSourceCenter( 0, 0 );
268 Point aDestCenter( 0, 0 );
270 aSourceCenter.X() = pSourceWin->GetPosPixel().X() + (long)( 0.5*pSourceWin->GetSizePixel().Width() );
271 aDestCenter.X() = pDestWin->GetPosPixel().X() + (long)( 0.5*pDestWin->GetSizePixel().Width() );
273 const OTableWindow* pFirstWin = pDestWin;
274 const OTableWindow* pSecondWin = pSourceWin;
275 Point* pFirstConPos = &m_aDestConnPos;
276 Point* pFirstDescrPos = &m_aDestDescrLinePos;
277 Point* pSecondConPos = &m_aSourceConnPos;
278 Point* pSecondDescrPos = &m_aSourceDescrLinePos;
279 if( aDestCenter.X() > aSourceCenter.X() )
281 pFirstWin = pSourceWin;
282 pSecondWin = pDestWin;
283 pFirstConPos = &m_aSourceConnPos;
284 pFirstDescrPos = &m_aSourceDescrLinePos;
285 pSecondConPos = &m_aDestConnPos;
286 pSecondDescrPos = &m_aDestDescrLinePos;
289 if ( pFirstWin == pSecondWin && pSourceEntry != pDestEntry )
290 calcPointX2(pFirstWin,*pFirstConPos,*pFirstDescrPos);
291 else
292 calcPointX1(pFirstWin,*pFirstConPos,*pFirstDescrPos);
293 calcPointX2(pSecondWin,*pSecondConPos,*pSecondDescrPos);
295 //////////////////////////////////////////////////////////////////////
296 // aSourceConnPosY bestimmen
297 calcPointsYValue(pSourceWin,pSourceEntry,m_aSourceConnPos,m_aSourceDescrLinePos);
299 //////////////////////////////////////////////////////////////////////
300 // aDestConnPosY bestimmen
301 calcPointsYValue(pDestWin,pDestEntry,m_aDestConnPos,m_aDestDescrLinePos);
303 return TRUE;
305 // -----------------------------------------------------------------------------
307 //------------------------------------------------------------------------
308 void OConnectionLine::Draw( OutputDevice* pOutDev )
310 const UINT16 nRectSize = 3;
312 //////////////////////////////////////////////////////////////////////
313 // Neue Dimensionen berechnen
314 if( !RecalcLine() )
315 return;
317 //////////////////////////////////////////////////////////////////////
318 // Zeichnen der Linien
319 if (m_pTabConn->IsSelected())
320 pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
321 else
322 pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
324 LineInfo aLineInfo;
325 if ( m_pTabConn->IsSelected() )
326 aLineInfo.SetWidth(3);
327 Polygon aPoly;
328 aPoly.Insert(0,m_aSourceDescrLinePos);
329 aPoly.Insert(1,m_aSourceConnPos);
330 aPoly.Insert(2,m_aDestConnPos);
331 aPoly.Insert(3,m_aDestDescrLinePos);
332 pOutDev->DrawPolyLine(aPoly,aLineInfo);
334 //////////////////////////////////////////////////////////////////////
335 // draw the connection rectangles
336 pOutDev->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
338 Point aVector(nRectSize,nRectSize);
339 pOutDev->DrawRect( calcRect(m_aSourceDescrLinePos,aVector) );
340 pOutDev->DrawRect( calcRect( m_aDestDescrLinePos,aVector) );
342 // -----------------------------------------------------------------------------
343 BOOL OConnectionLine::IsValid() const
345 return m_pData.isValid();
347 //------------------------------------------------------------------------
348 double dist_Euklid(const Point &p1, const Point& p2,const Point& pM, Point& q)
350 Point v(p2 - p1);
351 Point w(pM - p1);
352 double a = sqrt((double)(v.X()*v.X() + v.Y()*v.Y()));
353 double l = (v.X() * w.Y() - v.Y() * w.X()) / a;
354 double a2 = w.X()*v.X()+w.Y()*v.Y();
355 a = a2 / (a * a);
356 q.X() = long(p1.X() + a * v.X());
357 q.Y() = long(p1.Y() + a * v.Y());
358 return l;
360 //------------------------------------------------------------------------
361 bool OConnectionLine::CheckHit( const Point& rMousePos ) const
363 //////////////////////////////////////////////////////////////////////
365 Vorgehensweise beim HitTest:
366 Es wird der Abstand nach Euklid berechnet.
368 Point q;
369 double l = fabs(dist_Euklid(m_aSourceConnPos,m_aDestConnPos,rMousePos,q));
370 if( l < HIT_SENSITIVE_RADIUS)
372 if(::std::min(m_aSourceConnPos.X(),m_aDestConnPos.X()) <= q.X() && ::std::min(m_aSourceConnPos.Y(),m_aDestConnPos.Y()) <= q.Y()
373 && q.X() <= ::std::max(m_aDestConnPos.X(),m_aSourceConnPos.X()) && q.Y() <= ::std::max(m_aDestConnPos.Y(),m_aSourceConnPos.Y()))
374 return true;
377 return false;
379 // -----------------------------------------------------------------------------
380 Rectangle OConnectionLine::GetSourceTextPos() const
382 return GetTextPos(m_pTabConn->GetSourceWin(),m_aSourceConnPos,m_aSourceDescrLinePos);
384 // -----------------------------------------------------------------------------
385 Rectangle OConnectionLine::GetDestTextPos() const
387 return GetTextPos(m_pTabConn->GetDestWin(),m_aDestConnPos,m_aDestDescrLinePos);
389 // -----------------------------------------------------------------------------
390 Point OConnectionLine::getMidPoint() const
392 Point aDest = m_aDestConnPos - m_aSourceConnPos;
393 aDest.X() /= 2;
394 aDest.Y() /= 2;
396 return m_aSourceConnPos + aDest;
398 // -----------------------------------------------------------------------------