compile
[kdegraphics.git] / okular / core / area.cpp
blob3c97f42d0a2713ea287bc74ecabc524dc28abc2a
1 /***************************************************************************
2 * Copyright (C) 2004-05 by Enrico Ros <eros.kde@email.it> *
3 * Copyright (C) 2005 by Piotr Szymanski <niedakh@gmail.com> *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #include "area.h"
12 #include <QtCore/QRect>
13 #include <QtGui/QPolygonF>
14 #include <kdebug.h>
16 #include <math.h>
18 #include "action.h"
19 #include "annotations.h"
20 #include "annotations_p.h"
21 #include "debug_p.h"
22 #include "sourcereference.h"
24 using namespace Okular;
26 /** class NormalizedPoint **/
27 NormalizedPoint::NormalizedPoint()
28 : x( 0.0 ), y( 0.0 ) {}
30 NormalizedPoint::NormalizedPoint( double dX, double dY )
31 : x( dX ), y( dY ) {}
33 NormalizedPoint::NormalizedPoint( int iX, int iY, int xScale, int yScale )
34 : x( (double)iX / (double)xScale ), y( (double)iY / (double)yScale ) {}
36 NormalizedPoint& NormalizedPoint::operator=( const NormalizedPoint & p )
38 x = p.x;
39 y = p.y;
40 return *this;
43 void NormalizedPoint::transform( const QMatrix &matrix )
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;
52 QDebug operator<<( QDebug str, const Okular::NormalizedPoint& p )
54 str.nospace() << "NormPt(" << p.x << "," << p.y << ")";
55 return str.space();
58 /** class NormalizedRect **/
60 NormalizedRect::NormalizedRect()
61 : left( 0.0 ), top( 0.0 ), right( 0.0 ), bottom( 0.0 ) {}
63 NormalizedRect::NormalizedRect( double l, double t, double r, double b )
64 // note: check for swapping coords?
65 : left( l ), top( t ), right( r ), bottom( b ) {}
67 NormalizedRect::NormalizedRect( const QRect & r, double xScale, double yScale )
68 : left( (double)r.left() / xScale ), top( (double)r.top() / yScale ),
69 right( (double)r.right() / xScale ), bottom( (double)r.bottom() / yScale ) {}
71 NormalizedRect::NormalizedRect( const NormalizedRect & rect )
72 : left( rect.left ), top( rect.top ), right( rect.right ), bottom( rect.bottom ) {}
74 NormalizedRect NormalizedRect::fromQRectF( const QRectF &rect )
76 NormalizedRect ret;
77 ret.left = rect.left();
78 ret.top = rect.top();
79 ret.right = rect.right();
80 ret.bottom = rect.bottom();
81 return ret;
84 bool NormalizedRect::isNull() const
86 return left == 0 && top== 0 && right == 0 && bottom == 0;
89 bool NormalizedRect::contains( double x, double y ) const
91 return x >= left && x <= right && y >= top && y <= bottom;
94 bool NormalizedRect::intersects( const NormalizedRect & r ) const
96 return (r.left <= right) && (r.right >= left) && (r.top <= bottom) && (r.bottom >= top);
99 bool NormalizedRect::intersects( const NormalizedRect * r ) const
101 return (r->left <= right) && (r->right >= left) && (r->top <= bottom) && (r->bottom >= top);
104 bool NormalizedRect::intersects( double l, double t, double r, double b ) const
106 return (l <= right) && (r >= left) && (t <= bottom) && (b >= top);
109 NormalizedRect NormalizedRect::operator| (const NormalizedRect & r) const
111 NormalizedRect ret;
112 // todo !
113 ret.left=qMin(left,r.left);
114 ret.top=qMin(top,r.top);
115 ret.bottom=qMax(bottom,r.bottom);
116 ret.right=qMax(right,r.right);
117 return ret;
120 NormalizedRect& NormalizedRect::operator|= (const NormalizedRect & r)
122 left = qMin( left, r.left );
123 top = qMin( top, r.top );
124 bottom = qMax( bottom, r.bottom );
125 right = qMax( right, r.right );
126 return *this;
129 NormalizedRect NormalizedRect::operator&( const NormalizedRect & r ) const
131 if ( isNull() || r.isNull() )
132 return NormalizedRect();
134 NormalizedRect ret;
135 ret.left = qMax( left, r.left );
136 ret.top = qMax( top, r.top );
137 ret.bottom = qMin( bottom, r.bottom );
138 ret.right = qMin( right, r.right );
139 return ret;
142 NormalizedRect & NormalizedRect::operator=( const NormalizedRect & r )
144 left = r.left;
145 right = r.right;
146 top = r.top;
147 bottom = r.bottom;
148 return *this;
151 bool NormalizedRect::operator==( const NormalizedRect & r ) const
153 return ( isNull() && r.isNull() ) ||
154 ( fabs( left - r.left ) < 1e-4 &&
155 fabs( right - r.right ) < 1e-4 &&
156 fabs( top - r.top ) < 1e-4 &&
157 fabs( bottom - r.bottom ) < 1e-4 );
161 QDebug operator << (QDebug str , const NormalizedRect &r)
163 str << "[" <<r.left() << "," << r.top() << "] x "<< "[" <<r.right() << "," << r.bottom() << "]";
164 return str;
167 QRect NormalizedRect::geometry( int xScale, int yScale ) const
169 int l = (int)( left * xScale ),
170 t = (int)( top * yScale ),
171 r = (int)( right * xScale ),
172 b = (int)( bottom * yScale );
174 return QRect( l, t, r - l + 1, b - t + 1 );
177 void NormalizedRect::transform( const QMatrix &matrix )
179 QRectF rect( left, top, right - left, bottom - top );
180 rect = matrix.mapRect( rect );
182 left = rect.left();
183 top = rect.top();
184 right = rect.right();
185 bottom = rect.bottom();
188 QDebug operator<<( QDebug str, const Okular::NormalizedRect& r )
190 str.nospace() << "NormRect(" << r.left << "," << r.top << " x " << ( r.right - r.left ) << "+" << ( r.bottom - r.top ) << ")";
191 return str.space();
194 RegularAreaRect::RegularAreaRect()
195 : RegularArea< NormalizedRect, QRect >(), d( 0 )
199 RegularAreaRect::RegularAreaRect( const RegularAreaRect& rar )
200 : RegularArea< NormalizedRect, QRect >( rar ), d( 0 )
204 RegularAreaRect::~RegularAreaRect()
208 RegularAreaRect& RegularAreaRect::operator=( const RegularAreaRect& rar )
210 RegularArea< NormalizedRect, QRect >::operator=( rar );
211 return *this;
215 HighlightAreaRect::HighlightAreaRect( const RegularAreaRect *area )
216 : RegularAreaRect(), s_id( -1 )
218 if ( area )
220 RegularAreaRect::ConstIterator it = area->begin();
221 RegularAreaRect::ConstIterator itEnd = area->end();
222 for ( ; it != itEnd; ++it )
224 append( NormalizedRect( *it ) );
229 /** class ObjectRect **/
231 ObjectRect::ObjectRect( double l, double t, double r, double b, bool ellipse, ObjectType type, void * pnt )
232 : m_objectType( type ), m_object( pnt )
234 // assign coordinates swapping them if negative width or height
235 QRectF rect( r > l ? l : r, b > t ? t : b, fabs( r - l ), fabs( b - t ) );
236 if ( ellipse )
237 m_path.addEllipse( rect );
238 else
239 m_path.addRect( rect );
241 m_transformedPath = m_path;
244 ObjectRect::ObjectRect( const NormalizedRect& x, bool ellipse, ObjectType type, void * pnt )
245 : m_objectType( type ), m_object( pnt )
247 QRectF rect( x.left, x.top, fabs( x.right - x.left ), fabs( x.bottom - x.top ) );
248 if ( ellipse )
249 m_path.addEllipse( rect );
250 else
251 m_path.addRect( rect );
253 m_transformedPath = m_path;
256 ObjectRect::ObjectRect( const QPolygonF &poly, ObjectType type, void * pnt )
257 : m_objectType( type ), m_object( pnt )
259 m_path.addPolygon( poly );
261 m_transformedPath = m_path;
264 ObjectRect::ObjectType ObjectRect::objectType() const
266 return m_objectType;
269 const void * ObjectRect::object() const
271 return m_object;
274 const QPainterPath &ObjectRect::region() const
276 return m_transformedPath;
279 QRect ObjectRect::boundingRect( double xScale, double yScale ) const
281 const QRectF &br = m_transformedPath.boundingRect();
283 return QRect( (int)( br.left() * xScale ), (int)( br.top() * yScale ),
284 (int)( br.width() * xScale ), (int)( br.height() * yScale ) );
287 bool ObjectRect::contains( double x, double y, double, double ) const
289 return m_transformedPath.contains( QPointF( x, y ) );
292 void ObjectRect::transform( const QMatrix &matrix )
294 m_transformedPath = matrix.map( m_path );
297 ObjectRect::~ObjectRect()
299 if ( !m_object )
300 return;
302 if ( m_objectType == Action )
303 delete static_cast<Okular::Action*>( m_object );
304 else if ( m_objectType == SourceRef )
305 delete static_cast<Okular::SourceReference*>( m_object );
306 else
307 kDebug(OkularDebug).nospace() << "Object deletion not implemented for type '" << m_objectType << "'.";
310 /** class AnnotationObjectRect **/
312 AnnotationObjectRect::AnnotationObjectRect( Annotation * annotation )
313 : ObjectRect( QPolygonF(), OAnnotation, annotation ), m_annotation( annotation )
317 Annotation *AnnotationObjectRect::annotation() const
319 return m_annotation;
322 QRect AnnotationObjectRect::boundingRect( double xScale, double yScale ) const
324 return AnnotationUtils::annotationGeometry( m_annotation, xScale, yScale );
327 bool AnnotationObjectRect::contains( double x, double y, double xScale, double yScale ) const
329 return boundingRect( xScale, yScale ).contains( (int)( x * xScale ), (int)( y * yScale ), false );
332 AnnotationObjectRect::~AnnotationObjectRect()
334 // the annotation pointer is kept elsewehere (in Page, most probably),
335 // so just release its pointer
336 m_object = 0;
339 void AnnotationObjectRect::transform( const QMatrix &matrix )
341 m_annotation->d_func()->annotationTransform( matrix );
344 /** class SourceRefObjectRect **/
346 SourceRefObjectRect::SourceRefObjectRect( const NormalizedPoint& point, void * srcRef )
347 : ObjectRect( point.x, point.y, .0, .0, false, SourceRef, srcRef ), m_point( point )
351 QRect SourceRefObjectRect::boundingRect( double /*xScale*/, double /*yScale*/ ) const
353 return QRect();
356 bool SourceRefObjectRect::contains( double x, double y, double xScale, double yScale ) const
358 return ( pow( x - m_point.x, 2 ) + pow( y - m_point.y, 2 ) ) < ( pow( (double)7/xScale, 2 ) + pow( (double)7/yScale, 2 ) );