pageitemdelegate.cpp 2.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/***************************************************************************
 *   Copyright (C) 2006 by Pino Toscano <pino@kde.org>                     *
 *                                                                         *
 *   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 <qapplication.h>
#include <QItemDelegate>
#include <QTextDocument>

// 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"