pageitemdelegate.cpp 2.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/***************************************************************************
 *   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>
13
#include <QModelIndex>
14
#include <QTextDocument>
15
#include <qvariant.h>
16 17 18 19 20 21 22

// local includes
#include "pageitemdelegate.h"
#include "settings.h"

#define PAGEITEMDELEGATE_INTERNALMARGIN 3

23 24 25 26 27 28 29 30 31
class PageItemDelegate::Private
{
    public:
        Private()
        {}

        QModelIndex index;
};

32
PageItemDelegate::PageItemDelegate( QObject * parent )
33 34 35 36 37
    : QItemDelegate( parent ), d( new Private )
{
}

PageItemDelegate::~PageItemDelegate()
38
{
39 40 41 42 43 44 45
    delete d;
}

void PageItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    d->index = index;
    QItemDelegate::paint( painter, option, index );
46 47 48 49
}

void PageItemDelegate::drawDisplay( QPainter *painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const
{
50 51
    QVariant pageVariant = d->index.data( PageRole );
    if ( !pageVariant.canConvert( QVariant::String ) || !Okular::Settings::tocPageColumn() )
52 53 54 55
    {
        QItemDelegate::drawDisplay( painter, option, rect, text );
        return;
    }
56
    QString page = pageVariant.toString();
57 58 59 60 61 62 63 64 65 66
    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 )
Pino Toscano's avatar
Pino Toscano committed
67 68 69
        newRect.translate( pageRectWidth + PAGEITEMDELEGATE_INTERNALMARGIN, 0 );
    else
        pageRect.translate( newRect.width() + PAGEITEMDELEGATE_INTERNALMARGIN - 2 * margindelta, 0 );
70
    QItemDelegate::drawDisplay( painter, option, newRect, text );
71 72 73 74 75 76
    QStyleOptionViewItemV2 newoption( option );
    newoption.displayAlignment = ( option.displayAlignment & ~Qt::AlignHorizontal_Mask ) | Qt::AlignRight;
    QItemDelegate::drawDisplay( painter, newoption, pageRect, page );
}

#include "pageitemdelegate.moc"