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 "CSThumbProviderWords.h"
0025 
0026 #include <KoPart.h>
0027 
0028 #include <KWDocument.h>
0029 #include <KWPage.h>
0030 #include <KWCanvasItem.h>
0031 
0032 #include <frames/KWFrame.h>
0033 #include <frames/KWFrameSet.h>
0034 #include <frames/KWTextFrameSet.h>
0035 
0036 #include <KoShapeManager.h>
0037 #include <KoZoomHandler.h>
0038 #include <KoShapePainter.h>
0039 #include <KoPAUtil.h>
0040 
0041 #include <QPainter>
0042 #include <QApplication>
0043 
0044 CSThumbProviderWords::CSThumbProviderWords(KWDocument *doc)
0045 : m_doc(doc)
0046 {
0047 }
0048 
0049 CSThumbProviderWords::~CSThumbProviderWords()
0050 {
0051 }
0052 
0053 QVector<QImage> CSThumbProviderWords::createThumbnails(const QSize &thumbSize)
0054 {
0055     KWCanvasItem *canvasItem = static_cast<KWCanvasItem*>(m_doc->documentPart()->canvasItem(m_doc));
0056     KoZoomHandler zoomHandler;
0057 
0058     while (!m_doc->layoutFinishedAtleastOnce()) {
0059         QCoreApplication::processEvents();
0060 
0061         if (!QCoreApplication::hasPendingEvents())
0062             break;
0063     }
0064 
0065     KWPageManager *pageManager = m_doc->pageManager();
0066     KoShapeManager *shapeManager = canvasItem->shapeManager();
0067 
0068     QVector<QImage> thumbnails;
0069 
0070     foreach(const KWPage &page, pageManager->pages()) {
0071 
0072         QRectF pRect(page.rect());
0073         KoPageLayout layout;
0074         layout.width = pRect.width();
0075         layout.height = pRect.height();
0076 
0077         KoPAUtil::setZoom(layout, thumbSize, zoomHandler);
0078         QRect pageRect = KoPAUtil::pageRect(layout, thumbSize, zoomHandler);
0079 
0080         QImage thumbnail(thumbSize, QImage::Format_RGB32);
0081         thumbnail.fill(QColor(Qt::white).rgb());
0082         QPainter p(&thumbnail);
0083 
0084         QImage img = page.thumbnail(pageRect.size(), shapeManager);
0085         p.drawImage(pageRect, img);
0086 
0087         p.setPen(Qt::black);
0088         p.drawRect(pageRect);
0089 
0090         thumbnails.append(thumbnail);
0091     }
0092 
0093     return thumbnails;
0094 }
0095 
0096