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 Hiemstra <ahiemstra@heimr.nl> 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 "CQSpreadsheetListModel.h" 0021 0022 #include <QPainter> 0023 0024 #include <KoZoomHandler.h> 0025 #include <Map.h> 0026 #include <Sheet.h> 0027 #include <ui/SheetView.h> 0028 0029 #include "CQSpreadsheetCanvas.h" 0030 0031 class CQSpreadsheetListModel::Private 0032 { 0033 public: 0034 Private() : canvas(0), map(0), thumbnailSize(64, 64) { } 0035 0036 CQSpreadsheetCanvas* canvas; 0037 Calligra::Sheets::Map* map; 0038 0039 QHash<int, QPixmap> thumbnails; 0040 QSize thumbnailSize; 0041 }; 0042 0043 CQSpreadsheetListModel::CQSpreadsheetListModel(QObject* parent) 0044 : QAbstractListModel(parent), d(new Private()) 0045 { 0046 QHash<int, QByteArray> roleNames; 0047 roleNames.insert(SheetNameRole, "sheetName"); 0048 roleNames.insert(ThumbnailRole, "thumbnail"); 0049 setRoleNames(roleNames); 0050 } 0051 0052 CQSpreadsheetListModel::~CQSpreadsheetListModel() 0053 { 0054 delete d; 0055 } 0056 0057 QVariant CQSpreadsheetListModel::data(const QModelIndex& index, int role) const 0058 { 0059 if (!index.isValid() || !d->map) { 0060 return QVariant(); 0061 } 0062 0063 switch(role) { 0064 case SheetNameRole: 0065 return d->map->sheet(index.row())->sheetName(); 0066 case ThumbnailRole: { 0067 if (d->thumbnails.contains(index.row())) { 0068 return d->thumbnails.value(index.row()); 0069 } 0070 0071 QPixmap thumbnail(d->thumbnailSize); 0072 QRect rect(QPoint(0,0), d->thumbnailSize); 0073 0074 QPainter p(&thumbnail); 0075 0076 p.fillRect(rect, Qt::white); 0077 0078 Calligra::Sheets::SheetView sheetView(d->map->sheet(index.row())); 0079 0080 qreal zoom = 0.5; 0081 KoZoomHandler zoomHandler; 0082 zoomHandler.setZoom(zoom); 0083 p.setClipRect(rect); 0084 p.scale(zoom, zoom); 0085 sheetView.setViewConverter(&zoomHandler); 0086 0087 QRectF area = zoomHandler.viewToDocument(rect); 0088 QRect range = sheetView.sheet()->documentToCellCoordinates(area).adjusted(0, 0, 2, 2); 0089 sheetView.setPaintCellRange(range); 0090 sheetView.paintCells(p, area, QPointF(0,0)); 0091 0092 d->thumbnails.insert(index.row(), thumbnail); 0093 return thumbnail; 0094 } 0095 default: 0096 break; 0097 } 0098 0099 return QVariant(); 0100 } 0101 0102 int CQSpreadsheetListModel::rowCount(const QModelIndex& parent) const 0103 { 0104 Q_UNUSED(parent); 0105 if (d->map) { 0106 return d->map->count(); 0107 } 0108 0109 return 0; 0110 } 0111 0112 QObject* CQSpreadsheetListModel::canvas() const 0113 { 0114 return d->canvas; 0115 } 0116 0117 QSize CQSpreadsheetListModel::thumbnailSize() const 0118 { 0119 return d->thumbnailSize; 0120 } 0121 0122 void CQSpreadsheetListModel::setCanvas(QObject* canvas) 0123 { 0124 if (d->canvas != canvas) { 0125 d->canvas = qobject_cast<CQSpreadsheetCanvas*>(canvas); 0126 Q_ASSERT(d->canvas); 0127 if (d->map) { 0128 beginRemoveRows(QModelIndex(), 0, d->map->count()); 0129 endRemoveRows(); 0130 } 0131 d->map = d->canvas->documentMap(); 0132 beginInsertRows(QModelIndex(), 0, d->map->count()); 0133 endInsertRows(); 0134 emit canvasChanged(); 0135 } 0136 } 0137 0138 void CQSpreadsheetListModel::setThumbnailSize(const QSize& size) 0139 { 0140 if (size != d->thumbnailSize) { 0141 d->thumbnailSize = size; 0142 d->thumbnails.clear(); 0143 emit thumbnailSizeChanged(); 0144 } 0145 }