File indexing completed on 2024-05-12 16:28:24

0001 /*
0002  * This file is part of the KDE project
0003  *
0004  * Copyright (C) 2013 Arjen Hiemstra <ahiemstra@heimr.nl>
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Library General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Library General Public License
0017  * along with this library; see the file COPYING.LIB.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA.
0020  *
0021  */
0022 
0023 #include "SpreadsheetContentsModelImpl.h"
0024 
0025 #include <QPainter>
0026 
0027 #include <KoZoomHandler.h>
0028 #include <sheets/part/Doc.h>
0029 #include <sheets/Map.h>
0030 #include <sheets/Sheet.h>
0031 #include <sheets/ui/SheetView.h>
0032 
0033 using namespace Calligra::Components;
0034 
0035 class SpreadsheetContentsModelImpl::Private
0036 {
0037 public:
0038     Private()
0039     { }
0040 
0041     QImage renderThumbnail(Calligra::Sheets::Sheet* sheet, int width, int height);
0042 
0043     Calligra::Sheets::Doc* document;
0044 
0045     QHash<int, QImage> thumbnails;
0046 
0047     QSize thumbnailSize;
0048 };
0049 
0050 SpreadsheetContentsModelImpl::SpreadsheetContentsModelImpl(KoDocument* document)
0051     : d{new Private}
0052 {
0053     d->document = qobject_cast<Calligra::Sheets::Doc*>(document);
0054     Q_ASSERT(d->document);
0055 }
0056 
0057 SpreadsheetContentsModelImpl::~SpreadsheetContentsModelImpl()
0058 {
0059     delete d;
0060 }
0061 
0062 int SpreadsheetContentsModelImpl::rowCount() const
0063 {
0064     return d->document->map()->count();
0065 }
0066 
0067 QVariant SpreadsheetContentsModelImpl::data(int index, ContentsModel::Role role) const
0068 {
0069     Calligra::Sheets::Sheet* sheet = d->document->map()->sheet(index);
0070     switch(role) {
0071         case ContentsModel::TitleRole:
0072             return sheet->sheetName();
0073         case ContentsModel::LevelRole:
0074             return 0;
0075         case ContentsModel::ThumbnailRole: {
0076             if(d->thumbnails.contains(index)) {
0077                 return d->thumbnails.value(index);
0078             }
0079 
0080             if(d->thumbnailSize.isNull()) {
0081                 return QImage{};
0082             }
0083 
0084             QImage thumbnail = d->renderThumbnail(sheet, d->thumbnailSize.width(), d->thumbnailSize.height());
0085             d->thumbnails.insert(index, thumbnail);
0086             return thumbnail;
0087         }
0088         case ContentsModel::ContentIndexRole:
0089             return index;
0090         default:
0091             return QVariant();
0092     }
0093 }
0094 
0095 void SpreadsheetContentsModelImpl::setThumbnailSize(const QSize& size)
0096 {
0097     d->thumbnailSize = size;
0098     d->thumbnails.clear();
0099 }
0100 
0101 QImage SpreadsheetContentsModelImpl::thumbnail(int index, int width) const
0102 {
0103     return d->renderThumbnail(d->document->map()->sheet(index), width, width);
0104 }
0105 
0106 QImage SpreadsheetContentsModelImpl::Private::renderThumbnail(Calligra::Sheets::Sheet* sheet, int width, int height)
0107 {
0108     QImage thumbnail{width, height, QImage::Format_RGB32};
0109     QRect rect{0, 0, width, height};
0110 
0111     QPainter p{&thumbnail};
0112 
0113     p.fillRect(rect, Qt::white);
0114 
0115     Calligra::Sheets::SheetView sheetView{sheet};
0116 
0117     qreal zoom = 0.5;
0118     KoZoomHandler zoomHandler;
0119     zoomHandler.setZoom(zoom);
0120     p.setClipRect(rect);
0121     p.scale(zoom, zoom);
0122     sheetView.setViewConverter(&zoomHandler);
0123 
0124     QRectF area = zoomHandler.viewToDocument(rect);
0125     QRect range = sheet->documentToCellCoordinates(area).adjusted(0, 0, 2, 2);
0126     sheetView.setPaintCellRange(range);
0127     sheetView.paintCells(p, area, QPointF(0,0));
0128 
0129     return thumbnail;
0130 }