mirror of
https://invent.kde.org/graphics/okular
synced 2024-08-27 11:40:07 +00:00
* Add support for TableOfContents
* Add basic table rendering svn path=/trunk/playground/graphics/okular/; revision=585990
This commit is contained in:
parent
a8c90e618b
commit
e21d2af66a
|
@ -7,11 +7,15 @@
|
|||
* (at your option) any later version. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
#include <QtCore/QUrl>
|
||||
#include <QtGui/QAbstractTextDocumentLayout>
|
||||
#include <QtGui/QTextCursor>
|
||||
#include <QtGui/QTextDocument>
|
||||
#include <QtGui/QTextFrame>
|
||||
#include <QtGui/QTextList>
|
||||
#include <QtGui/QTextTableCell>
|
||||
#include <QtXml/QDomDocument>
|
||||
#include <QtXml/QDomElement>
|
||||
#include <QtXml/QDomText>
|
||||
|
@ -140,6 +144,9 @@ bool Converter::convertText( const QDomElement &element )
|
|||
} else if ( child.tagName() == QLatin1String( "list" ) ) {
|
||||
if ( !convertList( child ) )
|
||||
return false;
|
||||
} else if ( child.tagName() == QLatin1String( "table" ) ) {
|
||||
if ( !convertTable( child ) )
|
||||
return false;
|
||||
}
|
||||
|
||||
child = child.nextSiblingElement();
|
||||
|
@ -176,6 +183,45 @@ bool Converter::convertHeader( QTextCursor *cursor, const QDomElement &element )
|
|||
child = child.nextSibling();
|
||||
}
|
||||
|
||||
const QSizeF pageSize = mTextDocument->pageSize();
|
||||
|
||||
QTextBlock currentBlock = cursor->block();
|
||||
const QRectF rect = mTextDocument->documentLayout()->blockBoundingRect( currentBlock );
|
||||
// const QRectF rect(0, 0, 0, 0);
|
||||
|
||||
int page = qRound( rect.y() ) / qRound( pageSize.height() );
|
||||
int offset = qRound( rect.y() ) % qRound( pageSize.height() );
|
||||
|
||||
DocumentViewport viewport( page );
|
||||
viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width();
|
||||
viewport.rePos.normalizedY = (double)offset / (double)pageSize.height();
|
||||
viewport.rePos.enabled = true;
|
||||
viewport.rePos.pos = DocumentViewport::Center;
|
||||
|
||||
static QStack<QDomNode> parentNodeStack;
|
||||
static QDomNode parentNode = mTableOfContents;
|
||||
static int level = 2;
|
||||
|
||||
QDomElement item = mTableOfContents.createElement( element.text() );
|
||||
item.setAttribute( "Viewport", viewport.toString() );
|
||||
|
||||
int newLevel = element.attribute( "outline-level" ).toInt();
|
||||
if ( newLevel == level ) {
|
||||
parentNode.appendChild( item );
|
||||
} else if ( newLevel > level ) {
|
||||
parentNodeStack.push( parentNode );
|
||||
parentNode = parentNode.lastChildElement();
|
||||
parentNode.appendChild( item );
|
||||
level++;
|
||||
} else {
|
||||
for ( int i = level; i > newLevel; i-- ) {
|
||||
level--;
|
||||
parentNode = parentNodeStack.pop();
|
||||
}
|
||||
|
||||
parentNode.appendChild( item );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -288,6 +334,74 @@ bool Converter::convertList( const QDomElement &element )
|
|||
|
||||
bool Converter::convertTable( const QDomElement &element )
|
||||
{
|
||||
/**
|
||||
* Find out dimension of the table
|
||||
*/
|
||||
QDomElement rowElement = element.firstChildElement();
|
||||
|
||||
int rowCounter = 0;
|
||||
int columnCounter = 0;
|
||||
while ( !rowElement.isNull() ) {
|
||||
if ( rowElement.tagName() == QLatin1String( "table-row" ) ) {
|
||||
rowCounter++;
|
||||
|
||||
int counter = 0;
|
||||
QDomElement columnElement = rowElement.firstChildElement();
|
||||
while ( !columnElement.isNull() ) {
|
||||
if ( columnElement.tagName() == QLatin1String( "table-cell" ) ) {
|
||||
counter++;
|
||||
}
|
||||
columnElement = columnElement.nextSiblingElement();
|
||||
}
|
||||
|
||||
columnCounter = qMax( columnCounter, counter );
|
||||
}
|
||||
|
||||
rowElement = rowElement.nextSiblingElement();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create table
|
||||
*/
|
||||
QTextTable *table = mCursor->insertTable( rowCounter, columnCounter );
|
||||
|
||||
/**
|
||||
* Fill table
|
||||
*/
|
||||
rowElement = element.firstChildElement();
|
||||
|
||||
rowCounter = 0;
|
||||
while ( !rowElement.isNull() ) {
|
||||
if ( rowElement.tagName() == QLatin1String( "table-row" ) ) {
|
||||
|
||||
int columnCounter = 0;
|
||||
QDomElement columnElement = rowElement.firstChildElement();
|
||||
while ( !columnElement.isNull() ) {
|
||||
if ( columnElement.tagName() == QLatin1String( "table-cell" ) ) {
|
||||
|
||||
QDomElement paragraphElement = columnElement.firstChildElement();
|
||||
while ( !paragraphElement.isNull() ) {
|
||||
if ( paragraphElement.tagName() == QLatin1String( "p" ) ) {
|
||||
QTextTableCell cell = table->cellAt( rowCounter, columnCounter );
|
||||
QTextCursor cursor = cell.firstCursorPosition();
|
||||
|
||||
if ( !convertParagraph( &cursor, paragraphElement ) )
|
||||
return false;
|
||||
}
|
||||
|
||||
paragraphElement = paragraphElement.nextSiblingElement();
|
||||
}
|
||||
columnCounter++;
|
||||
}
|
||||
columnElement = columnElement.nextSiblingElement();
|
||||
}
|
||||
|
||||
rowCounter++;
|
||||
}
|
||||
|
||||
rowElement = rowElement.nextSiblingElement();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -320,3 +434,9 @@ MetaInformation::List Converter::metaInformation() const
|
|||
{
|
||||
return mStyleInformation->metaInformation();
|
||||
}
|
||||
|
||||
DocumentSynopsis Converter::tableOfContents() const
|
||||
{
|
||||
return mTableOfContents;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,8 @@
|
|||
|
||||
#include "styleinformation.h"
|
||||
|
||||
#include "core/document.h"
|
||||
|
||||
class QDomElement;
|
||||
class QDomText;
|
||||
|
||||
|
@ -46,6 +48,7 @@ class Converter
|
|||
|
||||
QTextDocument *textDocument() const;
|
||||
MetaInformation::List metaInformation() const;
|
||||
DocumentSynopsis tableOfContents() const;
|
||||
|
||||
bool convertBody( const QDomElement &element );
|
||||
bool convertText( const QDomElement &element );
|
||||
|
@ -64,6 +67,7 @@ class Converter
|
|||
QTextBlock *mLastTextBlock;
|
||||
|
||||
StyleInformation *mStyleInformation;
|
||||
DocumentSynopsis mTableOfContents;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <QtGui/QTextDocument>
|
||||
#include <QtGui/QAbstractTextDocumentLayout>
|
||||
|
||||
#include <QTime>
|
||||
|
||||
#include <qpainter.h>
|
||||
#include <qpixmap.h>
|
||||
#include <qimage.h>
|
||||
|
@ -43,10 +45,14 @@ bool KOOOGenerator::loadDocument( const QString & fileName, QVector<KPDFPage*> &
|
|||
|
||||
OOO::Converter converter( &document );
|
||||
|
||||
QTime time;
|
||||
time.start();
|
||||
if ( !converter.convert() )
|
||||
return false;
|
||||
qDebug( "creation time elapsed: %d", time.elapsed() );
|
||||
|
||||
mDocument = converter.textDocument();
|
||||
mDocumentSynopsis = converter.tableOfContents();
|
||||
|
||||
OOO::MetaInformation::List metaInformation = converter.metaInformation();
|
||||
for ( int i = 0; i < metaInformation.count(); ++i ) {
|
||||
|
@ -163,10 +169,17 @@ bool KOOOGenerator::print( KPrinter& printer )
|
|||
return true;
|
||||
}
|
||||
|
||||
const DocumentInfo * KOOOGenerator::generateDocumentInfo()
|
||||
const DocumentInfo* KOOOGenerator::generateDocumentInfo()
|
||||
{
|
||||
return &mDocumentInfo;
|
||||
}
|
||||
|
||||
const DocumentSynopsis* KOOOGenerator::generateDocumentSynopsis()
|
||||
{
|
||||
if ( !mDocumentSynopsis.hasChildNodes() )
|
||||
return 0;
|
||||
else
|
||||
return &mDocumentSynopsis;
|
||||
}
|
||||
#include "generator_ooo.moc"
|
||||
|
||||
|
|
|
@ -38,10 +38,12 @@ class KOOOGenerator : public Generator
|
|||
bool print( KPrinter& printer );
|
||||
|
||||
const DocumentInfo* generateDocumentInfo();
|
||||
const DocumentSynopsis* generateDocumentSynopsis();
|
||||
|
||||
private:
|
||||
QTextDocument *mDocument;
|
||||
DocumentInfo mDocumentInfo;
|
||||
DocumentSynopsis mDocumentSynopsis;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue