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