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 0 : : m_pTabConn(NULL)
121 : {
122 0 : m_pData = new OConnectionLineData( *_rLine.GetData() );
123 0 : *this = _rLine;
124 0 : }
125 :
126 0 : OConnectionLine::~OConnectionLine()
127 : {
128 0 : }
129 :
130 0 : OConnectionLine& OConnectionLine::operator=( const OConnectionLine& rLine )
131 : {
132 0 : if( &rLine != this )
133 : {
134 : // as the data does not belong to me, I don't delete the old one
135 0 : 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
137 :
138 0 : m_pTabConn = rLine.m_pTabConn;
139 0 : m_aSourceConnPos = rLine.m_aSourceConnPos;
140 0 : m_aDestConnPos = rLine.m_aDestConnPos;
141 0 : m_aSourceDescrLinePos = rLine.m_aSourceDescrLinePos;
142 0 : m_aDestDescrLinePos = rLine.m_aDestDescrLinePos;
143 : }
144 :
145 0 : return *this;
146 : }
147 :
148 0 : Rectangle OConnectionLine::GetBoundingRect()
149 : {
150 : // determine surrounding rectangle
151 0 : Rectangle aBoundingRect( Point(0,0), Point(0,0) );
152 0 : if( !IsValid() )
153 0 : return aBoundingRect;
154 :
155 0 : Point aTopLeft;
156 0 : Point aBottomRight;
157 :
158 0 : if( m_aSourceDescrLinePos.Y() <= m_aDestDescrLinePos.Y() )
159 : {
160 0 : aTopLeft.Y() = m_aSourceDescrLinePos.Y();
161 0 : aBottomRight.Y() = m_aDestDescrLinePos.Y();
162 : }
163 : else
164 : {
165 0 : aTopLeft.Y() = m_aDestDescrLinePos.Y();
166 0 : aBottomRight.Y() = m_aSourceDescrLinePos.Y();
167 : }
168 :
169 0 : if( m_aSourceDescrLinePos.X() <= m_aDestDescrLinePos.X() )
170 : {
171 0 : aTopLeft.X() = m_aSourceDescrLinePos.X();
172 0 : aBottomRight.X() = m_aDestDescrLinePos.X();
173 : }
174 : else
175 : {
176 0 : aTopLeft.X() = m_aDestDescrLinePos.X();
177 0 : aBottomRight.X() = m_aSourceDescrLinePos.X();
178 : }
179 :
180 0 : const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
181 0 : const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
182 : // line proceeds in z-Form
183 0 : if( pSourceWin == pDestWin || std::abs(m_aSourceConnPos.X() - m_aDestConnPos.X()) > std::abs(m_aSourceDescrLinePos.X() - m_aDestDescrLinePos.X()) )
184 : {
185 0 : aTopLeft.X() -= DESCRIPT_LINE_WIDTH;
186 0 : aBottomRight.X() += DESCRIPT_LINE_WIDTH;
187 : }
188 :
189 0 : aBoundingRect = Rectangle( aTopLeft-Point(2,17), aBottomRight+Point(2,2) );
190 :
191 0 : return aBoundingRect;
192 : }
193 :
194 0 : void calcPointX1(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
195 : {
196 0 : _rNewConPos.X() = _pWin->GetPosPixel().X() + _pWin->GetSizePixel().Width();
197 0 : _rNewDescrPos.X() = _rNewConPos.X();
198 0 : _rNewConPos.X() += DESCRIPT_LINE_WIDTH;
199 0 : }
200 :
201 0 : void calcPointX2(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
202 : {
203 0 : _rNewConPos.X() = _pWin->GetPosPixel().X();
204 0 : _rNewDescrPos.X() = _rNewConPos.X();
205 0 : _rNewConPos.X() -= DESCRIPT_LINE_WIDTH;
206 0 : }
207 :
208 0 : bool OConnectionLine::RecalcLine()
209 : {
210 : // Windows and entries must be set
211 0 : const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
212 0 : const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
213 :
214 0 : if( !pSourceWin || !pDestWin )
215 0 : return false;
216 :
217 0 : SvTreeListEntry* pSourceEntry = pSourceWin->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
218 0 : SvTreeListEntry* pDestEntry = pDestWin->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
219 :
220 : // determine X-coordinates
221 0 : Point aSourceCenter( 0, 0 );
222 0 : Point aDestCenter( 0, 0 );
223 :
224 0 : aSourceCenter.X() = pSourceWin->GetPosPixel().X() + (long)( 0.5*pSourceWin->GetSizePixel().Width() );
225 0 : aDestCenter.X() = pDestWin->GetPosPixel().X() + (long)( 0.5*pDestWin->GetSizePixel().Width() );
226 :
227 0 : const OTableWindow* pFirstWin = pDestWin;
228 0 : const OTableWindow* pSecondWin = pSourceWin;
229 0 : Point* pFirstConPos = &m_aDestConnPos;
230 0 : Point* pFirstDescrPos = &m_aDestDescrLinePos;
231 0 : Point* pSecondConPos = &m_aSourceConnPos;
232 0 : Point* pSecondDescrPos = &m_aSourceDescrLinePos;
233 0 : if( aDestCenter.X() > aSourceCenter.X() )
234 : {
235 0 : pFirstWin = pSourceWin;
236 0 : pSecondWin = pDestWin;
237 0 : pFirstConPos = &m_aSourceConnPos;
238 0 : pFirstDescrPos = &m_aSourceDescrLinePos;
239 0 : pSecondConPos = &m_aDestConnPos;
240 0 : pSecondDescrPos = &m_aDestDescrLinePos;
241 : }
242 :
243 0 : if ( pFirstWin == pSecondWin && pSourceEntry != pDestEntry )
244 0 : calcPointX2(pFirstWin,*pFirstConPos,*pFirstDescrPos);
245 : else
246 0 : calcPointX1(pFirstWin,*pFirstConPos,*pFirstDescrPos);
247 0 : calcPointX2(pSecondWin,*pSecondConPos,*pSecondDescrPos);
248 :
249 : // determine aSourceConnPosY
250 0 : calcPointsYValue(pSourceWin,pSourceEntry,m_aSourceConnPos,m_aSourceDescrLinePos);
251 :
252 : // determine aDestConnPosY
253 0 : calcPointsYValue(pDestWin,pDestEntry,m_aDestConnPos,m_aDestDescrLinePos);
254 :
255 0 : return true;
256 : }
257 :
258 0 : void OConnectionLine::Draw( OutputDevice* pOutDev )
259 : {
260 0 : const sal_uInt16 nRectSize = 3;
261 :
262 : // calculate new dimension
263 0 : if( !RecalcLine() )
264 0 : return;
265 :
266 : // draw lines
267 0 : if (m_pTabConn->IsSelected())
268 0 : pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
269 : else
270 0 : pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
271 :
272 0 : LineInfo aLineInfo;
273 0 : if ( m_pTabConn->IsSelected() )
274 0 : aLineInfo.SetWidth(3);
275 0 : Polygon aPoly;
276 0 : aPoly.Insert(0,m_aSourceDescrLinePos);
277 0 : aPoly.Insert(1,m_aSourceConnPos);
278 0 : aPoly.Insert(2,m_aDestConnPos);
279 0 : aPoly.Insert(3,m_aDestDescrLinePos);
280 0 : pOutDev->DrawPolyLine(aPoly,aLineInfo);
281 :
282 : // draw the connection rectangles
283 0 : pOutDev->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
284 :
285 0 : Point aVector(nRectSize,nRectSize);
286 0 : pOutDev->DrawRect( calcRect(m_aSourceDescrLinePos,aVector) );
287 0 : pOutDev->DrawRect( calcRect( m_aDestDescrLinePos,aVector) );
288 : }
289 :
290 0 : bool OConnectionLine::IsValid() const
291 : {
292 0 : return m_pData.is();
293 : }
294 :
295 0 : double dist_Euklid(const Point &p1, const Point& p2,const Point& pM, Point& q)
296 : {
297 0 : Point v(p2 - p1);
298 0 : Point w(pM - p1);
299 0 : double a = sqrt((double)(v.X()*v.X() + v.Y()*v.Y()));
300 0 : double l = (v.X() * w.Y() - v.Y() * w.X()) / a;
301 0 : double a2 = w.X()*v.X()+w.Y()*v.Y();
302 0 : a = a2 / (a * a);
303 0 : q.X() = long(p1.X() + a * v.X());
304 0 : q.Y() = long(p1.Y() + a * v.Y());
305 0 : return l;
306 : }
307 :
308 0 : bool OConnectionLine::CheckHit( const Point& rMousePos ) const
309 : {
310 : /*
311 : course of action with HitTest:
312 : the distance is calculated according to Euklid.
313 : */
314 0 : Point q;
315 0 : double l = fabs(dist_Euklid(m_aSourceConnPos,m_aDestConnPos,rMousePos,q));
316 0 : if( l < HIT_SENSITIVE_RADIUS)
317 : {
318 0 : if(::std::min(m_aSourceConnPos.X(),m_aDestConnPos.X()) <= q.X() && ::std::min(m_aSourceConnPos.Y(),m_aDestConnPos.Y()) <= q.Y()
319 0 : && q.X() <= ::std::max(m_aDestConnPos.X(),m_aSourceConnPos.X()) && q.Y() <= ::std::max(m_aDestConnPos.Y(),m_aSourceConnPos.Y()))
320 0 : return true;
321 : }
322 :
323 0 : return false;
324 : }
325 :
326 0 : Rectangle OConnectionLine::GetSourceTextPos() const
327 : {
328 0 : return GetTextPos(m_pTabConn->GetSourceWin(),m_aSourceConnPos,m_aSourceDescrLinePos);
329 : }
330 :
331 0 : Rectangle OConnectionLine::GetDestTextPos() const
332 : {
333 0 : return GetTextPos(m_pTabConn->GetDestWin(),m_aDestConnPos,m_aDestDescrLinePos);
334 : }
335 :
336 0 : Point OConnectionLine::getMidPoint() const
337 : {
338 0 : Point aDest = m_aDestConnPos - m_aSourceConnPos;
339 0 : aDest.X() /= 2;
340 0 : aDest.Y() /= 2;
341 :
342 0 : return m_aSourceConnPos + aDest;
343 36 : }
344 :
345 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|