2007-01-13 23:15:28 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by Tobias Koenig <tokoe@kde.org> *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "textdocumentgenerator.h"
|
|
|
|
#include "textdocumentgenerator_p.h"
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
#include <QtCore/QFile>
|
|
|
|
#include <QtCore/QStack>
|
|
|
|
#include <QtCore/QTextStream>
|
2007-05-03 21:45:14 +00:00
|
|
|
#include <QtCore/QVector>
|
2007-01-13 23:15:28 +00:00
|
|
|
#include <QtGui/QPainter>
|
|
|
|
#include <QtGui/QPixmap>
|
2007-10-15 23:01:27 +00:00
|
|
|
#include <QtGui/QPrinter>
|
2007-01-13 23:15:28 +00:00
|
|
|
|
2007-04-20 11:26:05 +00:00
|
|
|
#include <okular/core/action.h>
|
2007-01-13 23:15:28 +00:00
|
|
|
#include <okular/core/annotations.h>
|
|
|
|
#include <okular/core/page.h>
|
|
|
|
#include <okular/core/textpage.h>
|
|
|
|
|
|
|
|
#include "document.h"
|
|
|
|
|
|
|
|
using namespace Okular;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic Converter Implementation
|
|
|
|
*/
|
|
|
|
TextDocumentConverter::TextDocumentConverter()
|
2007-12-08 14:45:32 +00:00
|
|
|
: QObject( 0 ), d_ptr( 0 )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextDocumentConverter::~TextDocumentConverter()
|
|
|
|
{
|
2007-12-08 14:45:32 +00:00
|
|
|
delete d_ptr;
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-19 16:33:58 +00:00
|
|
|
DocumentViewport TextDocumentConverter::calculateViewport( QTextDocument *document, const QTextBlock &block )
|
|
|
|
{
|
|
|
|
return Utils::calculateViewport( document, block );
|
|
|
|
}
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
/**
|
|
|
|
* Generic Generator Implementation
|
|
|
|
*/
|
2007-09-14 13:31:55 +00:00
|
|
|
Okular::TextPage* TextDocumentGeneratorPrivate::createTextPage( int pageNumber ) const
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
Okular::TextPage *textPage = new Okular::TextPage;
|
|
|
|
|
|
|
|
int start, end;
|
|
|
|
|
|
|
|
Utils::calculatePositions( mDocument, pageNumber, start, end );
|
|
|
|
|
|
|
|
QTextCursor cursor( mDocument );
|
|
|
|
for ( int i = start; i < end - 1; ++i ) {
|
|
|
|
cursor.setPosition( i );
|
|
|
|
cursor.setPosition( i + 1, QTextCursor::KeepAnchor );
|
|
|
|
|
|
|
|
QString text = cursor.selectedText();
|
2007-05-12 08:17:02 +00:00
|
|
|
if ( text.length() == 1 ) {
|
2007-01-13 23:15:28 +00:00
|
|
|
QRectF rect;
|
|
|
|
Utils::calculateBoundingRect( mDocument, i, i + 1, rect, pageNumber );
|
2007-05-12 08:17:02 +00:00
|
|
|
if ( pageNumber == -1 )
|
|
|
|
text = "\n";
|
2007-01-13 23:15:28 +00:00
|
|
|
|
|
|
|
textPage->append( text, new Okular::NormalizedRect( rect.left(), rect.top(), rect.right(), rect.bottom() ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return textPage;
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::addAction( Action *action, int cursorBegin, int cursorEnd )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
2007-04-20 12:49:17 +00:00
|
|
|
if ( !action )
|
2007-01-13 23:15:28 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
LinkPosition position;
|
2007-04-20 12:49:17 +00:00
|
|
|
position.link = action;
|
2007-01-13 23:15:28 +00:00
|
|
|
position.startPosition = cursorBegin;
|
|
|
|
position.endPosition = cursorEnd;
|
|
|
|
|
|
|
|
mLinkPositions.append( position );
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::addAnnotation( Annotation *annotation, int cursorBegin, int cursorEnd )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
if ( !annotation )
|
|
|
|
return;
|
|
|
|
|
2007-12-08 13:45:06 +00:00
|
|
|
annotation->setFlags( annotation->flags() | Okular::Annotation::External );
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
AnnotationPosition position;
|
|
|
|
position.annotation = annotation;
|
|
|
|
position.startPosition = cursorBegin;
|
|
|
|
position.endPosition = cursorEnd;
|
|
|
|
|
|
|
|
mAnnotationPositions.append( position );
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::addTitle( int level, const QString &title, const QTextBlock &block )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
TitlePosition position;
|
|
|
|
position.level = level;
|
|
|
|
position.title = title;
|
|
|
|
position.block = block;
|
|
|
|
|
|
|
|
mTitlePositions.append( position );
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::addMetaData( const QString &key, const QString &value, const QString &title )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
mDocumentInfo.set( key, value, title );
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::generateLinkInfos()
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
for ( int i = 0; i < mLinkPositions.count(); ++i ) {
|
|
|
|
const LinkPosition &linkPosition = mLinkPositions[ i ];
|
|
|
|
|
|
|
|
LinkInfo info;
|
|
|
|
info.link = linkPosition.link;
|
|
|
|
|
|
|
|
Utils::calculateBoundingRect( mDocument, linkPosition.startPosition, linkPosition.endPosition,
|
|
|
|
info.boundingRect, info.page );
|
|
|
|
|
2007-12-10 21:03:32 +00:00
|
|
|
if ( info.page >= 0 )
|
|
|
|
mLinkInfos.append( info );
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::generateAnnotationInfos()
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
for ( int i = 0; i < mAnnotationPositions.count(); ++i ) {
|
|
|
|
const AnnotationPosition &annotationPosition = mAnnotationPositions[ i ];
|
|
|
|
|
|
|
|
AnnotationInfo info;
|
|
|
|
info.annotation = annotationPosition.annotation;
|
|
|
|
|
|
|
|
Utils::calculateBoundingRect( mDocument, annotationPosition.startPosition, annotationPosition.endPosition,
|
|
|
|
info.boundingRect, info.page );
|
|
|
|
|
2007-12-10 22:39:48 +00:00
|
|
|
if ( info.page >= 0 )
|
|
|
|
mAnnotationInfos.append( info );
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-14 13:31:55 +00:00
|
|
|
void TextDocumentGeneratorPrivate::generateTitleInfos()
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
QStack<QDomNode> parentNodeStack;
|
|
|
|
|
|
|
|
QDomNode parentNode = mDocumentSynopsis;
|
|
|
|
|
|
|
|
int level = 1000;
|
|
|
|
for ( int i = 0; i < mTitlePositions.count(); ++i )
|
|
|
|
level = qMin( level, mTitlePositions[ i ].level );
|
|
|
|
|
|
|
|
for ( int i = 0; i < mTitlePositions.count(); ++i ) {
|
|
|
|
const TitlePosition &position = mTitlePositions[ i ];
|
|
|
|
|
|
|
|
Okular::DocumentViewport viewport = Utils::calculateViewport( mDocument, position.block );
|
|
|
|
|
|
|
|
QDomElement item = mDocumentSynopsis.createElement( position.title );
|
|
|
|
item.setAttribute( "Viewport", viewport.toString() );
|
|
|
|
|
|
|
|
int newLevel = position.level;
|
|
|
|
if ( newLevel == level ) {
|
|
|
|
parentNode.appendChild( item );
|
|
|
|
} else if ( newLevel > level ) {
|
|
|
|
parentNodeStack.push( parentNode );
|
|
|
|
parentNode = parentNode.lastChildElement();
|
|
|
|
parentNode.appendChild( item );
|
|
|
|
level = newLevel;
|
|
|
|
} else {
|
|
|
|
for ( int i = level; i > newLevel; i-- ) {
|
|
|
|
level--;
|
|
|
|
parentNode = parentNodeStack.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
parentNode.appendChild( item );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-02 20:57:24 +00:00
|
|
|
TextDocumentGenerator::TextDocumentGenerator( TextDocumentConverter *converter, QObject *parent, const QVariantList &args )
|
|
|
|
: Okular::Generator( *new TextDocumentGeneratorPrivate( converter ), parent, args )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
2007-01-26 16:35:30 +00:00
|
|
|
setFeature( TextExtraction );
|
2007-11-27 16:29:50 +00:00
|
|
|
setFeature( PrintNative );
|
|
|
|
setFeature( PrintToFile );
|
2007-01-26 16:35:30 +00:00
|
|
|
|
2007-04-20 12:49:17 +00:00
|
|
|
connect( converter, SIGNAL( addAction( Action*, int, int ) ),
|
|
|
|
this, SLOT( addAction( Action*, int, int ) ) );
|
2007-01-13 23:15:28 +00:00
|
|
|
connect( converter, SIGNAL( addAnnotation( Annotation*, int, int ) ),
|
|
|
|
this, SLOT( addAnnotation( Annotation*, int, int ) ) );
|
|
|
|
connect( converter, SIGNAL( addTitle( int, const QString&, const QTextBlock& ) ),
|
|
|
|
this, SLOT( addTitle( int, const QString&, const QTextBlock& ) ) );
|
|
|
|
connect( converter, SIGNAL( addMetaData( const QString&, const QString&, const QString& ) ),
|
|
|
|
this, SLOT( addMetaData( const QString&, const QString&, const QString& ) ) );
|
2007-01-29 08:17:45 +00:00
|
|
|
|
|
|
|
connect( converter, SIGNAL( error( const QString&, int ) ),
|
|
|
|
this, SIGNAL( error( const QString&, int ) ) );
|
|
|
|
connect( converter, SIGNAL( warning( const QString&, int ) ),
|
|
|
|
this, SIGNAL( warning( const QString&, int ) ) );
|
|
|
|
connect( converter, SIGNAL( notice( const QString&, int ) ),
|
|
|
|
this, SIGNAL( notice( const QString&, int ) ) );
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextDocumentGenerator::~TextDocumentGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextDocumentGenerator::loadDocument( const QString & fileName, QVector<Okular::Page*> & pagesVector )
|
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
d->mDocument = d->mConverter->convert( fileName );
|
|
|
|
|
|
|
|
if ( !d->mDocument )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
d->generateTitleInfos();
|
|
|
|
d->generateLinkInfos();
|
|
|
|
d->generateAnnotationInfos();
|
|
|
|
|
|
|
|
pagesVector.resize( d->mDocument->pageCount() );
|
|
|
|
|
|
|
|
const QSize size = d->mDocument->pageSize().toSize();
|
2007-05-03 21:45:14 +00:00
|
|
|
|
|
|
|
QVector< QLinkedList<Okular::ObjectRect*> > objects( d->mDocument->pageCount() );
|
|
|
|
for ( int i = 0; i < d->mLinkInfos.count(); ++i ) {
|
2007-09-14 13:31:55 +00:00
|
|
|
const TextDocumentGeneratorPrivate::LinkInfo &info = d->mLinkInfos.at( i );
|
2007-05-03 21:45:14 +00:00
|
|
|
|
|
|
|
const QRectF rect = info.boundingRect;
|
|
|
|
objects[ info.page ].append( new Okular::ObjectRect( rect.left(), rect.top(), rect.right(), rect.bottom(), false,
|
|
|
|
Okular::ObjectRect::Action, info.link ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector< QLinkedList<Okular::Annotation*> > annots( d->mDocument->pageCount() );
|
|
|
|
for ( int i = 0; i < d->mAnnotationInfos.count(); ++i ) {
|
2007-09-14 13:31:55 +00:00
|
|
|
const TextDocumentGeneratorPrivate::AnnotationInfo &info = d->mAnnotationInfos[ i ];
|
2007-05-03 21:45:14 +00:00
|
|
|
|
|
|
|
QRect rect( 0, info.page * size.height(), size.width(), size.height() );
|
|
|
|
info.annotation->setBoundingRectangle( Okular::NormalizedRect( rect.left(), rect.top(), rect.right(), rect.bottom() ) );
|
|
|
|
annots[ info.page ].append( info.annotation );
|
|
|
|
}
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
for ( int i = 0; i < d->mDocument->pageCount(); ++i ) {
|
|
|
|
Okular::Page * page = new Okular::Page( i, size.width(), size.height(), Okular::Rotation0 );
|
|
|
|
pagesVector[ i ] = page;
|
2007-05-03 21:45:14 +00:00
|
|
|
|
|
|
|
if ( !objects.at( i ).isEmpty() ) {
|
|
|
|
page->setObjectRects( objects.at( i ) );
|
|
|
|
}
|
|
|
|
QLinkedList<Okular::Annotation*>::ConstIterator annIt = annots.at( i ).begin(), annEnd = annots.at( i ).end();
|
|
|
|
for ( ; annIt != annEnd; ++annIt ) {
|
|
|
|
page->addAnnotation( *annIt );
|
|
|
|
}
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-10-08 16:46:51 +00:00
|
|
|
bool TextDocumentGenerator::doCloseDocument()
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
delete d->mDocument;
|
|
|
|
d->mDocument = 0;
|
|
|
|
|
2007-05-21 22:26:37 +00:00
|
|
|
d->mTitlePositions.clear();
|
|
|
|
d->mLinkPositions.clear();
|
|
|
|
d->mLinkInfos.clear();
|
|
|
|
d->mAnnotationInfos.clear();
|
|
|
|
|
2007-01-13 23:15:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-01-24 15:06:45 +00:00
|
|
|
bool TextDocumentGenerator::canGeneratePixmap() const
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextDocumentGenerator::generatePixmap( Okular::PixmapRequest * request )
|
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
if ( !d->mDocument )
|
|
|
|
return;
|
|
|
|
|
|
|
|
const QSize size = d->mDocument->pageSize().toSize();
|
|
|
|
|
|
|
|
QPixmap *pixmap = new QPixmap( request->width(), request->height() );
|
|
|
|
pixmap->fill( Qt::white );
|
|
|
|
|
|
|
|
QPainter p;
|
|
|
|
p.begin( pixmap );
|
|
|
|
|
|
|
|
qreal width = request->width();
|
|
|
|
qreal height = request->height();
|
|
|
|
|
|
|
|
p.scale( width / (qreal)size.width(), height / (qreal)size.height() );
|
|
|
|
|
|
|
|
QRect rect;
|
|
|
|
rect = QRect( 0, request->pageNumber() * size.height(), size.width(), size.height() );
|
|
|
|
p.translate( QPoint( 0, request->pageNumber() * size.height() * -1 ) );
|
|
|
|
d->mDocument->drawContents( &p, rect );
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
request->page()->setPixmap( request->id(), pixmap );
|
|
|
|
|
2007-01-29 08:17:45 +00:00
|
|
|
signalPixmapRequestDone( request );
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2007-01-31 18:31:19 +00:00
|
|
|
Okular::TextPage* TextDocumentGenerator::textPage( Okular::Page * page )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-31 18:31:19 +00:00
|
|
|
return d->createTextPage( page->number() );
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
2007-10-15 23:01:27 +00:00
|
|
|
bool TextDocumentGenerator::print( QPrinter& printer )
|
2007-01-13 23:15:28 +00:00
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
if ( !d->mDocument )
|
|
|
|
return false;
|
|
|
|
|
2007-11-27 16:29:50 +00:00
|
|
|
d->mDocument->print( &printer );
|
2007-01-13 23:15:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Okular::DocumentInfo* TextDocumentGenerator::generateDocumentInfo()
|
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
return &d->mDocumentInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Okular::DocumentSynopsis* TextDocumentGenerator::generateDocumentSynopsis()
|
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
if ( !d->mDocumentSynopsis.hasChildNodes() )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return &d->mDocumentSynopsis;
|
|
|
|
}
|
|
|
|
|
|
|
|
Okular::ExportFormat::List TextDocumentGenerator::exportFormats( ) const
|
|
|
|
{
|
|
|
|
static Okular::ExportFormat::List formats;
|
|
|
|
if ( formats.isEmpty() ) {
|
2007-04-21 11:09:41 +00:00
|
|
|
formats.append( Okular::ExportFormat::standardFormat( Okular::ExportFormat::PlainText ) );
|
|
|
|
formats.append( Okular::ExportFormat::standardFormat( Okular::ExportFormat::PDF ) );
|
2007-01-13 23:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextDocumentGenerator::exportTo( const QString &fileName, const Okular::ExportFormat &format )
|
|
|
|
{
|
2007-09-14 13:31:55 +00:00
|
|
|
Q_D( TextDocumentGenerator );
|
2007-01-13 23:15:28 +00:00
|
|
|
if ( !d->mDocument )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( format.mimeType()->name() == QLatin1String( "application/pdf" ) ) {
|
|
|
|
QFile file( fileName );
|
|
|
|
if ( !file.open( QIODevice::WriteOnly ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QPrinter printer( QPrinter::HighResolution );
|
|
|
|
printer.setOutputFormat( QPrinter::PdfFormat );
|
|
|
|
printer.setOutputFileName( fileName );
|
|
|
|
d->mDocument->print( &printer );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
} else if ( format.mimeType()->name() == QLatin1String( "text/plain" ) ) {
|
|
|
|
QFile file( fileName );
|
|
|
|
if ( !file.open( QIODevice::WriteOnly ) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QTextStream out( &file );
|
|
|
|
out << d->mDocument->toPlainText();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "textdocumentgenerator.moc"
|
|
|
|
|