Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
30 :
31 : using namespace dbaui;
32 : const long DESCRIPT_LINE_WIDTH = 15;
33 : const long HIT_SENSITIVE_RADIUS = 5;
34 :
35 : namespace
36 : {
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
40 : */
41 0 : inline Rectangle calcRect(const Point& _rBase,const Point& _aVector)
42 : {
43 0 : return Rectangle( _rBase - _aVector, _rBase + _aVector );
44 : }
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
49 : */
50 0 : Rectangle GetTextPos(const OTableWindow* _pWin, const Point& _aConnPos,const Point& _aDescrLinePos)
51 : {
52 0 : OTableWindowListBox* pListBox = _pWin ? _pWin->GetListBox() : NULL;
53 : OSL_ENSURE(_pWin && pListBox, "OConnectionLine::GetSourceTextPos : invalid call !");
54 :
55 0 : Rectangle aReturn;
56 0 : if ( pListBox )
57 : {
58 0 : const long nRowHeight = pListBox->GetEntryHeight();
59 0 : aReturn.Top() = _aConnPos.Y() - nRowHeight;
60 0 : aReturn.Bottom() = aReturn.Top() + nRowHeight;
61 0 : if (_aDescrLinePos.X() < _aConnPos.X())
62 : {
63 0 : aReturn.Left() = _aDescrLinePos.X();
64 0 : aReturn.Right() = aReturn.Left() + _aConnPos.X() - _aDescrLinePos.X();
65 : }
66 : else
67 : {
68 0 : aReturn.Left() = _aConnPos.X();
69 0 : aReturn.Right() = aReturn.Left() + _aDescrLinePos.X() - _aConnPos.X();
70 : }
71 : }
72 :
73 0 : return aReturn;
74 : }
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
80 : */
81 0 : void calcPointsYValue(const OTableWindow* _pWin,SvTreeListEntry* _pEntry,Point& _rNewConPos,Point& _rNewDescrPos)
82 : {
83 0 : const OTableWindowListBox* pListBox = _pWin->GetListBox();
84 0 : _rNewConPos.Y() = _pWin->GetPosPixel().Y();
85 0 : if ( _pEntry )
86 : {
87 0 : const long nRowHeight = pListBox->GetEntryHeight();
88 0 : _rNewConPos.Y() += pListBox->GetPosPixel().Y();
89 0 : long nEntryPos = pListBox->GetEntryPosition( _pEntry ).Y();
90 :
91 0 : if( nEntryPos >= 0 )
92 : {
93 0 : _rNewConPos.Y() += nEntryPos;
94 0 : _rNewConPos.Y() += (long)( 0.5 * nRowHeight );
95 : }
96 : else
97 0 : _rNewConPos.Y() -= (long)( 0.5 * nRowHeight );
98 :
99 0 : long nListBoxBottom = _pWin->GetPosPixel().Y()
100 0 : + pListBox->GetPosPixel().Y()
101 0 : + pListBox->GetSizePixel().Height();
102 0 : if( _rNewConPos.Y() > nListBoxBottom )
103 0 : _rNewConPos.Y() = nListBoxBottom + 2;
104 : }
105 : else
106 0 : _rNewConPos.Y() += static_cast<sal_Int32>(pListBox->GetPosPixel().Y()*0.5);
107 :
108 0 : _rNewDescrPos.Y() = _rNewConPos.Y();
109 0 : }
110 : }
111 :
112 : // class OConnectionLine
113 0 : OConnectionLine::OConnectionLine( OTableConnection* _pConn, OConnectionLineDataRef _pLineData )
114 : : m_pTabConn( _pConn )
115 0 : ,m_pData( _pLineData )
116 : {
117 0 : }
118 :
119 0 : OConnectionLine::OConnectionLine( const OConnectionLine& _rLine )
120 : {
121 0 : m_pData = new OConnectionLineData( *_rLine.GetData() );
122 0 : *this = _rLine;
123 0 : }
124 :
125 0 : OConnectionLine::~OConnectionLine()
126 : {
127 0 : }
128 :
129 0 : OConnectionLine& OConnectionLine::operator=( const OConnectionLine& rLine )
130 : {
131 0 : if( &rLine != this )
132 : {
133 : // as the data does not belong to me, I don't delete the old one
134 0 : m_pData->CopyFrom(*rLine.GetData());
135 : // CopyFrom is virtual, therefore it is not a problem, if m_pData is of a type derived from OTableConnectionData
136 :
137 0 : m_pTabConn = rLine.m_pTabConn;
138 0 : m_aSourceConnPos = rLine.m_aSourceConnPos;
139 0 : m_aDestConnPos = rLine.m_aDestConnPos;
140 0 : m_aSourceDescrLinePos = rLine.m_aSourceDescrLinePos;
141 0 : m_aDestDescrLinePos = rLine.m_aDestDescrLinePos;
142 : }
143 :
144 0 : return *this;
145 : }
146 :
147 0 : Rectangle OConnectionLine::GetBoundingRect()
148 : {
149 : // determine surrounding rectangle
150 0 : Rectangle aBoundingRect( Point(0,0), Point(0,0) );
151 0 : if( !IsValid() )
152 0 : return aBoundingRect;
153 :
154 0 : Point aTopLeft;
155 0 : Point aBottomRight;
156 :
157 0 : if( m_aSourceDescrLinePos.Y() <= m_aDestDescrLinePos.Y() )
158 : {
159 0 : aTopLeft.Y() = m_aSourceDescrLinePos.Y();
160 0 : aBottomRight.Y() = m_aDestDescrLinePos.Y();
161 : }
162 : else
163 : {
164 0 : aTopLeft.Y() = m_aDestDescrLinePos.Y();
165 0 : aBottomRight.Y() = m_aSourceDescrLinePos.Y();
166 : }
167 :
168 0 : if( m_aSourceDescrLinePos.X() <= m_aDestDescrLinePos.X() )
169 : {
170 0 : aTopLeft.X() = m_aSourceDescrLinePos.X();
171 0 : aBottomRight.X() = m_aDestDescrLinePos.X();
172 : }
173 : else
174 : {
175 0 : aTopLeft.X() = m_aDestDescrLinePos.X();
176 0 : aBottomRight.X() = m_aSourceDescrLinePos.X();
177 : }
178 :
179 0 : const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
180 0 : const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
181 : // line proceeds in in z-Form
182 0 : if( pSourceWin == pDestWin || std::abs(m_aSourceConnPos.X() - m_aDestConnPos.X()) > std::abs(m_aSourceDescrLinePos.X() - m_aDestDescrLinePos.X()) )
183 : {
184 0 : aTopLeft.X() -= DESCRIPT_LINE_WIDTH;
185 0 : aBottomRight.X() += DESCRIPT_LINE_WIDTH;
186 : }
187 :
188 0 : aBoundingRect = Rectangle( aTopLeft-Point(2,17), aBottomRight+Point(2,2) );
189 :
190 0 : return aBoundingRect;
191 : }
192 :
193 0 : void calcPointX1(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
194 : {
195 0 : _rNewConPos.X() = _pWin->GetPosPixel().X() + _pWin->GetSizePixel().Width();
196 0 : _rNewDescrPos.X() = _rNewConPos.X();
197 0 : _rNewConPos.X() += DESCRIPT_LINE_WIDTH;
198 0 : }
199 :
200 0 : void calcPointX2(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
201 : {
202 0 : _rNewConPos.X() = _pWin->GetPosPixel().X();
203 0 : _rNewDescrPos.X() = _rNewConPos.X();
204 0 : _rNewConPos.X() -= DESCRIPT_LINE_WIDTH;
205 0 : }
206 :
207 0 : sal_Bool OConnectionLine::RecalcLine()
208 : {
209 : // Windows and entries must be set
210 0 : const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
211 0 : const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
212 :
213 0 : if( !pSourceWin || !pDestWin )
214 0 : return sal_False;
215 :
216 0 : SvTreeListEntry* pSourceEntry = pSourceWin->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
217 0 : SvTreeListEntry* pDestEntry = pDestWin->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
218 :
219 : // determine X-coordinates
220 0 : Point aSourceCenter( 0, 0 );
221 0 : Point aDestCenter( 0, 0 );
222 :
223 0 : aSourceCenter.X() = pSourceWin->GetPosPixel().X() + (long)( 0.5*pSourceWin->GetSizePixel().Width() );
224 0 : aDestCenter.X() = pDestWin->GetPosPixel().X() + (long)( 0.5*pDestWin->GetSizePixel().Width() );
225 :
226 0 : const OTableWindow* pFirstWin = pDestWin;
227 0 : const OTableWindow* pSecondWin = pSourceWin;
228 0 : Point* pFirstConPos = &m_aDestConnPos;
229 0 : Point* pFirstDescrPos = &m_aDestDescrLinePos;
230 0 : Point* pSecondConPos = &m_aSourceConnPos;
231 0 : Point* pSecondDescrPos = &m_aSourceDescrLinePos;
232 0 : if( aDestCenter.X() > aSourceCenter.X() )
233 : {
234 0 : pFirstWin = pSourceWin;
235 0 : pSecondWin = pDestWin;
236 0 : pFirstConPos = &m_aSourceConnPos;
237 0 : pFirstDescrPos = &m_aSourceDescrLinePos;
238 0 : pSecondConPos = &m_aDestConnPos;
239 0 : pSecondDescrPos = &m_aDestDescrLinePos;
240 : }
241 :
242 0 : if ( pFirstWin == pSecondWin && pSourceEntry != pDestEntry )
243 0 : calcPointX2(pFirstWin,*pFirstConPos,*pFirstDescrPos);
244 : else
245 0 : calcPointX1(pFirstWin,*pFirstConPos,*pFirstDescrPos);
246 0 : calcPointX2(pSecondWin,*pSecondConPos,*pSecondDescrPos);
247 :
248 : // determine aSourceConnPosY
249 0 : calcPointsYValue(pSourceWin,pSourceEntry,m_aSourceConnPos,m_aSourceDescrLinePos);
250 :
251 : // determine aDestConnPosY
252 0 : calcPointsYValue(pDestWin,pDestEntry,m_aDestConnPos,m_aDestDescrLinePos);
253 :
254 0 : return sal_True;
255 : }
256 :
257 0 : void OConnectionLine::Draw( OutputDevice* pOutDev )
258 : {
259 0 : const sal_uInt16 nRectSize = 3;
260 :
261 : // calculate new dimension
262 0 : if( !RecalcLine() )
263 0 : return;
264 :
265 : // draw lines
266 0 : if (m_pTabConn->IsSelected())
267 0 : pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
268 : else
269 0 : pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
270 :
271 0 : LineInfo aLineInfo;
272 0 : if ( m_pTabConn->IsSelected() )
273 0 : aLineInfo.SetWidth(3);
274 0 : Polygon aPoly;
275 0 : aPoly.Insert(0,m_aSourceDescrLinePos);
276 0 : aPoly.Insert(1,m_aSourceConnPos);
277 0 : aPoly.Insert(2,m_aDestConnPos);
278 0 : aPoly.Insert(3,m_aDestDescrLinePos);
279 0 : pOutDev->DrawPolyLine(aPoly,aLineInfo);
280 :
281 : // draw the connection rectangles
282 0 : pOutDev->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
283 :
284 0 : Point aVector(nRectSize,nRectSize);
285 0 : pOutDev->DrawRect( calcRect(m_aSourceDescrLinePos,aVector) );
286 0 : pOutDev->DrawRect( calcRect( m_aDestDescrLinePos,aVector) );
287 : }
288 :
289 0 : sal_Bool OConnectionLine::IsValid() const
290 : {
291 0 : return m_pData.is();
292 : }
293 :
294 0 : double dist_Euklid(const Point &p1, const Point& p2,const Point& pM, Point& q)
295 : {
296 0 : Point v(p2 - p1);
297 0 : Point w(pM - p1);
298 0 : double a = sqrt((double)(v.X()*v.X() + v.Y()*v.Y()));
299 0 : double l = (v.X() * w.Y() - v.Y() * w.X()) / a;
300 0 : double a2 = w.X()*v.X()+w.Y()*v.Y();
301 0 : a = a2 / (a * a);
302 0 : q.X() = long(p1.X() + a * v.X());
303 0 : q.Y() = long(p1.Y() + a * v.Y());
304 0 : return l;
305 : }
306 :
307 0 : bool OConnectionLine::CheckHit( const Point& rMousePos ) const
308 : {
309 : /*
310 : course of action with HitTest:
311 : the distance is calculated according to Euklid.
312 : */
313 0 : Point q;
314 0 : double l = fabs(dist_Euklid(m_aSourceConnPos,m_aDestConnPos,rMousePos,q));
315 0 : if( l < HIT_SENSITIVE_RADIUS)
316 : {
317 0 : if(::std::min(m_aSourceConnPos.X(),m_aDestConnPos.X()) <= q.X() && ::std::min(m_aSourceConnPos.Y(),m_aDestConnPos.Y()) <= q.Y()
318 0 : && q.X() <= ::std::max(m_aDestConnPos.X(),m_aSourceConnPos.X()) && q.Y() <= ::std::max(m_aDestConnPos.Y(),m_aSourceConnPos.Y()))
319 0 : return true;
320 : }
321 :
322 0 : return false;
323 : }
324 :
325 0 : Rectangle OConnectionLine::GetSourceTextPos() const
326 : {
327 0 : return GetTextPos(m_pTabConn->GetSourceWin(),m_aSourceConnPos,m_aSourceDescrLinePos);
328 : }
329 :
330 0 : Rectangle OConnectionLine::GetDestTextPos() const
331 : {
332 0 : return GetTextPos(m_pTabConn->GetDestWin(),m_aDestConnPos,m_aDestDescrLinePos);
333 : }
334 :
335 0 : Point OConnectionLine::getMidPoint() const
336 : {
337 0 : Point aDest = m_aDestConnPos - m_aSourceConnPos;
338 0 : aDest.X() /= 2;
339 0 : aDest.Y() /= 2;
340 :
341 0 : return m_aSourceConnPos + aDest;
342 : }
343 :
344 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|