LibreOffice Module dbaccess (master) 1
ConnectionLine.cxx
Go to the documentation of this file.
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>
22#include <TableWindow.hxx>
24#include <TableConnection.hxx>
25#include <utility>
26#include <vcl/svapp.hxx>
27#include <math.h>
28#include <osl/diagnose.h>
29#include <vcl/lineinfo.hxx>
30#include <vcl/settings.hxx>
31
32using namespace dbaui;
35
36namespace
37{
42 tools::Rectangle calcRect(const Point& _rBase,const Point& _aVector)
43 {
44 return tools::Rectangle( _rBase - _aVector, _rBase + _aVector );
45 }
51 tools::Rectangle GetTextPos(const OTableWindow* _pWin, const Point& _aConnPos,const Point& _aDescrLinePos)
52 {
53 VclPtr<OTableWindowListBox> pListBox = _pWin ? _pWin->GetListBox() : nullptr;
54 OSL_ENSURE(_pWin && pListBox, "OConnectionLine::GetSourceTextPos : invalid call !");
55
56 tools::Rectangle aReturn;
57 if ( pListBox )
58 {
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())
63 {
64 aReturn.SetLeft( _aDescrLinePos.X() );
65 aReturn.SetRight( aReturn.Left() + _aConnPos.X() - _aDescrLinePos.X() );
66 }
67 else
68 {
69 aReturn.SetLeft( _aConnPos.X() );
70 aReturn.SetRight( aReturn.Left() + _aDescrLinePos.X() - _aConnPos.X() );
71 }
72 }
73
74 return aReturn;
75 }
82 void calcPointsYValue(const OTableWindow* _pWin, int _nEntry, Point& _rNewConPos, Point& _rNewDescrPos)
83 {
84 const OTableWindowListBox* pListBox = _pWin->GetListBox();
85 _rNewConPos.setY( _pWin->GetPosPixel().Y() );
86
87 std::unique_ptr<weld::TreeIter> xEntry;
88 const weld::TreeView& rTreeView = pListBox->get_widget();
89
90 if (_nEntry != -1)
91 {
92 _rNewConPos.AdjustY(pListBox->GetPosPixel().Y() );
93 xEntry = rTreeView.make_iterator();
94 if (!rTreeView.get_iter_first(*xEntry) || !rTreeView.iter_nth_sibling(*xEntry, _nEntry))
95 xEntry.reset();
96 }
97
98 if (xEntry)
99 {
100 auto nEntryPos = rTreeView.get_row_area(*xEntry).Center().Y();
101
102 if( nEntryPos >= 0 )
103 {
104 _rNewConPos.AdjustY(nEntryPos);
105 }
106 else
107 {
108 const auto nRowHeight = rTreeView.get_height_rows(1);
109 _rNewConPos.AdjustY( -static_cast<tools::Long>( 0.5 * nRowHeight ) );
110 }
111
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 );
117 }
118 else
119 _rNewConPos.AdjustY(static_cast<sal_Int32>(pListBox->GetPosPixel().Y()*0.5) );
120
121 _rNewDescrPos.setY( _rNewConPos.Y() );
122 }
123}
124
125OConnectionLine::OConnectionLine( OTableConnection* _pConn, OConnectionLineDataRef _pLineData )
126 : m_pTabConn( _pConn )
127 , m_pData(std::move( _pLineData ))
128{
129}
130
132 : m_pTabConn(nullptr)
133{
134 m_pData = new OConnectionLineData( *_rLine.GetData() );
135 *this = _rLine;
136}
137
139{
140}
141
143{
144 if( &rLine != this )
145 {
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
149
150 m_pTabConn = rLine.m_pTabConn;
155 }
156
157 return *this;
158}
159
161{
162 // determine surrounding rectangle
163 tools::Rectangle aBoundingRect( Point(0,0), Point(0,0) );
164 if( !IsValid() )
165 return aBoundingRect;
166
167 Point aTopLeft;
168 Point aBottomRight;
169
171 {
172 aTopLeft.setY( m_aSourceDescrLinePos.Y() );
173 aBottomRight.setY( m_aDestDescrLinePos.Y() );
174 }
175 else
176 {
177 aTopLeft.setY( m_aDestDescrLinePos.Y() );
178 aBottomRight.setY( m_aSourceDescrLinePos.Y() );
179 }
180
182 {
183 aTopLeft.setX( m_aSourceDescrLinePos.X() );
184 aBottomRight.setX( m_aDestDescrLinePos.X() );
185 }
186 else
187 {
188 aTopLeft.setX( m_aDestDescrLinePos.X() );
189 aBottomRight.setX( m_aSourceDescrLinePos.X() );
190 }
191
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()) )
196 {
197 aTopLeft.AdjustX( -DESCRIPT_LINE_WIDTH );
198 aBottomRight.AdjustX(DESCRIPT_LINE_WIDTH );
199 }
200
201 aBoundingRect = tools::Rectangle( aTopLeft-Point(2,17), aBottomRight+Point(2,2) );
202
203 return aBoundingRect;
204}
205
206static void calcPointX1(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
207{
208 _rNewConPos.setX( _pWin->GetPosPixel().X() + _pWin->GetSizePixel().Width() );
209 _rNewDescrPos.setX( _rNewConPos.X() );
210 _rNewConPos.AdjustX(DESCRIPT_LINE_WIDTH );
211}
212
213static void calcPointX2(const OTableWindow* _pWin,Point& _rNewConPos,Point& _rNewDescrPos)
214{
215 _rNewConPos.setX( _pWin->GetPosPixel().X() );
216 _rNewDescrPos.setX( _rNewConPos.X() );
217 _rNewConPos.AdjustX( -DESCRIPT_LINE_WIDTH );
218}
219
221{
222 // Windows and entries must be set
223 const OTableWindow* pSourceWin = m_pTabConn->GetSourceWin();
224 const OTableWindow* pDestWin = m_pTabConn->GetDestWin();
225
226 if( !pSourceWin || !pDestWin )
227 return false;
228
229 int nSourceEntry = pSourceWin->GetListBox()->GetEntryFromText( GetData()->GetSourceFieldName() );
230 int nDestEntry = pDestWin->GetListBox()->GetEntryFromText( GetData()->GetDestFieldName() );
231
232 // determine X-coordinates
233 Point aSourceCenter( 0, 0 );
234 Point aDestCenter( 0, 0 );
235
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() ) );
238
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() )
246 {
247 pFirstWin = pSourceWin;
248 pSecondWin = pDestWin;
249 pFirstConPos = &m_aSourceConnPos;
250 pFirstDescrPos = &m_aSourceDescrLinePos;
251 pSecondConPos = &m_aDestConnPos;
252 pSecondDescrPos = &m_aDestDescrLinePos;
253 }
254
255 if (pFirstWin == pSecondWin && nSourceEntry != nDestEntry)
256 calcPointX2(pFirstWin,*pFirstConPos,*pFirstDescrPos);
257 else
258 calcPointX1(pFirstWin,*pFirstConPos,*pFirstDescrPos);
259 calcPointX2(pSecondWin,*pSecondConPos,*pSecondDescrPos);
260
261 // determine aSourceConnPosY
262 calcPointsYValue(pSourceWin, nSourceEntry, m_aSourceConnPos,m_aSourceDescrLinePos);
263
264 // determine aDestConnPosY
265 calcPointsYValue(pDestWin, nDestEntry, m_aDestConnPos,m_aDestDescrLinePos);
266
267 return true;
268}
269
271{
272 const sal_uInt16 nRectSize = 3;
273
274 // calculate new dimension
275 if( !RecalcLine() )
276 return;
277
278 // draw lines
279 if (m_pTabConn->IsSelected())
280 pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetHighlightColor());
281 else
282 pOutDev->SetLineColor(Application::GetSettings().GetStyleSettings().GetWindowTextColor());
283
284 LineInfo aLineInfo;
285 if ( m_pTabConn->IsSelected() )
286 aLineInfo.SetWidth(3);
287 tools::Polygon aPoly;
289 aPoly.Insert(1,m_aSourceConnPos);
290 aPoly.Insert(2,m_aDestConnPos);
292 pOutDev->DrawPolyLine(aPoly,aLineInfo);
293
294 // draw the connection rectangles
295 pOutDev->SetFillColor(Application::GetSettings().GetStyleSettings().GetWindowColor());
296
297 Point aVector(nRectSize,nRectSize);
298 pOutDev->DrawRect( calcRect(m_aSourceDescrLinePos,aVector) );
299 pOutDev->DrawRect( calcRect( m_aDestDescrLinePos,aVector) );
300}
301
303{
304 return m_pData.is();
305}
306
307static double dist_Euklid(const Point &p1, const Point& p2,const Point& pM, Point& q)
308{
309 Point v(p2 - p1);
310 Point w(pM - p1);
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();
314 a = a2 / (a * a);
315 q.setX( tools::Long(p1.X() + a * v.X()) );
316 q.setY( tools::Long(p1.Y() + a * v.Y()) );
317 return l;
318}
319
320bool OConnectionLine::CheckHit( const Point& rMousePos ) const
321{
322 /*
323 course of action with HitTest:
324 the distance is calculated according to Euklid.
325 */
326 Point q;
327 double l = fabs(dist_Euklid(m_aSourceConnPos,m_aDestConnPos,rMousePos,q));
328 if( l < HIT_SENSITIVE_RADIUS)
329 {
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()))
332 return true;
333 }
334
335 return false;
336}
337
339{
340 return GetTextPos(m_pTabConn->GetSourceWin(),m_aSourceConnPos,m_aSourceDescrLinePos);
341}
342
344{
345 return GetTextPos(m_pTabConn->GetDestWin(),m_aDestConnPos,m_aDestDescrLinePos);
346}
347
349{
351 aDest.setX( aDest.X() / 2 );
352 aDest.setY( aDest.Y() / 2 );
353
354 return m_aSourceConnPos + aDest;
355}
356
357/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const tools::Long DESCRIPT_LINE_WIDTH
static double dist_Euklid(const Point &p1, const Point &p2, const Point &pM, Point &q)
static void calcPointX1(const OTableWindow *_pWin, Point &_rNewConPos, Point &_rNewDescrPos)
const tools::Long HIT_SENSITIVE_RADIUS
static void calcPointX2(const OTableWindow *_pWin, Point &_rNewConPos, Point &_rNewDescrPos)
static const AllSettings & GetSettings()
void SetWidth(double nWidth)
void DrawPolyLine(const tools::Polygon &rPoly)
void DrawRect(const tools::Rectangle &rRect)
void SetLineColor()
void SetFillColor()
constexpr tools::Long Y() const
void setX(tools::Long nX)
void setY(tools::Long nY)
tools::Long AdjustY(tools::Long nVertMove)
tools::Long AdjustX(tools::Long nHorzMove)
constexpr tools::Long X() const
constexpr tools::Long Height() const
constexpr tools::Long Width() const
the class OConnectionLineData contains the data of a connection e.g.
OConnectionLine & operator=(const OConnectionLine &rLine)
void Draw(OutputDevice *pOutDev)
bool CheckHit(const Point &rMousePos) const
tools::Rectangle GetBoundingRect() const
VclPtr< OTableConnection > m_pTabConn
OConnectionLine(OTableConnection *pConn, OConnectionLineDataRef pLineData)
OConnectionLineDataRef m_pData
const OConnectionLineDataRef & GetData() const
tools::Rectangle GetDestTextPos() const
tools::Rectangle GetSourceTextPos() const
const weld::TreeView & get_widget() const
const VclPtr< OTableWindowListBox > & GetListBox() const
void Insert(sal_uInt16 nPos, const Point &rPt)
constexpr Point Center() const
constexpr void SetLeft(tools::Long v)
constexpr void SetTop(tools::Long v)
constexpr tools::Long Top() const
constexpr void SetRight(tools::Long v)
constexpr void SetBottom(tools::Long v)
constexpr tools::Long Left() const
virtual Point GetPosPixel() const
virtual Size GetSizePixel() const
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const=0
virtual bool get_iter_first(TreeIter &rIter) const=0
virtual int get_height_rows(int nRows) const=0
bool iter_nth_sibling(TreeIter &rIter, int nChild) const
virtual tools::Rectangle get_row_area(const weld::TreeIter &rIter) const=0
ControllerFrame_Data * m_pData
float v
uno_Any a
long Long
sal_Int32 w