2021-05-24 07:25:56 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2006 Pino Toscano <pino@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
2006-12-26 12:01:30 +00:00
|
|
|
|
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
|
2022-01-26 16:27:42 +00:00
|
|
|
#include "gui/tocmodel.h"
|
2006-12-26 12:01:30 +00:00
|
|
|
#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
|
|
|
|
{
|
2022-01-26 16:27:42 +00:00
|
|
|
QVariant pageVariant = d->index.data(TOCModel::PageRole);
|
|
|
|
QVariant labelVariant = d->index.data(TOCModel::PageLabelRole);
|
2007-01-19 23:30:32 +00:00
|
|
|
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);
|
2022-03-08 10:10:43 +00:00
|
|
|
}
|
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"
|