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