1 /***************************************************************************
2 * Copyright (C) 2006 by Pino Toscano <pino@kde.org> *
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"
13 #include <qapplication.h>
14 #include <QItemDelegate>
15 #include <QModelIndex>
16 #include <QTextDocument>
22 #define PAGEITEMDELEGATE_INTERNALMARGIN 3
24 class PageItemDelegate::Private
33 PageItemDelegate::PageItemDelegate( QObject
* parent
)
34 : QItemDelegate( parent
), d( new Private
)
38 PageItemDelegate::~PageItemDelegate()
43 void PageItemDelegate::paint( QPainter
*painter
, const QStyleOptionViewItem
& option
, const QModelIndex
& index
) const
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
);
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 );
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"