compile
[kdegraphics.git] / okular / core / textdocumentgenerator_p.h
blob956105beba3913b3afd1b5eaff13424619da5489
1 /***************************************************************************
2 * Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org> *
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 #ifndef _OKULAR_TEXTDOCUMENTGENERATOR_P_H_
11 #define _OKULAR_TEXTDOCUMENTGENERATOR_P_H_
13 #include <QtGui/QAbstractTextDocumentLayout>
14 #include <QtGui/QTextBlock>
15 #include <QtGui/QTextDocument>
17 #include "action.h"
18 #include "document.h"
19 #include "generator_p.h"
20 #include "textdocumentgenerator.h"
22 namespace Okular {
24 class Utils
26 public:
27 static void calculateBoundingRect( QTextDocument *document, int startPosition, int endPosition,
28 QRectF &rect, int &page )
30 const QSizeF pageSize = document->pageSize();
32 const QTextBlock startBlock = document->findBlock( startPosition );
33 const QRectF startBoundingRect = document->documentLayout()->blockBoundingRect( startBlock );
35 const QTextBlock endBlock = document->findBlock( endPosition );
36 const QRectF endBoundingRect = document->documentLayout()->blockBoundingRect( endBlock );
38 QTextLayout *startLayout = startBlock.layout();
39 QTextLayout *endLayout = endBlock.layout();
41 int startPos = startPosition - startBlock.position();
42 int endPos = endPosition - endBlock.position();
43 const QTextLine startLine = startLayout->lineForTextPosition( startPos );
44 const QTextLine endLine = endLayout->lineForTextPosition( endPos );
46 double x = startBoundingRect.x() + startLine.cursorToX( startPos );
47 double y = startBoundingRect.y() + startLine.y();
48 double r = endBoundingRect.x() + endLine.cursorToX( endPos );
49 double b = endBoundingRect.y() + endLine.y() + endLine.height();
51 int offset = qRound( y ) % qRound( pageSize.height() );
53 if ( x > r ) { // line break, so return a pseudo character on the start line
54 rect = QRectF( x / pageSize.width(), offset / pageSize.height(),
55 3 / pageSize.width(), startLine.height() / pageSize.height() );
56 page = -1;
57 return;
60 page = qRound( y ) / qRound( pageSize.height() );
61 rect = QRectF( x / pageSize.width(), offset / pageSize.height(),
62 (r - x) / pageSize.width(), (b - y) / pageSize.height() );
65 static void calculatePositions( QTextDocument *document, int page, int &start, int &end )
67 const QAbstractTextDocumentLayout *layout = document->documentLayout();
68 const QSizeF pageSize = document->pageSize();
69 double margin = document->rootFrame()->frameFormat().margin();
71 /**
72 * Take the upper left and lower left corner including the margin
74 start = layout->hitTest( QPointF( margin, (page * pageSize.height()) + margin ), Qt::FuzzyHit );
75 end = layout->hitTest( QPointF( margin, ((page + 1) * pageSize.height()) - margin ), Qt::FuzzyHit );
78 static Okular::DocumentViewport calculateViewport( QTextDocument *document, const QTextBlock &block )
80 const QSizeF pageSize = document->pageSize();
81 const QRectF rect = document->documentLayout()->blockBoundingRect( block );
83 int page = qRound( rect.y() ) / qRound( pageSize.height() );
84 int offset = qRound( rect.y() ) % qRound( pageSize.height() );
86 Okular::DocumentViewport viewport( page );
87 viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width();
88 viewport.rePos.normalizedY = (double)offset / (double)pageSize.height();
89 viewport.rePos.enabled = true;
90 viewport.rePos.pos = Okular::DocumentViewport::Center;
92 return viewport;
96 class TextDocumentConverterPrivate
98 public:
99 TextDocumentConverterPrivate()
100 : mParent( 0 )
104 TextDocumentGeneratorPrivate *mParent;
107 class TextDocumentGeneratorPrivate : public GeneratorPrivate
109 friend class TextDocumentConverter;
111 public:
112 TextDocumentGeneratorPrivate( TextDocumentConverter *converter )
113 : mConverter( converter ), mDocument( 0 )
117 virtual ~TextDocumentGeneratorPrivate()
119 delete mConverter;
120 delete mDocument;
123 Q_DECLARE_PUBLIC( TextDocumentGenerator )
125 /* reimp */ QVariant metaData( const QString &key, const QVariant &option ) const;
126 /* reimp */ QImage image( PixmapRequest * );
128 void calculateBoundingRect( int startPosition, int endPosition, QRectF &rect, int &page ) const;
129 void calculatePositions( int page, int &start, int &end ) const;
130 Okular::TextPage* createTextPage( int ) const;
132 void addAction( Action *action, int cursorBegin, int cursorEnd );
133 void addAnnotation( Annotation *annotation, int cursorBegin, int cursorEnd );
134 void addTitle( int level, const QString &title, const QTextBlock &position );
135 void addMetaData( const QString &key, const QString &value, const QString &title );
136 void addMetaData( DocumentInfo::Key, const QString &value );
138 void generateLinkInfos();
139 void generateAnnotationInfos();
140 void generateTitleInfos();
142 TextDocumentConverter *mConverter;
144 QTextDocument *mDocument;
145 Okular::DocumentInfo mDocumentInfo;
146 Okular::DocumentSynopsis mDocumentSynopsis;
148 struct TitlePosition
150 int level;
151 QString title;
152 QTextBlock block;
154 QList<TitlePosition> mTitlePositions;
156 struct LinkPosition
158 int startPosition;
159 int endPosition;
160 Action *link;
162 QList<LinkPosition> mLinkPositions;
164 struct LinkInfo
166 int page;
167 QRectF boundingRect;
168 Action *link;
170 QList<LinkInfo> mLinkInfos;
172 struct AnnotationPosition
174 int startPosition;
175 int endPosition;
176 Annotation *annotation;
178 QList<AnnotationPosition> mAnnotationPositions;
180 struct AnnotationInfo
182 int page;
183 QRectF boundingRect;
184 Annotation *annotation;
186 QList<AnnotationInfo> mAnnotationInfos;
191 #endif