1 /***************************************************************************
2 * Copyright (C) 2005 by Enrico Ros <eros.kde@email.it> *
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 "annotationtools.h"
17 #include "core/annotations.h"
19 AnnotatorEngine::AnnotatorEngine( const QDomElement
& engineElement
)
20 : m_engineElement( engineElement
), m_creationCompleted( false )
22 // parse common engine attributes
23 if ( engineElement
.hasAttribute( "color" ) )
24 m_engineColor
= QColor( engineElement
.attribute( "color" ) );
26 // get the annotation element
27 QDomElement annElement
= m_engineElement
.firstChild().toElement();
28 if ( !annElement
.isNull() && annElement
.tagName() == "annotation" )
29 m_annotElement
= annElement
;
32 AnnotatorEngine::~AnnotatorEngine()
37 /** SmoothPathEngine */
38 SmoothPathEngine::SmoothPathEngine( const QDomElement
& engineElement
)
39 : AnnotatorEngine( engineElement
)
41 // parse engine specific attributes
44 QRect
SmoothPathEngine::event( EventType type
, Button button
, double nX
, double nY
, double xScale
, double yScale
, const Okular::Page
* /*page*/ )
46 // only proceed if pressing left button
51 if ( type
== Press
&& points
.isEmpty() )
55 totalRect
.left
= totalRect
.right
= lastPoint
.x
;
56 totalRect
.top
= totalRect
.bottom
= lastPoint
.y
;
57 points
.append( lastPoint
);
59 // add a point to the path
60 else if ( type
== Move
&& points
.count() > 0 )
62 //double dist = hypot( nX - points.last().x, nY - points.last().y );
63 //if ( dist > 0.0001 )
65 // append mouse position (as normalized point) to the list
66 Okular::NormalizedPoint nextPoint
= Okular::NormalizedPoint( nX
, nY
);
67 points
.append( nextPoint
);
69 double dX
= 2.0 / (double)xScale
;
70 double dY
= 2.0 / (double)yScale
;
71 totalRect
.left
= qMin( totalRect
.left
, nX
- dX
);
72 totalRect
.top
= qMin( totalRect
.top
, nY
- dY
);
73 totalRect
.right
= qMax( nX
+ dX
, totalRect
.right
);
74 totalRect
.bottom
= qMax( nY
+ dY
, totalRect
.bottom
);
75 // paint the difference to previous full rect
76 Okular::NormalizedRect incrementalRect
;
77 incrementalRect
.left
= qMin( nextPoint
.x
, lastPoint
.x
) - dX
;
78 incrementalRect
.right
= qMax( nextPoint
.x
, lastPoint
.x
) + dX
;
79 incrementalRect
.top
= qMin( nextPoint
.y
, lastPoint
.y
) - dY
;
80 incrementalRect
.bottom
= qMax( nextPoint
.y
, lastPoint
.y
) + dY
;
81 lastPoint
= nextPoint
;
82 return incrementalRect
.geometry( (int)xScale
, (int)yScale
);
86 else if ( type
== Release
&& points
.count() > 0 )
88 if ( points
.count() < 2 )
91 m_creationCompleted
= true;
92 return totalRect
.geometry( (int)xScale
, (int)yScale
);
97 void SmoothPathEngine::paint( QPainter
* painter
, double xScale
, double yScale
, const QRect
& /*clipRect*/ )
99 // draw SmoothPaths with at least 2 points
100 if ( points
.count() > 1 )
102 // use engine's color for painting
103 painter
->setPen( QPen( m_engineColor
, 1 ) );
105 QLinkedList
<Okular::NormalizedPoint
>::const_iterator pIt
= points
.begin(), pEnd
= points
.end();
106 Okular::NormalizedPoint pA
= *pIt
;
108 for ( ; pIt
!= pEnd
; ++pIt
)
110 Okular::NormalizedPoint pB
= *pIt
;
111 painter
->drawLine( (int)(pA
.x
* (double)xScale
), (int)(pA
.y
* (double)yScale
),
112 (int)(pB
.x
* (double)xScale
), (int)(pB
.y
* (double)yScale
) );
118 QList
< Okular::Annotation
* > SmoothPathEngine::end()
120 m_creationCompleted
= false;
122 // find out annotation's description node
123 if ( m_annotElement
.isNull() )
124 return QList
< Okular::Annotation
* >();
126 // find out annotation's type
127 Okular::Annotation
* ann
= 0;
128 QString typeString
= m_annotElement
.attribute( "type" );
130 // create InkAnnotation from path
131 if ( typeString
== "Ink" )
133 Okular::InkAnnotation
* ia
= new Okular::InkAnnotation();
135 if ( m_annotElement
.hasAttribute( "width" ) )
136 ann
->style().setWidth( m_annotElement
.attribute( "width" ).toDouble() );
138 QList
< QLinkedList
<Okular::NormalizedPoint
> > list
= ia
->inkPaths();
139 list
.append( points
);
140 ia
->setInkPaths( list
);
142 ia
->setBoundingRectangle( totalRect
);
147 return QList
< Okular::Annotation
* >();
149 // set common attributes
150 ann
->style().setColor( m_annotElement
.hasAttribute( "color" ) ?
151 m_annotElement
.attribute( "color" ) : m_engineColor
);
152 if ( m_annotElement
.hasAttribute( "opacity" ) )
153 ann
->style().setOpacity( m_annotElement
.attribute( "opacity", "1.0" ).toDouble() );
156 return QList
< Okular::Annotation
* >() << ann
;