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-12-23 00:34:40 +00:00
|
|
|
#include <qapplication.h>
|
2006-12-22 19:26:28 +00:00
|
|
|
#include <qdom.h>
|
2006-05-19 11:32:08 +00:00
|
|
|
#include <qheaderview.h>
|
2006-12-22 19:26:28 +00:00
|
|
|
#include <QItemDelegate>
|
2006-05-28 17:27:24 +00:00
|
|
|
#include <qlayout.h>
|
2006-05-19 11:32:08 +00:00
|
|
|
#include <qstringlist.h>
|
2006-12-23 00:34:40 +00:00
|
|
|
#include <QTextDocument>
|
2006-05-28 17:27:24 +00:00
|
|
|
#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
|
|
|
|
2006-12-22 19:26:28 +00:00
|
|
|
#define TOC_SEPARATOR "@@@@@@@@@@"
|
|
|
|
#define TOC_DELEGATE_INTERNALMARGIN 3
|
|
|
|
|
|
|
|
class TOCDelegate : public QItemDelegate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TOCDelegate( QObject * parent = 0 )
|
|
|
|
: QItemDelegate( parent )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void drawDisplay( QPainter *painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const
|
|
|
|
{
|
|
|
|
if ( text.indexOf( TOC_SEPARATOR ) == -1 )
|
|
|
|
{
|
|
|
|
QItemDelegate::drawDisplay( painter, option, rect, text );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString realText = text.section( TOC_SEPARATOR, 1 );
|
2006-12-22 20:42:36 +00:00
|
|
|
if ( !Okular::Settings::tocPageColumn() )
|
|
|
|
{
|
|
|
|
QItemDelegate::drawDisplay( painter, option, rect, realText );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString page = text.section( TOC_SEPARATOR, 0, 0 );
|
2006-12-23 00:34:40 +00:00
|
|
|
QTextDocument document;
|
|
|
|
document.setPlainText( page );
|
|
|
|
document.setDefaultFont( option.font );
|
|
|
|
int margindelta = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 1;
|
|
|
|
int pageRectWidth = (int)document.size().width();
|
2006-12-22 19:26:28 +00:00
|
|
|
QRect newRect( rect );
|
2006-12-23 00:34:40 +00:00
|
|
|
QRect pageRect( rect );
|
|
|
|
pageRect.setWidth( pageRectWidth + 2 * margindelta );
|
2006-12-22 19:26:28 +00:00
|
|
|
newRect.setWidth( newRect.width() - pageRectWidth - TOC_DELEGATE_INTERNALMARGIN );
|
|
|
|
if ( option.direction == Qt::RightToLeft )
|
|
|
|
newRect.translate( pageRectWidth + TOC_DELEGATE_INTERNALMARGIN, 0 );
|
2006-12-23 00:34:40 +00:00
|
|
|
else
|
|
|
|
pageRect.translate( newRect.width() + TOC_DELEGATE_INTERNALMARGIN - 2 * margindelta, 0 );
|
2006-12-22 19:26:28 +00:00
|
|
|
QItemDelegate::drawDisplay( painter, option, newRect, realText );
|
2006-12-23 00:34:40 +00:00
|
|
|
QStyleOptionViewItemV2 newoption( option );
|
2006-12-22 19:26:28 +00:00
|
|
|
newoption.displayAlignment = ( option.displayAlignment & ~Qt::AlignHorizontal_Mask ) | Qt::AlignRight;
|
2006-12-23 00:34:40 +00:00
|
|
|
QItemDelegate::drawDisplay( painter, newoption, pageRect, page );
|
2006-12-22 19:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
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-12-22 19:26:28 +00:00
|
|
|
TOCItem( QTreeWidget *parent, TOCItem *after, Okular::Document *document, const QDomElement & e )
|
2006-05-19 11:32:08 +00:00
|
|
|
: QTreeWidgetItem( parent, after ), m_element( e )
|
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 )
|
2006-05-19 11:32:08 +00:00
|
|
|
: QTreeWidgetItem( parent, after ), m_element( e )
|
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" );
|
|
|
|
QString viewport = document->getMetaData( "NamedViewport", page ).toString();
|
|
|
|
if ( !viewport.isNull() )
|
|
|
|
m_viewport = Okular::DocumentViewport( viewport );
|
|
|
|
}
|
|
|
|
|
|
|
|
QString text = e.tagName();
|
2006-12-22 20:42:36 +00:00
|
|
|
if ( m_viewport.pageNumber != -1 )
|
2006-12-22 19:26:28 +00:00
|
|
|
text.prepend( QString::number( m_viewport.pageNumber + 1 ) + TOC_SEPARATOR );
|
|
|
|
setText( 0, text );
|
2004-12-12 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const QDomElement & element() const
|
|
|
|
{
|
|
|
|
return m_element;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2006-07-12 11:16:20 +00:00
|
|
|
setIcon( 0, selected ? KIcon( treeWidget()->layoutDirection() == Qt::RightToLeft ? "1leftarrow" : "1rightarrow" ) : QIcon() );
|
2006-06-20 13:59:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
private:
|
|
|
|
QDomElement m_element;
|
2006-12-22 19:26:28 +00:00
|
|
|
Okular::DocumentViewport m_viewport;
|
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 );
|
|
|
|
|
|
|
|
QHBoxLayout *searchlay = new QHBoxLayout();
|
2006-10-14 08:52:34 +00:00
|
|
|
searchlay->setMargin( 2 );
|
2006-05-28 17:27:24 +00:00
|
|
|
searchlay->setSpacing( 2 );
|
|
|
|
mainlay->addLayout( searchlay );
|
|
|
|
|
|
|
|
m_searchLine = new KTreeWidgetSearchLine( this );
|
|
|
|
searchlay->addWidget( m_searchLine );
|
|
|
|
|
|
|
|
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-22 19:26:28 +00:00
|
|
|
m_treeView->setItemDelegate( new TOCDelegate( 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
|
|
|
|
2006-09-21 08:45:36 +00:00
|
|
|
void TOC::notifySetup( const QVector< Okular::Page * > & /*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)
|
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 );
|
|
|
|
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
|
|
|
{
|
2006-12-22 19:26:28 +00:00
|
|
|
Okular::LinkGoto link( externalFileName, tocItem->viewport() );
|
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-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"
|