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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "toc.h"
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// qt/kde includes
|
2006-12-22 19:26:28 +00:00
|
|
|
#include <qdom.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 <qtreewidget.h>
|
2007-04-15 15:48:39 +00:00
|
|
|
#include <qvariant.h>
|
2006-05-28 17:27:24 +00:00
|
|
|
#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
|
2006-12-26 12:01:30 +00:00
|
|
|
#include "pageitemdelegate.h"
|
2007-04-20 11:26:05 +00:00
|
|
|
#include "core/action.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
|
|
|
|
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-12-22 19:26:28 +00:00
|
|
|
TOCItem( QTreeWidget *parent, TOCItem *after, Okular::Document *document, const QDomElement & e )
|
2007-01-25 21:35:20 +00:00
|
|
|
: QTreeWidgetItem( parent, after )
|
2004-12-12 11:11:57 +00:00
|
|
|
{
|
2006-12-22 19:26:28 +00:00
|
|
|
init( document, e );
|
2004-12-12 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
2006-12-22 19:26:28 +00:00
|
|
|
TOCItem( QTreeWidgetItem *parent, TOCItem *after, Okular::Document *document, const QDomElement & e )
|
2007-01-25 21:35:20 +00:00
|
|
|
: QTreeWidgetItem( parent, after )
|
2004-12-12 11:11:57 +00:00
|
|
|
{
|
2006-12-22 19:26:28 +00:00
|
|
|
init( document, e );
|
|
|
|
}
|
|
|
|
|
|
|
|
void init( Okular::Document *document, const QDomElement & e )
|
|
|
|
{
|
|
|
|
// viewport loading
|
|
|
|
if ( e.hasAttribute( "Viewport" ) )
|
|
|
|
{
|
|
|
|
// if the node has a viewport, set it
|
|
|
|
m_viewport = Okular::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" );
|
2007-01-02 22:37:55 +00:00
|
|
|
QString viewport = document->metaData( "NamedViewport", page ).toString();
|
2006-12-22 19:26:28 +00:00
|
|
|
if ( !viewport.isNull() )
|
|
|
|
m_viewport = Okular::DocumentViewport( viewport );
|
|
|
|
}
|
|
|
|
|
2006-12-26 12:26:49 +00:00
|
|
|
if ( m_viewport.isValid() )
|
2007-01-19 23:30:32 +00:00
|
|
|
{
|
2006-12-26 14:35:27 +00:00
|
|
|
setData( 0, PageItemDelegate::PageRole, QString::number( m_viewport.pageNumber + 1 ) );
|
2007-01-19 23:30:32 +00:00
|
|
|
QString label = document->page( m_viewport.pageNumber )->label();
|
|
|
|
if ( !label.isEmpty() )
|
|
|
|
setData( 0, PageItemDelegate::PageLabelRole, label );
|
|
|
|
}
|
2007-01-25 21:35:20 +00:00
|
|
|
m_extFileName = e.attribute( "ExternalFileName" );
|
2006-12-26 14:35:27 +00:00
|
|
|
setText( 0, e.tagName() );
|
2004-12-12 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
2007-01-25 21:35:20 +00:00
|
|
|
QString externalFileName() const
|
2004-12-12 11:11:57 +00:00
|
|
|
{
|
2007-01-25 21:35:20 +00:00
|
|
|
return m_extFileName;
|
2004-12-12 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
2006-12-22 19:26:28 +00:00
|
|
|
const Okular::DocumentViewport& viewport() const
|
|
|
|
{
|
|
|
|
return m_viewport;
|
|
|
|
}
|
|
|
|
|
2006-10-21 13:11:43 +00:00
|
|
|
void setCurrent( bool selected )
|
2006-06-20 13:59:08 +00:00
|
|
|
{
|
2007-03-08 21:41:46 +00:00
|
|
|
setIcon( 0, selected ? KIcon( treeWidget()->layoutDirection() == Qt::RightToLeft ? "arrow-left" : "arrow-right" ) : QIcon() );
|
2006-06-20 13:59:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
private:
|
2006-12-22 19:26:28 +00:00
|
|
|
Okular::DocumentViewport m_viewport;
|
2007-01-25 21:35:20 +00:00
|
|
|
QString m_extFileName;
|
2004-09-21 20:38:22 +00:00
|
|
|
};
|
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
TOC::TOC(QWidget *parent, Okular::Document *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 );
|
2007-08-24 14:20:03 +00:00
|
|
|
mainlay->setSpacing( 6 );
|
2006-05-28 17:27:24 +00:00
|
|
|
|
|
|
|
m_searchLine = new KTreeWidgetSearchLine( this );
|
2007-08-24 14:20:03 +00:00
|
|
|
mainlay->addWidget( m_searchLine );
|
2006-05-28 17:27:24 +00:00
|
|
|
|
|
|
|
m_treeView = new QTreeWidget( this );
|
|
|
|
mainlay->addWidget( m_treeView );
|
2006-05-19 11:32:08 +00:00
|
|
|
QStringList cols;
|
2006-12-22 19:26:28 +00:00
|
|
|
cols.append( "Topics" );
|
2006-05-28 17:27:24 +00:00
|
|
|
m_treeView->setHeaderLabels( cols );
|
|
|
|
m_treeView->setSortingEnabled( false );
|
|
|
|
m_treeView->setRootIsDecorated( true );
|
|
|
|
m_treeView->setAlternatingRowColors( true );
|
2006-12-26 12:01:30 +00:00
|
|
|
m_treeView->setItemDelegate( new PageItemDelegate( m_treeView ) );
|
2006-05-28 17:27:24 +00:00
|
|
|
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
|
|
|
|
2007-09-09 10:50:36 +00:00
|
|
|
void TOC::notifySetup( const QVector< Okular::Page * > & /*pages*/, int setupFlags )
|
2004-09-23 21:33:53 +00:00
|
|
|
{
|
2007-09-09 10:50:36 +00:00
|
|
|
if ( !( setupFlags & Okular::DocumentObserver::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)
|
2006-09-21 08:45:36 +00:00
|
|
|
const Okular::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 )
|
|
|
|
{
|
2006-10-21 13:11:43 +00:00
|
|
|
m_current->setCurrent( false );
|
2006-06-20 13:59:08 +00:00
|
|
|
m_current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeWidgetItemIterator it( m_treeView );
|
|
|
|
while ( (*it) && !m_current )
|
|
|
|
{
|
|
|
|
TOCItem *tmp = dynamic_cast<TOCItem*>( *it );
|
2006-12-22 19:26:28 +00:00
|
|
|
int p = tmp ? tmp->viewport().pageNumber : -1;
|
2006-06-20 13:59:08 +00:00
|
|
|
if ( p == newpage )
|
|
|
|
{
|
|
|
|
m_current = tmp;
|
2006-09-04 14:57:39 +00:00
|
|
|
if (m_current)
|
2006-10-21 13:11:43 +00:00
|
|
|
m_current->setCurrent( true );
|
2006-06-20 13:59:08 +00:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-22 20:42:36 +00:00
|
|
|
void TOC::reparseConfig()
|
|
|
|
{
|
|
|
|
m_treeView->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-12-22 19:26:28 +00:00
|
|
|
currentItem = new TOCItem( m_treeView, currentItem, m_document, e );
|
2004-12-12 11:11:57 +00:00
|
|
|
else
|
2006-12-22 19:26:28 +00:00
|
|
|
currentItem = new TOCItem( parentItem, currentItem, m_document, e );
|
2004-12-12 11:11:57 +00:00
|
|
|
|
|
|
|
// descend recursively and advance to the next node
|
|
|
|
if ( e.hasChildNodes() )
|
|
|
|
addChildren( n, currentItem );
|
2007-04-15 15:48:39 +00:00
|
|
|
|
|
|
|
// open/keep close the item
|
|
|
|
bool isOpen = false;
|
|
|
|
if ( e.hasAttribute( "Open" ) )
|
|
|
|
isOpen = QVariant( e.attribute( "Open" ) ).toBool();
|
|
|
|
currentItem->setExpanded( isOpen );
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
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;
|
2005-07-15 17:32:46 +00:00
|
|
|
|
2007-01-25 21:35:20 +00:00
|
|
|
QString externalFileName = tocItem->externalFileName();
|
2005-07-15 17:32:46 +00:00
|
|
|
if ( !externalFileName.isEmpty() )
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2007-05-02 22:50:27 +00:00
|
|
|
Okular::GotoAction action( externalFileName, tocItem->viewport() );
|
2007-04-20 12:49:17 +00:00
|
|
|
m_document->processAction( &action );
|
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-12-22 19:26:28 +00:00
|
|
|
m_document->setViewport( tocItem->viewport() );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "toc.moc"
|