Bump version to 5.0-14
[LibreOffice.git] / dbaccess / source / ui / querydesign / ConnectionLine.cxx
blobcdd075cac6786714757b9f227b5fa6776080858c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
26 #include <math.h>
27 #include <osl/diagnose.h>
28 #include <vcl/lineinfo.hxx>
29 #include <vcl/settings.hxx>
31 using namespace dbaui;
32 const long DESCRIPT_LINE_WIDTH = 15;
33 const long HIT_SENSITIVE_RADIUS = 5;
35 namespace
37 /** calcRect creates a new rectangle with the given points
38 @param _rBase the base point
39 @param _aVector the vector which will be added
41 inline Rectangle calcRect(const Point& _rBase,const Point& _aVector)
43 return Rectangle( _rBase - _aVector, _rBase + _aVector );
45 /** GetTextPos calculate the rectangle for the connection to be drawn
46 @param _pWin the table window where to draw it
47 @param _aConnPos the connection point
48 @param _aDescrLinePos the description line pos
50 Rectangle GetTextPos(const OTableWindow* _pWin, const Point& _aConnPos,const Point& _aDescrLinePos)
52 OTableWindowListBox* pListBox = _pWin ? _pWin->GetListBox() : NULL;
53 OSL_ENSURE(_pWin && pListBox, "OConnectionLine::GetSourceTextPos : invalid call !");
55 Rectangle aReturn;
56 if ( pListBox )
58 const long nRowHeight = pListBox->GetEntryHeight();
59 aReturn.Top() = _aConnPos.Y() - nRowHeight;
60 aReturn.Bottom() = aReturn.Top() + nRowHeight;
61 if (_aDescrLinePos.X() < _aConnPos.X())
63 aReturn.Left() = _aDescrLinePos.X();
64 aReturn.Right() = aReturn.Left() + _aConnPos.X() - _aDescrLinePos.X();
66 else
68 aReturn.Left() = _aConnPos.X();
69 aReturn.Right() = aReturn.Left() + _aDescrLinePos.X() - _aConnPos.X();
73 return aReturn;
75 /** calcPointsYValue calculate the points Y value in relation to the listbox entry
76 @param _pWin the corresponding window
77 @param _pEntry the source or dest entry
78 @param _rNewConPos (in/out) the connection pos
79 @param _rNewDescrPos (in/out) the description pos
81 void calcPointsYValue(const OTableWindow* _pWin,SvTreeListEntry* _pEntry,Point& _rNewConPos,Point& _rNewDescrPos)
83 const OTableWindowListBox* pListBox = _pWin->GetListBox();
84 _rNewConPos.Y() = _pWin->GetPosPixel().Y();
85 if ( _pEntry )
87 const long nRowHeight = pListBox->GetEntryHeight();
88 _rNewConPos.Y() += pListBox->GetPosPixel().Y();
89 long nEntryPos = pListBox->GetEntryPosition( _pEntry ).Y();
91 if( nEntryPos >= 0 )
93 _rNewConPos.Y() += nEntryPos;
94 _rNewConPos.Y() += (long)( 0.5 * nRowHeight );
96 else
97 _rNewConPos.Y() -= (long)( 0.5 * nRowHeight );
99 long nListBoxBottom = _pWin->GetPosPixel().Y()
100 + pListBox->GetPosPixel().Y()
101 + pListBox->GetSizePixel().Height();
102 if( _rNewConPos.Y() > nListBoxBottom )
103 _rNewConPos.Y() = nListBoxBottom + 2;
105 else
106 _rNewConPos.Y() += static_cast<sal_Int32>(pListBox->GetPosPixel().Y()*0.5);
108 _rNewDescrPos.Y() = _rNewConPos.Y();
112 // class OConnectionLine
113 OConnectionLine::OConnectionLine( OTableConnection* _pConn, OConnectionLineDataRef _pLineData )
114 : m_pTabConn( _pConn )
115 , m_pData( _pLineData )
119 OConnectionLine::OConnectionLine( const OConnectionLine& _rLine )
120 : m_pTabConn(NULL)
122 m_pData = new OConnectionLineData( *_rLine.GetData() );
123 *this = _rLine;
126 OConnectionLine::~OConnectionLine()
130 OConnectionLine& OConnectionLine::operator=( const OConnectionLine& rLine )
132 if( &rLine != this )
134 // as the data does not belong to me, I don't delete the old one
135 m_pData->CopyFrom(*rLine.GetData());
136 // CopyFrom is virtual, therefore it is not a problem, if m_pData is of a type derived from OTableConnectionData
138 m_pTabConn = rLine.m_pTabConn;
139 m_aSourceConnPos = rLine.m_aSourceConnPos;
140 m_aDestConnPos = rLine.m_aDestConnPos;
141 m_aSourceDescrLinePos = rLine.m_aSourceDescrLinePos;
142 m_aDestDescrLinePos = rLine.m_aDestDescrLinePos;
145 return *this;
148 Rectangle OConnectionLine::GetBoundingRect()
150 // determine surrounding rectangle
151 Rectangle aBoundingRect( Point(0,0), Point(0,0) );
152 if( !IsValid() )
153 return aBoundingRect;
155 Point aTopLeft;
156 Point aBottomRight;
158 if( m_aSourceDescrLinePos.Y() <= m_aDestDescrLinePos.Y() )
160 aTopLeft.Y() = m_aSourceDescrLinePos.Y();
161 aBottomRight.Y() = m_aDestDescrLinePos.Y();
163 else
165 aTopLeft.Y() = m_aDestDescrLinePos.Y();
166 aBottomRight.Y() = m_aSourceDescrLinePos.Y();
169 if( m_aSourceDescrLinePos.X() <= m_aDestDescrLinePos.X() )
171 aTopLeft.X() = m_aSourceDescrLinePos.X();
172 aBottomRight.X() = m_aDestDescrLinePos.X();
174 else
176 aTopLeft.X() = m_aDestDescrLinePos.X();
177 aBottomRight.X() = m_aSourceDescrLinePos.X();
180 const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
181 const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
182 // line proceeds in z-Form
183 if( pSourceWin == pDestWin || std::abs(m_aSourceConnPos.X() - m_aDestConnPos.X()) > std::abs(m_aSourceDescrLinePos.X() - m_aDestDescrLinePos.X()) )
185 aTopLeft.X() -= DESCRIPT_LINE_WIDTH;
186 aBottomRight.X() += DESCRIPT_LINE_WIDTH;
189 aBoundingRect = Rectangle( aTopLeft-Point(2,17), aBottomRight+Point(2,2) );
191 return aBoundingRect;
194 void calcPointX1(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
196 _rNewConPos.X() = _pWin->GetPosPixel().X() + _pWin->GetSizePixel().Width();
197 _rNewDescrPos.X() = _rNewConPos.X();
198 _rNewConPos.X() += DESCRIPT_LINE_WIDTH;
201 void calcPointX2(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
203 _rNewConPos.X() = _pWin->GetPosPixel().X();
204 _rNewDescrPos.X() = _rNewConPos.X();
205 _rNewConPos.X() -= DESCRIPT_LINE_WIDTH;
208 bool OConnectionLine::RecalcLine()
210 // Windows and entries must be set
211 const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
212 const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
214 if( !pSourceWin || !pDestWin )
215 return false;
217 SvTreeListEntry* pSourceEntry = pSourceWin->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
218 SvTreeListEntry* pDestEntry = pDestWin->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
220 // determine X-coordinates
221 Point aSourceCenter( 0, 0 );
222 Point aDestCenter( 0, 0 );
224 aSourceCenter.X() = pSourceWin->GetPosPixel().X() + (long)( 0.5*pSourceWin->GetSizePixel().Width() );
225 aDestCenter.X() = pDestWin->GetPosPixel().X() + (long)( 0.5*pDestWin->GetSizePixel().Width() );
227 const OTableWindow* pFirstWin = pDestWin;
228 const OTableWindow* pSecondWin = pSourceWin;
229 Point* pFirstConPos = &m_aDestConnPos;
230 Point* pFirstDescrPos = &m_aDestDescrLinePos;
231 Point* pSecondConPos = &m_aSourceConnPos;
232 Point* pSecondDescrPos = &m_aSourceDescrLinePos;
233 if( aDestCenter.X() > aSourceCenter.X() )
235 pFirstWin = pSourceWin;
236 pSecondWin = pDestWin;
237 pFirstConPos = &m_aSourceConnPos;
238 pFirstDescrPos = &m_aSourceDescrLinePos;
239 pSecondConPos = &m_aDestConnPos;
240 pSecondDescrPos = &m_aDestDescrLinePos;
243 if ( pFirstWin == pSecondWin && pSourceEntry != pDestEntry )
244 calcPointX2(pFirstWin,*pFirstConPos,*pFirstDescrPos);
245 else
246 calcPointX1(pFirstWin,*pFirstConPos,*pFirstDescrPos);
247 calcPointX2(pSecondWin,*pSecondConPos,*pSecondDescrPos);
249 // determine aSourceConnPosY
250 calcPointsYValue(pSourceWin,pSourceEntry,m_aSourceConnPos,m_aSourceDescrLinePos);
252 // determine aDestConnPosY
253 calcPointsYValue(pDestWin,pDestEntry,m_aDestConnPos,m_aDestDescrLinePos);
255 return true;
258 void OConnectionLine::Draw( OutputDevice* pOutDev )
260 const sal_uInt16 nRectSize = 3;
262 // calculate new dimension
263 if( !RecalcLine() )
264 return;
266 // draw lines
267 if (m_pTabConn->IsSelected())
268 pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
269 else
270 pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
272 LineInfo aLineInfo;
273 if ( m_pTabConn->IsSelected() )
274 aLineInfo.SetWidth(3);
275 Polygon aPoly;
276 aPoly.Insert(0,m_aSourceDescrLinePos);
277 aPoly.Insert(1,m_aSourceConnPos);
278 aPoly.Insert(2,m_aDestConnPos);
279 aPoly.Insert(3,m_aDestDescrLinePos);
280 pOutDev->DrawPolyLine(aPoly,aLineInfo);
282 // draw the connection rectangles
283 pOutDev->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
285 Point aVector(nRectSize,nRectSize);
286 pOutDev->DrawRect( calcRect(m_aSourceDescrLinePos,aVector) );
287 pOutDev->DrawRect( calcRect( m_aDestDescrLinePos,aVector) );
290 bool OConnectionLine::IsValid() const
292 return m_pData.is();
295 double dist_Euklid(const Point &p1, const Point& p2,const Point& pM, Point& q)
297 Point v(p2 - p1);
298 Point w(pM - p1);
299 double a = sqrt((double)(v.X()*v.X() + v.Y()*v.Y()));
300 double l = (v.X() * w.Y() - v.Y() * w.X()) / a;
301 double a2 = w.X()*v.X()+w.Y()*v.Y();
302 a = a2 / (a * a);
303 q.X() = long(p1.X() + a * v.X());
304 q.Y() = long(p1.Y() + a * v.Y());
305 return l;
308 bool OConnectionLine::CheckHit( const Point& rMousePos ) const
311 course of action with HitTest:
312 the distance is calculated according to Euklid.
314 Point q;
315 double l = fabs(dist_Euklid(m_aSourceConnPos,m_aDestConnPos,rMousePos,q));
316 if( l < HIT_SENSITIVE_RADIUS)
318 if(::std::min(m_aSourceConnPos.X(),m_aDestConnPos.X()) <= q.X() && ::std::min(m_aSourceConnPos.Y(),m_aDestConnPos.Y()) <= q.Y()
319 && q.X() <= ::std::max(m_aDestConnPos.X(),m_aSourceConnPos.X()) && q.Y() <= ::std::max(m_aDestConnPos.Y(),m_aSourceConnPos.Y()))
320 return true;
323 return false;
326 Rectangle OConnectionLine::GetSourceTextPos() const
328 return GetTextPos(m_pTabConn->GetSourceWin(),m_aSourceConnPos,m_aSourceDescrLinePos);
331 Rectangle OConnectionLine::GetDestTextPos() const
333 return GetTextPos(m_pTabConn->GetDestWin(),m_aDestConnPos,m_aDestDescrLinePos);
336 Point OConnectionLine::getMidPoint() const
338 Point aDest = m_aDestConnPos - m_aSourceConnPos;
339 aDest.X() /= 2;
340 aDest.Y() /= 2;
342 return m_aSourceConnPos + aDest;
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */