add a small widget for showing an animation from the "animations" category of the...
[kdegraphics.git] / okular / ui / pageitemdelegate.cpp
blob126b3d465830204fde96cf1f26dbf93161495ce5
1 /***************************************************************************
2 * Copyright (C) 2006 by Pino Toscano <pino@kde.org> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 ***************************************************************************/
10 #include "pageitemdelegate.h"
12 // qt/kde includes
13 #include <qapplication.h>
14 #include <QItemDelegate>
15 #include <QModelIndex>
16 #include <QTextDocument>
17 #include <qvariant.h>
19 // local includes
20 #include "settings.h"
22 #define PAGEITEMDELEGATE_INTERNALMARGIN 3
24 class PageItemDelegate::Private
26 public:
27 Private()
30 QModelIndex index;
33 PageItemDelegate::PageItemDelegate( QObject * parent )
34 : QItemDelegate( parent ), d( new Private )
38 PageItemDelegate::~PageItemDelegate()
40 delete d;
43 void PageItemDelegate::paint( QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
45 d->index = index;
46 QItemDelegate::paint( painter, option, index );
49 void PageItemDelegate::drawDisplay( QPainter *painter, const QStyleOptionViewItem & option, const QRect & rect, const QString & text ) const
51 QVariant pageVariant = d->index.data( PageRole );
52 QVariant labelVariant = d->index.data( PageLabelRole );
53 if ( ( labelVariant.type() != QVariant::String && !pageVariant.canConvert( QVariant::String ) ) || !Okular::Settings::tocPageColumn() )
55 QItemDelegate::drawDisplay( painter, option, rect, text );
56 return;
58 QString label = labelVariant.toString();
59 QString page = label.isEmpty() ? pageVariant.toString() : label;
60 QTextDocument document;
61 document.setPlainText( page );
62 document.setDefaultFont( option.font );
63 int margindelta = QApplication::style()->pixelMetric( QStyle::PM_FocusFrameHMargin ) + 1;
64 int pageRectWidth = (int)document.size().width();
65 QRect newRect( rect );
66 QRect pageRect( rect );
67 pageRect.setWidth( pageRectWidth + 2 * margindelta );
68 newRect.setWidth( newRect.width() - pageRectWidth - PAGEITEMDELEGATE_INTERNALMARGIN );
69 if ( option.direction == Qt::RightToLeft )
70 newRect.translate( pageRectWidth + PAGEITEMDELEGATE_INTERNALMARGIN, 0 );
71 else
72 pageRect.translate( newRect.width() + PAGEITEMDELEGATE_INTERNALMARGIN - 2 * margindelta, 0 );
73 QItemDelegate::drawDisplay( painter, option, newRect, text );
74 QStyleOptionViewItemV2 newoption( option );
75 newoption.displayAlignment = ( option.displayAlignment & ~Qt::AlignHorizontal_Mask ) | Qt::AlignRight;
76 QItemDelegate::drawDisplay( painter, newoption, pageRect, page );
79 #include "pageitemdelegate.moc"