compile
[kdegraphics.git] / okular / ui / annotationtools.cpp
blob0bd936d1ae333c0f7d98e1848a4b899e3abc9aa2
1 /***************************************************************************
2 * Copyright (C) 2005 by Enrico Ros <eros.kde@email.it> *
3 * *
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"
12 // qt / kde includes
13 #include <qcolor.h>
14 #include <qpainter.h>
16 // local includes
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
47 if ( button != Left )
48 return QRect();
50 // start operation
51 if ( type == Press && points.isEmpty() )
53 lastPoint.x = nX;
54 lastPoint.y = nY;
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 )
64 //{
65 // append mouse position (as normalized point) to the list
66 Okular::NormalizedPoint nextPoint = Okular::NormalizedPoint( nX, nY );
67 points.append( nextPoint );
68 // update total rect
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 );
83 //}
85 // terminate process
86 else if ( type == Release && points.count() > 0 )
88 if ( points.count() < 2 )
89 points.clear();
90 else
91 m_creationCompleted = true;
92 return totalRect.geometry( (int)xScale, (int)yScale );
94 return QRect();
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;
107 ++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) );
113 pA = pB;
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();
134 ann = ia;
135 if ( m_annotElement.hasAttribute( "width" ) )
136 ann->style().setWidth( m_annotElement.attribute( "width" ).toDouble() );
137 // fill points
138 QList< QLinkedList<Okular::NormalizedPoint> > list = ia->inkPaths();
139 list.append( points );
140 ia->setInkPaths( list );
141 // set boundaries
142 ia->setBoundingRectangle( totalRect );
145 // safety check
146 if ( !ann )
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() );
155 // return annotation
156 return QList< Okular::Annotation* >() << ann;