Okular

area.cpp
1 /*
2  SPDX-FileCopyrightText: 2004-05 Enrico Ros <[email protected]>
3  SPDX-FileCopyrightText: 2005 Piotr Szymanski <[email protected]>
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "area.h"
8 
9 #include <QPolygonF>
10 #include <QRect>
11 
12 #include "action.h"
13 #include "annotations.h"
14 #include "annotations_p.h"
15 #include "debug_p.h"
16 #include "sourcereference.h"
17 
18 using namespace Okular;
19 
20 /** class NormalizedPoint **/
22  : x(0.0)
23  , y(0.0)
24 {
25 }
26 
27 NormalizedPoint::NormalizedPoint(double dX, double dY)
28  : x(dX)
29  , y(dY)
30 {
31 }
32 
33 NormalizedPoint::NormalizedPoint(int iX, int iY, int xScale, int yScale)
34  : x((double)iX / (double)xScale)
35  , y((double)iY / (double)yScale)
36 {
37 }
38 
41 NormalizedPoint::~NormalizedPoint() = default;
42 
44 {
45  qreal tmp_x = (qreal)x;
46  qreal tmp_y = (qreal)y;
47  matrix.map(tmp_x, tmp_y, &tmp_x, &tmp_y);
48  x = tmp_x;
49  y = tmp_y;
50 }
51 
52 double NormalizedPoint::distanceSqr(double x, double y, double xScale, double yScale) const
53 {
54  return pow((this->x - x) * xScale, 2) + pow((this->y - y) * yScale, 2);
55 }
56 
57 /**
58  * Returns a vector from the given points @p a and @p b
59  * @internal
60  */
61 NormalizedPoint operator-(const NormalizedPoint &a, const NormalizedPoint &b)
62 {
63  return NormalizedPoint(a.x - b.x, a.y - b.y);
64 }
65 
66 /**
67  * @brief Calculates distance of the point @p x @p y @p xScale @p yScale to the line segment from @p start to @p end
68  */
69 double NormalizedPoint::distanceSqr(double x, double y, double xScale, double yScale, const NormalizedPoint &start, const NormalizedPoint &end)
70 {
71  NormalizedPoint point(x, y);
72  double thisDistance;
73  NormalizedPoint lineSegment(end - start);
74  const double lengthSqr = pow(lineSegment.x, 2) + pow(lineSegment.y, 2);
75 
76  // if the length of the current segment is null, we can just
77  // measure the distance to either end point
78  if (lengthSqr == 0.0) {
79  thisDistance = end.distanceSqr(x, y, xScale, yScale);
80  } else {
81  // vector from the start point of the current line segment to the measurement point
82  NormalizedPoint a = point - start;
83  // vector from the same start point to the end point of the current line segment
84  NormalizedPoint b = end - start;
85 
86  // we're using a * b (dot product) := |a| * |b| * cos(phi) and the knowledge
87  // that cos(phi) is adjacent side / hypotenuse (hypotenuse = |b|)
88  // therefore, t becomes the length of the vector that represents the projection of
89  // the point p onto the current line segment
90  //(hint: if this is still unclear, draw it!)
91  float t = (a.x * b.x + a.y * b.y) / lengthSqr;
92 
93  if (t < 0) {
94  // projection falls outside the line segment on the side of "start"
95  thisDistance = point.distanceSqr(start.x, start.y, xScale, yScale);
96  } else if (t > 1) {
97  // projection falls outside the line segment on the side of the current point
98  thisDistance = point.distanceSqr(end.x, end.y, xScale, yScale);
99  } else {
100  // projection is within [start, *i];
101  // determine the length of the perpendicular distance from the projection to the actual point
102  NormalizedPoint direction = end - start;
103  NormalizedPoint projection = start - NormalizedPoint(-t * direction.x, -t * direction.y);
104  thisDistance = projection.distanceSqr(x, y, xScale, yScale);
105  }
106  }
107  return thisDistance;
108 }
109 
111 {
112  str.nospace() << "NormPt(" << p.x << "," << p.y << ")";
113  return str.space();
114 }
115 
116 /** class NormalizedRect **/
117 
119  : left(0.0)
120  , top(0.0)
121  , right(0.0)
122  , bottom(0.0)
123 {
124 }
125 
126 NormalizedRect::NormalizedRect(double l, double t, double r, double b)
127  // note: check for swapping coords?
128  : left(l)
129  , top(t)
130  , right(r)
131  , bottom(b)
132 {
133 }
134 
135 NormalizedRect::NormalizedRect(const QRect &r, double xScale, double yScale) // clazy:exclude=function-args-by-value TODO when BIC changes are allowed
136  : left((double)r.left() / xScale)
137  , top((double)r.top() / yScale)
138  , right((double)r.right() / xScale)
139  , bottom((double)r.bottom() / yScale)
140 {
141 }
142 
143 NormalizedRect::NormalizedRect(const NormalizedRect &rect) = default;
144 
146 {
147  QRectF nrect = rect.normalized();
148  NormalizedRect ret;
149  ret.left = nrect.left();
150  ret.top = nrect.top();
151  ret.right = nrect.right();
152  ret.bottom = nrect.bottom();
153  return ret;
154 }
155 
157 {
158  return left == 0 && top == 0 && right == 0 && bottom == 0;
159 }
160 
161 bool NormalizedRect::contains(double x, double y) const
162 {
163  return x >= left && x <= right && y >= top && y <= bottom;
164 }
165 
167 {
168  return (r.left <= right) && (r.right >= left) && (r.top <= bottom) && (r.bottom >= top);
169 }
170 
172 {
173  return (r->left <= right) && (r->right >= left) && (r->top <= bottom) && (r->bottom >= top);
174 }
175 
176 bool NormalizedRect::intersects(double l, double t, double r, double b) const
177 {
178  return (l <= right) && (r >= left) && (t <= bottom) && (b >= top);
179 }
180 
182 {
183  NormalizedRect ret;
184  ret.left = qMin(left, r.left);
185  ret.top = qMin(top, r.top);
186  ret.bottom = qMax(bottom, r.bottom);
187  ret.right = qMax(right, r.right);
188  return ret;
189 }
190 
192 {
193  left = qMin(left, r.left);
194  top = qMin(top, r.top);
195  bottom = qMax(bottom, r.bottom);
196  right = qMax(right, r.right);
197  return *this;
198 }
199 
201 {
202  if (isNull() || r.isNull())
203  return NormalizedRect();
204 
205  NormalizedRect ret;
206  ret.left = qMax(left, r.left);
207  ret.top = qMax(top, r.top);
208  ret.bottom = qMin(bottom, r.bottom);
209  ret.right = qMin(right, r.right);
210  return ret;
211 }
212 
214 
215 NormalizedRect::~NormalizedRect() = default;
216 
218 {
219  return (isNull() && r.isNull()) || (fabs(left - r.left) < 1e-4 && fabs(right - r.right) < 1e-4 && fabs(top - r.top) < 1e-4 && fabs(bottom - r.bottom) < 1e-4);
220 }
221 
223 {
224  return NormalizedPoint((left + right) / 2.0, (top + bottom) / 2.0);
225 }
226 
227 /*
228 QDebug operator << (QDebug str , const NormalizedRect &r)
229 {
230  str << "[" <<r.left() << "," << r.top() << "] x "<< "[" <<r.right() << "," << r.bottom() << "]";
231  return str;
232 }*/
233 
234 QRect NormalizedRect::geometry(int xScale, int yScale) const
235 {
236  int l = (int)(left * xScale), t = (int)(top * yScale), r = (int)(right * xScale), b = (int)(bottom * yScale);
237 
238  return QRect(l, t, r - l + 1, b - t + 1);
239 }
240 
241 QRect NormalizedRect::roundedGeometry(int xScale, int yScale) const
242 {
243  int l = (int)(left * xScale + 0.5), t = (int)(top * yScale + 0.5), r = (int)(right * xScale + 0.5), b = (int)(bottom * yScale + 0.5);
244 
245  return QRect(l, t, r - l + 1, b - t + 1);
246 }
247 
249 {
250  QRectF rect(left, top, right - left, bottom - top);
251  rect = matrix.mapRect(rect);
252 
253  left = rect.left();
254  top = rect.top();
255  right = rect.right();
256  bottom = rect.bottom();
257 }
258 
259 uint Okular::qHash(const NormalizedRect &r, uint seed)
260 {
261  return ::qHash(r.bottom, ::qHash(r.right, ::qHash(r.top, ::qHash(r.left, seed))));
262 }
263 
265 {
266  str.nospace() << "NormRect(" << r.left << "," << r.top << " x " << (r.right - r.left) << "+" << (r.bottom - r.top) << ")";
267  return str.space();
268 }
269 
270 RegularAreaRect::RegularAreaRect()
272  , d(nullptr)
273 {
274 }
275 
276 RegularAreaRect::RegularAreaRect(const RegularAreaRect &rar)
278  , d(nullptr)
279 {
280 }
281 
282 RegularAreaRect::~RegularAreaRect()
283 {
284 }
285 
286 RegularAreaRect &RegularAreaRect::operator=(const RegularAreaRect &rar)
287 {
288  if (this != &rar) {
290  }
291  return *this;
292 }
293 
295  : RegularAreaRect()
296  , s_id(-1)
297 {
298  if (area) {
300  RegularAreaRect::ConstIterator itEnd = area->end();
301  for (; it != itEnd; ++it) {
302  append(NormalizedRect(*it));
303  }
304  }
305 }
306 
307 /** class ObjectRect **/
308 
309 ObjectRect::ObjectRect(double l, double t, double r, double b, bool ellipse, ObjectType type, void *object)
310  : m_objectType(type)
311  , m_object(object)
312 {
313  // assign coordinates swapping them if negative width or height
314  QRectF rect(r > l ? l : r, b > t ? t : b, fabs(r - l), fabs(b - t));
315  if (ellipse)
316  m_path.addEllipse(rect);
317  else
318  m_path.addRect(rect);
319 
320  m_transformedPath = m_path;
321 }
322 
323 ObjectRect::ObjectRect(const NormalizedRect &r, bool ellipse, ObjectType type, void *object)
324  : m_objectType(type)
325  , m_object(object)
326 {
327  QRectF rect(r.left, r.top, fabs(r.right - r.left), fabs(r.bottom - r.top));
328  if (ellipse)
329  m_path.addEllipse(rect);
330  else
331  m_path.addRect(rect);
332 
333  m_transformedPath = m_path;
334 }
335 
336 ObjectRect::ObjectRect(const QPolygonF &poly, ObjectType type, void *object)
337  : m_objectType(type)
338  , m_object(object)
339 {
340  m_path.addPolygon(poly);
341 
342  m_transformedPath = m_path;
343 }
344 
346 {
347  return m_objectType;
348 }
349 
350 const void *ObjectRect::object() const
351 {
352  return m_object;
353 }
354 
356 {
357  return m_transformedPath;
358 }
359 
360 QRect ObjectRect::boundingRect(double xScale, double yScale) const
361 {
362  const QRectF &br = m_transformedPath.boundingRect();
363 
364  return QRect((int)(br.left() * xScale), (int)(br.top() * yScale), (int)(br.width() * xScale), (int)(br.height() * yScale));
365 }
366 
367 bool ObjectRect::contains(double x, double y, double, double) const
368 {
369  return m_transformedPath.contains(QPointF(x, y));
370 }
371 
373 {
374  m_transformedPath = matrix.map(m_path);
375 }
376 
377 double ObjectRect::distanceSqr(double x, double y, double xScale, double yScale) const
378 {
379  switch (m_objectType) {
380  case Action:
381  case Image: {
382  const QRectF &rect(m_transformedPath.boundingRect());
383  return NormalizedRect(rect.x(), rect.y(), rect.right(), rect.bottom()).distanceSqr(x, y, xScale, yScale);
384  }
385  case OAnnotation: {
386  return static_cast<Annotation *>(m_object)->d_func()->distanceSqr(x, y, xScale, yScale);
387  }
388  case SourceRef: {
389  const SourceRefObjectRect *sr = static_cast<const SourceRefObjectRect *>(this);
390  const NormalizedPoint &point = sr->m_point;
391  if (point.x == -1.0) {
392  return pow((y - point.y) * yScale, 2);
393  } else if (point.y == -1.0) {
394  return pow((x - point.x) * xScale, 2);
395  } else {
396  return pow((x - point.x) * xScale, 2) + pow((y - point.y) * yScale, 2);
397  }
398  }
399  }
400  return 0.0;
401 }
402 
404 {
405  if (!m_object)
406  return;
407 
408  if (m_objectType == Action)
409  delete static_cast<Okular::Action *>(m_object);
410  else if (m_objectType == SourceRef)
411  delete static_cast<Okular::SourceReference *>(m_object);
412  else
413  qCDebug(OkularCoreDebug).nospace() << "Object deletion not implemented for type '" << m_objectType << "'.";
414 }
415 
416 /** class AnnotationObjectRect **/
417 
419  : ObjectRect(QPolygonF(), OAnnotation, annotation)
420  , m_annotation(annotation)
421 {
422 }
423 
425 {
426  return m_annotation;
427 }
428 
429 QRect AnnotationObjectRect::boundingRect(double xScale, double yScale) const
430 {
431  const QRect annotRect = AnnotationUtils::annotationGeometry(m_annotation, xScale, yScale);
432  const QPoint center = annotRect.center();
433 
434  // Make sure that the rectangle has a minimum size, so that it's possible
435  // to click on it
436  const int minSize = 14;
437  const QRect minRect(center.x() - minSize / 2, center.y() - minSize / 2, minSize, minSize);
438 
439  return annotRect | minRect;
440 }
441 
442 bool AnnotationObjectRect::contains(double x, double y, double xScale, double yScale) const
443 {
444  return boundingRect(xScale, yScale).contains((int)(x * xScale), (int)(y * yScale), false);
445 }
446 
448 {
449  // the annotation pointer is kept elsewehere (in Page, most probably),
450  // so just release its pointer
451  m_object = nullptr;
452 }
453 
455 {
456  m_annotation->d_func()->annotationTransform(matrix);
457 }
458 
459 /** class SourceRefObjectRect **/
460 
462  : ObjectRect(point.x, point.y, .0, .0, false, SourceRef, srcRef)
463  , m_point(point)
464 {
465  const double x = m_point.x < 0.0 ? 0.5 : m_point.x;
466  const double y = m_point.y < 0.0 ? 0.5 : m_point.y;
467  const QRectF rect(x - 2, y - 2, 5, 5);
468  m_path.addRect(rect);
469 
470  m_transformedPath = m_path;
471 }
472 
473 QRect SourceRefObjectRect::boundingRect(double xScale, double yScale) const
474 {
475  const double x = m_point.x < 0.0 ? 0.5 : m_point.x;
476  const double y = m_point.y < 0.0 ? 0.5 : m_point.y;
477 
478  return QRect(x * xScale, y * yScale, 1, 1);
479 }
480 
481 bool SourceRefObjectRect::contains(double x, double y, double xScale, double yScale) const
482 {
483  return distanceSqr(x, y, xScale, yScale) < (pow(7.0 / xScale, 2) + pow(7.0 / yScale, 2));
484 }
485 
486 /** class NonOwningObjectRect **/
487 
488 NonOwningObjectRect::NonOwningObjectRect(double left, double top, double right, double bottom, bool ellipse, ObjectType type, void *object)
489  : ObjectRect(left, top, right, bottom, ellipse, type, object)
490 {
491 }
492 
493 NonOwningObjectRect::~NonOwningObjectRect()
494 {
495  // Set m_object so that ~ObjectRect() doesn't delete it
496  m_object = nullptr;
497 }
void addEllipse(const QRectF &boundingRectangle)
SourceRefObjectRect(const NormalizedPoint &point, void *srcRef)
Creates a new source reference object rectangle.
Definition: area.cpp:461
NormalizedPoint is a helper class which stores the coordinates of a normalized point.
Definition: area.h:116
QRectF boundingRect() const const
QList< T > & operator=(const QList< T > &other)
NormalizedRect()
Creates a null normalized rectangle.
Definition: area.cpp:118
virtual QRect boundingRect(double xScale, double yScale) const
Returns the bounding rect of the object rectangle for the scaling factor xScale and yScale...
Definition: area.cpp:360
This class describes the object rectangle for a source reference.
Definition: area.h:593
QPoint map(const QPoint &point) const const
void transform(const QTransform &matrix)
Transforms the normalized rectangle with the operations defined by matrix.
Definition: area.cpp:248
NormalizedPoint center() const
Returns the center of the rectangle.
Definition: area.cpp:222
bool contains(const QPointF &point) const const
double left
The normalized left coordinate.
Definition: area.h:415
A NormalizedRect is a rectangle which can be defined by two NormalizedPoints.
Definition: area.h:189
QDebug & nospace()
NormalizedRect operator&(const NormalizedRect &other) const
Returns the intersection of this normalized rectangle with the specified other.
Definition: area.cpp:200
NormalizedRect & operator|=(const NormalizedRect &other)
Sets the normalized rectangle to the normalized bounding rectangle of itself combined with the other ...
Definition: area.cpp:191
const void * object() const
Returns the storable object of the object rectangle.
Definition: area.cpp:350
This is a list of NormalizedRect, to describe an area consisting of multiple rectangles using normali...
Definition: area.h:910
qreal top() const const
ObjectType objectType() const
Returns the object type of the object rectangle.
Definition: area.cpp:345
double y
The normalized y coordinate.
Definition: area.h:172
AnnotationObjectRect(Annotation *annotation)
Creates a new annotation object rectangle with the given annotation.
Definition: area.cpp:418
bool contains(double x, double y, double xScale, double yScale) const override
Returns whether the source reference object rectangle contains the point x, y for the scaling factor ...
Definition: area.cpp:481
NormalizedRect operator|(const NormalizedRect &other) const
Returns the normalized bounding rectangle of the normalized rectangle combined with the other normali...
Definition: area.cpp:181
global.h
Definition: action.h:16
HighlightAreaRect(const RegularAreaRect *area=nullptr)
Creates a new highlight area rect with the coordinates of the given area.
Definition: area.cpp:294
qreal left() const const
bool intersects(const NormalizedRect &other) const
Returns whether the normalized rectangle intersects the other normalized rectangle.
Definition: area.cpp:166
int x() const const
int y() const const
void transform(const QTransform &matrix) override
Transforms the annotation object rectangle with the operations defined by matrix. ...
Definition: area.cpp:454
bool contains(double x, double y, double xScale, double yScale) const override
Returns whether the annotation object rectangle contains the point x, y for the scaling factor xScale...
Definition: area.cpp:442
static QRect annotationGeometry(const Annotation *annotation, double scaleX, double scaleY)
Returns the geometry of the given annotation scaled by scaleX and scaleY.
ObjectRect(double left, double top, double right, double bottom, bool ellipse, ObjectType type, void *object)
Creates a new object rectangle.
Definition: area.cpp:309
qreal bottom() const const
bool contains(double x, double y) const
Returns whether the normalized rectangle contains the normalized point (x, y).
Definition: area.cpp:161
double right
The normalized right coordinate.
Definition: area.h:425
void append(const T &value)
void addPolygon(const QPolygonF &polygon)
NormalizedRect & operator=(const NormalizedRect &other)
An area with normalized coordinates that contains a reference to an object.
Definition: area.h:452
QRectF normalized() const const
void addRect(const QRectF &rectangle)
NormalizedPoint()
Creates a normalized point at (0, 0).
Definition: area.cpp:21
NonOwningObjectRect(double left, double top, double right, double bottom, bool ellipse, ObjectType type, void *object)
class NonOwningObjectRect
Definition: area.cpp:488
QPoint center() const const
bool operator==(const NormalizedRect &other) const
Returns whether the normalized rectangle is equal to the other normalized rectangle.
Definition: area.cpp:217
virtual void transform(const QTransform &matrix)
Transforms the object rectangle with the operations defined by matrix.
Definition: area.cpp:372
double distanceSqr(double x, double y, double xScale, double yScale) const
Returns squared distance to normalized point (x, y) on a reference area of size xScale x yScale...
Definition: area.cpp:52
QRect roundedGeometry(int xScale, int yScale) const
Same functionality as geometry, but the output is now rounded before typecasting to int...
Definition: area.cpp:241
QRect geometry(int xScale, int yScale) const
Returns the rectangle mapped to a reference area of xScale x yScale.
Definition: area.cpp:234
QList::iterator end()
qreal right() const const
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
QDebug & space()
double top
The normalized top coordinate.
Definition: area.h:420
Encapsulates data that describes an action.
Definition: action.h:40
virtual bool contains(double x, double y, double xScale, double yScale) const
Returns whether the object rectangle contains the point with absolute coordinates (x...
Definition: area.cpp:367
Defines a source reference.
ObjectType
Describes the type of storable object.
Definition: area.h:458
QRect boundingRect(double xScale, double yScale) const override
Returns the bounding rect of the source reference object rectangle for the scaling factor xScale and ...
Definition: area.cpp:473
QDataStream & operator<<(QDataStream &out, const KDateTime::Spec &spec)
bool contains(const QRect &rectangle, bool proper) const const
double x
The normalized x coordinate.
Definition: area.h:167
QRect boundingRect(double xScale, double yScale) const override
Returns the bounding rect of the annotation object rectangle for the scaling factor xScale and yScale...
Definition: area.cpp:429
qreal width() const const
A source reference.
Definition: area.h:462
double distanceSqr(double x, double y, double xScale, double yScale) const
Returns the squared distance between the object and the point with normalized coordinates (x...
Definition: area.cpp:377
Annotation struct holds properties shared by all annotations.
Definition: annotations.h:96
bool isNull() const
Returns whether this normalized rectangle is a null normalized rect.
Definition: area.cpp:156
Annotation * annotation() const
Returns the annotation object of the annotation object rectangle.
Definition: area.cpp:424
const QPainterPath & region() const
Returns the region that is covered by the object rectangle.
Definition: area.cpp:355
qreal height() const const
NormalizedPoint & operator=(const NormalizedPoint &)
static NormalizedRect fromQRectF(const QRectF &rect)
Build a normalized rect from a QRectF, which already has normalized coordinates.
Definition: area.cpp:145
double bottom
The normalized bottom coordinate.
Definition: area.h:430
~AnnotationObjectRect() override
Destroys the annotation object rectangle.
Definition: area.cpp:447
void transform(const QTransform &matrix)
Transforms the normalized point with the operations defined by matrix.
Definition: area.cpp:43
QRect mapRect(const QRect &rectangle) const const
QList::iterator begin()
virtual ~ObjectRect()
Destroys the object rectangle.
Definition: area.cpp:403
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:32:38 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.