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