1 /* This file is part of the KDE project
2  * Copyright (C) 2006 Thomas Zander <zander@kde.org>
3  * Copyright (C) 2007 Sebastian Sauer <mail@dipe.org>
4  * Copyright (C) 2008 Thorsten Zachmann <zachmann@kde.org>
5  * Copyright (C) 2008 Girish Ramakrishnan <girish@forwardbias.in>
6  * Copyright (C) 2009-2011 KO GmbH <cbo@kogmbh.com>
7  * Copyright (C) 2011-2012 Pierre Stirnweiss <pstirnweiss@googlemail.com>
8  * Copyright (C) 2012 Gopalakrishna Bhat A <gopalakbhat@gmail.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public License
21  * along with this library; see the file COPYING.LIB.  If not, write to
22  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  */
25 
26 #ifndef KOSTYLETHUMBNAILER_H
27 #define KOSTYLETHUMBNAILER_H
28 
29 #include "kritatextlayout_export.h"
30 
31 #include <QSize>
32 
33 class KoCharacterStyle;
34 class KoParagraphStyle;
35 
36 class QImage;
37 
38 /**
39  * Helper class to create (and cache) thumbnails of styles
40  */
41 class KRITATEXTLAYOUT_EXPORT KoStyleThumbnailer
42 {
43 public:
44     enum KoStyleThumbnailerFlag {
45         NoFlags = 0,
46         CenterAlignThumbnail = 1, ///< Vertically Center Align the layout of the thumbnail
47                                   ///     i.e the layout is done at the center of the area
48         UseStyleNameText = 2, ///< Use the style name as the text that is layouted inside the thumbnail
49         ScaleThumbnailFont = 4 ///< If set, then when the layout size is more than the size available
50                                ///  the font size is scaled down to fit the space available
51     };
52     Q_DECLARE_FLAGS(KoStyleThumbnailerFlags, KoStyleThumbnailerFlag)
53 
54     /**
55      * Create a new style thumbnailer.
56      */
57     explicit KoStyleThumbnailer();
58 
59     /**
60      * Destructor.
61      */
62     virtual ~KoStyleThumbnailer();
63 
64     /**
65      * @returns a thumbnail representing the @param style, constrained into the @param size.
66      * If there is no specified @param size, the thunbnail is the size specified with @fn setThumbnailSize or 250*48 pt if no size was provided.
67      * If the given @param size is too small, the font size will be decreased, so the thumbnail fits.
68      * The real font size is indicated in this case.
69      * If @param recreateThumbnail is true, do not return the cached thumbnail if it exist, but recreate a new one.
70      * The created thumbnail is cached.
71      */
72     QImage thumbnail(KoParagraphStyle *style,
73                      const QSize &size = QSize(), bool recreateThumbnail = false,
74                      KoStyleThumbnailerFlags flags =
75                           KoStyleThumbnailerFlags(CenterAlignThumbnail | UseStyleNameText | ScaleThumbnailFont));
76 
77     /**
78      * @returns a thumbnail representing the @param characterStyle applied on the given @param paragraphStyle, constrained into the @param size.
79      * If there is no specified @param size, the thunbnail is the size specified with @fn setThumbnailSize or 250*48 pt if no size was provided.
80      * If the given @param size is too small, the font size will be decreased, so the thumbnail fits.
81      * The real font size is indicated in this case.
82      * If @param recreateThumbnail is true, do not return the cached thumbnail if it exist, but recreate a new one.
83      * The created thumbnail is cached.
84      */
85     QImage thumbnail(KoCharacterStyle *characterStyle, KoParagraphStyle *paragraphStyle = 0,
86                      const QSize &size = QSize(), bool recreateThumbnail = false,
87                      KoStyleThumbnailerFlags flags =
88                           KoStyleThumbnailerFlags(CenterAlignThumbnail | UseStyleNameText | ScaleThumbnailFont));
89 
90     /**
91      * Sets the size of the thumbnails returned by the @fn thumbnail with no size arguments.
92      */
93     void setThumbnailSize(const QSize &size);
94 
95     /**
96      * Sets the text that will be layouted.
97      * @param text The text that will be layouted inside the thumbnail
98      *If the UseStyleNameText flag is set then this text will not be used
99      */
100     void setText(const QString &text);
101 
102     /**
103      * remove all occurrences of the style from the cache
104      */
105     void removeFromCache(KoParagraphStyle *style);
106 
107     /**
108      * remove all occurrences of the style from the cache
109      */
110     void removeFromCache(KoCharacterStyle *style);
111 
112 private:
113     void layoutThumbnail(const QSize &size, QImage *im, KoStyleThumbnailerFlags flags);
114     void removeFromCache(const QString &expr);
115 
116     class Private;
117     Private* const d;
118 };
119 
120 #endif
121