2004-09-21 20:38:22 +00:00
|
|
|
/***************************************************************************
|
2006-02-18 12:06:52 +00:00
|
|
|
* Copyright (C) 2004-2006 by Albert Astals Cid <tsdgeos@terra.es> *
|
2004-09-21 20:38:22 +00:00
|
|
|
* *
|
|
|
|
* 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
|
2006-06-20 13:59:08 +00:00
|
|
|
#include <qapplication.h>
|
2006-05-19 11:32:08 +00:00
|
|
|
#include <qheaderview.h>
|
2006-05-28 17:27:24 +00:00
|
|
|
#include <qlayout.h>
|
2006-05-19 11:32:08 +00:00
|
|
|
#include <qstringlist.h>
|
2006-05-28 17:27:24 +00:00
|
|
|
#include <qtoolbutton.h>
|
|
|
|
#include <qtreewidget.h>
|
|
|
|
#include <kicon.h>
|
2004-12-12 11:11:57 +00:00
|
|
|
#include <klocale.h>
|
2006-05-28 17:27:24 +00:00
|
|
|
#include <ktreewidgetsearchline.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"
|
2006-03-28 22:22:10 +00:00
|
|
|
#include "settings.h"
|
2005-07-15 18:20:57 +00:00
|
|
|
#include "core/link.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.
|
2005-07-05 19:10:48 +00:00
|
|
|
// to enable set TocPageColumn=true in [Nav Panel]
|
2004-12-12 11:11:57 +00:00
|
|
|
|
2006-05-19 11:32:08 +00:00
|
|
|
class TOCItem : public QTreeWidgetItem
|
2004-09-21 20:38:22 +00:00
|
|
|
{
|
2004-12-12 11:11:57 +00:00
|
|
|
public:
|
2006-05-19 11:32:08 +00:00
|
|
|
TOCItem( QTreeWidget *parent, TOCItem *after, const QDomElement & e )
|
|
|
|
: QTreeWidgetItem( parent, after ), m_element( e )
|
2004-12-12 11:11:57 +00:00
|
|
|
{
|
2006-05-19 11:32:08 +00:00
|
|
|
setText( 0, e.tagName() );
|
2005-11-04 11:59:51 +00:00
|
|
|
if ( KpdfSettings::tocPageColumn() && e.hasAttribute( "Page" ) )
|
2004-12-12 11:11:57 +00:00
|
|
|
setText( 1, e.attribute( "Page" ) );
|
|
|
|
}
|
|
|
|
|
2006-05-19 11:32:08 +00:00
|
|
|
TOCItem( QTreeWidgetItem *parent, TOCItem *after, const QDomElement & e )
|
|
|
|
: QTreeWidgetItem( parent, after ), m_element( e )
|
2004-12-12 11:11:57 +00:00
|
|
|
{
|
2006-05-19 11:32:08 +00:00
|
|
|
setText( 0, e.tagName() );
|
2005-11-04 11:59:51 +00:00
|
|
|
if ( KpdfSettings::tocPageColumn() && e.hasAttribute( "Page" ) )
|
2004-12-12 11:11:57 +00:00
|
|
|
setText( 1, e.attribute( "Page" ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
const QDomElement & element() const
|
|
|
|
{
|
|
|
|
return m_element;
|
|
|
|
}
|
|
|
|
|
2006-06-20 13:59:08 +00:00
|
|
|
void setSelected( bool selected )
|
|
|
|
{
|
|
|
|
setIcon( 0, selected ? KIcon( QApplication::layoutDirection() == Qt::RightToLeft ? "1leftarrow" : "1rightarrow" ) : QIcon() );
|
|
|
|
}
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
private:
|
|
|
|
QDomElement m_element;
|
2004-09-21 20:38:22 +00:00
|
|
|
};
|
|
|
|
|
2006-06-20 13:59:08 +00:00
|
|
|
TOC::TOC(QWidget *parent, KPDFDocument *document) : QWidget(parent), m_document(document), m_current(0), m_currentPage(-1)
|
2004-09-21 20:38:22 +00:00
|
|
|
{
|
2006-05-28 17:27:24 +00:00
|
|
|
QVBoxLayout *mainlay = new QVBoxLayout( this );
|
|
|
|
mainlay->setMargin( 0 );
|
|
|
|
|
|
|
|
QHBoxLayout *searchlay = new QHBoxLayout();
|
|
|
|
searchlay->setSpacing( 2 );
|
|
|
|
mainlay->addLayout( searchlay );
|
|
|
|
QToolButton *clearBtn = new QToolButton( this );
|
|
|
|
clearBtn->setIcon( KIcon( layoutDirection() == Qt::RightToLeft ? "clear_left" : "locationbar_erase" ) );
|
|
|
|
clearBtn->setToolTip( i18n( "Clear filter" ) );
|
|
|
|
clearBtn->setAutoRaise( true );
|
|
|
|
searchlay->addWidget( clearBtn );
|
|
|
|
|
|
|
|
m_searchLine = new KTreeWidgetSearchLine( this );
|
|
|
|
connect( clearBtn, SIGNAL( clicked() ), m_searchLine, SLOT( clear() ) );
|
|
|
|
searchlay->addWidget( m_searchLine );
|
|
|
|
|
|
|
|
m_treeView = new QTreeWidget( this );
|
|
|
|
mainlay->addWidget( m_treeView );
|
2006-05-19 11:32:08 +00:00
|
|
|
QStringList cols;
|
|
|
|
cols.append( i18n("Topic") );
|
2005-11-04 11:59:51 +00:00
|
|
|
if (KpdfSettings::tocPageColumn())
|
2006-05-19 11:32:08 +00:00
|
|
|
cols.append( i18n("Page") );
|
2006-05-28 17:27:24 +00:00
|
|
|
m_treeView->setHeaderLabels( cols );
|
|
|
|
m_treeView->setSortingEnabled( false );
|
|
|
|
m_treeView->setRootIsDecorated( true );
|
|
|
|
m_treeView->setAlternatingRowColors( true );
|
|
|
|
m_treeView->header()->setResizeMode( QHeaderView::Stretch );
|
|
|
|
m_treeView->header()->hide();
|
|
|
|
m_treeView->setSelectionBehavior( QAbstractItemView::SelectRows );
|
|
|
|
connect( m_treeView, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ), this, SLOT( slotExecuted( QTreeWidgetItem * ) ) );
|
|
|
|
connect( m_treeView, SIGNAL( itemActivated( QTreeWidgetItem *, int ) ), this, SLOT( slotExecuted( QTreeWidgetItem * ) ) );
|
|
|
|
m_searchLine->addTreeWidget( m_treeView );
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-18 12:06:52 +00:00
|
|
|
TOC::~TOC()
|
|
|
|
{
|
|
|
|
m_document->removeObserver( this );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2006-03-23 20:43:38 +00:00
|
|
|
void TOC::notifySetup( const QVector< KPDFPage * > & /*pages*/, bool documentChanged )
|
2004-09-23 21:33:53 +00:00
|
|
|
{
|
2005-11-04 11:59:51 +00:00
|
|
|
if ( !documentChanged )
|
2004-12-11 17:25:03 +00:00
|
|
|
return;
|
2004-09-23 21:33:53 +00:00
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// clear contents
|
2006-05-28 17:27:24 +00:00
|
|
|
m_treeView->clear();
|
|
|
|
m_searchLine->clear();
|
2006-06-20 13:59:08 +00:00
|
|
|
m_current = 0;
|
|
|
|
m_currentPage = -1;
|
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 );
|
|
|
|
}
|
|
|
|
|
2006-06-20 13:59:08 +00:00
|
|
|
void TOC::notifyViewportChanged( bool /*smoothMove*/ )
|
|
|
|
{
|
|
|
|
int newpage = m_document->viewport().pageNumber;
|
|
|
|
if ( m_currentPage == newpage )
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_currentPage = newpage;
|
|
|
|
|
|
|
|
if ( m_current )
|
|
|
|
{
|
|
|
|
m_current->setSelected( false );
|
|
|
|
m_current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeWidgetItemIterator it( m_treeView );
|
|
|
|
while ( (*it) && !m_current )
|
|
|
|
{
|
|
|
|
TOCItem *tmp = dynamic_cast<TOCItem*>( *it );
|
|
|
|
int p = tmp ? getViewport( tmp->element() ).pageNumber : -1;
|
|
|
|
if ( p == newpage )
|
|
|
|
{
|
|
|
|
m_current = tmp;
|
|
|
|
m_current->setSelected( true );
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-19 11:32:08 +00:00
|
|
|
void TOC::addChildren( const QDomNode & parentNode, QTreeWidgetItem * parentItem )
|
2004-12-12 11:11:57 +00:00
|
|
|
{
|
|
|
|
// 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 )
|
2006-05-28 17:27:24 +00:00
|
|
|
currentItem = new TOCItem( m_treeView, currentItem, e );
|
2004-12-12 11:11:57 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2006-05-19 11:32:08 +00:00
|
|
|
void TOC::slotExecuted( QTreeWidgetItem *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-07-15 17:32:46 +00:00
|
|
|
|
|
|
|
QString externalFileName = e.attribute( "ExternalFileName" );
|
|
|
|
if ( !externalFileName.isEmpty() )
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2005-11-04 11:59:51 +00:00
|
|
|
KPDFLinkGoto link( externalFileName, getViewport( e ) );
|
2005-07-15 17:32:46 +00:00
|
|
|
m_document->processLink( &link );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2005-07-15 17:32:46 +00:00
|
|
|
else
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2006-07-05 13:34:16 +00:00
|
|
|
m_document->setViewport( getViewport( e ) );
|
2005-11-04 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2005-11-04 11:59:51 +00:00
|
|
|
return DocumentViewport();
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "toc.moc"
|