File indexing completed on 2024-05-12 13:04:46

0001 /*
0002  * This file is part of the KDE project
0003  * SPDX-FileCopyrightText: 2013 Arjen-Wander Hiemstra <aw.hiemstra@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #include "CQPresentationModel.h"
0009 
0010 #include <stage/part/KPrDocument.h>
0011 
0012 #include "CQPresentationCanvas.h"
0013 
0014 class CQPresentationModel::Private
0015 {
0016 public:
0017     Private() : canvas(0), document(0), thumbnailSize(64, 64) { }
0018 
0019     CQPresentationCanvas* canvas;
0020     KPrDocument* document;
0021 
0022     QHash<int, QPixmap> thumbnails;
0023     QSizeF thumbnailSize;
0024 };
0025 
0026 CQPresentationModel::CQPresentationModel(QObject* parent)
0027     : QAbstractListModel(parent), d(new Private)
0028 {
0029     QHash<int, QByteArray> roles;
0030     roles.insert(ThumbnailRole, "thumbnail");
0031     setRoleNames(roles);
0032 }
0033 
0034 CQPresentationModel::~CQPresentationModel()
0035 {
0036     delete d;
0037 
0038 }
0039 
0040 QVariant CQPresentationModel::data(const QModelIndex& index, int role) const
0041 {
0042     if (!index.isValid() && d->document) {
0043         return QVariant();
0044     }
0045 
0046     switch(role) {
0047         case ThumbnailRole: {
0048             if (d->thumbnails.contains(index.row())) {
0049                 QPixmap thumb = d->thumbnails.value(index.row());
0050 
0051                 if (!thumb.isNull()) {
0052                     return thumb;
0053                 }
0054 
0055                 d->thumbnails.remove(index.row());
0056             }
0057 
0058             QPixmap pixmap = d->document->pageThumbnail(d->document->pageByIndex(index.row(), false), d->thumbnailSize.toSize());
0059             d->thumbnails.insert(index.row(), pixmap);
0060             return pixmap;
0061         }
0062         default:
0063             break;
0064     }
0065 
0066     return QVariant();
0067 }
0068 
0069 QPixmap CQPresentationModel::thumbnail(int index) const
0070 {
0071     return data(this->index(index), ThumbnailRole).value<QPixmap>();
0072 }
0073 
0074 int CQPresentationModel::rowCount(const QModelIndex& parent) const
0075 {
0076     Q_UNUSED(parent);
0077     if (d->document) {
0078         return d->document->pageCount();
0079     }
0080 
0081     return 0;
0082 }
0083 
0084 QDeclarativeItem* CQPresentationModel::canvas() const
0085 {
0086     return d->canvas;
0087 }
0088 
0089 QSizeF CQPresentationModel::thumbnailSize() const
0090 {
0091     return d->thumbnailSize;
0092 }
0093 
0094 void CQPresentationModel::setCanvas(QDeclarativeItem* canvas)
0095 {
0096     if (d->canvas != canvas && qobject_cast<CQPresentationCanvas*>(canvas))
0097     {
0098         d->canvas = qobject_cast<CQPresentationCanvas*>(canvas);
0099         connect(d->canvas, SIGNAL(sourceChanged()), SLOT(canvasSourceChanged()));
0100         canvasSourceChanged();
0101         emit canvasChanged();
0102     }
0103 }
0104 
0105 void CQPresentationModel::setThumbnailSize(const QSizeF& size)
0106 {
0107     if (size != d->thumbnailSize) {
0108         d->thumbnailSize = size;
0109         d->thumbnails.clear(); //Size changed, so cache is invalid
0110 
0111         if (d->document) {
0112             emit dataChanged(index(0, 0), index(d->document->pageCount() - 1));
0113         }
0114 
0115         emit thumbnailSizeChanged();
0116     }
0117 }
0118 
0119 void CQPresentationModel::canvasSourceChanged()
0120 {
0121     if (d->canvas->document()) {
0122         if (d->document) {
0123             beginRemoveRows(QModelIndex(), 0, d->document->pageCount());
0124             endRemoveRows();
0125         }
0126         d->document = d->canvas->document();
0127         beginInsertRows(QModelIndex(), 0, d->document->pageCount());
0128         endInsertRows();
0129     }
0130 }