2004-09-21 20:38:22 +00:00
|
|
|
/***************************************************************************
|
2013-03-14 22:09:07 +00:00
|
|
|
* Copyright (C) 2004-2006 by Albert Astals Cid <aacid@kde.org> *
|
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>
|
2007-09-09 16:15:33 +00:00
|
|
|
#include <qtreeview.h>
|
|
|
|
|
|
|
|
#include <klineedit.h>
|
2004-09-21 20:38:22 +00:00
|
|
|
|
2004-12-12 11:11:57 +00:00
|
|
|
// local includes
|
2007-09-09 16:15:33 +00:00
|
|
|
#include "ktreeviewsearchline.h"
|
2006-12-26 12:01:30 +00:00
|
|
|
#include "pageitemdelegate.h"
|
2007-09-09 16:15:33 +00:00
|
|
|
#include "tocmodel.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"
|
2009-11-19 17:26:41 +00:00
|
|
|
#include "settings.h"
|
2004-09-21 20:38:22 +00:00
|
|
|
|
2012-08-16 15:37:39 +00:00
|
|
|
TOC::TOC(QWidget *parent, Okular::Document *document) : QWidget(parent), m_document(document)
|
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
|
|
|
|
2007-09-09 16:15:33 +00:00
|
|
|
m_searchLine = new KTreeViewSearchLine( this );
|
2007-08-24 14:20:03 +00:00
|
|
|
mainlay->addWidget( m_searchLine );
|
2009-11-19 17:26:41 +00:00
|
|
|
m_searchLine->setCaseSensitivity( Okular::Settings::self()->contentsSearchCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive );
|
|
|
|
m_searchLine->setRegularExpression( Okular::Settings::self()->contentsSearchRegularExpression() );
|
2011-07-31 19:22:04 +00:00
|
|
|
connect( m_searchLine, SIGNAL(searchOptionsChanged()), this, SLOT(saveSearchOptions()) );
|
2006-05-28 17:27:24 +00:00
|
|
|
|
2007-09-09 16:15:33 +00:00
|
|
|
m_treeView = new QTreeView( this );
|
2006-05-28 17:27:24 +00:00
|
|
|
mainlay->addWidget( m_treeView );
|
2007-09-09 16:15:33 +00:00
|
|
|
m_model = new TOCModel( document, m_treeView );
|
|
|
|
m_treeView->setModel( m_model );
|
2006-05-28 17:27:24 +00:00
|
|
|
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 );
|
2011-07-31 19:22:04 +00:00
|
|
|
connect( m_treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(slotExecuted(QModelIndex)) );
|
|
|
|
connect( m_treeView, SIGNAL(activated(QModelIndex)), this, SLOT(slotExecuted(QModelIndex)) );
|
2007-09-09 16:15:33 +00:00
|
|
|
m_searchLine->addTreeView( m_treeView );
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2006-02-18 12:06:52 +00:00
|
|
|
TOC::~TOC()
|
|
|
|
{
|
|
|
|
m_document->removeObserver( this );
|
|
|
|
}
|
|
|
|
|
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
|
2007-09-09 16:15:33 +00:00
|
|
|
m_model->clear();
|
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();
|
2013-01-18 17:02:07 +00:00
|
|
|
if ( !syn )
|
|
|
|
{
|
|
|
|
if ( m_document->isOpened() )
|
|
|
|
{
|
|
|
|
// Make sure we clear the reload old model data
|
|
|
|
m_model->setOldModelData( 0, QVector<QModelIndex>() );
|
|
|
|
}
|
|
|
|
emit hasTOC( false );
|
|
|
|
return;
|
|
|
|
}
|
2004-12-12 11:11:57 +00:00
|
|
|
|
2007-09-09 16:15:33 +00:00
|
|
|
m_model->fill( syn );
|
2007-12-26 16:01:54 +00:00
|
|
|
emit hasTOC( !m_model->isEmpty() );
|
2004-12-12 11:11:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-16 15:37:39 +00:00
|
|
|
void TOC::notifyCurrentPageChanged( int, int )
|
2006-06-20 13:59:08 +00:00
|
|
|
{
|
2007-09-09 16:15:33 +00:00
|
|
|
m_model->setCurrentViewport( m_document->viewport() );
|
2006-06-20 13:59:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-09 22:31:23 +00:00
|
|
|
void TOC::prepareForReload()
|
|
|
|
{
|
2013-01-15 00:20:59 +00:00
|
|
|
if( m_model->isEmpty() )
|
|
|
|
return;
|
|
|
|
|
2013-01-09 22:31:23 +00:00
|
|
|
const QVector<QModelIndex> list = expandedNodes();
|
|
|
|
TOCModel *m = m_model;
|
|
|
|
m_model = new TOCModel( m_document, m_treeView );
|
|
|
|
m_model->setOldModelData( m, list );
|
2013-10-01 14:19:09 +00:00
|
|
|
m->setParent( 0 );
|
2013-01-18 17:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TOC::rollbackReload()
|
|
|
|
{
|
2013-06-02 13:55:49 +00:00
|
|
|
if( !m_model->hasOldModelData() )
|
|
|
|
return;
|
|
|
|
|
2013-01-18 17:02:07 +00:00
|
|
|
TOCModel *m = m_model;
|
|
|
|
m_model = m->clearOldModelData();
|
2013-10-01 14:19:09 +00:00
|
|
|
m_model->setParent( m_treeView );
|
2013-01-18 17:02:07 +00:00
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TOC::finishReload()
|
|
|
|
{
|
2013-01-09 22:31:23 +00:00
|
|
|
m_treeView->setModel( m_model );
|
2013-10-01 14:19:09 +00:00
|
|
|
m_model->setParent( m_treeView );
|
2013-01-09 22:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QVector<QModelIndex> TOC::expandedNodes( const QModelIndex &parent ) const
|
|
|
|
{
|
|
|
|
QVector<QModelIndex> list;
|
|
|
|
for ( int i = 0; i < m_model->rowCount( parent ); i++ )
|
|
|
|
{
|
|
|
|
const QModelIndex index = m_model->index( i, 0, parent );
|
|
|
|
if ( m_treeView->isExpanded( index ) )
|
|
|
|
{
|
|
|
|
list << index;
|
|
|
|
}
|
|
|
|
if ( m_model->hasChildren( index ) )
|
|
|
|
{
|
|
|
|
list << expandedNodes( index );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
2006-06-20 13:59:08 +00:00
|
|
|
|
2006-12-22 20:42:36 +00:00
|
|
|
void TOC::reparseConfig()
|
|
|
|
{
|
2009-11-19 17:26:41 +00:00
|
|
|
m_searchLine->setCaseSensitivity( Okular::Settings::contentsSearchCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive );
|
|
|
|
m_searchLine->setRegularExpression( Okular::Settings::contentsSearchRegularExpression() );
|
2006-12-22 20:42:36 +00:00
|
|
|
m_treeView->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-09 16:15:33 +00:00
|
|
|
void TOC::slotExecuted( const QModelIndex &index )
|
2004-09-21 20:38:22 +00:00
|
|
|
{
|
2007-09-09 16:15:33 +00:00
|
|
|
if ( !index.isValid() )
|
2005-06-13 16:35:31 +00:00
|
|
|
return;
|
2005-07-15 17:32:46 +00:00
|
|
|
|
2008-11-30 15:48:01 +00:00
|
|
|
QString url = m_model->urlForIndex( index );
|
|
|
|
if ( !url.isEmpty() )
|
|
|
|
{
|
2014-08-10 18:36:41 +00:00
|
|
|
Okular::BrowseAction action( QUrl::fromLocalFile( url ) );
|
2008-11-30 15:48:01 +00:00
|
|
|
m_document->processAction( &action );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-09 16:15:33 +00:00
|
|
|
QString externalFileName = m_model->externalFileNameForIndex( index );
|
|
|
|
Okular::DocumentViewport viewport = m_model->viewportForIndex( index );
|
2005-07-15 17:32:46 +00:00
|
|
|
if ( !externalFileName.isEmpty() )
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2007-09-09 16:15:33 +00:00
|
|
|
Okular::GotoAction action( externalFileName, viewport );
|
2007-04-20 12:49:17 +00:00
|
|
|
m_document->processAction( &action );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2008-11-30 11:28:08 +00:00
|
|
|
else if ( viewport.isValid() )
|
2005-01-07 13:07:29 +00:00
|
|
|
{
|
2007-09-09 16:15:33 +00:00
|
|
|
m_document->setViewport( viewport );
|
2005-01-07 13:07:29 +00:00
|
|
|
}
|
2004-09-21 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2009-11-19 17:26:41 +00:00
|
|
|
void TOC::saveSearchOptions()
|
|
|
|
{
|
|
|
|
Okular::Settings::setContentsSearchRegularExpression( m_searchLine->regularExpression() );
|
|
|
|
Okular::Settings::setContentsSearchCaseSensitive( m_searchLine->caseSensitivity() == Qt::CaseSensitive ? true : false );
|
|
|
|
Okular::Settings::self()->writeConfig();
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:00:07 +00:00
|
|
|
#include "moc_toc.cpp"
|