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

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * Copyright (C) 2013 Shantanu Tushar <shantanu@kde.org>
0005  * Copyright (C) 2013 Sujith Haridasan <sujith.h@gmail.com>
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public License
0018  * along with this library; see the file COPYING.LIB.  If not, write to
0019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  *
0022  */
0023 
0024 #include "CQTextDocumentModel.h"
0025 
0026 #include "CQImageProvider.h"
0027 
0028 #include <KWPage.h>
0029 #include <KWDocument.h>
0030 
0031 CQTextDocumentModel::CQTextDocumentModel(QObject* parent, KWDocument* document, KoShapeManager *shapemanager)
0032     : QAbstractListModel(parent), kw_document(document), kw_shapemanager(shapemanager), m_thumbnailSize(QSize(512, 512))
0033 {
0034 }
0035 
0036 int CQTextDocumentModel::rowCount(const QModelIndex& parent) const
0037 {
0038     if(parent.isValid()) {
0039         return 0;
0040     }
0041     return kw_document->pageManager()->pageCount();
0042 }
0043 
0044 QVariant CQTextDocumentModel::data(const QModelIndex& index, int role) const
0045 {
0046 
0047     if (!kw_document || !index.isValid()) {
0048        return QVariant();
0049     }
0050 
0051     if (CQImageProvider::s_imageProvider) {
0052         if (role == Qt::DecorationRole) {
0053             const QString id = kw_document->caption() + "textData" + QString::number(index.row());
0054             if (!CQImageProvider::s_imageProvider->containsId(id)) {
0055                 KWPage pagePreview = kw_document->pageManager()->page(index.row()+1);
0056                 QImage image = pagePreview.thumbnail(m_thumbnailSize, kw_shapemanager);
0057                 if (image.isNull() == true) {
0058                     return QVariant();
0059                 }
0060                 CQImageProvider::s_imageProvider->addImage(id, image);
0061             }
0062             return QString("image://%1/%2").arg(CQImageProvider::identificationString).arg(id);
0063         }
0064     }
0065     return QVariant();
0066 }
0067 
0068 QSize CQTextDocumentModel::thumbnailSize() const
0069 {
0070     return m_thumbnailSize;
0071 }
0072 
0073 void CQTextDocumentModel::setThumbnailSize(const QSize& newSize)
0074 {
0075     m_thumbnailSize = newSize;
0076     if (newSize.height() == 0) {
0077         m_thumbnailSize = QSize(512, 512);
0078     }
0079     if (CQImageProvider::s_imageProvider) {
0080         CQImageProvider::s_imageProvider->clearCache();
0081         dataChanged(index(0), index(kw_document->pageCount() - 1));
0082     }
0083     emit thumbnailSizeChanged();
0084 }