1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org 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
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #ifndef SD_SLIDESORTER_GENERIC_PAGE_CACHE_HXX
29 #define SD_SLIDESORTER_GENERIC_PAGE_CACHE_HXX
30 
31 #include "SlideSorter.hxx"
32 #include "SlsRequestQueue.hxx"
33 #include <boost/scoped_ptr.hpp>
34 
35 namespace sd { namespace slidesorter { namespace cache {
36 
37 class BitmapCache;
38 class QueueProcessor;
39 
40 /** This basically is the implementation class for the PageCache class.
41 */
42 class GenericPageCache
43 {
44 public:
45     /** The page chache is created with a reference to the SlideSorter and
46         thus has access to both view and model.  This allows the cache to
47         fill itself with requests for all pages or just the visible ones.
48         @param rPreviewSize
49             The size of the previews is expected in pixel values.
50         @param bDoSuperSampling
51             When <TRUE/> the previews are rendered larger and then scaled
52             down to the requested size to improve image quality.
53     */
54     GenericPageCache (
55         const Size& rPreviewSize,
56         const bool bDoSuperSampling,
57         const SharedCacheContext& rpCacheContext);
58 
59     ~GenericPageCache (void);
60 
61     /** Change the size of the preview bitmaps.  This may be caused by a
62         resize of the slide sorter window or a change of the number of
63         columns.
64     */
65     void ChangePreviewSize (
66         const Size& rPreviewSize,
67         const bool bDoSuperSampling);
68 
69     /** Request a preview bitmap for the specified page object in the
70         specified size.  The returned bitmap may be a preview of the preview,
71         i.e. either a scaled (up or down) version of a previous preview (of
72         the wrong size) or an empty bitmap.  In this case a request for the
73         generation of a new preview is created and inserted into the request
74         queue.  When the preview is available the page shape will be told to
75         paint itself again.  When it then calls this method again if
76         receives the correctly sized preview bitmap.
77         @param rRequestData
78             This data is used to determine the preview.
79         @param bResize
80             When <TRUE/> then when the available bitmap has not the
81             requested size, it is scaled before it is returned.  When
82             <FALSE/> then the bitmap is returned in the wrong size and it is
83             the task of the caller to scale it.
84         @return
85             Returns a bitmap that is either empty, contains a scaled (up or
86             down) version or is the requested bitmap.
87     */
88     Bitmap GetPreviewBitmap (
89         const CacheKey aKey,
90         const bool bResize);
91     Bitmap GetMarkedPreviewBitmap (
92         const CacheKey aKey,
93         const bool bResize);
94     void SetMarkedPreviewBitmap (
95         const CacheKey aKey,
96         const Bitmap& rMarkedBitmap);
97 
98     /** When the requested preview bitmap does not yet exist or is not
99         up-to-date then the rendering of one is scheduled.  Otherwise this
100         method does nothing.
101         @param rRequestData
102             This data is used to determine the preview.
103         @param bMayBeUpToDate
104             This flag helps the method to determine whether an existing
105             preview that matches the request is up to date.  If the caller
106             knows that it is not then by passing <FALSE/> he tells us that we
107             do not have to check the up-to-date flag a second time.  If
108             unsure use <TRUE/>.
109     */
110     void RequestPreviewBitmap (
111         const CacheKey aKey,
112         const bool bMayBeUpToDate = true);
113 
114     /** Tell the cache to replace the bitmap associated with the given
115         request data with a new one that reflects recent changes in the
116         content of the page object.
117         @return
118             When the key is kown then return <TRUE/>.
119     */
120     bool InvalidatePreviewBitmap (const CacheKey aKey);
121 
122     /** Call this method when a view-object-contact object is being deleted
123         and does not need (a) its current bitmap in the cache and (b) a
124         requested a new bitmap.
125     */
126     void ReleasePreviewBitmap (const CacheKey aKey);
127 
128     /** Call this method when all preview bitmaps have to be generated anew.
129         This is the case when the size of the page objects on the screen has
130         changed or when the model has changed.
131     */
132     void InvalidateCache (const bool bUpdateCache);
133 
134     /** With the precious flag you can control whether a bitmap can be
135         removed from the cache or reduced in size to make room for other
136         bitmaps or is so precious that it will not be touched.  A typical
137         use is to set the precious flag for the visible pages.
138     */
139     void SetPreciousFlag (const CacheKey aKey, const bool bIsPrecious);
140 
141     void Pause (void);
142     void Resume (void);
143 
144 private:
145     ::boost::shared_ptr<BitmapCache> mpBitmapCache;
146 
147     RequestQueue maRequestQueue;
148 
149     ::boost::scoped_ptr<QueueProcessor> mpQueueProcessor;
150 
151     SharedCacheContext mpCacheContext;
152 
153     /** The current size of preview bitmaps.
154     */
155     Size maPreviewSize;
156 
157     bool mbDoSuperSampling;
158 
159     /** Both bitmap cache and queue processor are created on demand by this
160         method.
161     */
162     void ProvideCacheAndProcessor (void);
163 };
164 
165 
166 } } } // end of namespace ::sd::slidesorter::cache
167 
168 #endif
169