QVariant TextContentsModelImpl::data(int index, Calligra::Components::ContentsModel::Role role) const
{
    if(d->entries.count() > 0) {
        auto entry = d->entries.at(index);
        switch(role) {
            case ContentsModel::TitleRole:
                return entry.title;
            case ContentsModel::LevelRole:
                return entry.level;
            case ContentsModel::ThumbnailRole: {
                if(d->thumbnails.contains(entry.pageNumber)) {
                    return d->thumbnails.value(entry.pageNumber);
                }

                if(d->thumbnailSize.isNull()) {
                    return QImage{};
                }

                QImage thumb = entry.page->thumbnail(d->thumbnailSize, d->canvas->shapeManager());
                d->thumbnails.insert(entry.pageNumber, thumb);
                return thumb;
            }
            case ContentsModel::ContentIndexRole: {
                return entry.pageNumber - 1;
            }
            default:
                return QVariant();
        }
    }

    //Fallback behaviour when we don't have a ToC
    KWPage page = d->document->pageManager()->page(index + 1);
    if(!page.isValid())
        return QVariant();

    switch(role) {
        case ContentsModel::TitleRole:
            return QString(i18n("Page %1")).arg(page.pageNumber());
        case ContentsModel::LevelRole:
            return 0;
        case ContentsModel::ThumbnailRole: {
            if(d->thumbnails.contains(index)) {
                return d->thumbnails.value(index);
            }

            if(d->thumbnailSize.isNull()) {
                return QImage{};
            }

            QImage thumb = page.thumbnail(d->thumbnailSize, d->canvas->shapeManager());
            d->thumbnails.insert(index, thumb);
            return thumb;
        }
        case ContentsModel::ContentIndexRole: {
            return index;
        }
        default:
            return QVariant();
    }
}
QImage TextContentsModelImpl::thumbnail(int index, int width) const
{
    KWPage page = d->document->pageManager()->page( index + 1 );
    return page.thumbnail(QSize{ width, int((page.height() / page.width()) * width)}, d->canvas->shapeManager());
}