1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #include "ConnectionLine.hxx"
21 #include "ConnectionLineData.hxx"
22 #include "TableWindow.hxx"
23 #include "TableWindowListBox.hxx"
24 #include "TableConnection.hxx"
25 #include <vcl/svapp.hxx>
29 #include <osl/diagnose.h>
30 #include <vcl/lineinfo.hxx>
33 using namespace dbaui
;
34 const long DESCRIPT_LINE_WIDTH
= 15;
35 const long HIT_SENSITIVE_RADIUS
= 5;
39 /** calcRect creates a new rectangle with the given points
40 @param _rBase the base point
41 @param _aVector the vector which will be added
43 inline Rectangle
calcRect(const Point
& _rBase
,const Point
& _aVector
)
45 return Rectangle( _rBase
- _aVector
, _rBase
+ _aVector
);
47 // -----------------------------------------------------------------------------
48 /** GetTextPos calculate the rectangle for the connection to be drawn
49 @param _pWin the table window where to draw it
50 @param _aConnPos the connection point
51 @param _aDescrLinePos the description line pos
53 Rectangle
GetTextPos(const OTableWindow
* _pWin
, const Point
& _aConnPos
,const Point
& _aDescrLinePos
)
55 OTableWindowListBox
* pListBox
= _pWin
? _pWin
->GetListBox() : NULL
;
56 OSL_ENSURE(_pWin
&& pListBox
, "OConnectionLine::GetSourceTextPos : invalid call !");
61 const long nRowHeight
= pListBox
->GetEntryHeight();
62 aReturn
.Top() = _aConnPos
.Y() - nRowHeight
;
63 aReturn
.Bottom() = aReturn
.Top() + nRowHeight
;
64 if (_aDescrLinePos
.X() < _aConnPos
.X())
66 aReturn
.Left() = _aDescrLinePos
.X();
67 aReturn
.Right() = aReturn
.Left() + _aConnPos
.X() - _aDescrLinePos
.X();
71 aReturn
.Left() = _aConnPos
.X();
72 aReturn
.Right() = aReturn
.Left() + _aDescrLinePos
.X() - _aConnPos
.X();
78 // -----------------------------------------------------------------------------
79 /** calcPointsYValue calculate the points Y value in relation to the listbox entry
80 @param _pWin the corresponding window
81 @param _pEntry the source or dest entry
82 @param _rNewConPos (in/out) the connection pos
83 @param _rNewDescrPos (in/out) the description pos
85 void calcPointsYValue(const OTableWindow
* _pWin
,SvTreeListEntry
* _pEntry
,Point
& _rNewConPos
,Point
& _rNewDescrPos
)
87 const OTableWindowListBox
* pListBox
= _pWin
->GetListBox();
88 _rNewConPos
.Y() = _pWin
->GetPosPixel().Y();
91 const long nRowHeight
= pListBox
->GetEntryHeight();
92 _rNewConPos
.Y() += pListBox
->GetPosPixel().Y();
93 long nEntryPos
= pListBox
->GetEntryPosition( _pEntry
).Y();
97 _rNewConPos
.Y() += nEntryPos
;
98 _rNewConPos
.Y() += (long)( 0.5 * nRowHeight
);
101 _rNewConPos
.Y() -= (long)( 0.5 * nRowHeight
);
103 long nListBoxBottom
= _pWin
->GetPosPixel().Y()
104 + pListBox
->GetPosPixel().Y()
105 + pListBox
->GetSizePixel().Height();
106 if( _rNewConPos
.Y() > nListBoxBottom
)
107 _rNewConPos
.Y() = nListBoxBottom
+ 2;
110 _rNewConPos
.Y() += static_cast<sal_Int32
>(pListBox
->GetPosPixel().Y()*0.5);
112 _rNewDescrPos
.Y() = _rNewConPos
.Y();
114 // -----------------------------------------------------------------------------
117 //========================================================================
118 // class OConnectionLine
119 //========================================================================
120 DBG_NAME(OConnectionLine
)
121 //------------------------------------------------------------------------
122 OConnectionLine::OConnectionLine( OTableConnection
* _pConn
, OConnectionLineDataRef _pLineData
)
123 : m_pTabConn( _pConn
)
124 ,m_pData( _pLineData
)
126 DBG_CTOR(OConnectionLine
,NULL
);
129 //------------------------------------------------------------------------
130 OConnectionLine::OConnectionLine( const OConnectionLine
& _rLine
)
132 DBG_CTOR(OConnectionLine
,NULL
);
133 m_pData
= new OConnectionLineData( *_rLine
.GetData() );
137 //------------------------------------------------------------------------
138 OConnectionLine::~OConnectionLine()
140 DBG_DTOR(OConnectionLine
,NULL
);
143 //------------------------------------------------------------------------
144 OConnectionLine
& OConnectionLine::operator=( const OConnectionLine
& rLine
)
148 // as the data does not belong to me, I don't delete the old one
149 m_pData
->CopyFrom(*rLine
.GetData());
150 // CopyFrom is virtual, therefore it is not a problem, if m_pData is of a type derived from OTableConnectionData
152 m_pTabConn
= rLine
.m_pTabConn
;
153 m_aSourceConnPos
= rLine
.m_aSourceConnPos
;
154 m_aDestConnPos
= rLine
.m_aDestConnPos
;
155 m_aSourceDescrLinePos
= rLine
.m_aSourceDescrLinePos
;
156 m_aDestDescrLinePos
= rLine
.m_aDestDescrLinePos
;
162 //------------------------------------------------------------------------
163 Rectangle
OConnectionLine::GetBoundingRect()
165 //////////////////////////////////////////////////////////////////////
166 // determine surrounding rectangle
167 Rectangle
aBoundingRect( Point(0,0), Point(0,0) );
169 return aBoundingRect
;
174 if( m_aSourceDescrLinePos
.Y() <= m_aDestDescrLinePos
.Y() )
176 aTopLeft
.Y() = m_aSourceDescrLinePos
.Y();
177 aBottomRight
.Y() = m_aDestDescrLinePos
.Y();
181 aTopLeft
.Y() = m_aDestDescrLinePos
.Y();
182 aBottomRight
.Y() = m_aSourceDescrLinePos
.Y();
185 if( m_aSourceDescrLinePos
.X() <= m_aDestDescrLinePos
.X() )
187 aTopLeft
.X() = m_aSourceDescrLinePos
.X();
188 aBottomRight
.X() = m_aDestDescrLinePos
.X();
192 aTopLeft
.X() = m_aDestDescrLinePos
.X();
193 aBottomRight
.X() = m_aSourceDescrLinePos
.X();
196 const OTableWindow
* pSourceWin
= m_pTabConn
->GetSourceWin();
197 const OTableWindow
* pDestWin
= m_pTabConn
->GetDestWin();
198 //////////////////////////////////////////////////////////////////////
199 // line proceeds in in z-Form
200 if( pSourceWin
== pDestWin
|| std::abs(m_aSourceConnPos
.X() - m_aDestConnPos
.X()) > std::abs(m_aSourceDescrLinePos
.X() - m_aDestDescrLinePos
.X()) )
202 aTopLeft
.X() -= DESCRIPT_LINE_WIDTH
;
203 aBottomRight
.X() += DESCRIPT_LINE_WIDTH
;
206 aBoundingRect
= Rectangle( aTopLeft
-Point(2,17), aBottomRight
+Point(2,2) );
208 return aBoundingRect
;
210 // -----------------------------------------------------------------------------
211 void calcPointX1(const OTableWindow
* _pWin
,Point
& _rNewConPos
,Point
& _rNewDescrPos
)
213 _rNewConPos
.X() = _pWin
->GetPosPixel().X() + _pWin
->GetSizePixel().Width();
214 _rNewDescrPos
.X() = _rNewConPos
.X();
215 _rNewConPos
.X() += DESCRIPT_LINE_WIDTH
;
217 // -----------------------------------------------------------------------------
218 void calcPointX2(const OTableWindow
* _pWin
,Point
& _rNewConPos
,Point
& _rNewDescrPos
)
220 _rNewConPos
.X() = _pWin
->GetPosPixel().X();
221 _rNewDescrPos
.X() = _rNewConPos
.X();
222 _rNewConPos
.X() -= DESCRIPT_LINE_WIDTH
;
224 //------------------------------------------------------------------------
225 sal_Bool
OConnectionLine::RecalcLine()
227 //////////////////////////////////////////////////////////////////////
228 // Windows and entries must be set
229 const OTableWindow
* pSourceWin
= m_pTabConn
->GetSourceWin();
230 const OTableWindow
* pDestWin
= m_pTabConn
->GetDestWin();
232 if( !pSourceWin
|| !pDestWin
)
235 SvTreeListEntry
* pSourceEntry
= pSourceWin
->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
236 SvTreeListEntry
* pDestEntry
= pDestWin
->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
238 //////////////////////////////////////////////////////////////////////
239 // determine X-coordinates
240 Point
aSourceCenter( 0, 0 );
241 Point
aDestCenter( 0, 0 );
243 aSourceCenter
.X() = pSourceWin
->GetPosPixel().X() + (long)( 0.5*pSourceWin
->GetSizePixel().Width() );
244 aDestCenter
.X() = pDestWin
->GetPosPixel().X() + (long)( 0.5*pDestWin
->GetSizePixel().Width() );
246 const OTableWindow
* pFirstWin
= pDestWin
;
247 const OTableWindow
* pSecondWin
= pSourceWin
;
248 Point
* pFirstConPos
= &m_aDestConnPos
;
249 Point
* pFirstDescrPos
= &m_aDestDescrLinePos
;
250 Point
* pSecondConPos
= &m_aSourceConnPos
;
251 Point
* pSecondDescrPos
= &m_aSourceDescrLinePos
;
252 if( aDestCenter
.X() > aSourceCenter
.X() )
254 pFirstWin
= pSourceWin
;
255 pSecondWin
= pDestWin
;
256 pFirstConPos
= &m_aSourceConnPos
;
257 pFirstDescrPos
= &m_aSourceDescrLinePos
;
258 pSecondConPos
= &m_aDestConnPos
;
259 pSecondDescrPos
= &m_aDestDescrLinePos
;
262 if ( pFirstWin
== pSecondWin
&& pSourceEntry
!= pDestEntry
)
263 calcPointX2(pFirstWin
,*pFirstConPos
,*pFirstDescrPos
);
265 calcPointX1(pFirstWin
,*pFirstConPos
,*pFirstDescrPos
);
266 calcPointX2(pSecondWin
,*pSecondConPos
,*pSecondDescrPos
);
268 //////////////////////////////////////////////////////////////////////
269 // determine aSourceConnPosY
270 calcPointsYValue(pSourceWin
,pSourceEntry
,m_aSourceConnPos
,m_aSourceDescrLinePos
);
272 //////////////////////////////////////////////////////////////////////
273 // determine aDestConnPosY
274 calcPointsYValue(pDestWin
,pDestEntry
,m_aDestConnPos
,m_aDestDescrLinePos
);
278 // -----------------------------------------------------------------------------
280 //------------------------------------------------------------------------
281 void OConnectionLine::Draw( OutputDevice
* pOutDev
)
283 const sal_uInt16 nRectSize
= 3;
285 //////////////////////////////////////////////////////////////////////
286 // calculate new dimension
290 //////////////////////////////////////////////////////////////////////
292 if (m_pTabConn
->IsSelected())
293 pOutDev
->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
295 pOutDev
->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
298 if ( m_pTabConn
->IsSelected() )
299 aLineInfo
.SetWidth(3);
301 aPoly
.Insert(0,m_aSourceDescrLinePos
);
302 aPoly
.Insert(1,m_aSourceConnPos
);
303 aPoly
.Insert(2,m_aDestConnPos
);
304 aPoly
.Insert(3,m_aDestDescrLinePos
);
305 pOutDev
->DrawPolyLine(aPoly
,aLineInfo
);
307 //////////////////////////////////////////////////////////////////////
308 // draw the connection rectangles
309 pOutDev
->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
311 Point
aVector(nRectSize
,nRectSize
);
312 pOutDev
->DrawRect( calcRect(m_aSourceDescrLinePos
,aVector
) );
313 pOutDev
->DrawRect( calcRect( m_aDestDescrLinePos
,aVector
) );
315 // -----------------------------------------------------------------------------
316 sal_Bool
OConnectionLine::IsValid() const
320 //------------------------------------------------------------------------
321 double dist_Euklid(const Point
&p1
, const Point
& p2
,const Point
& pM
, Point
& q
)
325 double a
= sqrt((double)(v
.X()*v
.X() + v
.Y()*v
.Y()));
326 double l
= (v
.X() * w
.Y() - v
.Y() * w
.X()) / a
;
327 double a2
= w
.X()*v
.X()+w
.Y()*v
.Y();
329 q
.X() = long(p1
.X() + a
* v
.X());
330 q
.Y() = long(p1
.Y() + a
* v
.Y());
333 //------------------------------------------------------------------------
334 bool OConnectionLine::CheckHit( const Point
& rMousePos
) const
336 //////////////////////////////////////////////////////////////////////
338 course of action with HitTest:
339 the distance is calculated according to Euklid.
342 double l
= fabs(dist_Euklid(m_aSourceConnPos
,m_aDestConnPos
,rMousePos
,q
));
343 if( l
< HIT_SENSITIVE_RADIUS
)
345 if(::std::min(m_aSourceConnPos
.X(),m_aDestConnPos
.X()) <= q
.X() && ::std::min(m_aSourceConnPos
.Y(),m_aDestConnPos
.Y()) <= q
.Y()
346 && q
.X() <= ::std::max(m_aDestConnPos
.X(),m_aSourceConnPos
.X()) && q
.Y() <= ::std::max(m_aDestConnPos
.Y(),m_aSourceConnPos
.Y()))
352 // -----------------------------------------------------------------------------
353 Rectangle
OConnectionLine::GetSourceTextPos() const
355 return GetTextPos(m_pTabConn
->GetSourceWin(),m_aSourceConnPos
,m_aSourceDescrLinePos
);
357 // -----------------------------------------------------------------------------
358 Rectangle
OConnectionLine::GetDestTextPos() const
360 return GetTextPos(m_pTabConn
->GetDestWin(),m_aDestConnPos
,m_aDestDescrLinePos
);
362 // -----------------------------------------------------------------------------
363 Point
OConnectionLine::getMidPoint() const
365 Point aDest
= m_aDestConnPos
- m_aSourceConnPos
;
369 return m_aSourceConnPos
+ aDest
;
371 // -----------------------------------------------------------------------------
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */