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

0001 /*
0002  * This file is part of Calligra
0003  *
0004  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
0005  *
0006  * Contact: Thorsten Zachmann thorsten.zachmann@nokia.com
0007  *
0008  * This library is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Lesser General Public License
0010  * version 2.1 as published by the Free Software Foundation.
0011  *
0012  * This library is distributed in the hope that it will be useful, but
0013  * WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020  * 02110-1301 USA
0021  *
0022  */
0023 
0024 #include "CSThumbProviderTables.h"
0025 
0026 #include <sheets/part/Doc.h>
0027 #include <sheets/Sheet.h>
0028 #include <sheets/Map.h>
0029 #include <sheets/PrintSettings.h>
0030 #include <sheets/ui/SheetView.h>
0031 #include <sheets/SheetPrint.h>
0032 #include <KoZoomHandler.h>
0033 #include <KoShapePainter.h>
0034 #include <KoPAUtil.h>
0035 #include <QPainter>
0036 
0037 CSThumbProviderTables::CSThumbProviderTables(Calligra::Sheets::Doc *doc)
0038 : m_doc(doc)
0039 {
0040 }
0041 
0042 CSThumbProviderTables::~CSThumbProviderTables()
0043 {
0044 }
0045 
0046 QVector<QImage> CSThumbProviderTables::createThumbnails(const QSize &thumbSize)
0047 {
0048     QVector<QImage> thumbnails;
0049     if (0 != m_doc->map()) {
0050         foreach(Calligra::Sheets::Sheet* sheet, m_doc->map()->sheetList()) {
0051             QImage thumbnail(thumbSize, QImage::Format_RGB32);
0052             thumbnail.fill(QColor(Qt::white).rgb());
0053             QPainter p(&thumbnail);
0054 
0055             KoPageLayout pageLayout;
0056             pageLayout.format = KoPageFormat::IsoA4Size;
0057             pageLayout.leftMargin = 0;
0058             pageLayout.rightMargin = 0;
0059             pageLayout.topMargin = 0;
0060             pageLayout.bottomMargin = 0;
0061             sheet->printSettings()->setPageLayout(pageLayout);
0062             sheet->print()->setSettings(*sheet->printSettings(), true);
0063 
0064             Calligra::Sheets::SheetView sheetView(sheet);
0065 
0066             // only paint first page for now
0067             KoZoomHandler zoomHandler;
0068             KoPAUtil::setZoom(pageLayout, thumbSize, zoomHandler);
0069             sheetView.setViewConverter(&zoomHandler);
0070 
0071             QRect range(sheet->print()->cellRange(1));
0072             // paint also half cells on page edge
0073             range.setWidth(range.width() + 1);
0074             sheetView.setPaintCellRange(range); // first page
0075 
0076             QRect pRect(KoPAUtil::pageRect(pageLayout, thumbSize, zoomHandler));
0077 
0078             p.setClipRect(pRect);
0079             p.translate(pRect.topLeft());
0080             sheetView.paintCells(p, QRect(0, 0, pageLayout.width, pageLayout.height), QPointF(0, 0));
0081 
0082             const Qt::LayoutDirection direction = sheet->layoutDirection();
0083 
0084             KoShapePainter shapePainter(direction == Qt::LeftToRight ? (KoShapeManagerPaintingStrategy *)0 : (KoShapeManagerPaintingStrategy *)0 /*RightToLeftPaintingStrategy(shapeManager, d->canvas)*/);
0085             shapePainter.setShapes(sheet->shapes());
0086             shapePainter.paint(p, zoomHandler);
0087 
0088             thumbnails.append(thumbnail);
0089         }
0090     }
0091     return thumbnails;
0092 }