2004-09-21 20:38:22 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2004 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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// qt/kde includes
|
2004-09-21 20:38:22 +00:00
|
|
|
#include <qheader.h>
|
2004-12-12 11:11:57 +00:00
|
|
|
#include <klocale.h>
|
2004-09-21 20:38:22 +00:00
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// local includes
|
2004-09-21 20:38:22 +00:00
|
|
|
#include "toc.h"
|
2005-01-02 14:37:49 +00:00
|
|
|
#include "core/document.h"
|
2005-01-02 14:55:14 +00:00
|
|
|
#include "core/page.h"
|
2004-09-21 20:38:22 +00:00
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
// 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.
|
2004-12-12 11:11:57 +00:00
|
|
|
//#define TOC_ENABLE_PAGE_COLUMN
|
|
|
|
|
2004-09-21 20:38:22 +00:00
|
|
|
class TOCItem : public KListViewItem
|
|
|
|
{
|
2004-12-12 11:11:57 +00:00
|
|
|
public:
|
|
|
|
TOCItem( KListView *parent, TOCItem *after, const QDomElement & e )
|
|
|
|
: KListViewItem( parent, after, e.tagName() ), m_element( e )
|
|
|
|
{
|
|
|
|
#ifdef TOC_ENABLE_PAGE_COLUMN
|
|
|
|
if ( e.hasAttribute( "Page" ) )
|
|
|
|
setText( 1, e.attribute( "Page" ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
TOCItem( KListViewItem *parent, TOCItem *after, const QDomElement & e )
|
|
|
|
: KListViewItem( parent, after, e.tagName() ), m_element( e )
|
|
|
|
{
|
|
|
|
#ifdef TOC_ENABLE_PAGE_COLUMN
|
|
|
|
if ( e.hasAttribute( "Page" ) )
|
|
|
|
setText( 1, e.attribute( "Page" ) );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDomElement & element() const
|
|
|
|
{
|
|
|
|
return m_element;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
QDomElement m_element;
|
2004-09-21 20:38:22 +00:00
|
|
|
};
|
|
|
|
|
2004-09-23 21:33:53 +00:00
|
|
|
TOC::TOC(QWidget *parent, KPDFDocument *document) : KListView(parent), m_document(document)
|
2004-09-21 20:38:22 +00:00
|
|
|
{
|
2004-12-12 11:11:57 +00:00
|
|
|
addColumn( i18n("Topic") );
|
|
|
|
#ifdef TOC_ENABLE_PAGE_COLUMN
|
|
|
|
addColumn( i18n("Page") );
|
|
|
|
#else
|
2004-12-11 17:25:03 +00:00
|
|
|
header() -> hide();
|
2004-12-12 11:11:57 +00:00
|
|
|
#endif
|
2004-12-11 17:25:03 +00:00
|
|
|
setSorting(-1);
|
|
|
|
setRootIsDecorated(true);
|
|
|
|
setResizeMode(AllColumns);
|
2005-06-13 16:32:59 +00:00
|
|
|
setAllColumnsShowFocus(true);
|
2005-06-13 16:34:07 +00:00
|
|
|
connect(this, SIGNAL(clicked(QListViewItem *)), this, SLOT(slotExecuted(QListViewItem *)));
|
|
|
|
connect(this, SIGNAL(returnPressed(QListViewItem *)), this, SLOT(slotExecuted(QListViewItem *)));
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2004-10-24 13:08:31 +00:00
|
|
|
uint TOC::observerId() const
|
2004-09-21 20:38:22 +00:00
|
|
|
{
|
2004-12-11 17:25:03 +00:00
|
|
|
return TOC_ID;
|
2004-09-23 21:33:53 +00:00
|
|
|
}
|
2004-09-21 20:38:22 +00:00
|
|
|
|
2005-01-09 23:37:07 +00:00
|
|
|
void TOC::notifySetup( const QValueVector< KPDFPage * > & pages, bool documentChanged )
|
2004-09-23 21:33:53 +00:00
|
|
|
{
|
2004-12-11 17:25:03 +00:00
|
|
|
if ( !documentChanged || pages.size() < 1 )
|
|
|
|
return;
|
2004-09-23 21:33:53 +00:00
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// clear contents
|
2004-12-11 17:25:03 +00:00
|
|
|
clear();
|
2004-12-12 11:11:57 +00:00
|
|
|
|
|
|
|
// request synopsis description (is a dom tree)
|
2004-12-11 17:25:03 +00:00
|
|
|
const DocumentSynopsis * syn = m_document->documentSynopsis();
|
2004-12-12 11:11:57 +00:00
|
|
|
|
|
|
|
// if not present, disable the contents tab
|
|
|
|
if ( !syn )
|
2004-12-11 17:25:03 +00:00
|
|
|
{
|
2004-12-12 11:11:57 +00:00
|
|
|
emit hasTOC( false );
|
|
|
|
return;
|
2004-12-11 17:25:03 +00:00
|
|
|
}
|
2004-09-21 20:38:22 +00:00
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// else populate the listview and enable the tab
|
|
|
|
addChildren( *syn );
|
|
|
|
emit hasTOC( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOC::addChildren( const QDomNode & parentNode, KListViewItem * 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();
|
|
|
|
}
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
void TOC::slotExecuted( QListViewItem *i )
|
2004-09-21 20:38:22 +00:00
|
|
|
{
|
2005-06-13 16:35:31 +00:00
|
|
|
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();
|
2005-01-10 18:28:32 +00:00
|
|
|
if ( e.hasAttribute( "Viewport" ) )
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2005-01-09 23:37:07 +00:00
|
|
|
// if the node has a viewport, set it
|
2005-01-10 18:28:32 +00:00
|
|
|
m_document->setViewport( DocumentViewport( e.attribute( "Viewport" ) ), TOC_ID );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2005-01-09 23:37:07 +00:00
|
|
|
else if ( e.hasAttribute( "ViewportName" ) )
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2005-01-09 23:37:07 +00:00
|
|
|
// 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() )
|
2005-01-10 13:43:44 +00:00
|
|
|
m_document->setViewport( DocumentViewport( viewport ), TOC_ID );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "toc.moc"
|