diff --git a/CMakeLists.txt b/CMakeLists.txt index 57fdb593a..6cdc328e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,7 @@ set(okularpart_SRCS ui/annotationwidgets.cpp ui/minibar.cpp ui/newstuff.cpp + ui/pageitemdelegate.cpp ui/pagepainter.cpp ui/pagesizelabel.cpp ui/pageviewannotator.cpp diff --git a/ui/pageitemdelegate.cpp b/ui/pageitemdelegate.cpp new file mode 100644 index 000000000..17c0e0d02 --- /dev/null +++ b/ui/pageitemdelegate.cpp @@ -0,0 +1,59 @@ +/*************************************************************************** + * Copyright (C) 2006 by Pino Toscano * + * * + * 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. * + ***************************************************************************/ + +// qt/kde includes +#include +#include +#include + +// local includes +#include "pageitemdelegate.h" +#include "settings.h" + +#define PAGEITEMDELEGATE_INTERNALMARGIN 3 + +PageItemDelegate::PageItemDelegate( QObject * parent ) + : QItemDelegate( parent ) +{ +} + +void PageItemDelegate::drawDisplay( QPainter *painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const +{ + if ( text.indexOf( PAGEITEMDELEGATE_SEPARATOR ) == -1 ) + { + QItemDelegate::drawDisplay( painter, option, rect, text ); + return; + } + QString realText = text.section( PAGEITEMDELEGATE_SEPARATOR, 1 ); + if ( !Okular::Settings::tocPageColumn() ) + { + QItemDelegate::drawDisplay( painter, option, rect, realText ); + return; + } + QString page = text.section( PAGEITEMDELEGATE_SEPARATOR, 0, 0 ); + QTextDocument document; + document.setPlainText( page ); + document.setDefaultFont( option.font ); + int margindelta = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 1; + int pageRectWidth = (int)document.size().width(); + QRect newRect( rect ); + QRect pageRect( rect ); + pageRect.setWidth( pageRectWidth + 2 * margindelta ); + newRect.setWidth( newRect.width() - pageRectWidth - PAGEITEMDELEGATE_INTERNALMARGIN ); + if ( option.direction == Qt::RightToLeft ) + newRect.translate( pageRectWidth + PAGEITEMDELEGATE_INTERNALMARGIN, 0 ); + else + pageRect.translate( newRect.width() + PAGEITEMDELEGATE_INTERNALMARGIN - 2 * margindelta, 0 ); + QItemDelegate::drawDisplay( painter, option, newRect, realText ); + QStyleOptionViewItemV2 newoption( option ); + newoption.displayAlignment = ( option.displayAlignment & ~Qt::AlignHorizontal_Mask ) | Qt::AlignRight; + QItemDelegate::drawDisplay( painter, newoption, pageRect, page ); +} + +#include "pageitemdelegate.moc" diff --git a/ui/pageitemdelegate.h b/ui/pageitemdelegate.h new file mode 100644 index 000000000..30a64ce7a --- /dev/null +++ b/ui/pageitemdelegate.h @@ -0,0 +1,28 @@ +/*************************************************************************** + * Copyright (C) 2006 by Pino Toscano * + * * + * 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. * + ***************************************************************************/ + +#ifndef PAGEITEMDELEGATE_H +#define PAGEITEMDELEGATE_H + +#include + +#define PAGEITEMDELEGATE_SEPARATOR "@@@@@@@@@@" + +class PageItemDelegate : public QItemDelegate +{ + Q_OBJECT + + public: + PageItemDelegate( QObject * parent = 0 ); + + protected: + virtual void drawDisplay( QPainter *painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const; +}; + +#endif diff --git a/ui/toc.cpp b/ui/toc.cpp index 48af0e64f..417f87f81 100644 --- a/ui/toc.cpp +++ b/ui/toc.cpp @@ -8,72 +8,22 @@ ***************************************************************************/ // qt/kde includes -#include #include #include -#include #include #include -#include #include #include #include #include // local includes +#include "pageitemdelegate.h" #include "toc.h" #include "core/document.h" #include "core/page.h" -#include "settings.h" #include "core/link.h" -#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 ); - if ( !Okular::Settings::tocPageColumn() ) - { - QItemDelegate::drawDisplay( painter, option, rect, realText ); - return; - } - QString page = text.section( TOC_SEPARATOR, 0, 0 ); - QTextDocument document; - document.setPlainText( page ); - document.setDefaultFont( option.font ); - int margindelta = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 1; - int pageRectWidth = (int)document.size().width(); - QRect newRect( rect ); - QRect pageRect( rect ); - pageRect.setWidth( pageRectWidth + 2 * margindelta ); - newRect.setWidth( newRect.width() - pageRectWidth - TOC_DELEGATE_INTERNALMARGIN ); - if ( option.direction == Qt::RightToLeft ) - newRect.translate( pageRectWidth + TOC_DELEGATE_INTERNALMARGIN, 0 ); - else - pageRect.translate( newRect.width() + TOC_DELEGATE_INTERNALMARGIN - 2 * margindelta, 0 ); - QItemDelegate::drawDisplay( painter, option, newRect, realText ); - QStyleOptionViewItemV2 newoption( option ); - newoption.displayAlignment = ( option.displayAlignment & ~Qt::AlignHorizontal_Mask ) | Qt::AlignRight; - QItemDelegate::drawDisplay( painter, newoption, pageRect, page ); - } - -}; - class TOCItem : public QTreeWidgetItem { public: @@ -108,7 +58,7 @@ class TOCItem : public QTreeWidgetItem QString text = e.tagName(); if ( m_viewport.pageNumber != -1 ) - text.prepend( QString::number( m_viewport.pageNumber + 1 ) + TOC_SEPARATOR ); + text.prepend( QString::number( m_viewport.pageNumber + 1 ) + PAGEITEMDELEGATE_SEPARATOR ); setText( 0, text ); } @@ -153,7 +103,7 @@ TOC::TOC(QWidget *parent, Okular::Document *document) : QWidget(parent), m_docum m_treeView->setSortingEnabled( false ); m_treeView->setRootIsDecorated( true ); m_treeView->setAlternatingRowColors( true ); - m_treeView->setItemDelegate( new TOCDelegate( m_treeView ) ); + m_treeView->setItemDelegate( new PageItemDelegate( m_treeView ) ); m_treeView->header()->hide(); m_treeView->setSelectionBehavior( QAbstractItemView::SelectRows ); connect( m_treeView, SIGNAL( itemClicked( QTreeWidgetItem *, int ) ), this, SLOT( slotExecuted( QTreeWidgetItem * ) ) );