You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.0 KiB
160 lines
5.0 KiB
/*************************************************************************** |
|
* Copyright (C) 2004-2006 by Albert Astals Cid <tsdgeos@terra.es> * |
|
* * |
|
* 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. * |
|
***************************************************************************/ |
|
|
|
// qt/kde includes |
|
#include <klocale.h> |
|
|
|
// local includes |
|
#include "toc.h" |
|
#include "core/document.h" |
|
#include "core/page.h" |
|
#include "settings.h" |
|
#include "core/link.h" |
|
|
|
// uncomment following to enable a 2nd column showing the page referred |
|
// by each tree entry note: PDF uses often references to viewports and |
|
// they're slow when converted to page number. drop the 2nd column idea. |
|
// to enable set TocPageColumn=true in [Nav Panel] |
|
|
|
class TOCItem : public K3ListViewItem |
|
{ |
|
public: |
|
TOCItem( K3ListView *parent, TOCItem *after, const QDomElement & e ) |
|
: K3ListViewItem( parent, after, e.tagName() ), m_element( e ) |
|
{ |
|
if ( KpdfSettings::tocPageColumn() && e.hasAttribute( "Page" ) ) |
|
setText( 1, e.attribute( "Page" ) ); |
|
setMultiLinesEnabled( true ); |
|
} |
|
|
|
TOCItem( K3ListViewItem *parent, TOCItem *after, const QDomElement & e ) |
|
: K3ListViewItem( parent, after, e.tagName() ), m_element( e ) |
|
{ |
|
if ( KpdfSettings::tocPageColumn() && e.hasAttribute( "Page" ) ) |
|
setText( 1, e.attribute( "Page" ) ); |
|
setMultiLinesEnabled( true ); |
|
} |
|
|
|
const QDomElement & element() const |
|
{ |
|
return m_element; |
|
} |
|
|
|
private: |
|
QDomElement m_element; |
|
}; |
|
|
|
TOC::TOC(QWidget *parent, KPDFDocument *document) : K3ListView(parent), m_document(document) |
|
{ |
|
addColumn( i18n("Topic") ); |
|
if (KpdfSettings::tocPageColumn()) |
|
addColumn( i18n("Page") ); |
|
setSorting(-1); |
|
setRootIsDecorated(true); |
|
setResizeMode(AllColumns); |
|
setAllColumnsShowFocus(true); |
|
connect(this, SIGNAL(clicked(Q3ListViewItem *)), this, SLOT(slotExecuted(Q3ListViewItem *))); |
|
connect(this, SIGNAL(returnPressed(Q3ListViewItem *)), this, SLOT(slotExecuted(Q3ListViewItem *))); |
|
} |
|
|
|
TOC::~TOC() |
|
{ |
|
m_document->removeObserver( this ); |
|
} |
|
|
|
uint TOC::observerId() const |
|
{ |
|
return TOC_ID; |
|
} |
|
|
|
void TOC::notifySetup( const QVector< KPDFPage * > & /*pages*/, bool documentChanged ) |
|
{ |
|
if ( !documentChanged ) |
|
return; |
|
|
|
// clear contents |
|
clear(); |
|
|
|
// request synopsis description (is a dom tree) |
|
const DocumentSynopsis * syn = m_document->documentSynopsis(); |
|
|
|
// if not present, disable the contents tab |
|
if ( !syn ) |
|
{ |
|
emit hasTOC( false ); |
|
return; |
|
} |
|
|
|
// else populate the listview and enable the tab |
|
addChildren( *syn ); |
|
emit hasTOC( true ); |
|
} |
|
|
|
void TOC::addChildren( const QDomNode & parentNode, K3ListViewItem * parentItem ) |
|
{ |
|
// keep track of the current listViewItem |
|
TOCItem * currentItem = 0; |
|
QDomNode n = parentNode.firstChild(); |
|
while( !n.isNull() ) |
|
{ |
|
// convert the node to an element (sure it is) |
|
QDomElement e = n.toElement(); |
|
|
|
// insert the entry as top level (listview parented) or 2nd+ level |
|
if ( !parentItem ) |
|
currentItem = new TOCItem( this, currentItem, e ); |
|
else |
|
currentItem = new TOCItem( parentItem, currentItem, e ); |
|
|
|
// descend recursively and advance to the next node |
|
if ( e.hasChildNodes() ) |
|
addChildren( n, currentItem ); |
|
n = n.nextSibling(); |
|
} |
|
} |
|
|
|
void TOC::slotExecuted( Q3ListViewItem *i ) |
|
{ |
|
TOCItem* tocItem = dynamic_cast<TOCItem*>( i ); |
|
// that filters clicks on [+] that for a strange reason don't seem to be TOCItem* |
|
if (tocItem == NULL) |
|
return; |
|
const QDomElement & e = tocItem->element(); |
|
|
|
QString externalFileName = e.attribute( "ExternalFileName" ); |
|
if ( !externalFileName.isEmpty() ) |
|
{ |
|
KPDFLinkGoto link( externalFileName, getViewport( e ) ); |
|
m_document->processLink( &link ); |
|
} |
|
else |
|
{ |
|
m_document->setViewport( getViewport( e ), TOC_ID ); |
|
} |
|
} |
|
|
|
DocumentViewport TOC::getViewport( const QDomElement &e ) const |
|
{ |
|
if ( e.hasAttribute( "Viewport" ) ) |
|
{ |
|
// if the node has a viewport, set it |
|
return DocumentViewport( e.attribute( "Viewport" ) ); |
|
} |
|
else if ( e.hasAttribute( "ViewportName" ) ) |
|
{ |
|
// if the node references a viewport, get the reference and set it |
|
const QString & page = e.attribute( "ViewportName" ); |
|
const QString & viewport = m_document->getMetaData( "NamedViewport", page ); |
|
if ( !viewport.isNull() ) |
|
return DocumentViewport( viewport ); |
|
} |
|
return DocumentViewport(); |
|
} |
|
|
|
#include "toc.moc"
|
|
|