File indexing completed on 2024-05-05 17:09:08

0001 /*
0002  * This file is part of the KDE project
0003  * Copyright (C) 2013 Arjen-Wander Hiemstra <aw.hiemstra@gmail.com>
0004  *
0005  * This program is free software; you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation; either version 2 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "CQPresentationModel.h"
0021 
0022 #include <stage/part/KPrDocument.h>
0023 
0024 #include "CQPresentationCanvas.h"
0025 
0026 class CQPresentationModel::Private
0027 {
0028 public:
0029     Private() : canvas(0), document(0), thumbnailSize(64, 64) { }
0030 
0031     CQPresentationCanvas* canvas;
0032     KPrDocument* document;
0033 
0034     QHash<int, QPixmap> thumbnails;
0035     QSizeF thumbnailSize;
0036 };
0037 
0038 CQPresentationModel::CQPresentationModel(QObject* parent)
0039     : QAbstractListModel(parent), d(new Private)
0040 {
0041     QHash<int, QByteArray> roles;
0042     roles.insert(ThumbnailRole, "thumbnail");
0043     setRoleNames(roles);
0044 }
0045 
0046 CQPresentationModel::~CQPresentationModel()
0047 {
0048     delete d;
0049 
0050 }
0051 
0052 QVariant CQPresentationModel::data(const QModelIndex& index, int role) const
0053 {
0054     if (!index.isValid() && d->document) {
0055         return QVariant();
0056     }
0057 
0058     switch(role) {
0059         case ThumbnailRole: {
0060             if (d->thumbnails.contains(index.row())) {
0061                 QPixmap thumb = d->thumbnails.value(index.row());
0062 
0063                 if (!thumb.isNull()) {
0064                     return thumb;
0065                 }
0066 
0067                 d->thumbnails.remove(index.row());
0068             }
0069 
0070             QPixmap pixmap = d->document->pageThumbnail(d->document->pageByIndex(index.row(), false), d->thumbnailSize.toSize());
0071             d->thumbnails.insert(index.row(), pixmap);
0072             return pixmap;
0073         }
0074         default:
0075             break;
0076     }
0077 
0078     return QVariant();
0079 }
0080 
0081 QPixmap CQPresentationModel::thumbnail(int index) const
0082 {
0083     return data(this->index(index), ThumbnailRole).value<QPixmap>();
0084 }
0085 
0086 int CQPresentationModel::rowCount(const QModelIndex& parent) const
0087 {
0088     Q_UNUSED(parent);
0089     if (d->document) {
0090         return d->document->pageCount();
0091     }
0092 
0093     return 0;
0094 }
0095 
0096 QDeclarativeItem* CQPresentationModel::canvas() const
0097 {
0098     return d->canvas;
0099 }
0100 
0101 QSizeF CQPresentationModel::thumbnailSize() const
0102 {
0103     return d->thumbnailSize;
0104 }
0105 
0106 void CQPresentationModel::setCanvas(QDeclarativeItem* canvas)
0107 {
0108     if (d->canvas != canvas && qobject_cast<CQPresentationCanvas*>(canvas))
0109     {
0110         d->canvas = qobject_cast<CQPresentationCanvas*>(canvas);
0111         connect(d->canvas, SIGNAL(sourceChanged()), SLOT(canvasSourceChanged()));
0112         canvasSourceChanged();
0113         emit canvasChanged();
0114     }
0115 }
0116 
0117 void CQPresentationModel::setThumbnailSize(const QSizeF& size)
0118 {
0119     if (size != d->thumbnailSize) {
0120         d->thumbnailSize = size;
0121         d->thumbnails.clear(); //Size changed, so cache is invalid
0122 
0123         if (d->document) {
0124             emit dataChanged(index(0, 0), index(d->document->pageCount() - 1));
0125         }
0126 
0127         emit thumbnailSizeChanged();
0128     }
0129 }
0130 
0131 void CQPresentationModel::canvasSourceChanged()
0132 {
0133     if (d->canvas->document()) {
0134         if (d->document) {
0135             beginRemoveRows(QModelIndex(), 0, d->document->pageCount());
0136             endRemoveRows();
0137         }
0138         d->document = d->canvas->document();
0139         beginInsertRows(QModelIndex(), 0, d->document->pageCount());
0140         endInsertRows();
0141     }
0142 }