1 /*
2  * This file is part of Calligra
3  *
4  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5  *
6  * Contact: Thorsten Zachmann thorsten.zachmann@nokia.com
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
24 #include "CSThumbProviderWords.h"
25 
26 #include <KoPart.h>
27 
28 #include <KWDocument.h>
29 #include <KWPage.h>
30 #include <KWCanvasItem.h>
31 
32 #include <frames/KWFrame.h>
33 #include <frames/KWFrameSet.h>
34 #include <frames/KWTextFrameSet.h>
35 
36 #include <KoShapeManager.h>
37 #include <KoZoomHandler.h>
38 #include <KoShapePainter.h>
39 #include <KoPAUtil.h>
40 
41 #include <QPainter>
42 #include <QApplication>
43 
CSThumbProviderWords(KWDocument * doc)44 CSThumbProviderWords::CSThumbProviderWords(KWDocument *doc)
45 : m_doc(doc)
46 {
47 }
48 
~CSThumbProviderWords()49 CSThumbProviderWords::~CSThumbProviderWords()
50 {
51 }
52 
createThumbnails(const QSize & thumbSize)53 QVector<QImage> CSThumbProviderWords::createThumbnails(const QSize &thumbSize)
54 {
55     KWCanvasItem *canvasItem = static_cast<KWCanvasItem*>(m_doc->documentPart()->canvasItem(m_doc));
56     KoZoomHandler zoomHandler;
57 
58     while (!m_doc->layoutFinishedAtleastOnce()) {
59         QCoreApplication::processEvents();
60 
61         if (!QCoreApplication::hasPendingEvents())
62             break;
63     }
64 
65     KWPageManager *pageManager = m_doc->pageManager();
66     KoShapeManager *shapeManager = canvasItem->shapeManager();
67 
68     QVector<QImage> thumbnails;
69 
70     foreach(const KWPage &page, pageManager->pages()) {
71 
72         QRectF pRect(page.rect());
73         KoPageLayout layout;
74         layout.width = pRect.width();
75         layout.height = pRect.height();
76 
77         KoPAUtil::setZoom(layout, thumbSize, zoomHandler);
78         QRect pageRect = KoPAUtil::pageRect(layout, thumbSize, zoomHandler);
79 
80         QImage thumbnail(thumbSize, QImage::Format_RGB32);
81         thumbnail.fill(QColor(Qt::white).rgb());
82         QPainter p(&thumbnail);
83 
84         QImage img = page.thumbnail(pageRect.size(), shapeManager);
85         p.drawImage(pageRect, img);
86 
87         p.setPen(Qt::black);
88         p.drawRect(pageRect);
89 
90         thumbnails.append(thumbnail);
91     }
92 
93     return thumbnails;
94 }
95 
96 
97