2006-12-26 12:01:30 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* 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. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
2007-04-19 18:30:20 +00:00
|
|
|
#include "pageitemdelegate.h"
|
|
|
|
|
2006-12-26 12:01:30 +00:00
|
|
|
// qt/kde includes
|
2020-07-08 11:54:37 +00:00
|
|
|
#include <QApplication>
|
2006-12-26 14:35:27 +00:00
|
|
|
#include <QModelIndex>
|
2006-12-26 12:01:30 +00:00
|
|
|
#include <QTextDocument>
|
2020-07-08 11:54:37 +00:00
|
|
|
#include <QVariant>
|
2006-12-26 12:01:30 +00:00
|
|
|
|
|
|
|
// local includes
|
|
|
|
#include "settings.h"
|
|
|
|
|
|
|
|
#define PAGEITEMDELEGATE_INTERNALMARGIN 3
|
|
|
|
|
2006-12-26 14:35:27 +00:00
|
|
|
class PageItemDelegate::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Private()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex index;
|
|
|
|
};
|
|
|
|
|
2006-12-26 12:01:30 +00:00
|
|
|
PageItemDelegate::PageItemDelegate(QObject *parent)
|
2006-12-26 14:35:27 +00:00
|
|
|
: QItemDelegate(parent)
|
|
|
|
, d(new Private)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PageItemDelegate::~PageItemDelegate()
|
2006-12-26 12:01:30 +00:00
|
|
|
{
|
2006-12-26 14:35:27 +00:00
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PageItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
d->index = index;
|
|
|
|
QItemDelegate::paint(painter, option, index);
|
2006-12-26 12:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PageItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
|
|
|
|
{
|
2006-12-26 14:35:27 +00:00
|
|
|
QVariant pageVariant = d->index.data(PageRole);
|
2007-01-19 23:30:32 +00:00
|
|
|
QVariant labelVariant = d->index.data(PageLabelRole);
|
|
|
|
if ((labelVariant.type() != QVariant::String && !pageVariant.canConvert(QVariant::String)) || !Okular::Settings::tocPageColumn()) {
|
2006-12-26 12:01:30 +00:00
|
|
|
QItemDelegate::drawDisplay(painter, option, rect, text);
|
|
|
|
return;
|
|
|
|
}
|
2007-01-19 23:30:32 +00:00
|
|
|
QString label = labelVariant.toString();
|
|
|
|
QString page = label.isEmpty() ? pageVariant.toString() : label;
|
2006-12-26 12:01:30 +00:00
|
|
|
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)
|
2006-12-26 14:36:26 +00:00
|
|
|
newRect.translate(pageRectWidth + PAGEITEMDELEGATE_INTERNALMARGIN, 0);
|
|
|
|
else
|
|
|
|
pageRect.translate(newRect.width() + PAGEITEMDELEGATE_INTERNALMARGIN - 2 * margindelta, 0);
|
2006-12-26 14:35:27 +00:00
|
|
|
QItemDelegate::drawDisplay(painter, option, newRect, text);
|
2016-07-11 21:24:47 +00:00
|
|
|
QStyleOptionViewItem newoption(option);
|
2006-12-26 12:01:30 +00:00
|
|
|
newoption.displayAlignment = (option.displayAlignment & ~Qt::AlignHorizontal_Mask) | Qt::AlignRight;
|
|
|
|
QItemDelegate::drawDisplay(painter, newoption, pageRect, page);
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:00:07 +00:00
|
|
|
#include "moc_pageitemdelegate.cpp"
|