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>
26 #include <vcl/svapp.hxx>
28 #include <osl/diagnose.h>
29 #include <vcl/lineinfo.hxx>
30 #include <vcl/settings.hxx>
32 using namespace dbaui
;
33 const tools::Long DESCRIPT_LINE_WIDTH
= 15;
34 const tools::Long HIT_SENSITIVE_RADIUS
= 5;
38 /** calcRect creates a new rectangle with the given points
39 @param _rBase the base point
40 @param _aVector the vector which will be added
42 tools::Rectangle
calcRect(const Point
& _rBase
,const Point
& _aVector
)
44 return tools::Rectangle( _rBase
- _aVector
, _rBase
+ _aVector
);
46 /** GetTextPos calculate the rectangle for the connection to be drawn
47 @param _pWin the table window where to draw it
48 @param _aConnPos the connection point
49 @param _aDescrLinePos the description line pos
51 tools::Rectangle
GetTextPos(const OTableWindow
* _pWin
, const Point
& _aConnPos
,const Point
& _aDescrLinePos
)
53 VclPtr
<OTableWindowListBox
> pListBox
= _pWin
? _pWin
->GetListBox() : nullptr;
54 OSL_ENSURE(_pWin
&& pListBox
, "OConnectionLine::GetSourceTextPos : invalid call !");
56 tools::Rectangle aReturn
;
59 const tools::Long nRowHeight
= pListBox
->get_widget().get_height_rows(1);
60 aReturn
.SetTop( _aConnPos
.Y() - nRowHeight
);
61 aReturn
.SetBottom( aReturn
.Top() + nRowHeight
);
62 if (_aDescrLinePos
.X() < _aConnPos
.X())
64 aReturn
.SetLeft( _aDescrLinePos
.X() );
65 aReturn
.SetRight( aReturn
.Left() + _aConnPos
.X() - _aDescrLinePos
.X() );
69 aReturn
.SetLeft( _aConnPos
.X() );
70 aReturn
.SetRight( aReturn
.Left() + _aDescrLinePos
.X() - _aConnPos
.X() );
76 /** calcPointsYValue calculate the points Y value in relation to the listbox entry
77 @param _pWin the corresponding window
78 @param _nEntry the source or dest entry
79 @param _rNewConPos (in/out) the connection pos
80 @param _rNewDescrPos (in/out) the description pos
82 void calcPointsYValue(const OTableWindow
* _pWin
, int _nEntry
, Point
& _rNewConPos
, Point
& _rNewDescrPos
)
84 const OTableWindowListBox
* pListBox
= _pWin
->GetListBox();
85 _rNewConPos
.setY( _pWin
->GetPosPixel().Y() );
87 std::unique_ptr
<weld::TreeIter
> xEntry
;
88 const weld::TreeView
& rTreeView
= pListBox
->get_widget();
92 _rNewConPos
.AdjustY(pListBox
->GetPosPixel().Y() );
93 xEntry
= rTreeView
.make_iterator();
94 if (!rTreeView
.get_iter_first(*xEntry
) || !rTreeView
.iter_nth_sibling(*xEntry
, _nEntry
))
100 auto nEntryPos
= rTreeView
.get_row_area(*xEntry
).Center().Y();
104 _rNewConPos
.AdjustY(nEntryPos
);
108 const auto nRowHeight
= rTreeView
.get_height_rows(1);
109 _rNewConPos
.AdjustY( -static_cast<tools::Long
>( 0.5 * nRowHeight
) );
112 tools::Long nListBoxBottom
= _pWin
->GetPosPixel().Y()
113 + pListBox
->GetPosPixel().Y()
114 + pListBox
->GetSizePixel().Height();
115 if( _rNewConPos
.Y() > nListBoxBottom
)
116 _rNewConPos
.setY( nListBoxBottom
+ 2 );
119 _rNewConPos
.AdjustY(static_cast<sal_Int32
>(pListBox
->GetPosPixel().Y()*0.5) );
121 _rNewDescrPos
.setY( _rNewConPos
.Y() );
125 OConnectionLine::OConnectionLine( OTableConnection
* _pConn
, OConnectionLineDataRef _pLineData
)
126 : m_pTabConn( _pConn
)
127 , m_pData(std::move( _pLineData
))
131 OConnectionLine::OConnectionLine( const OConnectionLine
& _rLine
)
132 : m_pTabConn(nullptr)
134 m_pData
= new OConnectionLineData( *_rLine
.GetData() );
138 OConnectionLine::~OConnectionLine()
142 OConnectionLine
& OConnectionLine::operator=( const OConnectionLine
& rLine
)
146 // as the data does not belong to me, I don't delete the old one
147 m_pData
->CopyFrom(*rLine
.GetData());
148 // CopyFrom is virtual, therefore it is not a problem, if m_pData is of a type derived from OTableConnectionData
150 m_pTabConn
= rLine
.m_pTabConn
;
151 m_aSourceConnPos
= rLine
.m_aSourceConnPos
;
152 m_aDestConnPos
= rLine
.m_aDestConnPos
;
153 m_aSourceDescrLinePos
= rLine
.m_aSourceDescrLinePos
;
154 m_aDestDescrLinePos
= rLine
.m_aDestDescrLinePos
;
160 tools::Rectangle
OConnectionLine::GetBoundingRect() const
162 // determine surrounding rectangle
163 tools::Rectangle
aBoundingRect( Point(0,0), Point(0,0) );
165 return aBoundingRect
;
170 if( m_aSourceDescrLinePos
.Y() <= m_aDestDescrLinePos
.Y() )
172 aTopLeft
.setY( m_aSourceDescrLinePos
.Y() );
173 aBottomRight
.setY( m_aDestDescrLinePos
.Y() );
177 aTopLeft
.setY( m_aDestDescrLinePos
.Y() );
178 aBottomRight
.setY( m_aSourceDescrLinePos
.Y() );
181 if( m_aSourceDescrLinePos
.X() <= m_aDestDescrLinePos
.X() )
183 aTopLeft
.setX( m_aSourceDescrLinePos
.X() );
184 aBottomRight
.setX( m_aDestDescrLinePos
.X() );
188 aTopLeft
.setX( m_aDestDescrLinePos
.X() );
189 aBottomRight
.setX( m_aSourceDescrLinePos
.X() );
192 const OTableWindow
* pSourceWin
= m_pTabConn
->GetSourceWin();
193 const OTableWindow
* pDestWin
= m_pTabConn
->GetDestWin();
194 // line proceeds in z-Form
195 if( pSourceWin
== pDestWin
|| std::abs(m_aSourceConnPos
.X() - m_aDestConnPos
.X()) > std::abs(m_aSourceDescrLinePos
.X() - m_aDestDescrLinePos
.X()) )
197 aTopLeft
.AdjustX( -DESCRIPT_LINE_WIDTH
);
198 aBottomRight
.AdjustX(DESCRIPT_LINE_WIDTH
);
201 aBoundingRect
= tools::Rectangle( aTopLeft
-Point(2,17), aBottomRight
+Point(2,2) );
203 return aBoundingRect
;
206 static void calcPointX1(const OTableWindow
* _pWin
,Point
& _rNewConPos
,Point
& _rNewDescrPos
)
208 _rNewConPos
.setX( _pWin
->GetPosPixel().X() + _pWin
->GetSizePixel().Width() );
209 _rNewDescrPos
.setX( _rNewConPos
.X() );
210 _rNewConPos
.AdjustX(DESCRIPT_LINE_WIDTH
);
213 static void calcPointX2(const OTableWindow
* _pWin
,Point
& _rNewConPos
,Point
& _rNewDescrPos
)
215 _rNewConPos
.setX( _pWin
->GetPosPixel().X() );
216 _rNewDescrPos
.setX( _rNewConPos
.X() );
217 _rNewConPos
.AdjustX( -DESCRIPT_LINE_WIDTH
);
220 bool OConnectionLine::RecalcLine()
222 // Windows and entries must be set
223 const OTableWindow
* pSourceWin
= m_pTabConn
->GetSourceWin();
224 const OTableWindow
* pDestWin
= m_pTabConn
->GetDestWin();
226 if( !pSourceWin
|| !pDestWin
)
229 int nSourceEntry
= pSourceWin
->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
230 int nDestEntry
= pDestWin
->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
232 // determine X-coordinates
233 Point
aSourceCenter( 0, 0 );
234 Point
aDestCenter( 0, 0 );
236 aSourceCenter
.setX( pSourceWin
->GetPosPixel().X() + static_cast<tools::Long
>( 0.5*pSourceWin
->GetSizePixel().Width() ) );
237 aDestCenter
.setX( pDestWin
->GetPosPixel().X() + static_cast<tools::Long
>( 0.5*pDestWin
->GetSizePixel().Width() ) );
239 const OTableWindow
* pFirstWin
= pDestWin
;
240 const OTableWindow
* pSecondWin
= pSourceWin
;
241 Point
* pFirstConPos
= &m_aDestConnPos
;
242 Point
* pFirstDescrPos
= &m_aDestDescrLinePos
;
243 Point
* pSecondConPos
= &m_aSourceConnPos
;
244 Point
* pSecondDescrPos
= &m_aSourceDescrLinePos
;
245 if( aDestCenter
.X() > aSourceCenter
.X() )
247 pFirstWin
= pSourceWin
;
248 pSecondWin
= pDestWin
;
249 pFirstConPos
= &m_aSourceConnPos
;
250 pFirstDescrPos
= &m_aSourceDescrLinePos
;
251 pSecondConPos
= &m_aDestConnPos
;
252 pSecondDescrPos
= &m_aDestDescrLinePos
;
255 if (pFirstWin
== pSecondWin
&& nSourceEntry
!= nDestEntry
)
256 calcPointX2(pFirstWin
,*pFirstConPos
,*pFirstDescrPos
);
258 calcPointX1(pFirstWin
,*pFirstConPos
,*pFirstDescrPos
);
259 calcPointX2(pSecondWin
,*pSecondConPos
,*pSecondDescrPos
);
261 // determine aSourceConnPosY
262 calcPointsYValue(pSourceWin
, nSourceEntry
, m_aSourceConnPos
,m_aSourceDescrLinePos
);
264 // determine aDestConnPosY
265 calcPointsYValue(pDestWin
, nDestEntry
, m_aDestConnPos
,m_aDestDescrLinePos
);
270 void OConnectionLine::Draw( OutputDevice
* pOutDev
)
272 const sal_uInt16 nRectSize
= 3;
274 // calculate new dimension
279 if (m_pTabConn
->IsSelected())
280 pOutDev
->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
282 pOutDev
->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
285 if ( m_pTabConn
->IsSelected() )
286 aLineInfo
.SetWidth(3);
287 tools::Polygon aPoly
;
288 aPoly
.Insert(0,m_aSourceDescrLinePos
);
289 aPoly
.Insert(1,m_aSourceConnPos
);
290 aPoly
.Insert(2,m_aDestConnPos
);
291 aPoly
.Insert(3,m_aDestDescrLinePos
);
292 pOutDev
->DrawPolyLine(aPoly
,aLineInfo
);
294 // draw the connection rectangles
295 pOutDev
->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
297 Point
aVector(nRectSize
,nRectSize
);
298 pOutDev
->DrawRect( calcRect(m_aSourceDescrLinePos
,aVector
) );
299 pOutDev
->DrawRect( calcRect( m_aDestDescrLinePos
,aVector
) );
302 bool OConnectionLine::IsValid() const
307 static double dist_Euklid(const Point
&p1
, const Point
& p2
,const Point
& pM
, Point
& q
)
311 double a
= std::hypot(v
.X(), v
.Y());
312 double l
= (v
.X() * w
.Y() - v
.Y() * w
.X()) / a
;
313 double a2
= w
.X()*v
.X()+w
.Y()*v
.Y();
315 q
.setX( tools::Long(p1
.X() + a
* v
.X()) );
316 q
.setY( tools::Long(p1
.Y() + a
* v
.Y()) );
320 bool OConnectionLine::CheckHit( const Point
& rMousePos
) const
323 course of action with HitTest:
324 the distance is calculated according to Euklid.
327 double l
= fabs(dist_Euklid(m_aSourceConnPos
,m_aDestConnPos
,rMousePos
,q
));
328 if( l
< HIT_SENSITIVE_RADIUS
)
330 if(std::min(m_aSourceConnPos
.X(),m_aDestConnPos
.X()) <= q
.X() && std::min(m_aSourceConnPos
.Y(),m_aDestConnPos
.Y()) <= q
.Y()
331 && q
.X() <= std::max(m_aDestConnPos
.X(),m_aSourceConnPos
.X()) && q
.Y() <= std::max(m_aDestConnPos
.Y(),m_aSourceConnPos
.Y()))
338 tools::Rectangle
OConnectionLine::GetSourceTextPos() const
340 return GetTextPos(m_pTabConn
->GetSourceWin(),m_aSourceConnPos
,m_aSourceDescrLinePos
);
343 tools::Rectangle
OConnectionLine::GetDestTextPos() const
345 return GetTextPos(m_pTabConn
->GetDestWin(),m_aDestConnPos
,m_aDestDescrLinePos
);
348 Point
OConnectionLine::getMidPoint() const
350 Point aDest
= m_aDestConnPos
- m_aSourceConnPos
;
351 aDest
.setX( aDest
.X() / 2 );
352 aDest
.setY( aDest
.Y() / 2 );
354 return m_aSourceConnPos
+ aDest
;
357 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */