LibreOffice Module svx (master)  1
svdglue.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 <tools/debug.hxx>
21 #include <vcl/window.hxx>
22 
23 #include <svx/svdglue.hxx>
24 #include <svx/svdobj.hxx>
25 #include <svx/svdtrans.hxx>
26 
27 static const Size aGlueHalfSize(4,4);
28 
29 void SdrGluePoint::SetReallyAbsolute(bool bOn, const SdrObject& rObj)
30 {
31  if ( bReallyAbsolute != bOn )
32  {
33  if ( bOn )
34  {
35  aPos=GetAbsolutePos(rObj);
36  bReallyAbsolute=bOn;
37  }
38  else
39  {
40  bReallyAbsolute=bOn;
41  Point aPt(aPos);
42  SetAbsolutePos(aPt,rObj);
43  }
44  }
45 }
46 
48 {
49  if (bReallyAbsolute) return aPos;
50  tools::Rectangle aSnap(rObj.GetSnapRect());
51  tools::Rectangle aBound(rObj.GetSnapRect());
52  Point aPt(aPos);
53 
54  Point aOfs(aSnap.Center());
55  switch (GetHorzAlign()) {
56  case SdrAlign::HORZ_LEFT : aOfs.setX(aSnap.Left() ); break;
57  case SdrAlign::HORZ_RIGHT : aOfs.setX(aSnap.Right() ); break;
58  default: break;
59  }
60  switch (GetVertAlign()) {
61  case SdrAlign::VERT_TOP : aOfs.setY(aSnap.Top() ); break;
62  case SdrAlign::VERT_BOTTOM: aOfs.setY(aSnap.Bottom() ); break;
63  default: break;
64  }
65  if (!bNoPercent) {
66  long nXMul=aSnap.Right()-aSnap.Left();
67  long nYMul=aSnap.Bottom()-aSnap.Top();
68  long nXDiv=10000;
69  long nYDiv=10000;
70  if (nXMul!=nXDiv) {
71  aPt.setX( aPt.X() * nXMul );
72  aPt.setX( aPt.X() / nXDiv );
73  }
74  if (nYMul!=nYDiv) {
75  aPt.setY( aPt.Y() * nYMul );
76  aPt.setY( aPt.Y() / nYDiv );
77  }
78  }
79  aPt+=aOfs;
80  // Now limit to the BoundRect of the object
81  if (aPt.X()<aBound.Left ()) aPt.setX(aBound.Left () );
82  if (aPt.X()>aBound.Right ()) aPt.setX(aBound.Right () );
83  if (aPt.Y()<aBound.Top ()) aPt.setY(aBound.Top () );
84  if (aPt.Y()>aBound.Bottom()) aPt.setY(aBound.Bottom() );
85  return aPt;
86 }
87 
88 void SdrGluePoint::SetAbsolutePos(const Point& rNewPos, const SdrObject& rObj)
89 {
90  if (bReallyAbsolute) {
91  aPos=rNewPos;
92  return;
93  }
94  tools::Rectangle aSnap(rObj.GetSnapRect());
95  Point aPt(rNewPos);
96 
97  Point aOfs(aSnap.Center());
98  switch (GetHorzAlign()) {
99  case SdrAlign::HORZ_LEFT : aOfs.setX(aSnap.Left() ); break;
100  case SdrAlign::HORZ_RIGHT : aOfs.setX(aSnap.Right() ); break;
101  default: break;
102  }
103  switch (GetVertAlign()) {
104  case SdrAlign::VERT_TOP : aOfs.setY(aSnap.Top() ); break;
105  case SdrAlign::VERT_BOTTOM: aOfs.setY(aSnap.Bottom() ); break;
106  default: break;
107  }
108  aPt-=aOfs;
109  if (!bNoPercent) {
110  long nXMul=aSnap.Right()-aSnap.Left();
111  long nYMul=aSnap.Bottom()-aSnap.Top();
112  if (nXMul==0) nXMul=1;
113  if (nYMul==0) nYMul=1;
114  long nXDiv=10000;
115  long nYDiv=10000;
116  if (nXMul!=nXDiv) {
117  aPt.setX( aPt.X() * nXDiv );
118  aPt.setX( aPt.X() / nXMul );
119  }
120  if (nYMul!=nYDiv) {
121  aPt.setY( aPt.Y() * nYDiv );
122  aPt.setY( aPt.Y() / nYMul );
123  }
124  }
125  aPos=aPt;
126 }
127 
129 {
131  return 0; // Invalid!
133  return 0;
135  return 4500;
137  return 9000;
139  return 13500;
141  return 18000;
143  return 22500;
145  return 27000;
147  return 31500;
148  return 0;
149 }
150 
152 {
153  nAngle=NormAngle36000(nAngle);
154  if (nAngle>=33750 || nAngle<2250) nAlign=SdrAlign::HORZ_RIGHT |SdrAlign::VERT_CENTER;
155  else if (nAngle< 6750) nAlign=SdrAlign::HORZ_RIGHT |SdrAlign::VERT_TOP ;
156  else if (nAngle<11250) nAlign=SdrAlign::HORZ_CENTER|SdrAlign::VERT_TOP ;
157  else if (nAngle<15750) nAlign=SdrAlign::HORZ_LEFT |SdrAlign::VERT_TOP ;
158  else if (nAngle<20250) nAlign=SdrAlign::HORZ_LEFT |SdrAlign::VERT_CENTER;
159  else if (nAngle<24750) nAlign=SdrAlign::HORZ_LEFT |SdrAlign::VERT_BOTTOM;
160  else if (nAngle<29250) nAlign=SdrAlign::HORZ_CENTER|SdrAlign::VERT_BOTTOM;
161  else if (nAngle<33750) nAlign=SdrAlign::HORZ_RIGHT |SdrAlign::VERT_BOTTOM;
162 }
163 
165 {
166  switch (nEsc) {
167  case SdrEscapeDirection::RIGHT : return 0;
168  case SdrEscapeDirection::TOP : return 9000;
169  case SdrEscapeDirection::LEFT : return 18000;
170  case SdrEscapeDirection::BOTTOM: return 27000;
171  default: break;
172  } // switch
173  return 0;
174 }
175 
177 {
178  nAngle=NormAngle36000(nAngle);
179  if (nAngle>=31500 || nAngle<4500)
181  if (nAngle<13500)
183  if (nAngle<22500)
185  /* (nAngle<31500)*/
187 }
188 
189 void SdrGluePoint::Rotate(const Point& rRef, long nAngle, double sn, double cs, const SdrObject* pObj)
190 {
191  Point aPt(pObj!=nullptr ? GetAbsolutePos(*pObj) : GetPos());
192  RotatePoint(aPt,rRef,sn,cs);
193  // rotate reference edge
195  {
196  SetAlignAngle(GetAlignAngle()+nAngle);
197  }
198  // rotate exit directions
199  SdrEscapeDirection nEscDir0=nEscDir;
201  if (nEscDir0&SdrEscapeDirection::LEFT ) nEscDir1 |= EscAngleToDir(EscDirToAngle(SdrEscapeDirection::LEFT )+nAngle);
202  if (nEscDir0&SdrEscapeDirection::TOP ) nEscDir1 |= EscAngleToDir(EscDirToAngle(SdrEscapeDirection::TOP )+nAngle);
203  if (nEscDir0&SdrEscapeDirection::RIGHT ) nEscDir1 |= EscAngleToDir(EscDirToAngle(SdrEscapeDirection::RIGHT )+nAngle);
204  if (nEscDir0&SdrEscapeDirection::BOTTOM) nEscDir1 |= EscAngleToDir(EscDirToAngle(SdrEscapeDirection::BOTTOM)+nAngle);
205  nEscDir=nEscDir1;
206  if (pObj!=nullptr) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
207 }
208 
209 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, long nAngle, const SdrObject* pObj)
210 {
211  Point aPt(pObj!=nullptr ? GetAbsolutePos(*pObj) : GetPos());
212  MirrorPoint(aPt,rRef1,rRef2);
213  // mirror reference edge
215  {
216  long nAW=GetAlignAngle();
217  nAW+=2*(nAngle-nAW);
218  SetAlignAngle(nAW);
219  }
220  // mirror exit directions
221  SdrEscapeDirection nEscDir0=nEscDir;
223  if (nEscDir0&SdrEscapeDirection::LEFT) {
224  long nEW=EscDirToAngle(SdrEscapeDirection::LEFT);
225  nEW+=2*(nAngle-nEW);
226  nEscDir1|=EscAngleToDir(nEW);
227  }
228  if (nEscDir0&SdrEscapeDirection::TOP) {
229  long nEW=EscDirToAngle(SdrEscapeDirection::TOP);
230  nEW+=2*(nAngle-nEW);
231  nEscDir1|=EscAngleToDir(nEW);
232  }
233  if (nEscDir0&SdrEscapeDirection::RIGHT) {
234  long nEW=EscDirToAngle(SdrEscapeDirection::RIGHT);
235  nEW+=2*(nAngle-nEW);
236  nEscDir1|=EscAngleToDir(nEW);
237  }
238  if (nEscDir0&SdrEscapeDirection::BOTTOM) {
239  long nEW=EscDirToAngle(SdrEscapeDirection::BOTTOM);
240  nEW+=2*(nAngle-nEW);
241  nEscDir1|=EscAngleToDir(nEW);
242  }
243  nEscDir=nEscDir1;
244  if (pObj!=nullptr) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
245 }
246 
247 void SdrGluePoint::Shear(const Point& rRef, double tn, bool bVShear, const SdrObject* pObj)
248 {
249  Point aPt(pObj!=nullptr ? GetAbsolutePos(*pObj) : GetPos());
250  ShearPoint(aPt,rRef,tn,bVShear);
251  if (pObj!=nullptr) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
252 }
253 
254 void SdrGluePoint::Invalidate(vcl::Window& rWin, const SdrObject* pObj) const
255 {
256  bool bMapMode=rWin.IsMapModeEnabled();
257  Point aPt(pObj!=nullptr ? GetAbsolutePos(*pObj) : GetPos());
258  aPt=rWin.LogicToPixel(aPt);
259  rWin.EnableMapMode(false);
260 
261  Size aSiz( aGlueHalfSize );
262  tools::Rectangle aRect(aPt.X()-aSiz.Width(),aPt.Y()-aSiz.Height(),
263  aPt.X()+aSiz.Width(),aPt.Y()+aSiz.Height());
264 
265  // do not erase background, that causes flicker (!)
266  rWin.Invalidate(aRect, InvalidateFlags::NoErase);
267 
268  rWin.EnableMapMode(bMapMode);
269 }
270 
271 bool SdrGluePoint::IsHit(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj) const
272 {
273  Point aPt(pObj!=nullptr ? GetAbsolutePos(*pObj) : GetPos());
274  Size aSiz=rOut.PixelToLogic(aGlueHalfSize);
275  tools::Rectangle aRect(aPt.X()-aSiz.Width(),aPt.Y()-aSiz.Height(),aPt.X()+aSiz.Width(),aPt.Y()+aSiz.Height());
276  return aRect.IsInside(rPnt);
277 }
278 
279 
281 {
282  aList.clear();
283 }
284 
286 {
287  if (GetCount()!=0) Clear();
288  sal_uInt16 nCount=rSrcList.GetCount();
289  for (sal_uInt16 i=0; i<nCount; i++) {
290  Insert(rSrcList[i]);
291  }
292  return *this;
293 }
294 
295 // The ID's of the glue points always increase monotonously!
296 // If an ID is taken already, the new glue point gets a new ID. ID 0 is reserved.
298 {
299  SdrGluePoint* pGP=new SdrGluePoint(rGP);
300  sal_uInt16 nId=pGP->GetId();
301  sal_uInt16 nCount=GetCount();
302  sal_uInt16 nInsPos=nCount;
303  sal_uInt16 nLastId=nCount!=0 ? aList[nCount-1]->GetId() : 0;
304  DBG_ASSERT(nLastId>=nCount,"SdrGluePointList::Insert(): nLastId<nCount");
305  bool bHole = nLastId>nCount;
306  if (nId<=nLastId) {
307  if (!bHole || nId==0) {
308  nId=nLastId+1;
309  } else {
310  bool bBrk = false;
311  for (sal_uInt16 nNum=0; nNum<nCount && !bBrk; nNum++) {
312  const auto& pGP2=aList[nNum];
313  sal_uInt16 nTmpId=pGP2->GetId();
314  if (nTmpId==nId) {
315  nId=nLastId+1; // already in use
316  bBrk = true;
317  }
318  if (nTmpId>nId) {
319  nInsPos=nNum; // insert here (sort)
320  bBrk = true;
321  }
322  }
323  }
324  pGP->SetId(nId);
325  }
326  aList.emplace(aList.begin()+nInsPos, pGP);
327  return nInsPos;
328 }
329 
330 void SdrGluePointList::Invalidate(vcl::Window& rWin, const SdrObject* pObj) const
331 {
332  for (auto& xGP : aList)
333  xGP->Invalidate(rWin,pObj);
334 }
335 
336 sal_uInt16 SdrGluePointList::FindGluePoint(sal_uInt16 nId) const
337 {
338  // TODO: Implement a better search algorithm
339  // List should be sorted at all times!
340  sal_uInt16 nCount=GetCount();
341  sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
342  for (sal_uInt16 nNum=0; nNum<nCount && nRet==SDRGLUEPOINT_NOTFOUND; nNum++) {
343  const auto& pGP=aList[nNum];
344  if (pGP->GetId()==nId) nRet=nNum;
345  }
346  return nRet;
347 }
348 
349 sal_uInt16 SdrGluePointList::HitTest(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj) const
350 {
351  sal_uInt16 nCount = GetCount();
352  sal_uInt16 nRet = SDRGLUEPOINT_NOTFOUND;
353  sal_uInt16 nNum = nCount;
354  while ((nNum>0) && nRet==SDRGLUEPOINT_NOTFOUND) {
355  nNum--;
356  const auto& pGP = aList[nNum];
357  if (pGP->IsHit(rPnt,rOut,pObj))
358  nRet = nNum;
359  }
360  return nRet;
361 }
362 
364 {
365  for (auto& xGP : aList)
366  xGP->SetReallyAbsolute(bOn,rObj);
367 }
368 
369 void SdrGluePointList::Rotate(const Point& rRef, long nAngle, double sn, double cs, const SdrObject* pObj)
370 {
371  for (auto& xGP : aList)
372  xGP->Rotate(rRef,nAngle,sn,cs,pObj);
373 }
374 
375 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
376 {
377  Point aPt(rRef2); aPt-=rRef1;
378  long nAngle=GetAngle(aPt);
379  Mirror(rRef1,rRef2,nAngle,pObj);
380 }
381 
382 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, long nAngle, const SdrObject* pObj)
383 {
384  for (auto& xGP : aList)
385  xGP->Mirror(rRef1,rRef2,nAngle,pObj);
386 }
387 
388 void SdrGluePointList::Shear(const Point& rRef, double tn, bool bVShear, const SdrObject* pObj)
389 {
390  for (auto& xGP : aList)
391  xGP->Shear(rRef,tn,bVShear,pObj);
392 }
393 
394 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void Rotate(const Point &rRef, long nAngle, double sn, double cs, const SdrObject *pObj)
Definition: svdglue.cxx:189
void SetId(sal_uInt16 nNewId)
Definition: svdglue.hxx:87
void ShearPoint(Point &rPnt, const Point &rRef, double tn, bool bVShear=false)
Definition: svdtrans.hxx:122
static const Size aGlueHalfSize(4, 4)
SdrAlign GetVertAlign() const
Definition: svdglue.hxx:101
long Height() const
SdrEscapeDirection
Definition: svdglue.hxx:35
bool IsMapModeEnabled() const
sal_Int16 nId
void EnableMapMode(bool bEnable=true)
sal_uInt16 GetCount() const
Definition: svdglue.hxx:126
long NormAngle36000(long a)
Normalize angle to -180.00..179.99.
Definition: svdtrans.cxx:407
SdrGluePointList & operator=(const SdrGluePointList &rSrcList)
Definition: svdglue.cxx:285
virtual const tools::Rectangle & GetSnapRect() const
Definition: svdobj.cxx:1624
sal_uInt16 GetId() const
Definition: svdglue.hxx:86
void Mirror(const Point &rRef1, const Point &rRef2, const SdrObject *pObj)
Definition: svdglue.cxx:375
sal_uInt16 HitTest(const Point &rPnt, const OutputDevice &rOut, const SdrObject *pObj) const
Definition: svdglue.cxx:349
int nCount
bool IsHit(const Point &rPnt, const OutputDevice &rOut, const SdrObject *pObj) const
Definition: svdglue.cxx:271
SdrAlign GetHorzAlign() const
Definition: svdglue.hxx:99
SdrAlign nAlign
Definition: svdglue.hxx:75
void SetAlignAngle(long nAngle)
Definition: svdglue.cxx:151
#define DBG_ASSERT(sCon, aError)
std::vector< std::unique_ptr< SdrGluePoint > > aList
Definition: svdglue.hxx:119
#define SDRGLUEPOINT_NOTFOUND
Definition: svdglue.hxx:116
int i
Point aPos
Definition: svdglue.hxx:72
virtual void Invalidate(InvalidateFlags nFlags=InvalidateFlags::NONE)
void MirrorPoint(Point &rPnt, const Point &rRef1, const Point &rRef2)
Definition: svdtrans.cxx:103
void Invalidate(vcl::Window &rWin, const SdrObject *pObj) const
Definition: svdglue.cxx:254
bool bNoPercent
Definition: svdglue.hxx:76
bool IsInside(const Point &rPOINT) const
void SetAbsolutePos(const Point &rNewPos, const SdrObject &rObj)
Definition: svdglue.cxx:88
void Mirror(const Point &rRef1, const Point &rRef2, long nAngle, const SdrObject *pObj)
Definition: svdglue.cxx:209
Abstract DrawObject.
Definition: svdobj.hxx:312
void SetReallyAbsolute(bool bOn, const SdrObject &rObj)
Definition: svdglue.cxx:363
long X() const
Point GetAbsolutePos(const SdrObject &rObj) const
Definition: svdglue.cxx:47
sal_uInt16 FindGluePoint(sal_uInt16 nId) const
Definition: svdglue.cxx:336
sal_uInt16 Insert(const SdrGluePoint &rGP)
Definition: svdglue.cxx:297
Point PixelToLogic(const Point &rDevicePt) const
Point LogicToPixel(const Point &rLogicPt) const
void RotatePoint(Point &rPnt, const Point &rRef, double sn, double cs)
Definition: svdtrans.hxx:114
SdrEscapeDirection nEscDir
Definition: svdglue.hxx:73
void Shear(const Point &rRef, double tn, bool bVShear, const SdrObject *pObj)
Definition: svdglue.cxx:247
const Point & GetPos() const
Definition: svdglue.hxx:82
void SetReallyAbsolute(bool bOn, const SdrObject &rObj)
Definition: svdglue.cxx:29
long GetAngle(const Point &rPnt)
The Y axis points down! The function negates the Y axis, when calculating the angle, such that GetAngle(Point(0,-1))=90 deg.
Definition: svdtrans.cxx:385
static long EscDirToAngle(SdrEscapeDirection nEsc)
Definition: svdglue.cxx:164
void Rotate(const Point &rRef, long nAngle, double sn, double cs, const SdrObject *pObj)
Definition: svdglue.cxx:369
void Shear(const Point &rRef, double tn, bool bVShear, const SdrObject *pObj)
Definition: svdglue.cxx:388
long GetAlignAngle() const
Definition: svdglue.cxx:128
void SetPos(const Point &rNewPos)
Definition: svdglue.hxx:83
long Y() const
void Invalidate(vcl::Window &rWin, const SdrObject *pObj) const
Definition: svdglue.cxx:330
bool bReallyAbsolute
Definition: svdglue.hxx:77
static SdrEscapeDirection EscAngleToDir(long nAngle)
Definition: svdglue.cxx:176